Debian Codegraph @

resources/src/mediawiki.widgets/MediaSearch/mw.widgets.MediaResultWidget.js

raw symbols SCIP
1 /*!
2 * MediaWiki Widgets - MediaResultWidget class.
3 *
4 * @copyright 2011-2016 VisualEditor Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7 ( function () {
8
9 /**
10 * @classdesc Media result widget.
11 *
12 * @class
13 * @extends OO.ui.OptionWidget
14 *
15 * @constructor
16 * @description Creates an mw.widgets.MediaResultWidget object.
17 * @param {Object} [config] Configuration options
18 * @param {number} [config.rowHeight] Height of the row this result is part of
19 * @param {number} [config.maxRowWidth] A limit for the width of the row this
20 * result is a part of.
21 * @param {number} [config.minWidth] Minimum width for the result
22 * @param {number} [config.maxWidth] Maximum width for the result
23 */
24 mw.widgets.MediaResultWidget = function MwWidgetsMediaResultWidget( config ) {
25 // Configuration initialization
26 config = config || {};
27
28 // Parent constructor
29 mw.widgets.MediaResultWidget.super.call( this, config );
30
31 // Properties
32 this.setRowHeight( config.rowHeight || 150 );
33 this.maxRowWidth = config.maxRowWidth || 500;
34 this.minWidth = config.minWidth || this.maxRowWidth / 5;
35 this.maxWidth = config.maxWidth || this.maxRowWidth * 2 / 3;
36
37 this.imageDimensions = {};
38
39 this.isAudio = this.data.mediatype === 'AUDIO';
40
41 // Store the thumbnail url
42 this.thumbUrl = this.data.thumburl;
43 this.src = null;
44 this.row = null;
45
46 this.$thumb = $( '<img>' )
47 .addClass( 'mw-widget-mediaResultWidget-thumbnail' )
48 .attr( 'alt', '' )
49 .on( {
50 load: this.onThumbnailLoad.bind( this ),
51 error: this.onThumbnailError.bind( this )
52 } );
53 this.$overlay = $( '<div>' )
54 .addClass( 'mw-widget-mediaResultWidget-overlay' );
55
56 this.calculateSizing( this.data );
57
58 // Initialization
59 this.setLabel( new mw.Title( this.data.title ).getNameText() );
60 this.$label.addClass( 'mw-widget-mediaResultWidget-nameLabel' );
61
62 this.$element
63 .addClass( 'mw-widget-mediaResultWidget ve-ui-texture-pending' )
64 .prepend( this.$thumb, this.$overlay );
65 };
66
67 /* Inheritance */
68
69 OO.inheritClass( mw.widgets.MediaResultWidget, OO.ui.OptionWidget );
70
71 /* Static methods */
72
73 // Copied from ve.dm.MWImageNode
74 mw.widgets.MediaResultWidget.static.resizeToBoundingBox = function ( imageDimensions, boundingBox ) {
75 let newDimensions = OO.copy( imageDimensions );
76 const scale = Math.min(
77 boundingBox.height / imageDimensions.height,
78 boundingBox.width / imageDimensions.width
79 );
80
81 if ( scale < 1 ) {
82 // Scale down
83 newDimensions = {
84 width: Math.floor( newDimensions.width * scale ),
85 height: Math.floor( newDimensions.height * scale )
86 };
87 }
88 return newDimensions;
89 };
90
91 /* Methods */
92 mw.widgets.MediaResultWidget.prototype.onThumbnailLoad = function () {
93 this.$thumb.first().addClass( 've-ui-texture-transparency' );
94 this.$element
95 .addClass( 'mw-widget-mediaResultWidget-done' )
96 .removeClass( 've-ui-texture-pending' );
97 };
98
99 mw.widgets.MediaResultWidget.prototype.onThumbnailError = function () {
100 this.$thumb.last()
101 .css( 'background-image', '' )
102 .addClass( 've-ui-texture-alert' );
103 this.$element
104 .addClass( 'mw-widget-mediaResultWidget-error' )
105 .removeClass( 've-ui-texture-pending' );
106 };
107
108 /**
109 * Resize the thumbnail and wrapper according to row height and bounding boxes, if given.
110 *
111 * @param {Object} originalDimensions Original image dimensions with width and height values
112 * @param {Object} [boundingBox] Specific bounding box, if supplied
113 */
114 mw.widgets.MediaResultWidget.prototype.calculateSizing = function ( originalDimensions, boundingBox ) {
115 boundingBox = boundingBox || {};
116
117 let imageDimensions;
118 if ( this.isAudio ) {
119 // HACK: We are getting the wrong information from the
120 // API about audio files. Set their thumbnail to square 120px
121 imageDimensions = {
122 width: 120,
123 height: 120
124 };
125 } else {
126 // Get the image within the bounding box
127 imageDimensions = this.constructor.static.resizeToBoundingBox(
128 // Image original dimensions
129 {
130 width: originalDimensions.width || originalDimensions.thumbwidth,
131 height: originalDimensions.height || originalDimensions.thumbwidth
132 },
133 // Bounding box
134 {
135 width: boundingBox.width || this.getImageMaxWidth(),
136 height: boundingBox.height || this.getRowHeight()
137 }
138 );
139 }
140 this.imageDimensions = imageDimensions;
141 // Set the thumbnail size
142 this.$thumb.css( this.imageDimensions );
143
144 // Set the box size
145 const wrapperPadding = this.calculateWrapperPadding( this.imageDimensions );
146 this.$element.css( wrapperPadding );
147 };
148
149 /**
150 * Replace the empty .src attribute of the image with the
151 * actual src.
152 */
153 mw.widgets.MediaResultWidget.prototype.lazyLoad = function () {
154 if ( !this.hasSrc() ) {
155 this.src = this.thumbUrl;
156 this.$thumb.attr( 'src', this.thumbUrl );
157 }
158 };
159
160 /**
161 * Retrieve the store dimensions object.
162 *
163 * @return {Object} Thumb dimensions
164 */
165 mw.widgets.MediaResultWidget.prototype.getDimensions = function () {
166 return this.dimensions;
167 };
168
169 /**
170 * Resize thumbnail and element according to the resize factor.
171 *
172 * @param {number} resizeFactor The resizing factor for the image
173 */
174 mw.widgets.MediaResultWidget.prototype.resizeThumb = function ( resizeFactor ) {
175 const imageOriginalWidth = this.imageDimensions.width,
176 wrapperWidth = this.$element.width();
177 // Set the new row height
178 this.setRowHeight( Math.ceil( this.getRowHeight() * resizeFactor ) );
179
180 const boundingBox = {
181 width: Math.ceil( this.imageDimensions.width * resizeFactor ),
182 height: this.getRowHeight()
183 };
184
185 this.calculateSizing( this.data, boundingBox );
186
187 // We need to adjust the wrapper this time to fit the "perfect"
188 // dimensions, regardless of how small the image is
189 if ( imageOriginalWidth < wrapperWidth ) {
190 boundingBox.width = wrapperWidth * resizeFactor;
191 }
192 this.$element.css( this.calculateWrapperPadding( boundingBox ) );
193 };
194
195 /**
196 * Adjust the wrapper padding for small images.
197 *
198 * @param {Object} thumbDimensions Thumbnail dimensions
199 * @return {Object} Css styling for the wrapper
200 */
201 mw.widgets.MediaResultWidget.prototype.calculateWrapperPadding = function ( thumbDimensions ) {
202 const css = {
203 height: this.rowHeight,
204 width: thumbDimensions.width,
205 lineHeight: this.getRowHeight() + 'px'
206 };
207
208 // Check if the image is too thin so we can make a bit of space around it
209 if ( thumbDimensions.width < this.minWidth ) {
210 css.width = this.minWidth;
211 }
212
213 return css;
214 };
215
216 /**
217 * Set the row height for all size calculations.
218 *
219 * @return {number} rowHeight Row height
220 */
221 mw.widgets.MediaResultWidget.prototype.getRowHeight = function () {
222 return this.rowHeight;
223 };
224
225 /**
226 * Set the row height for all size calculations.
227 *
228 * @param {number} rowHeight Row height
229 */
230 mw.widgets.MediaResultWidget.prototype.setRowHeight = function ( rowHeight ) {
231 this.rowHeight = rowHeight;
232 };
233
234 mw.widgets.MediaResultWidget.prototype.setImageMaxWidth = function ( width ) {
235 this.maxWidth = width;
236 };
237 mw.widgets.MediaResultWidget.prototype.getImageMaxWidth = function () {
238 return this.maxWidth;
239 };
240
241 /**
242 * Set the row this result is in.
243 *
244 * @param {number} row Row number
245 */
246 mw.widgets.MediaResultWidget.prototype.setRow = function ( row ) {
247 this.row = row;
248 };
249
250 /**
251 * Get the row this result is in.
252 *
253 * @return {number} row Row number
254 */
255 mw.widgets.MediaResultWidget.prototype.getRow = function () {
256 return this.row;
257 };
258
259 /**
260 * Check if the image has a src attribute already.
261 *
262 * @return {boolean} Thumbnail has its source attribute set
263 */
264 mw.widgets.MediaResultWidget.prototype.hasSrc = function () {
265 return !!this.src;
266 };
267 }() );