Merge "RCFilters UI: Select tag when filter item is selected"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Tue, 18 Apr 2017 23:03:45 +0000 (23:03 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 18 Apr 2017 23:03:45 +0000 (23:03 +0000)
resources/src/mediawiki.rcfilters/ui/mw.rcfilters.ui.FilterTagItemWidget.js
resources/src/mediawiki.rcfilters/ui/mw.rcfilters.ui.FilterTagMultiselectWidget.js

index cfffc65..f79e013 100644 (file)
@@ -18,6 +18,7 @@
 
                this.controller = controller;
                this.model = model;
+               this.selected = false;
 
                mw.rcfilters.ui.FilterTagItemWidget.parent.call( this, $.extend( {
                        data: this.model.getName(),
                }
        };
 
+       /**
+        * Get the selected state of this widget
+        *
+        * @return {boolean} Tag is selected
+        */
+       mw.rcfilters.ui.FilterTagItemWidget.prototype.isSelected = function () {
+               return this.selected;
+       };
+
        /**
         * Get item name
         *
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
         */