this.views = {};
this.currentView = 'default';
+ this.searchQuery = null;
// Events
this.aggregate( { update: 'filterItemUpdate' } );
mw.rcfilters.dm.FiltersViewModel.prototype.getFirstConflictedItem = function () {
var conflictedItem;
- $.each( this.getItems(), function ( index, filterItem ) {
+ this.getItems().forEach( function ( filterItem ) {
if ( filterItem.isSelected() && filterItem.isConflicted() ) {
conflictedItem = filterItem;
return false;
}
} );
- this.currentView = 'default';
+ this.setSearch( '' );
this.updateHighlightedState();
$.each( this.groups, function ( name, model ) {
if ( model.isSticky() ) {
- $.extend( true, result, model.getDefaultParams() );
+ $.extend( true, result, model.getParamRepresentation() );
}
} );
mw.rcfilters.dm.FiltersViewModel.prototype.areNamespacesEffectivelyInverted = function () {
return this.getInvertModel().isSelected() &&
this.getSelectedItems().some( function ( itemModel ) {
- return itemModel.getGroupModel().getView() === 'namespace';
+ return itemModel.getGroupModel().getName() === 'namespace';
} );
};
return allSelected;
};
- /**
- * Switch the current view
- *
- * @param {string} view View name
- * @fires update
- */
- mw.rcfilters.dm.FiltersViewModel.prototype.switchView = function ( view ) {
- if ( this.views[ view ] && this.currentView !== view ) {
- this.currentView = view;
- this.emit( 'update' );
- }
- };
-
/**
* Get the current view
*
return result;
};
+ /**
+ * Return a version of the given string that is without any
+ * view triggers.
+ *
+ * @param {string} str Given string
+ * @return {string} Result
+ */
+ mw.rcfilters.dm.FiltersViewModel.prototype.removeViewTriggers = function ( str ) {
+ if ( this.getViewFromString( str ) !== 'default' ) {
+ str = str.substr( 1 );
+ }
+
+ return str;
+ };
+
+ /**
+ * Get the view from the given string by a trigger, if it exists
+ *
+ * @param {string} str Given string
+ * @return {string} View name
+ */
+ mw.rcfilters.dm.FiltersViewModel.prototype.getViewFromString = function ( str ) {
+ return this.getViewByTrigger( str.substr( 0, 1 ) );
+ };
+
+ /**
+ * Set the current search for the system.
+ * This also dictates what items and groups are visible according
+ * to the search in #findMatches
+ *
+ * @param {string} searchQuery Search query, including triggers
+ * @fires searchChange
+ */
+ mw.rcfilters.dm.FiltersViewModel.prototype.setSearch = function ( searchQuery ) {
+ var visibleGroups, visibleGroupNames;
+
+ if ( this.searchQuery !== searchQuery ) {
+ // Check if the view changed
+ this.switchView( this.getViewFromString( searchQuery ) );
+
+ visibleGroups = this.findMatches( searchQuery );
+ visibleGroupNames = Object.keys( visibleGroups );
+
+ // Update visibility of items and groups
+ $.each( this.getFilterGroups(), function ( groupName, groupModel ) {
+ // Check if the group is visible at all
+ groupModel.toggleVisible( visibleGroupNames.indexOf( groupName ) !== -1 );
+ groupModel.setVisibleItems( visibleGroups[ groupName ] || [] );
+ } );
+
+ this.searchQuery = searchQuery;
+ this.emit( 'searchChange', this.searchQuery );
+ }
+ };
+
+ /**
+ * Get the current search
+ *
+ * @return {string} Current search query
+ */
+ mw.rcfilters.dm.FiltersViewModel.prototype.getSearch = function () {
+ return this.searchQuery;
+ };
+
+ /**
+ * Switch the current view
+ *
+ * @private
+ * @param {string} view View name
+ */
+ mw.rcfilters.dm.FiltersViewModel.prototype.switchView = function ( view ) {
+ if ( this.views[ view ] && this.currentView !== view ) {
+ this.currentView = view;
+ }
+ };
+
/**
* Toggle the highlight feature on and off.
* Propagate the change to filter items.
this.getItemByName( filterName ).clearHighlightColor();
};
- /**
- * Return a version of the given string that is without any
- * view triggers.
- *
- * @param {string} str Given string
- * @return {string} Result
- */
- mw.rcfilters.dm.FiltersViewModel.prototype.removeViewTriggers = function ( str ) {
- if ( this.getViewByTrigger( str.substr( 0, 1 ) ) !== 'default' ) {
- str = str.substr( 1 );
- }
-
- return str;
- };
}( mediaWiki, jQuery ) );