Merge "Add config for serving main Page from the domain root"
[lhc/web/wiklou.git] / resources / src / mediawiki.page.gallery.slideshow.js
index b62b45e..a36e409 100644 (file)
@@ -24,7 +24,7 @@
                this.drawCarousel();
                this.setSizeRequirement();
                this.toggleThumbnails( !!this.$gallery.attr( 'data-showthumbnails' ) );
-               this.showCurrentImage();
+               this.showCurrentImage( true );
 
                // Events
                $( window ).on(
         * @property {jQuery} $img The `<img>` element that'll display the current image.
         */
 
-       /**
-        * @property {jQuery} $imgLink The `<a>` element that links to the image's File page.
-        */
-
        /**
         * @property {jQuery} $imgCaption The `<p>` element that holds the image caption.
         */
                this.$interface = interfaceElements.$element;
 
                // Containers for the current image, caption etc.
-               this.$img = $( '<img>' );
-               this.$imgLink = $( '<a>' ).append( this.$img );
                this.$imgCaption = $( '<p>' ).attr( 'class', 'mw-gallery-slideshow-caption' );
                this.$imgContainer = $( '<div>' )
-                       .attr( 'class', 'mw-gallery-slideshow-img-container' )
-                       .append( this.$imgLink );
+                       .attr( 'class', 'mw-gallery-slideshow-img-container' );
 
                carouselStack = new OO.ui.StackLayout( {
                        continuous: true,
 
                // 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 (
 
        /**
         * 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 $thumbnail,
+       mw.GallerySlideshow.prototype.showCurrentImage = function ( init ) {
+               var $thumbnail, $imgLink,
                        $imageLi = this.getCurrentImage(),
                        $caption = $imageLi.find( '.gallerytext' );
 
                        .removeClass( 'slideshow-current' );
                $imageLi.addClass( 'slideshow-current' );
 
-               // 2. Show thumbnail
                this.$thumbnail = $imageLi.find( 'img' );
-               this.$img.attr( {
-                       src: this.$thumbnail.attr( 'src' ),
-                       alt: this.$thumbnail.attr( 'alt' )
-               } );
-               this.$imgLink.attr( 'href', $imageLi.find( 'a' ).eq( 0 ).attr( 'href' ) );
+               if ( this.$thumbnail.length ) {
+                       // 2. Create and show thumbnail
+                       this.$img = $( '<img>' ).attr( {
+                               src: this.$thumbnail.attr( 'src' ),
+                               alt: this.$thumbnail.attr( 'alt' )
+                       } );
+                       // 'image' class required for detection by MultimediaViewer
+                       $imgLink = $( '<a>' ).addClass( 'image' )
+                               .attr( 'href', $imageLi.find( 'a' ).eq( 0 ).attr( 'href' ) )
+                               .append( this.$img );
+
+                       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() );
 
+               if ( !this.$thumbnail.length ) {
+                       return;
+               }
+
                // 4. Stretch thumbnail to correct size
                this.setImageSize();
 
                        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 );
+                               }
 
                                // 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 ) );
        };
 
         *  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 () {
                        img.onerror = function () {
                                d.reject();
                        };
-               } ).fail( function () {
-                       d.reject();
+                       return d.promise();
                } );
-
-               return d.promise();
        };
 
        /**