X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=resources%2Fsrc%2Fmediawiki.rcfilters%2Fdm%2Fmw.rcfilters.dm.FiltersViewModel.js;h=cdf1f63517ed09ca529e2c141c72a6a1d4b378d7;hb=e143c40fee88922eb393a88f94e60afc439d1f49;hp=f7c2aaf184f45435ed23f5d9e74a2915be0038fe;hpb=96188af6dbb48a4b930c8741bcd5787534e1cb05;p=lhc%2Fweb%2Fwiklou.git diff --git a/resources/src/mediawiki.rcfilters/dm/mw.rcfilters.dm.FiltersViewModel.js b/resources/src/mediawiki.rcfilters/dm/mw.rcfilters.dm.FiltersViewModel.js index f7c2aaf184..cdf1f63517 100644 --- a/resources/src/mediawiki.rcfilters/dm/mw.rcfilters.dm.FiltersViewModel.js +++ b/resources/src/mediawiki.rcfilters/dm/mw.rcfilters.dm.FiltersViewModel.js @@ -20,6 +20,7 @@ this.views = {}; this.currentView = 'default'; + this.searchQuery = null; // Events this.aggregate( { update: 'filterItemUpdate' } ); @@ -148,7 +149,7 @@ groupModel.areAllSelectedInConflictWith( filterItem ) && // Every selected member of the item's own group is also // in conflict with the other group - filterItemGroup.getSelectedItems().every( function ( otherGroupItem ) { + filterItemGroup.findSelectedItems().every( function ( otherGroupItem ) { return groupModel.areAllSelectedInConflictWith( otherGroupItem ); } ) ); @@ -184,7 +185,7 @@ 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; @@ -401,7 +402,7 @@ } } ); - this.currentView = 'default'; + this.setSearch( '' ); this.updateHighlightedState(); @@ -909,8 +910,8 @@ */ mw.rcfilters.dm.FiltersViewModel.prototype.areNamespacesEffectivelyInverted = function () { return this.getInvertModel().isSelected() && - this.getSelectedItems().some( function ( itemModel ) { - return itemModel.getGroupModel().getView() === 'namespace'; + this.findSelectedItems().some( function ( itemModel ) { + return itemModel.getGroupModel().getName() === 'namespace'; } ); }; @@ -1085,29 +1086,16 @@ * * @return {mw.rcfilters.dm.FilterItem[]} Selected items */ - mw.rcfilters.dm.FiltersViewModel.prototype.getSelectedItems = function () { + mw.rcfilters.dm.FiltersViewModel.prototype.findSelectedItems = function () { var allSelected = []; $.each( this.getFilterGroups(), function ( groupName, groupModel ) { - allSelected = allSelected.concat( groupModel.getSelectedItems() ); + allSelected = allSelected.concat( groupModel.findSelectedItems() ); } ); 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 * @@ -1147,6 +1135,82 @@ 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. @@ -1209,18 +1273,4 @@ 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 ) );