| 1 |
( function () { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
mw.GallerySlideshow = function ( gallery ) { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
this.$gallery = $( gallery ); |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
this.$galleryCaption = this.$gallery.find( '.gallerycaption' ); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
this.$galleryBox = this.$gallery.find( '.gallerybox' ); |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
this.$currentImage = null; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
this.imageInfoCache = {}; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
this.drawCarousel(); |
| 106 |
this.setSizeRequirement(); |
| 107 |
this.toggleThumbnails( !!this.$gallery.attr( 'data-showthumbnails' ) ); |
| 108 |
this.showCurrentImage( true ); |
| 109 |
|
| 110 |
|
| 111 |
$( window ).on( |
| 112 |
'resize', |
| 113 |
OO.ui.debounce( |
| 114 |
this.setSizeRequirement.bind( this ), |
| 115 |
100 |
| 116 |
) |
| 117 |
); |
| 118 |
|
| 119 |
|
| 120 |
this.$galleryBox.on( 'click', ( e ) => { |
| 121 |
this.$currentImage = $( e.currentTarget ); |
| 122 |
this.showCurrentImage(); |
| 123 |
return false; |
| 124 |
} ); |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
OO.initClass( mw.GallerySlideshow ); |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
mw.GallerySlideshow.prototype.drawCarousel = function () { |
| 135 |
this.$carousel = $( '<li>' ).addClass( 'gallerycarousel' ); |
| 136 |
|
| 137 |
|
| 138 |
const prevButton = new OO.ui.ButtonWidget( { |
| 139 |
framed: false, |
| 140 |
icon: 'previous' |
| 141 |
} ).connect( this, { click: 'prevImage' } ); |
| 142 |
|
| 143 |
const nextButton = new OO.ui.ButtonWidget( { |
| 144 |
framed: false, |
| 145 |
icon: 'next' |
| 146 |
} ).connect( this, { click: 'nextImage' } ); |
| 147 |
|
| 148 |
const toggleButton = new OO.ui.ButtonWidget( { |
| 149 |
framed: false, |
| 150 |
icon: 'imageGallery', |
| 151 |
title: mw.msg( 'gallery-slideshow-toggle' ) |
| 152 |
} ).connect( this, { click: 'toggleThumbnails' } ); |
| 153 |
|
| 154 |
const interfaceElements = new OO.ui.PanelLayout( { |
| 155 |
expanded: false, |
| 156 |
classes: [ 'mw-gallery-slideshow-buttons' ], |
| 157 |
$content: $( '<div>' ).append( |
| 158 |
prevButton.$element, |
| 159 |
toggleButton.$element, |
| 160 |
nextButton.$element |
| 161 |
) |
| 162 |
} ); |
| 163 |
this.$interface = interfaceElements.$element; |
| 164 |
|
| 165 |
|
| 166 |
this.$imgCaption = $( '<p>' ).attr( 'class', 'mw-gallery-slideshow-caption' ); |
| 167 |
this.$imgContainer = $( '<div>' ) |
| 168 |
.attr( 'class', 'mw-gallery-slideshow-img-container' ); |
| 169 |
|
| 170 |
const carouselStack = new OO.ui.StackLayout( { |
| 171 |
continuous: true, |
| 172 |
expanded: false, |
| 173 |
items: [ |
| 174 |
interfaceElements, |
| 175 |
new OO.ui.PanelLayout( { |
| 176 |
expanded: false, |
| 177 |
$content: this.$imgContainer |
| 178 |
} ), |
| 179 |
new OO.ui.PanelLayout( { |
| 180 |
expanded: false, |
| 181 |
$content: this.$imgCaption |
| 182 |
} ) |
| 183 |
] |
| 184 |
} ); |
| 185 |
this.$carousel.append( carouselStack.$element ); |
| 186 |
|
| 187 |
|
| 188 |
if ( this.$galleryCaption.length !== 0 ) { |
| 189 |
this.$galleryCaption.after( this.$carousel ); |
| 190 |
} else { |
| 191 |
this.$gallery.prepend( this.$carousel ); |
| 192 |
} |
| 193 |
}; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
mw.GallerySlideshow.prototype.setSizeRequirement = function () { |
| 202 |
let w = this.$imgContainer.width(), |
| 203 |
h = Math.min( $( window ).height() * ( 3 / 4 ), this.$imgContainer.width() ) - this.getChromeHeight(); |
| 204 |
|
| 205 |
|
| 206 |
w = Math.round( w ); |
| 207 |
h = Math.round( h ); |
| 208 |
|
| 209 |
|
| 210 |
if ( w !== this.imageWidth || h !== this.imageHeight ) { |
| 211 |
this.imageWidth = w; |
| 212 |
this.imageHeight = h; |
| 213 |
this.imageInfoCache = {}; |
| 214 |
this.setImageSize(); |
| 215 |
} |
| 216 |
}; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
mw.GallerySlideshow.prototype.getChromeHeight = function () { |
| 225 |
return this.$interface.outerHeight() + ( this.$galleryCaption.outerHeight() || 0 ); |
| 226 |
}; |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
mw.GallerySlideshow.prototype.setImageSize = function () { |
| 234 |
if ( this.$img === undefined || this.$thumbnail === undefined ) { |
| 235 |
return; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
this.$img |
| 240 |
.removeAttr( 'width' ) |
| 241 |
.removeAttr( 'height' ); |
| 242 |
|
| 243 |
|
| 244 |
this.$img.attr( 'height', ( this.imageHeight - this.$imgCaption.outerHeight() ) + 'px' ); |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
this.getImageInfo( this.$thumbnail ).then( ( info ) => { |
| 249 |
|
| 250 |
|
| 251 |
if ( |
| 252 |
info.thumbwidth < this.$img.width() || |
| 253 |
info.thumbheight < this.$img.height() |
| 254 |
) { |
| 255 |
this.$img.attr( { |
| 256 |
width: info.thumbwidth + 'px', |
| 257 |
height: info.thumbheight + 'px' |
| 258 |
} ); |
| 259 |
} |
| 260 |
} ); |
| 261 |
}; |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
mw.GallerySlideshow.prototype.showCurrentImage = function ( init ) { |
| 270 |
const $imageLi = this.getCurrentImage(); |
| 271 |
const $caption = $imageLi.find( '.gallerytext' ); |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
this.$gallery |
| 276 |
.find( '.gallerybox.slideshow-current' ) |
| 277 |
.removeClass( 'slideshow-current' ); |
| 278 |
$imageLi.addClass( 'slideshow-current' ); |
| 279 |
|
| 280 |
this.$thumbnail = $imageLi.find( 'img' ); |
| 281 |
if ( this.$thumbnail.length ) { |
| 282 |
|
| 283 |
this.$img = $( '<img>' ).attr( { |
| 284 |
src: this.$thumbnail.attr( 'src' ), |
| 285 |
alt: this.$thumbnail.attr( 'alt' ) |
| 286 |
} ); |
| 287 |
|
| 288 |
const $imgLink = $( '<a>' ).addClass( 'image' ) |
| 289 |
.attr( 'href', $imageLi.find( 'a' ).eq( 0 ).attr( 'href' ) ) |
| 290 |
.append( this.$img ); |
| 291 |
|
| 292 |
this.$imgContainer.empty().append( $imgLink ); |
| 293 |
} else { |
| 294 |
|
| 295 |
this.$imgContainer.text( $imageLi.find( '.thumb' ).text() ); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
this.$imgCaption |
| 300 |
.empty() |
| 301 |
.append( $caption.clone() ); |
| 302 |
|
| 303 |
if ( !this.$thumbnail.length ) { |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
this.setImageSize(); |
| 309 |
|
| 310 |
const $thumbnail = this.$thumbnail; |
| 311 |
|
| 312 |
this.loadImage( this.$thumbnail ).done( ( info ) => { |
| 313 |
|
| 314 |
if ( this.$thumbnail.attr( 'src' ) === $thumbnail.attr( 'src' ) ) { |
| 315 |
this.$img.attr( 'src', info.thumburl ); |
| 316 |
this.setImageSize(); |
| 317 |
|
| 318 |
if ( !init ) { |
| 319 |
mw.hook( 'wikipage.content' ).fire( this.$imgContainer ); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
this.loadImage( this.getNextImage().find( 'img' ) ); |
| 324 |
} |
| 325 |
} ).fail( () => { |
| 326 |
|
| 327 |
const title = mw.Title.newFromImg( this.$img ); |
| 328 |
this.$imgContainer.text( title ? title.getMainText() : '' ); |
| 329 |
} ); |
| 330 |
}; |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
mw.GallerySlideshow.prototype.loadImage = function ( $img ) { |
| 340 |
return this.getImageInfo( $img ).then( ( info ) => { |
| 341 |
const d = $.Deferred(); |
| 342 |
const img = new Image(); |
| 343 |
img.src = info.thumburl; |
| 344 |
img.onload = function () { |
| 345 |
d.resolve( info ); |
| 346 |
}; |
| 347 |
img.onerror = function () { |
| 348 |
d.reject(); |
| 349 |
}; |
| 350 |
return d.promise(); |
| 351 |
} ); |
| 352 |
}; |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
mw.GallerySlideshow.prototype.getImageInfo = function ( $img ) { |
| 361 |
const imageSrc = $img.attr( 'src' ); |
| 362 |
|
| 363 |
|
| 364 |
if ( $img[ 0 ] === undefined ) { |
| 365 |
return $.Deferred().reject(); |
| 366 |
} |
| 367 |
|
| 368 |
if ( this.imageInfoCache[ imageSrc ] === undefined ) { |
| 369 |
const api = new mw.Api(); |
| 370 |
|
| 371 |
const title = mw.Title.newFromImg( $img ); |
| 372 |
const params = { |
| 373 |
action: 'query', |
| 374 |
formatversion: 2, |
| 375 |
titles: title.toString(), |
| 376 |
prop: 'imageinfo', |
| 377 |
iiprop: 'url' |
| 378 |
}; |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
if ( this.getDimensionToRequest( $img ) === 'height' ) { |
| 383 |
params.iiurlheight = this.imageHeight; |
| 384 |
} else { |
| 385 |
params.iiurlwidth = this.imageWidth; |
| 386 |
} |
| 387 |
|
| 388 |
this.imageInfoCache[ imageSrc ] = api.get( params ).then( ( data ) => { |
| 389 |
if ( OO.getProp( data, 'query', 'pages', 0, 'imageinfo', 0, 'thumburl' ) !== undefined ) { |
| 390 |
return data.query.pages[ 0 ].imageinfo[ 0 ]; |
| 391 |
} else { |
| 392 |
return $.Deferred().reject(); |
| 393 |
} |
| 394 |
} ); |
| 395 |
} |
| 396 |
|
| 397 |
return this.imageInfoCache[ imageSrc ]; |
| 398 |
}; |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
mw.GallerySlideshow.prototype.getDimensionToRequest = function ( $img ) { |
| 408 |
const ratio = $img.width() / $img.height(); |
| 409 |
|
| 410 |
if ( this.imageHeight * ratio <= this.imageWidth ) { |
| 411 |
return 'height'; |
| 412 |
} else { |
| 413 |
return 'width'; |
| 414 |
} |
| 415 |
}; |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
mw.GallerySlideshow.prototype.toggleThumbnails = function ( show ) { |
| 423 |
this.$galleryBox.toggle( show ); |
| 424 |
this.$carousel.toggleClass( 'mw-gallery-slideshow-thumbnails-toggled', show ); |
| 425 |
}; |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
mw.GallerySlideshow.prototype.getCurrentImage = function () { |
| 433 |
this.$currentImage = this.$currentImage || this.$galleryBox.eq( 0 ); |
| 434 |
return this.$currentImage; |
| 435 |
}; |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
mw.GallerySlideshow.prototype.getNextImage = function () { |
| 444 |
|
| 445 |
if ( this.$currentImage.next( '.gallerybox' )[ 0 ] !== undefined ) { |
| 446 |
return this.$currentImage.next( '.gallerybox' ); |
| 447 |
} else { |
| 448 |
return this.$galleryBox.eq( 0 ); |
| 449 |
} |
| 450 |
}; |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
mw.GallerySlideshow.prototype.getPrevImage = function () { |
| 459 |
|
| 460 |
if ( this.$currentImage.prev( '.gallerybox' )[ 0 ] !== undefined ) { |
| 461 |
return this.$currentImage.prev( '.gallerybox' ); |
| 462 |
} else { |
| 463 |
return this.$galleryBox.last(); |
| 464 |
} |
| 465 |
}; |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
mw.GallerySlideshow.prototype.nextImage = function () { |
| 472 |
this.$currentImage = this.getNextImage(); |
| 473 |
this.showCurrentImage(); |
| 474 |
}; |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
mw.GallerySlideshow.prototype.prevImage = function () { |
| 481 |
this.$currentImage = this.getPrevImage(); |
| 482 |
this.showCurrentImage(); |
| 483 |
}; |
| 484 |
|
| 485 |
|
| 486 |
mw.hook( 'wikipage.content' ).add( ( $content ) => { |
| 487 |
$content.find( '.mw-gallery-slideshow' ).filter( function () { |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
return $( this ).find( 'img' ).length > 0; |
| 492 |
} ).each( function () { |
| 493 |
|
| 494 |
new mw.GallerySlideshow( this ); |
| 495 |
} ); |
| 496 |
} ); |
| 497 |
|
| 498 |
}() ); |