2 * mw.GallerySlideshow: Interface controls for the slideshow gallery
4 ( function ( mw
, $, OO
) {
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
= {};
22 if ( this.$gallery
.parent().attr( 'id' ) !== 'mw-content-text' ) {
23 this.$container
= this.$gallery
.parent();
28 this.setSizeRequirement();
29 this.toggleThumbnails( !!this.$gallery
.attr( 'data-showthumbnails' ) );
30 this.showCurrentImage();
36 this.setSizeRequirement
.bind( this ),
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();
51 * @property {jQuery} $gallery The `<ul>` element of the gallery.
55 * @property {jQuery} $galleryCaption The `<li>` that has the gallery caption.
59 * @property {jQuery} $galleryBox Selection of `<li>` elements that have thumbnails.
63 * @property {jQuery} $carousel The `<li>` elements that contains the carousel.
67 * @property {jQuery} $interface The `<div>` elements that contains the interface buttons.
71 * @property {jQuery} $img The `<img>` element that'll display the current image.
75 * @property {jQuery} $imgLink The `<a>` element that links to the image's File page.
79 * @property {jQuery} $imgCaption The `<p>` element that holds the image caption.
83 * @property {jQuery} $imgContainer The `<div>` element that contains the image.
87 * @property {jQuery} $currentImage The `<li>` element of the current image.
91 * @property {jQuery} $container If the gallery contained in an element that is
92 * not the main content element, then it stores that element.
96 * @property {Object} imageInfoCache A key value pair of thumbnail URLs and image info.
100 * @property {number} imageWidth Width of the image based on viewport size
104 * @property {number} imageHeight Height of the image based on viewport size
105 * the URLs in the required size.
109 OO
.initClass( mw
.GallerySlideshow
);
113 * Draws the carousel and the interface around it.
115 mw
.GallerySlideshow
.prototype.drawCarousel = function () {
116 var next
, prev
, toggle
, interfaceElements
, carouselStack
;
118 this.$carousel
= $( '<li>' ).addClass( 'gallerycarousel' );
120 // Buttons for the interface
121 prev
= new OO
.ui
.ButtonWidget( {
124 } ).on( 'click', this.prevImage
.bind( this ) );
126 next
= new OO
.ui
.ButtonWidget( {
129 } ).on( 'click', this.nextImage
.bind( this ) );
131 toggle
= new OO
.ui
.ButtonWidget( {
133 icon
: 'imageGallery',
134 title
: mw
.msg( 'gallery-slideshow-toggle' )
135 } ).on( 'click', this.toggleThumbnails
.bind( this ) );
137 interfaceElements
= new OO
.ui
.PanelLayout( {
139 classes
: [ 'mw-gallery-slideshow-buttons' ],
140 $content
: $( '<div>' ).append(
146 this.$interface = interfaceElements
.$element
;
148 // Containers for the current image, caption etc.
149 this.$img
= $( '<img>' );
150 this.$imgLink
= $( '<a>' ).append( this.$img
);
151 this.$imgCaption
= $( '<p>' ).attr( 'class', 'mw-gallery-slideshow-caption' );
152 this.$imgContainer
= $( '<div>' )
153 .attr( 'class', 'mw-gallery-slideshow-img-container' )
154 .append( this.$imgLink
);
156 carouselStack
= new OO
.ui
.StackLayout( {
161 new OO
.ui
.PanelLayout( {
163 $content
: this.$imgContainer
165 new OO
.ui
.PanelLayout( {
167 $content
: this.$imgCaption
171 this.$carousel
.append( carouselStack
.$element
);
173 // Append below the caption or as the first element in the gallery
174 if ( this.$galleryCaption
.length
!== 0 ) {
175 this.$galleryCaption
.after( this.$carousel
);
177 this.$gallery
.prepend( this.$carousel
);
182 * Sets the {@link #imageWidth} and {@link #imageHeight} properties
183 * based on the size of the window. Also flushes the
184 * {@link #imageInfoCache} as we'll now need URLs for a different
187 mw
.GallerySlideshow
.prototype.setSizeRequirement = function () {
190 if ( this.$container
!== undefined ) {
191 w
= this.$container
.width() * 0.9;
192 h
= ( this.$container
.height() - this.getChromeHeight() ) * 0.9;
194 w
= this.$imgContainer
.width();
195 h
= Math
.min( $( window
).height() * ( 3 / 4 ), this.$imgContainer
.width() ) - this.getChromeHeight();
198 // Only update and flush the cache if the size changed
199 if ( w
!== this.imageWidth
|| h
!== this.imageHeight
) {
201 this.imageHeight
= h
;
202 this.imageInfoCache
= {};
208 * Gets the height of the interface elements and the
211 * @return {number} Height
213 mw
.GallerySlideshow
.prototype.getChromeHeight = function () {
214 return this.$interface.outerHeight() + this.$galleryCaption
.outerHeight();
218 * Sets the height and width of {@link #$img} based on the
219 * proportion of the image and the values generated by
220 * {@link #setSizeRequirement}.
222 * @return {boolean} Whether or not the image was sized.
224 mw
.GallerySlideshow
.prototype.setImageSize = function () {
225 if ( this.$img
=== undefined || this.$thumbnail
=== undefined ) {
229 // Reset height and width
231 .removeAttr( 'width' )
232 .removeAttr( 'height' );
234 // Stretch image to take up the required size
235 this.$img
.attr( 'height', ( this.imageHeight
- this.$imgCaption
.outerHeight() ) + 'px' );
237 // Make the image smaller in case the current image
238 // size is larger than the original file size.
239 this.getImageInfo( this.$thumbnail
).done( function ( info
) {
240 // NOTE: There will be a jump when resizing the window
241 // because the cache is cleared and this a new network request.
243 info
.thumbwidth
< this.$img
.width() ||
244 info
.thumbheight
< this.$img
.height()
246 this.$img
.attr( 'width', info
.thumbwidth
+ 'px' );
247 this.$img
.attr( 'height', info
.thumbheight
+ 'px' );
255 * Displays the image set as {@link #$currentImage} in the carousel.
257 mw
.GallerySlideshow
.prototype.showCurrentImage = function () {
258 var imageLi
= this.getCurrentImage(),
259 caption
= imageLi
.find( '.gallerytext' );
261 // The order of the following is important for size calculations
262 // 1. Highlight current thumbnail
264 .find( '.gallerybox.slideshow-current' )
265 .removeClass( 'slideshow-current' );
266 imageLi
.addClass( 'slideshow-current' );
269 this.$thumbnail
= imageLi
.find( 'img' );
270 this.$img
.attr( 'src', this.$thumbnail
.attr( 'src' ) );
271 this.$img
.attr( 'alt', this.$thumbnail
.attr( 'alt' ) );
272 this.$imgLink
.attr( 'href', imageLi
.find( 'a' ).eq( 0 ).attr( 'href' ) );
277 .append( caption
.clone() );
279 // 4. Stretch thumbnail to correct size
282 // 5. Load image at the required size
283 this.loadImage( this.$thumbnail
).done( function ( info
, $img
) {
284 // Show this image to the user only if its still the current one
285 if ( this.$thumbnail
.attr( 'src' ) === $img
.attr( 'src' ) ) {
286 this.$img
.attr( 'src', info
.thumburl
);
289 // Keep the next image ready
290 this.loadImage( this.getNextImage().find( 'img' ) );
296 * Loads the full image given the `<img>` element of the thumbnail.
298 * @param {Object} $img
299 * @return {jQuery.Promise} Resolves with the images URL and original
300 * element once the image has loaded.
302 mw
.GallerySlideshow
.prototype.loadImage = function ( $img
) {
303 var img
, d
= $.Deferred();
305 this.getImageInfo( $img
).done( function ( info
) {
307 img
.src
= info
.thumburl
;
308 img
.onload = function () {
309 d
.resolve( info
, $img
);
311 img
.onerror = function () {
314 } ).fail( function () {
322 * Gets the image's info given an `<img>` element.
324 * @param {Object} $img
325 * @return {jQuery.Promise} Resolves with the image's info.
327 mw
.GallerySlideshow
.prototype.getImageInfo = function ( $img
) {
328 var api
, title
, params
,
329 imageSrc
= $img
.attr( 'src' );
331 // Reject promise if there is no thumbnail image
332 if ( $img
[ 0 ] === undefined ) {
333 return $.Deferred().reject();
336 if ( this.imageInfoCache
[ imageSrc
] === undefined ) {
338 // TODO: This supports only gallery of images
339 title
= mw
.Title
.newFromImg( $img
);
343 titles
: title
.toString(),
348 // Check which dimension we need to request, based on
349 // image and container proportions.
350 if ( this.getDimensionToRequest( $img
) === 'height' ) {
351 params
.iiurlheight
= this.imageHeight
;
353 params
.iiurlwidth
= this.imageWidth
;
356 this.imageInfoCache
[ imageSrc
] = api
.get( params
).then( function ( data
) {
357 if ( OO
.getProp( data
, 'query', 'pages', 0, 'imageinfo', 0, 'thumburl' ) !== undefined ) {
358 return data
.query
.pages
[ 0 ].imageinfo
[ 0 ];
360 return $.Deferred().reject();
365 return this.imageInfoCache
[ imageSrc
];
369 * Given an image, the method checks whether to use the height
370 * or the width to request the larger image.
372 * @param {jQuery} $img
375 mw
.GallerySlideshow
.prototype.getDimensionToRequest = function ( $img
) {
376 var ratio
= $img
.width() / $img
.height();
378 if ( this.imageHeight
* ratio
<= this.imageWidth
) {
386 * Toggles visibility of the thumbnails.
388 * @param {boolean} show Optional argument to control the state
390 mw
.GallerySlideshow
.prototype.toggleThumbnails = function ( show
) {
391 this.$galleryBox
.toggle( show
);
392 this.$carousel
.toggleClass( 'mw-gallery-slideshow-thumbnails-toggled', show
);
396 * Getter method for {@link #$currentImage}
400 mw
.GallerySlideshow
.prototype.getCurrentImage = function () {
401 this.$currentImage
= this.$currentImage
|| this.$galleryBox
.eq( 0 );
402 return this.$currentImage
;
406 * Gets the image after the current one. Returns the first image if
407 * the current one is the last.
411 mw
.GallerySlideshow
.prototype.getNextImage = function () {
412 // Not the last image in the gallery
413 if ( this.$currentImage
.next( '.gallerybox' )[ 0 ] !== undefined ) {
414 return this.$currentImage
.next( '.gallerybox' );
416 return this.$galleryBox
.eq( 0 );
421 * Gets the image before the current one. Returns the last image if
422 * the current one is the first.
426 mw
.GallerySlideshow
.prototype.getPrevImage = function () {
427 // Not the first image in the gallery
428 if ( this.$currentImage
.prev( '.gallerybox' )[ 0 ] !== undefined ) {
429 return this.$currentImage
.prev( '.gallerybox' );
431 return this.$galleryBox
.last();
436 * Sets the {@link #$currentImage} to the next one and shows
439 mw
.GallerySlideshow
.prototype.nextImage = function () {
440 this.$currentImage
= this.getNextImage();
441 this.showCurrentImage();
445 * Sets the {@link #$currentImage} to the previous one and shows
448 mw
.GallerySlideshow
.prototype.prevImage = function () {
449 this.$currentImage
= this.getPrevImage();
450 this.showCurrentImage();
453 // Bootstrap all slideshow galleries
454 mw
.hook( 'wikipage.content' ).add( function ( $content
) {
455 $content
.find( '.mw-gallery-slideshow' ).each( function () {
456 // eslint-disable-next-line no-new
457 new mw
.GallerySlideshow( this );
460 }( mediaWiki
, jQuery
, OO
) );