Merge "Convert Special:NewFiles to use OOUI."
[lhc/web/wiklou.git] / resources / src / mediawiki / page / gallery-slideshow.js
1 /*!
2 * mw.GallerySlideshow: Interface controls for the slideshow gallery
3 */
4 ( function ( mw, $, OO ) {
5 /**
6 * mw.GallerySlideshow encapsulates the user interface of the slideshow
7 * galleries. An object is instantiated for each `.mw-gallery-slideshow`
8 * element.
9 *
10 * @class mw.GallerySlideshow
11 * @uses mw.Title
12 * @uses mw.Api
13 * @param {jQuery} gallery The `<ul>` element of the gallery.
14 */
15 mw.GallerySlideshow = function ( gallery ) {
16 // Properties
17 this.$gallery = $( gallery );
18 this.$galleryCaption = this.$gallery.find( '.gallerycaption' );
19 this.$galleryBox = this.$gallery.find( '.gallerybox' );
20 this.$currentImage = null;
21 this.imageInfoCache = {};
22 if ( this.$gallery.parent().attr( 'id' ) !== 'mw-content-text' ) {
23 this.$container = this.$gallery.parent();
24 }
25
26 // Initialize
27 this.drawCarousel();
28 this.setSizeRequirement();
29 this.toggleThumbnails( false );
30 this.showCurrentImage();
31
32 // Events
33 $( window ).on(
34 'resize',
35 OO.ui.debounce(
36 this.setSizeRequirement.bind( this ),
37 100
38 )
39 );
40
41 // Disable thumbnails' link, instead show the image in the carousel
42 this.$galleryBox.on( 'click', function ( e ) {
43 this.$currentImage = $( e.currentTarget );
44 this.showCurrentImage();
45 return false;
46 }.bind( this ) );
47 };
48
49 /* Properties */
50 /**
51 * @property {jQuery} $gallery The `<ul>` element of the gallery.
52 */
53
54 /**
55 * @property {jQuery} $galleryCaption The `<li>` that has the gallery caption.
56 */
57
58 /**
59 * @property {jQuery} $galleryBox Selection of `<li>` elements that have thumbnails.
60 */
61
62 /**
63 * @property {jQuery} $carousel The `<li>` elements that contains the carousel.
64 */
65
66 /**
67 * @property {jQuery} $interface The `<div>` elements that contains the interface buttons.
68 */
69
70 /**
71 * @property {jQuery} $img The `<img>` element that'll display the current image.
72 */
73
74 /**
75 * @property {jQuery} $imgLink The `<a>` element that links to the image's File page.
76 */
77
78 /**
79 * @property {jQuery} $imgCaption The `<p>` element that holds the image caption.
80 */
81
82 /**
83 * @property {jQuery} $imgContainer The `<div>` element that contains the image.
84 */
85
86 /**
87 * @property {jQuery} $currentImage The `<li>` element of the current image.
88 */
89
90 /**
91 * @property {jQuery} $container If the gallery contained in an element that is
92 * not the main content element, then it stores that element.
93 */
94
95 /**
96 * @property {Object} imageInfoCache A key value pair of thumbnail URLs and image info.
97 */
98
99 /**
100 * @property {number} imageWidth Width of the image based on viewport size
101 */
102
103 /**
104 * @property {number} imageHeight Height of the image based on viewport size
105 * the URLs in the required size.
106 */
107
108 /* Setup */
109 OO.initClass( mw.GallerySlideshow );
110
111 /* Methods */
112 /**
113 * Draws the carousel and the interface around it.
114 */
115 mw.GallerySlideshow.prototype.drawCarousel = function () {
116 var next, prev, toggle, interfaceElements, carouselStack;
117
118 this.$carousel = $( '<li>' ).addClass( 'gallerycarousel' );
119
120 // Buttons for the interface
121 prev = new OO.ui.ButtonWidget( {
122 framed: false,
123 icon: 'previous'
124 } ).on( 'click', this.prevImage.bind( this ) );
125
126 next = new OO.ui.ButtonWidget( {
127 framed: false,
128 icon: 'next'
129 } ).on( 'click', this.nextImage.bind( this ) );
130
131 toggle = new OO.ui.ButtonWidget( {
132 framed: false,
133 icon: 'imageGallery'
134 } ).on( 'click', this.toggleThumbnails.bind( this ) );
135
136 interfaceElements = new OO.ui.PanelLayout( {
137 expanded: false,
138 classes: [ 'mw-gallery-slideshow-buttons' ],
139 $content: $( '<div>' ).append(
140 prev.$element,
141 toggle.$element,
142 next.$element
143 )
144 } );
145 this.$interface = interfaceElements.$element;
146
147 // Containers for the current image, caption etc.
148 this.$img = $( '<img>' );
149 this.$imgLink = $( '<a>' ).append( this.$img );
150 this.$imgCaption = $( '<p>' ).attr( 'class', 'mw-gallery-slideshow-caption' );
151 this.$imgContainer = $( '<div>' )
152 .attr( 'class', 'mw-gallery-slideshow-img-container' )
153 .append( this.$imgLink );
154
155 carouselStack = new OO.ui.StackLayout( {
156 continuous: true,
157 expanded: false,
158 items: [
159 interfaceElements,
160 new OO.ui.PanelLayout( {
161 expanded: false,
162 $content: this.$imgContainer
163 } ),
164 new OO.ui.PanelLayout( {
165 expanded: false,
166 $content: this.$imgCaption
167 } )
168 ]
169 } );
170 this.$carousel.append( carouselStack.$element );
171
172 // Append below the caption or as the first element in the gallery
173 if ( this.$galleryCaption.length !== 0 ) {
174 this.$galleryCaption.after( this.$carousel );
175 } else {
176 this.$gallery.prepend( this.$carousel );
177 }
178 };
179
180 /**
181 * Sets the {@link #imageWidth} and {@link #imageHeight} properties
182 * based on the size of the window. Also flushes the
183 * {@link #imageInfoCache} as we'll now need URLs for a different
184 * size.
185 */
186 mw.GallerySlideshow.prototype.setSizeRequirement = function () {
187 var w, h;
188
189 if ( this.$container !== undefined ) {
190 w = this.$container.width() * 0.9;
191 h = ( this.$container.height() - this.getChromeHeight() ) * 0.9;
192 } else {
193 w = this.$imgContainer.width();
194 h = Math.min( $( window ).height() * ( 3 / 4 ), this.$imgContainer.width() ) - this.getChromeHeight();
195 }
196
197 // Only update and flush the cache if the size changed
198 if ( w !== this.imageWidth || h !== this.imageHeight ) {
199 this.imageWidth = w;
200 this.imageHeight = h;
201 this.imageInfoCache = {};
202 this.setImageSize();
203 }
204 };
205
206 /**
207 * Gets the height of the interface elements and the
208 * gallery's caption.
209 */
210 mw.GallerySlideshow.prototype.getChromeHeight = function () {
211 return this.$interface.outerHeight() + this.$galleryCaption.outerHeight();
212 };
213
214 /**
215 * Sets the height and width of {@link #$img} based on the
216 * proportion of the image and the values generated by
217 * {@link #setSizeRequirement}.
218 *
219 * @return {boolean} Whether or not the image was sized.
220 */
221 mw.GallerySlideshow.prototype.setImageSize = function () {
222 if ( this.$img === undefined || this.$thumbnail === undefined ) {
223 return false;
224 }
225
226 // Reset height and width
227 this.$img
228 .removeAttr( 'width' )
229 .removeAttr( 'height' );
230
231 // Stretch image to take up the required size
232 if ( this.$thumbnail.width() > this.$thumbnail.height() ) {
233 this.$img.attr( 'width', this.imageWidth + 'px' );
234 } else {
235 this.$img.attr( 'height', this.imageHeight + 'px' );
236 }
237
238 // Make the image smaller in case the current image
239 // size is larger than the original file size.
240 this.getImageInfo( this.$thumbnail ).done( function ( info ) {
241 // NOTE: There will be a jump when resizing the window
242 // because the cache is cleared and this a new network request.
243 if (
244 info.thumbwidth < this.$img.width() ||
245 info.thumbheight < this.$img.height()
246 ) {
247 this.$img.attr( 'width', info.thumbwidth + 'px' );
248 this.$img.attr( 'height', info.thumbheight + 'px' );
249 }
250 }.bind( this ) );
251
252 return true;
253 };
254
255 /**
256 * Displays the image set as {@link #$currentImage} in the carousel.
257 */
258 mw.GallerySlideshow.prototype.showCurrentImage = function () {
259 var imageLi = this.getCurrentImage(),
260 caption = imageLi.find( '.gallerytext' );
261
262 // Highlight current thumbnail
263 this.$gallery
264 .find( '.gallerybox.slideshow-current' )
265 .removeClass( 'slideshow-current' );
266 imageLi.addClass( 'slideshow-current' );
267
268 // Show thumbnail stretched to the right size while the image loads
269 this.$thumbnail = imageLi.find( 'img' );
270 this.$img.attr( 'src', this.$thumbnail.attr( 'src' ) );
271 this.$imgLink.attr( 'href', imageLi.find( 'a' ).eq( 0 ).attr( 'href' ) );
272 this.setImageSize();
273
274 // Copy caption
275 this.$imgCaption
276 .empty()
277 .append( caption.clone() );
278
279 // Load image at the required size
280 this.loadImage( this.$thumbnail ).done( function ( info, $img ) {
281 // Show this image to the user only if its still the current one
282 if ( this.$thumbnail.attr( 'src' ) === $img.attr( 'src' ) ) {
283 this.$img.attr( 'src', info.thumburl );
284 this.setImageSize();
285
286 // Keep the next image ready
287 this.loadImage( this.getNextImage().find( 'img' ) );
288 }
289 }.bind( this ) );
290 };
291
292 /**
293 * Loads the full image given the `<img>` element of the thumbnail.
294 *
295 * @param {Object} $img
296 * @return {jQuery.Promise} Resolves with the images URL and original
297 * element once the image has loaded.
298 */
299 mw.GallerySlideshow.prototype.loadImage = function ( $img ) {
300 var img, d = $.Deferred();
301
302 this.getImageInfo( $img ).done( function ( info ) {
303 img = new Image();
304 img.src = info.thumburl;
305 img.onload = function () {
306 d.resolve( info, $img );
307 };
308 img.onerror = function () {
309 d.reject();
310 };
311 } ).fail( function () {
312 d.reject();
313 } );
314
315 return d.promise();
316 };
317
318 /**
319 * Gets the image's info given an `<img>` element.
320 *
321 * @param {Object} $img
322 * @return {jQuery.Promise} Resolves with the image's info.
323 */
324 mw.GallerySlideshow.prototype.getImageInfo = function ( $img ) {
325 var api, title, params,
326 imageSrc = $img.attr( 'src' );
327
328 if ( this.imageInfoCache[ imageSrc ] === undefined ) {
329 api = new mw.Api();
330 // TODO: This supports only gallery of images
331 title = new mw.Title.newFromImg( $img );
332 params = {
333 action: 'query',
334 formatversion: 2,
335 titles: title.toString(),
336 prop: 'imageinfo',
337 iiprop: 'url'
338 };
339
340 // Check which dimension we need to request, based on
341 // image and container proportions.
342 if ( this.getDimensionToRequest( $img ) === 'height' ) {
343 params.iiurlheight = this.imageHeight;
344 } else {
345 params.iiurlwidth = this.imageWidth;
346 }
347
348 this.imageInfoCache[ imageSrc ] = api.get( params ).then( function ( data ) {
349 if ( OO.getProp( data, 'query', 'pages', 0, 'imageinfo', 0, 'thumburl' ) !== undefined ) {
350 return data.query.pages[ 0 ].imageinfo[ 0 ];
351 } else {
352 return $.Deferred().reject();
353 }
354 } );
355 }
356
357 return this.imageInfoCache[ imageSrc ];
358 };
359
360 /**
361 * Given an image, the method checks whether to use the height
362 * or the width to request the larger image.
363 *
364 * @param {jQuery} $img
365 * @return {string}
366 */
367 mw.GallerySlideshow.prototype.getDimensionToRequest = function ( $img ) {
368 var ratio = $img.width() / $img.height();
369
370 if ( this.imageHeight * ratio <= this.imageWidth ) {
371 return 'height';
372 } else {
373 return 'width';
374 }
375 };
376
377 /**
378 * Toggles visibility of the thumbnails.
379 *
380 * @param {boolean} show Optional argument to control the state
381 */
382 mw.GallerySlideshow.prototype.toggleThumbnails = function ( show ) {
383 this.$galleryBox.toggle( show );
384 this.$carousel.toggleClass( 'mw-gallery-slideshow-thumbnails-toggled', show );
385 };
386
387 /**
388 * Getter method for {@link #$currentImage}
389 *
390 * @return {jQuery}
391 */
392 mw.GallerySlideshow.prototype.getCurrentImage = function () {
393 this.$currentImage = this.$currentImage || this.$galleryBox.eq( 0 );
394 return this.$currentImage;
395 };
396
397 /**
398 * Gets the image after the current one. Returns the first image if
399 * the current one is the last.
400 *
401 * @return {jQuery}
402 */
403 mw.GallerySlideshow.prototype.getNextImage = function () {
404 // Not the last image in the gallery
405 if ( this.$currentImage.next( '.gallerybox' )[ 0 ] !== undefined ) {
406 return this.$currentImage.next( '.gallerybox' );
407 } else {
408 return this.$galleryBox.eq( 0 );
409 }
410 };
411
412 /**
413 * Gets the image before the current one. Returns the last image if
414 * the current one is the first.
415 *
416 * @return {jQuery}
417 */
418 mw.GallerySlideshow.prototype.getPrevImage = function () {
419 // Not the first image in the gallery
420 if ( this.$currentImage.prev( '.gallerybox' )[ 0 ] !== undefined ) {
421 return this.$currentImage.prev( '.gallerybox' );
422 } else {
423 return this.$galleryBox.last();
424 }
425 };
426
427 /**
428 * Sets the {@link #$currentImage} to the next one and shows
429 * it in the carousel
430 */
431 mw.GallerySlideshow.prototype.nextImage = function () {
432 this.$currentImage = this.getNextImage();
433 this.showCurrentImage();
434 };
435
436 /**
437 * Sets the {@link #$currentImage} to the previous one and shows
438 * it in the carousel
439 */
440 mw.GallerySlideshow.prototype.prevImage = function () {
441 this.$currentImage = this.getPrevImage();
442 this.showCurrentImage();
443 };
444
445 // Bootstrap all slideshow galleries
446 $( function () {
447 $( '.mw-gallery-slideshow' ).each( function () {
448 /*jshint -W031 */
449 new mw.GallerySlideshow( this );
450 /*jshint +W031 */
451 } );
452 } );
453 }( mediaWiki, jQuery, OO ) );