Merge "RCFilters: Change `What's this?` i18n based on user testing"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / mw.rcfilters.Controller.js
index 27387c9..a9283b9 100644 (file)
@@ -15,6 +15,8 @@
                this.baseFilterState = {};
                this.uriProcessor = null;
                this.initializing = false;
+
+               this.prevLoggedItems = [];
        };
 
        /* Initialization */
                        this.filtersModel.toggleFilterSelected( filterName, false );
                        this.updateChangesList();
                        this.filtersModel.reassessFilterInteractions( filterItem );
+
+                       // Log filter grouping
+                       this.trackFilterGroupings( 'removefilter' );
                }
 
                if ( isHighlighted ) {
                this._trackHighlight( 'clear', filterName );
        };
 
+       /**
+        * Enable or disable live updates.
+        * @param {boolean} enable True to enable, false to disable
+        */
+       mw.rcfilters.Controller.prototype.toggleLiveUpdate = function ( enable ) {
+               if ( enable && !this.liveUpdateTimeout ) {
+                       this._scheduleLiveUpdate();
+               } else if ( !enable && this.liveUpdateTimeout ) {
+                       clearTimeout( this.liveUpdateTimeout );
+                       this.liveUpdateTimeout = null;
+               }
+       };
+
+       /**
+        * Set a timeout for the next live update.
+        * @private
+        */
+       mw.rcfilters.Controller.prototype._scheduleLiveUpdate = function () {
+               this.liveUpdateTimeout = setTimeout( this._doLiveUpdate.bind( this ), 3000 );
+       };
+
+       /**
+        * Perform a live update.
+        * @private
+        */
+       mw.rcfilters.Controller.prototype._doLiveUpdate = function () {
+               var controller = this;
+               this.updateChangesList( {}, true )
+                       .always( function () {
+                               if ( controller.liveUpdateTimeout ) {
+                                       // Live update was not disabled in the meantime
+                                       controller._scheduleLiveUpdate();
+                               }
+                       } );
+       };
+
        /**
         * Save the current model state as a saved query
         *
                        this.filtersModel.reassessFilterInteractions();
 
                        this.updateChangesList();
+
+                       // Log filter grouping
+                       this.trackFilterGroupings( 'savedfilters' );
                }
        };
 
         * Update the list of changes and notify the model
         *
         * @param {Object} [params] Extra parameters to add to the API call
+        * @param {boolean} [isLiveUpdate] Don't update the URL or invalidate the changes list
+        * @return {jQuery.Promise} Promise that is resolved when the update is complete
         */
-       mw.rcfilters.Controller.prototype.updateChangesList = function ( params ) {
-               this._updateURL( params );
-               this.changesListModel.invalidate();
-               this._fetchChangesList()
+       mw.rcfilters.Controller.prototype.updateChangesList = function ( params, isLiveUpdate ) {
+               if ( !isLiveUpdate ) {
+                       this._updateURL( params );
+                       this.changesListModel.invalidate();
+               }
+               return this._fetchChangesList()
                        .then(
                                // Success
                                function ( pieces ) {
                );
        };
 
+       /**
+        * Track filter grouping usage
+        *
+        * @param {string} action Action taken
+        */
+       mw.rcfilters.Controller.prototype.trackFilterGroupings = function ( action ) {
+               var controller = this,
+                       rightNow = new Date().getTime(),
+                       randomIdentifier = String( mw.user.sessionId() ) + String( rightNow ) + String( Math.random() ),
+                       // Get all current filters
+                       filters = this.filtersModel.getSelectedItems().map( function ( item ) {
+                               return item.getName();
+                       } );
+
+               action = action || 'filtermenu';
+
+               // Check if these filters were the ones we just logged previously
+               // (Don't log the same grouping twice, in case the user opens/closes)
+               // the menu without action, or with the same result
+               if (
+                       // Only log if the two arrays are different in size
+                       filters.length !== this.prevLoggedItems.length ||
+                       // Or if any filters are not the same as the cached filters
+                       filters.some( function ( filterName ) {
+                               return controller.prevLoggedItems.indexOf( filterName ) === -1;
+                       } ) ||
+                       // Or if any cached filters are not the same as given filters
+                       this.prevLoggedItems.some( function ( filterName ) {
+                               return filters.indexOf( filterName ) === -1;
+                       } )
+               ) {
+                       filters.forEach( function ( filterName ) {
+                               mw.track(
+                                       'event.ChangesListFilterGrouping',
+                                       {
+                                               action: action,
+                                               groupIdentifier: randomIdentifier,
+                                               filter: filterName,
+                                               userId: mw.user.getId()
+                                       }
+                               );
+                       } );
+
+                       // Cache the filter names
+                       this.prevLoggedItems = filters;
+               }
+       };
 }( mediaWiki, jQuery ) );