RCFilters UI: Check that filter exists before changing its state
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / mw.rcfilters.Controller.js
index f8008b6..669420c 100644 (file)
         * @param {Array} filterStructure Filter definition and structure for the model
         */
        mw.rcfilters.Controller.prototype.initialize = function ( filterStructure ) {
+               var $changesList = $( '.mw-changeslist' ).first().contents();
                // Initialize the model
                this.filtersModel.initializeFilters( filterStructure );
                this.updateStateBasedOnUrl();
+
+               // Update the changes list with the existing data
+               // so it gets processed
+               this.changesListModel.update(
+                       $changesList.length ? $changesList : 'NO_RESULTS',
+                       $( 'fieldset.rcoptions' ).first()
+               );
+
        };
 
        /**
         * Empty all selected filters
         */
        mw.rcfilters.Controller.prototype.emptyFilters = function () {
+               var highlightedFilterNames = this.filtersModel
+                       .getHighlightedItems()
+                       .map( function ( filterItem ) { return { name: filterItem.getName() }; } );
+
                this.filtersModel.emptyAllFilters();
                this.filtersModel.clearAllHighlightColors();
                // Check all filter interactions
                this.filtersModel.reassessFilterInteractions();
 
                this.updateChangesList();
+
+               if ( highlightedFilterNames ) {
+                       this.trackHighlight( 'clearAll', highlightedFilterNames );
+               }
        };
 
        /**
        mw.rcfilters.Controller.prototype.toggleFilterSelect = function ( filterName, isSelected ) {
                var filterItem = this.filtersModel.getItemByName( filterName );
 
+               if ( !filterItem ) {
+                       // If no filter was found, break
+                       return;
+               }
+
                isSelected = isSelected === undefined ? !filterItem.isSelected() : isSelected;
 
                if ( filterItem.isSelected() !== isSelected ) {
        mw.rcfilters.Controller.prototype.toggleHighlight = function () {
                this.filtersModel.toggleHighlight();
                this.updateURL();
+
+               if ( this.filtersModel.isHighlightEnabled() ) {
+                       mw.hook( 'RcFilters.highlight.enable' ).fire();
+               }
        };
 
        /**
        mw.rcfilters.Controller.prototype.setHighlightColor = function ( filterName, color ) {
                this.filtersModel.setHighlightColor( filterName, color );
                this.updateURL();
+               this.trackHighlight( 'set', { name: filterName, color: color } );
        };
 
        /**
        mw.rcfilters.Controller.prototype.clearHighlightColor = function ( filterName ) {
                this.filtersModel.clearHighlightColor( filterName );
                this.updateURL();
+               this.trackHighlight( 'clear', filterName );
        };
 
        /**
         * @param {string} filterName Name of the filter item
         */
        mw.rcfilters.Controller.prototype.clearFilter = function ( filterName ) {
-               var filterItem = this.filtersModel.getItemByName( filterName );
+               var filterItem = this.filtersModel.getItemByName( filterName ),
+                       isHighlighted = filterItem.isHighlighted();
 
-               if ( filterItem.isSelected() || filterItem.isHighlighted() ) {
+               if ( filterItem.isSelected() || isHighlighted ) {
                        this.filtersModel.clearHighlightColor( filterName );
                        this.filtersModel.toggleFilterSelected( filterName, false );
                        this.updateChangesList();
                        this.filtersModel.reassessFilterInteractions( filterItem );
                }
+
+               if ( isHighlighted ) {
+                       this.trackHighlight( 'clear', filterName );
+               }
        };
 
        /**
                        this.getUpdatedUri().toString()
                );
        };
+
+       /**
+        * Track usage of highlight feature
+        *
+        * @param {string} action
+        * @param {array|object|string} filters
+        */
+       mw.rcfilters.Controller.prototype.trackHighlight = function ( action, filters ) {
+               filters = typeof filters === 'string' ? { name: filters } : filters;
+               filters = !Array.isArray( filters ) ? [ filters ] : filters;
+               mw.track(
+                       'event.ChangesListHighlights',
+                       {
+                               action: action,
+                               filters: filters,
+                               userId: mw.user.getId()
+                       }
+               );
+       };
 }( mediaWiki, jQuery ) );