Merge "statsd: Rename MediawikiStatsdDataFactory to IBufferingStatsdDataFactory"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / mw.rcfilters.Controller.js
index 20f28b3..5858566 100644 (file)
         */
        mw.rcfilters.Controller.prototype.initialize = function ( filterStructure, namespaceStructure, tagList ) {
                var parsedSavedQueries,
+                       views = {},
+                       items = [],
                        uri = new mw.Uri(),
                        $changesList = $( '.mw-changeslist' ).first().contents();
 
+               // Prepare views
+               if ( namespaceStructure ) {
+                       items = [];
+                       $.each( namespaceStructure, function ( namespaceID, label ) {
+                               // Build and clean up the individual namespace items definition
+                               items.push( {
+                                       name: namespaceID,
+                                       label: label || mw.msg( 'blanknamespace' ),
+                                       description: '',
+                                       identifiers: [
+                                               ( namespaceID < 0 || namespaceID % 2 === 0 ) ?
+                                                       'subject' : 'talk'
+                                       ],
+                                       cssClass: 'mw-changeslist-ns-' + namespaceID
+                               } );
+                       } );
+
+                       views.namespaces = {
+                               title: mw.msg( 'namespaces' ),
+                               trigger: ':',
+                               groups: [ {
+                                       // Group definition (single group)
+                                       name: 'namespace', // parameter name is singular
+                                       type: 'string_options',
+                                       title: mw.msg( 'namespaces' ),
+                                       labelPrefixKey: { 'default': 'rcfilters-tag-prefix-namespace', inverted: 'rcfilters-tag-prefix-namespace-inverted' },
+                                       separator: ';',
+                                       fullCoverage: true,
+                                       filters: items
+                               } ]
+                       };
+               }
+               if ( tagList ) {
+                       views.tags = {
+                               title: mw.msg( 'rcfilters-view-tags' ),
+                               trigger: '#',
+                               groups: [ {
+                                       // Group definition (single group)
+                                       name: 'tagfilter', // Parameter name
+                                       type: 'string_options',
+                                       title: 'rcfilters-view-tags', // Message key
+                                       labelPrefixKey: 'rcfilters-tag-prefix-tags',
+                                       separator: '|',
+                                       fullCoverage: false,
+                                       filters: tagList
+                               } ]
+                       };
+               }
+
                // Initialize the model
-               this.filtersModel.initializeFilters( filterStructure, namespaceStructure, tagList );
+               this.filtersModel.initializeFilters( filterStructure, views );
 
                this._buildBaseFilterState();
 
                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
         *
         * @param {string} queryID Query id
         */
        mw.rcfilters.Controller.prototype.removeSavedQuery = function ( queryID ) {
-               var query = this.savedQueriesModel.getItemByID( queryID );
-
-               this.savedQueriesModel.removeItems( [ query ] );
+               this.savedQueriesModel.removeQuery( queryID );
 
-               // Check if this item was the default
-               if ( this.savedQueriesModel.getDefault() === queryID ) {
-                       // Nulify the default
-                       this.savedQueriesModel.setDefault( null );
-               }
                this._saveSavedQueries();
        };
 
         * 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 ) {
                        this.uriProcessor.getVersion( currentUri.query ) !== 2 ||
                        this.uriProcessor.isNewState( currentUri.query, updatedUri.query )
                ) {
-                       if ( this.initializing ) {
-                               // Initially, when we just build the first page load
-                               // out of defaults, we want to replace the history
-                               mw.rcfilters.UriProcessor.static.replaceState( updatedUri );
-                       } else {
-                               mw.rcfilters.UriProcessor.static.pushState( updatedUri );
-                       }
+                       mw.rcfilters.UriProcessor.static.replaceState( updatedUri );
                }
        };