2 * mw.GallerySlideshow: Interface controls for the slideshow gallery
6 * mw.GallerySlideshow encapsulates the user interface of the slideshow
7 * galleries. An object is instantiated for each `.mw-gallery-slideshow`
10 * @class mw.GallerySlideshow
13 * @param {jQuery} gallery The `<ul>` element of the gallery.
15 mw
.GallerySlideshow = function ( gallery
) {
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
= {};
25 this.setSizeRequirement();
26 this.toggleThumbnails( !!this.$gallery
.attr( 'data-showthumbnails' ) );
27 this.showCurrentImage();
33 this.setSizeRequirement
.bind( this ),
38 // Disable thumbnails' link, instead show the image in the carousel
39 this.$galleryBox
.on( 'click', function ( e
) {
40 this.$currentImage
= $( e
.currentTarget
);
41 this.showCurrentImage();
48 * @property {jQuery} $gallery The `<ul>` element of the gallery.
52 * @property {jQuery} $galleryCaption The `<li>` that has the gallery caption.
56 * @property {jQuery} $galleryBox Selection of `<li>` elements that have thumbnails.
60 * @property {jQuery} $carousel The `<li>` elements that contains the carousel.
64 * @property {jQuery} $interface The `<div>` elements that contains the interface buttons.
68 * @property {jQuery} $img The `<img>` element that'll display the current image.
72 * @property {jQuery} $imgLink The `<a>` element that links to the image's File page.
76 * @property {jQuery} $imgCaption The `<p>` element that holds the image caption.
80 * @property {jQuery} $imgContainer The `<div>` element that contains the image.
84 * @property {jQuery} $currentImage The `<li>` element of the current image.
88 * @property {Object} imageInfoCache A key value pair of thumbnail URLs and image info.
92 * @property {number} imageWidth Width of the image based on viewport size
96 * @property {number} imageHeight Height of the image based on viewport size
97 * the URLs in the required size.
101 OO
.initClass( mw
.GallerySlideshow
);
105 * Draws the carousel and the interface around it.
107 mw
.GallerySlideshow
.prototype.drawCarousel = function () {
108 var nextButton
, prevButton
, toggleButton
, interfaceElements
, carouselStack
;
110 this.$carousel
= $( '<li>' ).addClass( 'gallerycarousel' );
112 // Buttons for the interface
113 prevButton
= new OO
.ui
.ButtonWidget( {
116 } ).connect( this, { click
: 'prevImage' } );
118 nextButton
= new OO
.ui
.ButtonWidget( {
121 } ).connect( this, { click
: 'nextImage' } );
123 toggleButton
= new OO
.ui
.ButtonWidget( {
125 icon
: 'imageGallery',
126 title
: mw
.msg( 'gallery-slideshow-toggle' )
127 } ).connect( this, { click
: 'toggleThumbnails' } );
129 interfaceElements
= new OO
.ui
.PanelLayout( {
131 classes
: [ 'mw-gallery-slideshow-buttons' ],
132 $content
: $( '<div>' ).append(
134 toggleButton
.$element
,
138 this.$interface = interfaceElements
.$element
;
140 // Containers for the current image, caption etc.
141 this.$img
= $( '<img>' );
142 this.$imgLink
= $( '<a>' ).append( this.$img
);
143 this.$imgCaption
= $( '<p>' ).attr( 'class', 'mw-gallery-slideshow-caption' );
144 this.$imgContainer
= $( '<div>' )
145 .attr( 'class', 'mw-gallery-slideshow-img-container' )
146 .append( this.$imgLink
);
148 carouselStack
= new OO
.ui
.StackLayout( {
153 new OO
.ui
.PanelLayout( {
155 $content
: this.$imgContainer
157 new OO
.ui
.PanelLayout( {
159 $content
: this.$imgCaption
163 this.$carousel
.append( carouselStack
.$element
);
165 // Append below the caption or as the first element in the gallery
166 if ( this.$galleryCaption
.length
!== 0 ) {
167 this.$galleryCaption
.after( this.$carousel
);
169 this.$gallery
.prepend( this.$carousel
);
174 * Sets the {@link #imageWidth} and {@link #imageHeight} properties
175 * based on the size of the window. Also flushes the
176 * {@link #imageInfoCache} as we'll now need URLs for a different
179 mw
.GallerySlideshow
.prototype.setSizeRequirement = function () {
180 var w
= this.$imgContainer
.width(),
181 h
= Math
.min( $( window
).height() * ( 3 / 4 ), this.$imgContainer
.width() ) - this.getChromeHeight();
183 // Only update and flush the cache if the size changed
184 if ( w
!== this.imageWidth
|| h
!== this.imageHeight
) {
186 this.imageHeight
= h
;
187 this.imageInfoCache
= {};
193 * Gets the height of the interface elements and the
196 * @return {number} Height
198 mw
.GallerySlideshow
.prototype.getChromeHeight = function () {
199 return this.$interface.outerHeight() + ( this.$galleryCaption
.outerHeight() || 0 );
203 * Sets the height and width of {@link #$img} based on the
204 * proportion of the image and the values generated by
205 * {@link #setSizeRequirement}.
207 mw
.GallerySlideshow
.prototype.setImageSize = function () {
208 if ( this.$img
=== undefined || this.$thumbnail
=== undefined ) {
212 // Reset height and width
214 .removeAttr( 'width' )
215 .removeAttr( 'height' );
217 // Stretch image to take up the required size
218 this.$img
.attr( 'height', ( this.imageHeight
- this.$imgCaption
.outerHeight() ) + 'px' );
220 // Make the image smaller in case the current image
221 // size is larger than the original file size.
222 this.getImageInfo( this.$thumbnail
).done( function ( info
) {
223 // NOTE: There will be a jump when resizing the window
224 // because the cache is cleared and this a new network request.
226 info
.thumbwidth
< this.$img
.width() ||
227 info
.thumbheight
< this.$img
.height()
230 width
: info
.thumbwidth
+ 'px',
231 height
: info
.thumbheight
+ 'px'
238 * Displays the image set as {@link #$currentImage} in the carousel.
240 mw
.GallerySlideshow
.prototype.showCurrentImage = function () {
242 $imageLi
= this.getCurrentImage(),
243 $caption
= $imageLi
.find( '.gallerytext' );
245 // The order of the following is important for size calculations
246 // 1. Highlight current thumbnail
248 .find( '.gallerybox.slideshow-current' )
249 .removeClass( 'slideshow-current' );
250 $imageLi
.addClass( 'slideshow-current' );
253 this.$thumbnail
= $imageLi
.find( 'img' );
255 src
: this.$thumbnail
.attr( 'src' ),
256 alt
: this.$thumbnail
.attr( 'alt' )
258 this.$imgLink
.attr( 'href', $imageLi
.find( 'a' ).eq( 0 ).attr( 'href' ) );
263 .append( $caption
.clone() );
265 // 4. Stretch thumbnail to correct size
268 $thumbnail
= this.$thumbnail
;
269 // 5. Load image at the required size
270 this.loadImage( this.$thumbnail
).done( function ( info
) {
271 // Show this image to the user only if its still the current one
272 if ( this.$thumbnail
.attr( 'src' ) === $thumbnail
.attr( 'src' ) ) {
273 this.$img
.attr( 'src', info
.thumburl
);
276 // Pre-fetch the next image
277 this.loadImage( this.getNextImage().find( 'img' ) );
283 * Loads the full image given the `<img>` element of the thumbnail.
285 * @param {jQuery} $img
286 * @return {jQuery.Promise} Resolves with the images URL and original
287 * element once the image has loaded.
289 mw
.GallerySlideshow
.prototype.loadImage = function ( $img
) {
290 var img
, d
= $.Deferred();
292 this.getImageInfo( $img
).done( function ( info
) {
294 img
.src
= info
.thumburl
;
295 img
.onload = function () {
298 img
.onerror = function () {
301 } ).fail( function () {
309 * Gets the image's info given an `<img>` element.
311 * @param {Object} $img
312 * @return {jQuery.Promise} Resolves with the image's info.
314 mw
.GallerySlideshow
.prototype.getImageInfo = function ( $img
) {
315 var api
, title
, params
,
316 imageSrc
= $img
.attr( 'src' );
318 // Reject promise if there is no thumbnail image
319 if ( $img
[ 0 ] === undefined ) {
320 return $.Deferred().reject();
323 if ( this.imageInfoCache
[ imageSrc
] === undefined ) {
325 // TODO: This supports only gallery of images
326 title
= mw
.Title
.newFromImg( $img
);
330 titles
: title
.toString(),
335 // Check which dimension we need to request, based on
336 // image and container proportions.
337 if ( this.getDimensionToRequest( $img
) === 'height' ) {
338 params
.iiurlheight
= this.imageHeight
;
340 params
.iiurlwidth
= this.imageWidth
;
343 this.imageInfoCache
[ imageSrc
] = api
.get( params
).then( function ( data
) {
344 if ( OO
.getProp( data
, 'query', 'pages', 0, 'imageinfo', 0, 'thumburl' ) !== undefined ) {
345 return data
.query
.pages
[ 0 ].imageinfo
[ 0 ];
347 return $.Deferred().reject();
352 return this.imageInfoCache
[ imageSrc
];
356 * Given an image, the method checks whether to use the height
357 * or the width to request the larger image.
359 * @param {jQuery} $img
362 mw
.GallerySlideshow
.prototype.getDimensionToRequest = function ( $img
) {
363 var ratio
= $img
.width() / $img
.height();
365 if ( this.imageHeight
* ratio
<= this.imageWidth
) {
373 * Toggles visibility of the thumbnails.
375 * @param {boolean} show Optional argument to control the state
377 mw
.GallerySlideshow
.prototype.toggleThumbnails = function ( show
) {
378 this.$galleryBox
.toggle( show
);
379 this.$carousel
.toggleClass( 'mw-gallery-slideshow-thumbnails-toggled', show
);
383 * Getter method for {@link #$currentImage}
387 mw
.GallerySlideshow
.prototype.getCurrentImage = function () {
388 this.$currentImage
= this.$currentImage
|| this.$galleryBox
.eq( 0 );
389 return this.$currentImage
;
393 * Gets the image after the current one. Returns the first image if
394 * the current one is the last.
398 mw
.GallerySlideshow
.prototype.getNextImage = function () {
399 // Not the last image in the gallery
400 if ( this.$currentImage
.next( '.gallerybox' )[ 0 ] !== undefined ) {
401 return this.$currentImage
.next( '.gallerybox' );
403 return this.$galleryBox
.eq( 0 );
408 * Gets the image before the current one. Returns the last image if
409 * the current one is the first.
413 mw
.GallerySlideshow
.prototype.getPrevImage = function () {
414 // Not the first image in the gallery
415 if ( this.$currentImage
.prev( '.gallerybox' )[ 0 ] !== undefined ) {
416 return this.$currentImage
.prev( '.gallerybox' );
418 return this.$galleryBox
.last();
423 * Sets the {@link #$currentImage} to the next one and shows
426 mw
.GallerySlideshow
.prototype.nextImage = function () {
427 this.$currentImage
= this.getNextImage();
428 this.showCurrentImage();
432 * Sets the {@link #$currentImage} to the previous one and shows
435 mw
.GallerySlideshow
.prototype.prevImage = function () {
436 this.$currentImage
= this.getPrevImage();
437 this.showCurrentImage();
440 // Bootstrap all slideshow galleries
441 mw
.hook( 'wikipage.content' ).add( function ( $content
) {
442 $content
.find( '.mw-gallery-slideshow' ).each( function () {
443 // eslint-disable-next-line no-new
444 new mw
.GallerySlideshow( this );