X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=resources%2Fsrc%2Fmediawiki.page.gallery.slideshow.js;h=17caa9e04dc338ccba2aca9f4ebf2dc25aa194b0;hb=9e8439e79d67788916d488f645108f79016d9aca;hp=204d9154abce865d8fac424c95ddbb1203a0dea9;hpb=67dc9082463b6023fa1e79cc22cec3ac8589b322;p=lhc%2Fweb%2Fwiklou.git diff --git a/resources/src/mediawiki.page.gallery.slideshow.js b/resources/src/mediawiki.page.gallery.slideshow.js index 204d9154ab..a36e409aa5 100644 --- a/resources/src/mediawiki.page.gallery.slideshow.js +++ b/resources/src/mediawiki.page.gallery.slideshow.js @@ -19,15 +19,12 @@ this.$galleryBox = this.$gallery.find( '.gallerybox' ); this.$currentImage = null; this.imageInfoCache = {}; - if ( this.$gallery.parent().attr( 'id' ) !== 'mw-content-text' ) { - this.$container = this.$gallery.parent(); - } // Initialize this.drawCarousel(); this.setSizeRequirement(); this.toggleThumbnails( !!this.$gallery.attr( 'data-showthumbnails' ) ); - this.showCurrentImage(); + this.showCurrentImage( true ); // Events $( window ).on( @@ -71,10 +68,6 @@ * @property {jQuery} $img The `` element that'll display the current image. */ - /** - * @property {jQuery} $imgLink The `` element that links to the image's File page. - */ - /** * @property {jQuery} $imgCaption The `

` element that holds the image caption. */ @@ -87,11 +80,6 @@ * @property {jQuery} $currentImage The `

  • ` element of the current image. */ - /** - * @property {jQuery} $container If the gallery contained in an element that is - * not the main content element, then it stores that element. - */ - /** * @property {Object} imageInfoCache A key value pair of thumbnail URLs and image info. */ @@ -113,45 +101,42 @@ * Draws the carousel and the interface around it. */ mw.GallerySlideshow.prototype.drawCarousel = function () { - var next, prev, toggle, interfaceElements, carouselStack; + var nextButton, prevButton, toggleButton, interfaceElements, carouselStack; this.$carousel = $( '
  • ' ).addClass( 'gallerycarousel' ); // Buttons for the interface - prev = new OO.ui.ButtonWidget( { + prevButton = new OO.ui.ButtonWidget( { framed: false, icon: 'previous' - } ).on( 'click', this.prevImage.bind( this ) ); + } ).connect( this, { click: 'prevImage' } ); - next = new OO.ui.ButtonWidget( { + nextButton = new OO.ui.ButtonWidget( { framed: false, icon: 'next' - } ).on( 'click', this.nextImage.bind( this ) ); + } ).connect( this, { click: 'nextImage' } ); - toggle = new OO.ui.ButtonWidget( { + toggleButton = new OO.ui.ButtonWidget( { framed: false, icon: 'imageGallery', title: mw.msg( 'gallery-slideshow-toggle' ) - } ).on( 'click', this.toggleThumbnails.bind( this ) ); + } ).connect( this, { click: 'toggleThumbnails' } ); interfaceElements = new OO.ui.PanelLayout( { expanded: false, classes: [ 'mw-gallery-slideshow-buttons' ], $content: $( '
    ' ).append( - prev.$element, - toggle.$element, - next.$element + prevButton.$element, + toggleButton.$element, + nextButton.$element ) } ); this.$interface = interfaceElements.$element; // Containers for the current image, caption etc. - this.$img = $( '' ); - this.$imgLink = $( '' ).append( this.$img ); this.$imgCaption = $( '

    ' ).attr( 'class', 'mw-gallery-slideshow-caption' ); this.$imgContainer = $( '

    ' ) - .attr( 'class', 'mw-gallery-slideshow-img-container' ) - .append( this.$imgLink ); + .attr( 'class', 'mw-gallery-slideshow-img-container' ); carouselStack = new OO.ui.StackLayout( { continuous: true, @@ -185,15 +170,8 @@ * size. */ mw.GallerySlideshow.prototype.setSizeRequirement = function () { - var w, h; - - if ( this.$container !== undefined ) { - w = this.$container.width() * 0.9; - h = ( this.$container.height() - this.getChromeHeight() ) * 0.9; - } else { - w = this.$imgContainer.width(); + var w = this.$imgContainer.width(), h = Math.min( $( window ).height() * ( 3 / 4 ), this.$imgContainer.width() ) - this.getChromeHeight(); - } // Only update and flush the cache if the size changed if ( w !== this.imageWidth || h !== this.imageHeight ) { @@ -211,19 +189,17 @@ * @return {number} Height */ mw.GallerySlideshow.prototype.getChromeHeight = function () { - return this.$interface.outerHeight() + this.$galleryCaption.outerHeight(); + return this.$interface.outerHeight() + ( this.$galleryCaption.outerHeight() || 0 ); }; /** * Sets the height and width of {@link #$img} based on the * proportion of the image and the values generated by * {@link #setSizeRequirement}. - * - * @return {boolean} Whether or not the image was sized. */ mw.GallerySlideshow.prototype.setImageSize = function () { if ( this.$img === undefined || this.$thumbnail === undefined ) { - return false; + return; } // Reset height and width @@ -236,86 +212,110 @@ // Make the image smaller in case the current image // size is larger than the original file size. - this.getImageInfo( this.$thumbnail ).done( function ( info ) { + this.getImageInfo( this.$thumbnail ).then( function ( info ) { // NOTE: There will be a jump when resizing the window // because the cache is cleared and this a new network request. if ( info.thumbwidth < this.$img.width() || info.thumbheight < this.$img.height() ) { - this.$img.attr( 'width', info.thumbwidth + 'px' ); - this.$img.attr( 'height', info.thumbheight + 'px' ); + this.$img.attr( { + width: info.thumbwidth + 'px', + height: info.thumbheight + 'px' + } ); } }.bind( this ) ); - - return true; }; /** * Displays the image set as {@link #$currentImage} in the carousel. + * + * @param {boolean} init Image being show during gallery init (i.e. first image) */ - mw.GallerySlideshow.prototype.showCurrentImage = function () { - var imageLi = this.getCurrentImage(), - caption = imageLi.find( '.gallerytext' ); + mw.GallerySlideshow.prototype.showCurrentImage = function ( init ) { + var $thumbnail, $imgLink, + $imageLi = this.getCurrentImage(), + $caption = $imageLi.find( '.gallerytext' ); // The order of the following is important for size calculations // 1. Highlight current thumbnail this.$gallery .find( '.gallerybox.slideshow-current' ) .removeClass( 'slideshow-current' ); - imageLi.addClass( 'slideshow-current' ); + $imageLi.addClass( 'slideshow-current' ); + + this.$thumbnail = $imageLi.find( 'img' ); + if ( this.$thumbnail.length ) { + // 2. Create and show thumbnail + this.$img = $( '' ).attr( { + src: this.$thumbnail.attr( 'src' ), + alt: this.$thumbnail.attr( 'alt' ) + } ); + // 'image' class required for detection by MultimediaViewer + $imgLink = $( '' ).addClass( 'image' ) + .attr( 'href', $imageLi.find( 'a' ).eq( 0 ).attr( 'href' ) ) + .append( this.$img ); - // 2. Show thumbnail - this.$thumbnail = imageLi.find( 'img' ); - this.$img.attr( 'src', this.$thumbnail.attr( 'src' ) ); - this.$img.attr( 'alt', this.$thumbnail.attr( 'alt' ) ); - this.$imgLink.attr( 'href', imageLi.find( 'a' ).eq( 0 ).attr( 'href' ) ); + this.$imgContainer.empty().append( $imgLink ); + } else { + // 2b. No image found (e.g. file doesn't exist) + this.$imgContainer.text( $imageLi.find( '.thumb' ).text() ); + } // 3. Copy caption this.$imgCaption .empty() - .append( caption.clone() ); + .append( $caption.clone() ); + + if ( !this.$thumbnail.length ) { + return; + } // 4. Stretch thumbnail to correct size this.setImageSize(); + $thumbnail = this.$thumbnail; // 5. Load image at the required size - this.loadImage( this.$thumbnail ).done( function ( info, $img ) { + this.loadImage( this.$thumbnail ).done( function ( info ) { // Show this image to the user only if its still the current one - if ( this.$thumbnail.attr( 'src' ) === $img.attr( 'src' ) ) { + if ( this.$thumbnail.attr( 'src' ) === $thumbnail.attr( 'src' ) ) { this.$img.attr( 'src', info.thumburl ); this.setImageSize(); + // Don't fire hook twice during init + if ( !init ) { + mw.hook( 'wikipage.content' ).fire( this.$imgContainer ); + } - // Keep the next image ready + // Pre-fetch the next image this.loadImage( this.getNextImage().find( 'img' ) ); } + }.bind( this ) ).fail( function () { + // Image didn't load + var title = mw.Title.newFromImg( this.$img ); + this.$imgContainer.text( title ? title.getMainText() : '' ); }.bind( this ) ); }; /** * Loads the full image given the `` element of the thumbnail. * - * @param {Object} $img + * @param {jQuery} $img * @return {jQuery.Promise} Resolves with the images URL and original - * element once the image has loaded. + * element once the image has loaded. */ mw.GallerySlideshow.prototype.loadImage = function ( $img ) { - var img, d = $.Deferred(); - - this.getImageInfo( $img ).done( function ( info ) { + return this.getImageInfo( $img ).then( function ( info ) { + var img, d = $.Deferred(); img = new Image(); img.src = info.thumburl; img.onload = function () { - d.resolve( info, $img ); + d.resolve( info ); }; img.onerror = function () { d.reject(); }; - } ).fail( function () { - d.reject(); + return d.promise(); } ); - - return d.promise(); }; /**