Merge "Add 3D filetype for STL files"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.FilterTagMultiselectWidget.js
index e14c1fa..268138f 100644 (file)
                this.resetButton.$element.on( 'mousedown', function ( e ) { e.stopPropagation(); } );
                this.model.connect( this, {
                        initialize: 'onModelInitialize',
+                       update: 'onModelUpdate',
                        itemUpdate: 'onModelItemUpdate',
                        highlightChange: 'onModelHighlightChange'
                } );
+               this.input.connect( this, { change: 'onInputChange' } );
                this.queriesModel.connect( this, { itemUpdate: 'onSavedQueriesItemUpdate' } );
 
                // The filter list and button should appear side by side regardless of how
                this.$element
                        .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
 
-               this.populateFromModel();
                this.reevaluateResetRestoreState();
        };
 
 
        /* Methods */
 
+       /**
+        * Respond to input change event
+        *
+        * @param {string} value Value of the input
+        */
+       mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onInputChange = function ( value ) {
+               var view = this.model.getViewByTrigger( value.substr( 0, 1 ) );
+
+               this.controller.switchView( view );
+       };
        /**
         * Respond to query button click
         */
                } else {
                        // Clear selection
                        this.selectTag( null );
+
+                       // Clear input if the only thing in the input is the prefix
+                       if (
+                               this.input.getValue() === this.model.getViewTrigger( this.model.getCurrentView() )
+                       ) {
+                               // Clear the input
+                               this.input.setValue( '' );
+                       }
                }
        };
 
         * Respond to model initialize event
         */
        mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelInitialize = function () {
-               this.populateFromModel();
-
                this.setSavedQueryVisibility();
        };
 
+       /**
+        * Respond to model update event
+        */
+       mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelUpdate = function () {
+               this.updateElementsForView();
+       };
+
+       /**
+        * Update the elements in the widget to the current view
+        */
+       mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.updateElementsForView = function () {
+               var view = this.model.getCurrentView(),
+                       inputValue = this.input.getValue(),
+                       inputView = this.model.getViewByTrigger( inputValue.substr( 0, 1 ) );
+
+               if ( inputView !== 'default' ) {
+                       // We have a prefix already, remove it
+                       inputValue = inputValue.substr( 1 );
+               }
+
+               if ( inputView !== view ) {
+                       // Add the correct prefix
+                       inputValue = this.model.getViewTrigger( view ) + inputValue;
+               }
+
+               // Update input
+               this.input.setValue( inputValue );
+       };
+
        /**
         * Set the visibility of the saved query button
         */
                        );
                }
        };
+
        /**
         * Respond to model itemUpdate event
         *
         */
        mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.isAllowedData = function ( data ) {
                return (
-                       this.menu.getItemFromData( data ) &&
+                       this.model.getItemByName( data ) &&
                        !this.isDuplicateData( data )
                );
        };
         */
        mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onTagSelect = function ( tagItem ) {
                var widget = this,
-                       menuOption = this.menu.getItemFromData( tagItem.getData() ),
+                       menuOption = this.menu.getItemFromModel( tagItem.getModel() ),
                        oldInputValue = this.input.getValue();
 
                // Reset input
                this.input.setValue( '' );
 
+               // Switch view
+               this.controller.switchView( tagItem.getView() );
+
                // Parent method
                mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onTagSelect.call( this, tagItem );
 
                );
        };
 
-       /**
-        * Populate the menu from the model
-        */
-       mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.populateFromModel = function () {
-               var widget = this,
-                       items = [];
-
-               // Reset
-               this.getMenu().clearItems();
-
-               $.each( this.model.getFilterGroups(), function ( groupName, groupModel ) {
-                       items.push(
-                               // Group section
-                               new mw.rcfilters.ui.FilterMenuSectionOptionWidget(
-                                       widget.controller,
-                                       groupModel,
-                                       {
-                                               $overlay: widget.$overlay
-                                       }
-                               )
-                       );
-
-                       // Add items
-                       widget.model.getGroupFilters( groupName ).forEach( function ( filterItem ) {
-                               items.push(
-                                       new mw.rcfilters.ui.FilterMenuOptionWidget(
-                                               widget.controller,
-                                               filterItem,
-                                               {
-                                                       $overlay: widget.$overlay
-                                               }
-                                       )
-                               );
-                       } );
-               } );
-
-               // Add all items to the menu
-               this.getMenu().addItems( items );
-       };
-
        /**
         * @inheritdoc
         */