Merge "Improve "selfmove" message's wording"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.ChangesListWrapperWidget.js
index 42fb5cc..ea32e36 100644 (file)
                this.filtersViewModel = filtersViewModel;
                this.changesListViewModel = changesListViewModel;
                this.controller = controller;
+               this.highlightClasses = null;
+               this.filtersModelInitialized = false;
 
                // Events
                this.filtersViewModel.connect( this, {
                        itemUpdate: 'onItemUpdate',
-                       highlightChange: 'onHighlightChange'
+                       highlightChange: 'onHighlightChange',
+                       initialize: 'onFiltersModelInitialize'
                } );
                this.changesListViewModel.connect( this, {
                        invalidate: 'onModelInvalidate',
                        // We handle our own display/hide of the empty results message
                        .removeClass( 'mw-changeslist-empty' );
 
-               // Set up highlight containers
-               this.setupHighlightContainers( this.$element );
-
-               this.setupNewChangesButtonContainer( this.$element );
+               this.setupNewChangesButtonContainer();
        };
 
        /* Initialization */
 
        OO.inheritClass( mw.rcfilters.ui.ChangesListWrapperWidget, OO.ui.Widget );
 
+       /**
+        * Respond to filters model initialize event
+        */
+       mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onFiltersModelInitialize = function () {
+               this.filtersModelInitialized = true;
+               // Set up highlight containers. We need to wait for the filters model
+               // to be initialized, so we can make sure we have all the css class definitions
+               // we get from the server with our filters
+               this.setupHighlightContainers( this.$element );
+       };
+
+       /**
+        * Get all available highlight classes
+        *
+        * @return {string[]} An array of available highlight class names
+        */
+       mw.rcfilters.ui.ChangesListWrapperWidget.prototype.getHighlightClasses = function () {
+               if ( !this.highlightClasses || !this.highlightClasses.length ) {
+                       this.highlightClasses = this.filtersViewModel.getItemsSupportingHighlights()
+                               .map( function ( filterItem ) {
+                                       return filterItem.getCssClass();
+                               } );
+               }
+
+               return this.highlightClasses;
+       };
+
        /**
         * Respond to the highlight feature being toggled on and off
         *
@@ -72,7 +99,7 @@
         * Respond to a filter item model update
         */
        mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onItemUpdate = function () {
-               if ( this.filtersViewModel.isHighlightEnabled() ) {
+               if ( this.filtersModelInitialized && this.filtersViewModel.isHighlightEnabled() ) {
                        this.clearHighlight();
                        this.applyHighlight();
                }
         * Respond to changes list model invalidate
         */
        mw.rcfilters.ui.ChangesListWrapperWidget.prototype.onModelInvalidate = function () {
-               $( '.rcfilters-spinner' ).removeClass( 'mw-rcfilters-ui-ready' );
-               this.$element.removeClass( 'mw-rcfilters-ui-ready' );
+               $( 'body' ).addClass( 'mw-rcfilters-ui-loading' );
        };
 
        /**
                        $message = $( '<div>' )
                                .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-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' ) ?
                                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 );
+                                       this.emphasizeNewChanges( from );
                                }
                        }
 
                                mw.hook( 'wikipage.content' ).fire( widget.$element );
                        }
 
-                       $( '.rcfilters-spinner' ).addClass( 'mw-rcfilters-ui-ready' );
-                       widget.$element.addClass( 'mw-rcfilters-ui-ready' );
+                       $( 'body' ).removeClass( 'mw-rcfilters-ui-loading' );
                } );
        };
 
+       /**
+        * Emphasize the elements (or groups) newer than the 'from' parameter
+        * @param {string} from Anything newer than this is considered 'new'
+        */
+       mw.rcfilters.ui.ChangesListWrapperWidget.prototype.emphasizeNewChanges = function ( from ) {
+               var $firstNew,
+                       $indicator,
+                       $newChanges = $( [] ),
+                       selector = this.inEnhancedMode() ?
+                               'table.mw-enhanced-rc[data-mw-ts]' :
+                               'li[data-mw-ts]',
+                       set = this.$element.find( selector ),
+                       length = set.length;
+
+               set.each( function ( index ) {
+                       var $this = $( this ),
+                               ts = $this.data( 'mw-ts' );
+
+                       if ( ts >= from ) {
+                               $newChanges = $newChanges.add( $this );
+                               $firstNew = $this;
+
+                               // guards against putting the marker after the last element
+                               if ( index === ( length - 1 ) ) {
+                                       $firstNew = null;
+                               }
+                       }
+               } );
+
+               if ( $firstNew ) {
+                       $indicator = $( '<div>' )
+                               .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-previousChangesIndicator' );
+
+                       $firstNew.after( $indicator );
+               }
+
+               $newChanges
+                       .hide()
+                       .fadeIn( 1000 );
+       };
+
        /**
         * Respond to changes list model newChangesExist
         *
 
        /**
         * Setup the container for the 'new changes' button.
-        *
-        * @param {jQuery} $content
         */
-       mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupNewChangesButtonContainer = function ( $content ) {
+       mw.rcfilters.ui.ChangesListWrapperWidget.prototype.setupNewChangesButtonContainer = function () {
                this.showNewChangesLink = new OO.ui.ButtonWidget( {
                        framed: false,
                        label: mw.message( 'rcfilters-show-new-changes' ).text(),
                this.showNewChangesLink.connect( this, { click: 'onShowNewChangesClick' } );
                this.showNewChangesLink.toggle( false );
 
-               $content.before(
+               // HACK: Add the -newChanges div inside rcfilters-head, rather than right above us
+               // Visually it's the same place, but by putting it inside rcfilters-head we are
+               // able to use the min-height rule to prevent the page from jumping when this is added.
+               this.$element.parent().find( '.rcfilters-head' ).append(
                        $( '<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 uri = new mw.Uri(),
+               var $enhancedTopPageCell, $enhancedNestedPagesCell,
+                       widget = this,
                        highlightClass = 'mw-rcfilters-ui-changesListWrapperWidget-highlights',
                        $highlights = $( '<div>' )
                                .addClass( highlightClass )
                                .append(
                                        $( '<div>' )
+                                               .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
                                                .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-none' )
                                                .prop( 'data-color', 'none' )
                                );
                        $highlights.append(
                                $( '<div>' )
                                        .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-' + color )
+                                       .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
                                        .prop( 'data-color', color )
                        );
                } );
 
-               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()
+               if ( this.inEnhancedMode() ) {
+                       $enhancedTopPageCell = $content.find( 'table.mw-enhanced-rc.mw-collapsible' );
+                       $enhancedNestedPagesCell = $content.find( 'td.mw-enhanced-rc-nested' );
+
+                       // Enhanced RC highlight containers
+                       $content.find( 'table.mw-enhanced-rc tr:first-child' )
+                               .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-toplevel' )
                                .prepend(
                                        $( '<td>' )
                                                .append( $highlights.clone() )
                                );
+
+                       // We are adding and changing cells in a table that, despite having nested rows,
+                       // is actually all one big table. To prevent the highlights cell in the "nested"
+                       // rows from stretching out the cell with the flags and timestamp in the top row,
+                       // we give the latter colspan=2. Then to make things line up again, we add
+                       // an empty <td> to the "nested" rows.
+
+                       // Set colspan=2 on cell with flags and timestamp in top row
+                       $content.find( 'table.mw-enhanced-rc tr:first-child td.mw-enhanced-rc' )
+                               .prop( 'colspan', '2' );
+                       // Add empty <td> to nested rows to compensate
+                       $enhancedNestedPagesCell.parent().prepend( $( '<td>' ) );
+                       // Add highlights cell to nested rows
+                       $enhancedNestedPagesCell
+                               .before(
+                                       $( '<td>' )
+                                               .append( $highlights.clone().addClass( 'mw-enhanced-rc-nested' ) )
+                               );
+
+                       // We need to target the nested rows differently than the top rows so that the
+                       // LESS rules applies correctly. In top rows, the rule should highlight all but
+                       // the first 2 cells td:not( :nth-child( -n+2 ) and the nested rows, the rule
+                       // should highlight all but the first 4 cells td:not( :nth-child( -n+4 )
+                       $enhancedNestedPagesCell
+                               .closest( 'tr' )
+                               .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-nested' );
+
+                       // Go over pages that have sub results
+                       // HACK: We really only can collect those by targetting the collapsible class
+                       $enhancedTopPageCell.each( function () {
+                               var collectedClasses,
+                                       $table = $( this );
+
+                               // Go over <tr>s and pick up all recognized classes
+                               collectedClasses = widget.getHighlightClasses().filter( function ( className ) {
+                                       return $table.find( 'tr' ).hasClass( className );
+                               } );
+
+                               $table.find( 'tr:first-child' )
+                                       .addClass( collectedClasses.join( ' ' ) );
+                       } );
+
+                       $content.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhancedView' );
                } else {
                        // Regular RC
                        $content.find( 'ul.special li' )
                }
        };
 
+       /**
+        * In enhanced mode, we need to check whether the grouped results all have the
+        * same active highlights in order to see whether the "parent" of the group should
+        * be grey or highlighted normally.
+        *
+        * This is called every time highlights are applied.
+        */
+       mw.rcfilters.ui.ChangesListWrapperWidget.prototype.updateEnhancedParentHighlight = function () {
+               var activeHighlightClasses,
+                       $enhancedTopPageCell = this.$element.find( 'table.mw-enhanced-rc.mw-collapsible' );
+
+               activeHighlightClasses = this.filtersViewModel.getCurrentlyUsedHighlightColors().map( function ( color ) {
+                       return 'mw-rcfilters-highlight-color-' + color;
+               } );
+
+               // Go over top pages and their children, and figure out if all sub-pages have the
+               // same highlights between themselves. If they do, the parent should be highlighted
+               // with all colors. If classes are different, the parent should receive a grey
+               // background
+               $enhancedTopPageCell.each( function () {
+                       var firstChildClasses, $rowsWithDifferentHighlights,
+                               $table = $( this );
+
+                       // Collect the relevant classes from the first nested child
+                       firstChildClasses = activeHighlightClasses.filter( function ( className ) {
+                               return $table.find( 'tr:nth-child(2)' ).hasClass( className );
+                       } );
+                       // Filter the non-head rows and see if they all have the same classes
+                       // to the first row
+                       $rowsWithDifferentHighlights = $table.find( 'tr:not(:first-child)' ).filter( function () {
+                               var classesInThisRow,
+                                       $this = $( this );
+
+                               classesInThisRow = activeHighlightClasses.filter( function ( className ) {
+                                       return $this.hasClass( className );
+                               } );
+
+                               return !OO.compare( firstChildClasses, classesInThisRow );
+                       } );
+
+                       // If classes are different, tag the row for using grey color
+                       $table.find( 'tr:first-child' )
+                               .toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey', $rowsWithDifferentHighlights.length > 0 );
+               } );
+       };
+
+       /**
+        * @return {boolean} Whether the changes are grouped by page
+        */
+       mw.rcfilters.ui.ChangesListWrapperWidget.prototype.inEnhancedMode = function () {
+               var uri = new mw.Uri();
+               return ( uri.query.enhanced !== undefined && Number( uri.query.enhanced ) ) ||
+                       ( uri.query.enhanced === undefined && Number( mw.user.options.get( 'usenewrc' ) ) );
+       };
+
        /**
         * Apply color classes based on filters highlight configuration
         */
                }
 
                this.filtersViewModel.getHighlightedItems().forEach( function ( filterItem ) {
+                       var $elements = this.$element.find( '.' + filterItem.getCssClass() );
+
                        // Add highlight class to all highlighted list items
-                       this.$element.find( '.' + filterItem.getCssClass() )
+                       $elements
                                .addClass( 'mw-rcfilters-highlight-color-' + filterItem.getHighlightColor() );
+
+                       $elements.each( function () {
+                               var filterString = $( this ).attr( 'data-highlightedFilters' ) || '',
+                                       filters = filterString ? filterString.split( '|' ) : [];
+
+                               if ( filters.indexOf( filterItem.getLabel() ) === -1 ) {
+                                       filters.push( filterItem.getLabel() );
+                               }
+
+                               $( this )
+                                       .attr( 'data-highlightedFilters', filters.join( '|' ) );
+                       } );
                }.bind( this ) );
+               // Apply a title for relevant filters
+               this.$element.find( '[data-highlightedFilters]' ).each( function () {
+                       var filterString = $( this ).attr( 'data-highlightedFilters' ) || '',
+                               filters = filterString ? filterString.split( '|' ) : [];
+
+                       if ( filterString ) {
+                               $( this ).attr( 'title', mw.msg( 'rcfilters-highlighted-filters-list', filters.join( ', ' ) ) );
+                       }
+               } );
+
+               if ( this.inEnhancedMode() ) {
+                       this.updateEnhancedParentHighlight();
+               }
 
                // Turn on highlights
                this.$element.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
                        this.$element.find( '.mw-rcfilters-highlight-color-' + color ).removeClass( 'mw-rcfilters-highlight-color-' + color );
                }.bind( this ) );
 
+               this.$element.find( '[data-highlightedFilters]' )
+                       .removeAttr( 'title' )
+                       .removeAttr( 'data-highlightedFilters' );
+
+               // Remove grey from enhanced rows
+               this.$element.find( '.mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' )
+                       .removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' );
+
                // Turn off highlights
                this.$element.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
        };