Merge "Show protection log on creation-protected pages"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.ChangesListWrapperWidget.js
index c7754be..42fb5cc 100644 (file)
@@ -3,17 +3,18 @@
         * List of changes
         *
         * @extends OO.ui.Widget
-        * @mixins OO.ui.mixin.PendingElement
         *
         * @constructor
         * @param {mw.rcfilters.dm.FiltersViewModel} filtersViewModel View model
         * @param {mw.rcfilters.dm.ChangesListViewModel} changesListViewModel View model
+        * @param {mw.rcfilters.Controller} controller
         * @param {jQuery} $changesListRoot Root element of the changes list to attach to
-        * @param {Object} config Configuration object
+        * @param {Object} [config] Configuration object
         */
        mw.rcfilters.ui.ChangesListWrapperWidget = function MwRcfiltersUiChangesListWrapperWidget(
                filtersViewModel,
                changesListViewModel,
+               controller,
                $changesListRoot,
                config
        ) {
 
                // Parent
                mw.rcfilters.ui.ChangesListWrapperWidget.parent.call( this, config );
-               // Mixin constructors
-               OO.ui.mixin.PendingElement.call( this, config );
 
                this.filtersViewModel = filtersViewModel;
                this.changesListViewModel = changesListViewModel;
+               this.controller = controller;
 
                // Events
                this.filtersViewModel.connect( this, {
@@ -36,7 +36,8 @@
                } );
                this.changesListViewModel.connect( this, {
                        invalidate: 'onModelInvalidate',
-                       update: 'onModelUpdate'
+                       update: 'onModelUpdate',
+                       newChangesExist: 'onNewChangesExist'
                } );
 
                this.$element
 
                // Set up highlight containers
                this.setupHighlightContainers( this.$element );
+
+               this.setupNewChangesButtonContainer( this.$element );
        };
 
        /* Initialization */
 
        OO.inheritClass( mw.rcfilters.ui.ChangesListWrapperWidget, OO.ui.Widget );
-       OO.mixinClass( mw.rcfilters.ui.ChangesListWrapperWidget, OO.ui.mixin.PendingElement );
 
        /**
         * Respond to the highlight feature being toggled on and off
         * Respond to changes list model invalidate
         */
        mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onModelInvalidate = function () {
-               this.pushPending();
+               $( '.rcfilters-spinner' ).removeClass( 'mw-rcfilters-ui-ready' );
+               this.$element.removeClass( 'mw-rcfilters-ui-ready' );
        };
 
        /**
         * Respond to changes list model update
         *
         * @param {jQuery|string} $changesListContent The content of the updated changes list
+        * @param {jQuery} $fieldset The content of the updated fieldset
+        * @param {boolean} isInitialDOM Whether $changesListContent is the existing (already attached) DOM
+        * @param {boolean} from Timestamp of the new changes
         */
-       mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onModelUpdate = function ( $changesListContent ) {
+       mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onModelUpdate = function (
+               $changesListContent, $fieldset, isInitialDOM, from
+       ) {
                var conflictItem,
                        $message = $( '<div>' )
                                .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results' ),
-                       isEmpty = $changesListContent === 'NO_RESULTS';
+                       isEmpty = $changesListContent === 'NO_RESULTS',
+                       $lastSeen,
+                       $indicator,
+                       $newChanges = $( [] ),
+                       // For enhanced mode, we have to load these modules, which are
+                       // not loaded for the 'regular' mode in the backend
+                       loaderPromise = mw.user.options.get( 'usenewrc' ) ?
+                               mw.loader.using( [ 'mediawiki.special.changeslist.enhanced', 'mediawiki.icon' ] ) :
+                               $.Deferred().resolve(),
+                       widget = this;
 
                this.$element.toggleClass( 'mw-changeslist', !isEmpty );
                if ( isEmpty ) {
-                       this.$changesListContent = null;
                        this.$element.empty();
 
                        if ( this.filtersViewModel.hasConflict() ) {
 
                        this.$element.append( $message );
                } else {
-                       this.$changesListContent = $changesListContent;
-                       this.$element.empty().append( this.$changesListContent );
+                       if ( !isInitialDOM ) {
+                               this.$element.empty().append( $changesListContent );
+
+                               if ( from ) {
+                                       $lastSeen = null;
+                                       this.$element.find( 'li[data-mw-ts]' ).each( function () {
+                                               var $li = $( this ),
+                                                       ts = $li.data( 'mw-ts' );
+
+                                               if ( ts >= from ) {
+                                                       $newChanges = $newChanges.add( $li );
+                                               } else if ( $lastSeen === null ) {
+                                                       $lastSeen = $li;
+                                                       return false;
+                                               }
+                                       } );
+
+                                       if ( $lastSeen ) {
+                                               $indicator = $( '<div>' )
+                                                       .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-previousChangesIndicator' )
+                                                       .text( mw.message( 'rcfilters-previous-changes-label' ).text() );
+
+                                               $indicator.on( 'click', function () {
+                                                       $indicator.detach();
+                                               } );
+
+                                               $lastSeen.before( $indicator );
+                                       }
+
+                                       $newChanges
+                                               .hide()
+                                               .fadeIn( 1000 );
+                               }
+                       }
+
                        // Set up highlight containers
                        this.setupHighlightContainers( this.$element );
 
                        // Apply highlight
                        this.applyHighlight();
 
-                       // Make sure enhanced RC re-initializes correctly
-                       mw.hook( 'wikipage.content' ).fire( this.$element );
                }
-               this.popPending();
+
+               loaderPromise.done( function () {
+                       if ( !isInitialDOM && !isEmpty ) {
+                               // Make sure enhanced RC re-initializes correctly
+                               mw.hook( 'wikipage.content' ).fire( widget.$element );
+                       }
+
+                       $( '.rcfilters-spinner' ).addClass( 'mw-rcfilters-ui-ready' );
+                       widget.$element.addClass( 'mw-rcfilters-ui-ready' );
+               } );
+       };
+
+       /**
+        * Respond to changes list model newChangesExist
+        *
+        * @param {boolean} newChangesExist Whether new changes exist
+        */
+       mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onNewChangesExist = function ( newChangesExist ) {
+               this.showNewChangesLink.toggle( newChangesExist );
+       };
+
+       /**
+        * Respond to the user clicking the 'show new changes' button
+        */
+       mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onShowNewChangesClick = function () {
+               this.controller.showNewChanges();
+       };
+
+       /**
+        * Setup the container for the 'new changes' button.
+        *
+        * @param {jQuery} $content
+        */
+       mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupNewChangesButtonContainer = function ( $content ) {
+               this.showNewChangesLink = new OO.ui.ButtonWidget( {
+                       framed: false,
+                       label: mw.message( 'rcfilters-show-new-changes' ).text(),
+                       flags: [ 'progressive' ]
+               } );
+               this.showNewChangesLink.connect( this, { click: 'onShowNewChangesClick' } );
+               this.showNewChangesLink.toggle( false );
+
+               $content.before(
+                       $( '<div>' )
+                               .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-newChanges' )
+                               .append( this.showNewChangesLink.$element )
+               );
        };
 
        /**
         * @param {jQuery|string} $content The content of the updated changes list
         */
        mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupHighlightContainers = function ( $content ) {
-               var $highlights = $( '<div>' )
-                               .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights' )
+               var uri = new mw.Uri(),
+                       highlightClass = 'mw-rcfilters-ui-changesListWrapperWidget-highlights',
+                       $highlights = $( '<div>' )
+                               .addClass( highlightClass )
                                .append(
                                        $( '<div>' )
                                                .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-none' )
                        );
                } );
 
-               if ( Number( mw.user.options.get( 'usenewrc' ) ) ) {
+               if (
+                       ( uri.query.enhanced !== undefined && Number( uri.query.enhanced ) ) ||
+                       ( uri.query.enhanced === undefined && Number( mw.user.options.get( 'usenewrc' ) ) )
+               ) {
                        // Enhanced RC
                        $content.find( 'td.mw-enhanced-rc' )
                                .parent()