RCFilters UI: Select tag when filter item is selected
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.FilterTagMultiselectWidget.js
index 8e26271..6fd3585 100644 (file)
                } else {
                        // Clear selection
                        this.getMenu().selectItem( null );
+                       this.selectTag( null );
                }
        };
 
        mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onMenuChoose = function ( item ) {
                this.controller.toggleFilterSelect( item.model.getName() );
 
+               // Select the tag if it exists, or reset selection otherwise
+               this.selectTag( this.getItemFromData( item.model.getName() ) );
+
                this.focus();
        };
 
                mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onTagSelect.call( this, tagItem );
 
                this.menu.selectItem( menuOption );
+               this.selectTag( tagItem );
 
                // Scroll to the item
                if ( oldInputValue ) {
                }
        };
 
+       /**
+        * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
+        * If no items are given, reset selection from all.
+        *
+        * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
+        *  omit to deselect all
+        */
+       mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.selectTag = function ( item ) {
+               var i, len, selected;
+
+               for ( i = 0, len = this.items.length; i < len; i++ ) {
+                       selected = this.items[ i ] === item;
+                       if ( this.items[ i ].isSelected() !== selected ) {
+                               this.items[ i ].toggleSelected( selected );
+                       }
+               }
+       };
        /**
         * @inheritdoc
         */