Introduce InterwikiLookupAdapter on top of SiteLookup
[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 // Reject promise if there is no thumbnail image
329 if ( $img[ 0 ] === undefined ) {
330 return $.Deferred().reject();
331 }
332
333 if ( this.imageInfoCache[ imageSrc ] === undefined ) {
334 api = new mw.Api();
335 // TODO: This supports only gallery of images
336 title = new mw.Title.newFromImg( $img );
337 params = {
338 action: 'query',
339 formatversion: 2,
340 titles: title.toString(),
341 prop: 'imageinfo',
342 iiprop: 'url'
343 };
344
345 // Check which dimension we need to request, based on
346 // image and container proportions.
347 if ( this.getDimensionToRequest( $img ) === 'height' ) {
348 params.iiurlheight = this.imageHeight;
349 } else {
350 params.iiurlwidth = this.imageWidth;
351 }
352
353 this.imageInfoCache[ imageSrc ] = api.get( params ).then( function ( data ) {
354 if ( OO.getProp( data, 'query', 'pages', 0, 'imageinfo', 0, 'thumburl' ) !== undefined ) {
355 return data.query.pages[ 0 ].imageinfo[ 0 ];
356 } else {
357 return $.Deferred().reject();
358 }
359 } );
360 }
361
362 return this.imageInfoCache[ imageSrc ];
363 };
364
365 /**
366 * Given an image, the method checks whether to use the height
367 * or the width to request the larger image.
368 *
369 * @param {jQuery} $img
370 * @return {string}
371 */
372 mw.GallerySlideshow.prototype.getDimensionToRequest = function ( $img ) {
373 var ratio = $img.width() / $img.height();
374
375 if ( this.imageHeight * ratio <= this.imageWidth ) {
376 return 'height';
377 } else {
378 return 'width';
379 }
380 };
381
382 /**
383 * Toggles visibility of the thumbnails.
384 *
385 * @param {boolean} show Optional argument to control the state
386 */
387 mw.GallerySlideshow.prototype.toggleThumbnails = function ( show ) {
388 this.$galleryBox.toggle( show );
389 this.$carousel.toggleClass( 'mw-gallery-slideshow-thumbnails-toggled', show );
390 };
391
392 /**
393 * Getter method for {@link #$currentImage}
394 *
395 * @return {jQuery}
396 */
397 mw.GallerySlideshow.prototype.getCurrentImage = function () {
398 this.$currentImage = this.$currentImage || this.$galleryBox.eq( 0 );
399 return this.$currentImage;
400 };
401
402 /**
403 * Gets the image after the current one. Returns the first image if
404 * the current one is the last.
405 *
406 * @return {jQuery}
407 */
408 mw.GallerySlideshow.prototype.getNextImage = function () {
409 // Not the last image in the gallery
410 if ( this.$currentImage.next( '.gallerybox' )[ 0 ] !== undefined ) {
411 return this.$currentImage.next( '.gallerybox' );
412 } else {
413 return this.$galleryBox.eq( 0 );
414 }
415 };
416
417 /**
418 * Gets the image before the current one. Returns the last image if
419 * the current one is the first.
420 *
421 * @return {jQuery}
422 */
423 mw.GallerySlideshow.prototype.getPrevImage = function () {
424 // Not the first image in the gallery
425 if ( this.$currentImage.prev( '.gallerybox' )[ 0 ] !== undefined ) {
426 return this.$currentImage.prev( '.gallerybox' );
427 } else {
428 return this.$galleryBox.last();
429 }
430 };
431
432 /**
433 * Sets the {@link #$currentImage} to the next one and shows
434 * it in the carousel
435 */
436 mw.GallerySlideshow.prototype.nextImage = function () {
437 this.$currentImage = this.getNextImage();
438 this.showCurrentImage();
439 };
440
441 /**
442 * Sets the {@link #$currentImage} to the previous one and shows
443 * it in the carousel
444 */
445 mw.GallerySlideshow.prototype.prevImage = function () {
446 this.$currentImage = this.getPrevImage();
447 this.showCurrentImage();
448 };
449
450 // Bootstrap all slideshow galleries
451 $( function () {
452 $( '.mw-gallery-slideshow' ).each( function () {
453 /*jshint -W031 */
454 new mw.GallerySlideshow( this );
455 /*jshint +W031 */
456 } );
457 } );
458 }( mediaWiki, jQuery, OO ) );