Merge "RCFilters: Change the input icon on interaction"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.FilterTagMultiselectWidget.js
1 ( function ( mw ) {
2 /**
3 * List displaying all filter groups
4 *
5 * @extends OO.ui.MenuTagMultiselectWidget
6 * @mixins OO.ui.mixin.PendingElement
7 *
8 * @constructor
9 * @param {mw.rcfilters.Controller} controller Controller
10 * @param {mw.rcfilters.dm.FiltersViewModel} model View model
11 * @param {mw.rcfilters.dm.SavedQueriesModel} savedQueriesModel Saved queries model
12 * @param {Object} config Configuration object
13 * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
14 */
15 mw.rcfilters.ui.FilterTagMultiselectWidget = function MwRcfiltersUiFilterTagMultiselectWidget( controller, model, savedQueriesModel, config ) {
16 var rcFiltersRow,
17 areSavedQueriesEnabled = mw.config.get( 'wgStructuredChangeFiltersEnableSaving' ),
18 title = new OO.ui.LabelWidget( {
19 label: mw.msg( 'rcfilters-activefilters' ),
20 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-title' ]
21 } ),
22 $contentWrapper = $( '<div>' )
23 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper' );
24
25 config = config || {};
26
27 this.controller = controller;
28 this.model = model;
29 this.queriesModel = savedQueriesModel;
30 this.$overlay = config.$overlay || this.$element;
31 this.matchingQuery = null;
32 this.areSavedQueriesEnabled = areSavedQueriesEnabled;
33
34 // Parent
35 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.call( this, $.extend( true, {
36 label: mw.msg( 'rcfilters-filterlist-title' ),
37 placeholder: mw.msg( 'rcfilters-empty-filter' ),
38 inputPosition: 'outline',
39 allowArbitrary: false,
40 allowDisplayInvalidTags: false,
41 allowReordering: false,
42 $overlay: this.$overlay,
43 menu: {
44 hideWhenOutOfView: false,
45 hideOnChoose: false,
46 width: 650,
47 footers: [
48 {
49 name: 'viewSelect',
50 // View select menu, appears on default view only
51 $element: $( '<div>' )
52 .append( new mw.rcfilters.ui.ViewSwitchWidget( this.controller, this.model ).$element ),
53 views: [ 'default' ]
54 },
55 {
56 name: 'feedback',
57 // Feedback footer, appears on all views
58 $element: $( '<div>' )
59 .append(
60 new OO.ui.ButtonWidget( {
61 framed: false,
62 icon: 'feedback',
63 flags: [ 'progressive' ],
64 label: mw.msg( 'rcfilters-filterlist-feedbacklink' ),
65 href: 'https://www.mediawiki.org/wiki/Help_talk:New_filters_for_edit_review'
66 } ).$element
67 )
68 }
69 ]
70 },
71 input: {
72 icon: 'menu',
73 placeholder: mw.msg( 'rcfilters-search-placeholder' )
74 }
75 }, config ) );
76
77 this.savedQueryTitle = new OO.ui.LabelWidget( {
78 label: '',
79 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-savedQueryTitle' ]
80 } );
81
82 this.resetButton = new OO.ui.ButtonWidget( {
83 framed: false,
84 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-resetButton' ]
85 } );
86
87 if ( areSavedQueriesEnabled ) {
88 this.saveQueryButton = new mw.rcfilters.ui.SaveFiltersPopupButtonWidget(
89 this.controller,
90 this.queriesModel
91 );
92
93 this.saveQueryButton.$element.on( 'mousedown', function ( e ) { e.stopPropagation(); } );
94
95 this.saveQueryButton.connect( this, {
96 click: 'onSaveQueryButtonClick',
97 saveCurrent: 'setSavedQueryVisibility'
98 } );
99 }
100
101 this.emptyFilterMessage = new OO.ui.LabelWidget( {
102 label: mw.msg( 'rcfilters-empty-filter' ),
103 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
104 } );
105 this.$content.append( this.emptyFilterMessage.$element );
106
107 // Events
108 this.resetButton.connect( this, { click: 'onResetButtonClick' } );
109 // Stop propagation for mousedown, so that the widget doesn't
110 // trigger the focus on the input and scrolls up when we click the reset button
111 this.resetButton.$element.on( 'mousedown', function ( e ) { e.stopPropagation(); } );
112 this.model.connect( this, {
113 initialize: 'onModelInitialize',
114 update: 'onModelUpdate',
115 itemUpdate: 'onModelItemUpdate',
116 highlightChange: 'onModelHighlightChange'
117 } );
118 this.input.connect( this, { change: 'onInputChange' } );
119 this.queriesModel.connect( this, { itemUpdate: 'onSavedQueriesItemUpdate' } );
120
121 // The filter list and button should appear side by side regardless of how
122 // wide the button is; the button also changes its width depending
123 // on language and its state, so the safest way to present both side
124 // by side is with a table layout
125 rcFiltersRow = $( '<div>' )
126 .addClass( 'mw-rcfilters-ui-row' )
127 .append(
128 this.$content
129 .addClass( 'mw-rcfilters-ui-cell' )
130 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
131 );
132
133 if ( areSavedQueriesEnabled ) {
134 rcFiltersRow.append(
135 $( '<div>' )
136 .addClass( 'mw-rcfilters-ui-cell' )
137 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
138 .append( this.saveQueryButton.$element )
139 );
140 }
141
142 if ( mw.config.get( 'wgStructuredChangeFiltersEnableExperimentalViews' ) ) {
143 // Add a selector at the right of the input
144 this.viewsSelectWidget = new OO.ui.ButtonSelectWidget( {
145 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget' ],
146 items: [
147 new OO.ui.ButtonOptionWidget( {
148 data: 'namespaces',
149 icon: 'article',
150 title: mw.msg( 'namespaces' )
151 } ),
152 new OO.ui.ButtonOptionWidget( {
153 data: 'tags',
154 icon: 'tag',
155 title: mw.msg( 'rcfilters-view-tags' )
156 } )
157 ]
158 } );
159
160 // Rearrange the UI so the select widget is at the right of the input
161 this.$element.append(
162 $( '<div>' )
163 .addClass( 'mw-rcfilters-ui-table' )
164 .append(
165 $( '<div>' )
166 .addClass( 'mw-rcfilters-ui-row' )
167 .append(
168 $( '<div>' )
169 .addClass( 'mw-rcfilters-ui-cell' )
170 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-input' )
171 .append( this.input.$element ),
172 $( '<div>' )
173 .addClass( 'mw-rcfilters-ui-cell' )
174 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select' )
175 .append( this.viewsSelectWidget.$element )
176 )
177 )
178 );
179
180 // Event
181 this.viewsSelectWidget.connect( this, { choose: 'onViewsSelectWidgetChoose' } );
182 }
183
184 rcFiltersRow.append(
185 $( '<div>' )
186 .addClass( 'mw-rcfilters-ui-cell' )
187 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
188 .append( this.resetButton.$element )
189 );
190
191 // Build the content
192 $contentWrapper.append(
193 title.$element,
194 this.savedQueryTitle.$element,
195 $( '<div>' )
196 .addClass( 'mw-rcfilters-ui-table' )
197 .append(
198 rcFiltersRow
199 )
200 );
201
202 // Initialize
203 this.$handle.append( $contentWrapper );
204 this.emptyFilterMessage.toggle( this.isEmpty() );
205 this.savedQueryTitle.toggle( false );
206
207 this.$element
208 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
209
210 this.reevaluateResetRestoreState();
211 };
212
213 /* Initialization */
214
215 OO.inheritClass( mw.rcfilters.ui.FilterTagMultiselectWidget, OO.ui.MenuTagMultiselectWidget );
216
217 /* Methods */
218
219 /**
220 * Respond to view select widget choose event
221 *
222 * @param {OO.ui.ButtonOptionWidget} buttonOptionWidget Chosen widget
223 */
224 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onViewsSelectWidgetChoose = function ( buttonOptionWidget ) {
225 this.controller.switchView( buttonOptionWidget.getData() );
226 this.viewsSelectWidget.selectItem( null );
227 this.focus();
228 };
229
230 /**
231 * Respond to input change event
232 *
233 * @param {string} value Value of the input
234 */
235 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onInputChange = function ( value ) {
236 var view = this.model.getViewByTrigger( value.substr( 0, 1 ) );
237
238 this.controller.switchView( view );
239 };
240 /**
241 * Respond to query button click
242 */
243 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onSaveQueryButtonClick = function () {
244 this.getMenu().toggle( false );
245 };
246
247 /**
248 * Respond to save query item change. Mainly this is done to update the label in case
249 * a query item has been edited
250 *
251 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
252 */
253 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onSavedQueriesItemUpdate = function ( item ) {
254 if ( this.matchingQuery === item ) {
255 // This means we just edited the item that is currently matched
256 this.savedQueryTitle.setLabel( item.getLabel() );
257 }
258 };
259
260 /**
261 * Respond to menu toggle
262 *
263 * @param {boolean} isVisible Menu is visible
264 */
265 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onMenuToggle = function ( isVisible ) {
266 // Parent
267 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onMenuToggle.call( this );
268
269 if ( isVisible ) {
270 mw.hook( 'RcFilters.popup.open' ).fire();
271
272 if ( !this.getMenu().getSelectedItem() ) {
273 // If there are no selected items, scroll menu to top
274 // This has to be in a setTimeout so the menu has time
275 // to be positioned and fixed
276 setTimeout( function () { this.getMenu().scrollToTop(); }.bind( this ), 0 );
277 }
278 } else {
279 // Clear selection
280 this.selectTag( null );
281
282 // Clear input if the only thing in the input is the prefix
283 if (
284 this.input.getValue() === this.model.getViewTrigger( this.model.getCurrentView() )
285 ) {
286 // Clear the input
287 this.input.setValue( '' );
288 }
289 }
290
291 this.input.setIcon( isVisible ? 'search' : 'menu' );
292 };
293
294 /**
295 * @inheritdoc
296 */
297 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onInputFocus = function () {
298 // Parent
299 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onInputFocus.call( this );
300
301 // Scroll to top
302 this.scrollToTop( this.$element );
303 };
304
305 /**
306 * @inheritdoc
307 */
308 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.doInputEscape = function () {
309 // Parent
310 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.doInputEscape.call( this );
311
312 // Blur the input
313 this.input.$input.blur();
314 };
315
316 /**
317 * @inheridoc
318 */
319 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onChangeTags = function () {
320 // Parent method
321 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onChangeTags.call( this );
322
323 this.emptyFilterMessage.toggle( this.isEmpty() );
324 };
325
326 /**
327 * Respond to model initialize event
328 */
329 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelInitialize = function () {
330 this.setSavedQueryVisibility();
331 };
332
333 /**
334 * Respond to model update event
335 */
336 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelUpdate = function () {
337 this.updateElementsForView();
338 };
339
340 /**
341 * Update the elements in the widget to the current view
342 */
343 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.updateElementsForView = function () {
344 var view = this.model.getCurrentView(),
345 inputValue = this.input.getValue(),
346 inputView = this.model.getViewByTrigger( inputValue.substr( 0, 1 ) );
347
348 if ( inputView !== 'default' ) {
349 // We have a prefix already, remove it
350 inputValue = inputValue.substr( 1 );
351 }
352
353 if ( inputView !== view ) {
354 // Add the correct prefix
355 inputValue = this.model.getViewTrigger( view ) + inputValue;
356 }
357
358 // Update input
359 this.input.setValue( inputValue );
360 };
361
362 /**
363 * Set the visibility of the saved query button
364 */
365 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.setSavedQueryVisibility = function () {
366 if ( this.areSavedQueriesEnabled ) {
367 this.matchingQuery = this.controller.findQueryMatchingCurrentState();
368
369 this.savedQueryTitle.setLabel(
370 this.matchingQuery ? this.matchingQuery.getLabel() : ''
371 );
372 this.savedQueryTitle.toggle( !!this.matchingQuery );
373 this.saveQueryButton.toggle(
374 !this.isEmpty() &&
375 !this.matchingQuery
376 );
377 }
378 };
379
380 /**
381 * Respond to model itemUpdate event
382 *
383 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
384 */
385 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelItemUpdate = function ( item ) {
386 if (
387 item.isSelected() ||
388 (
389 this.model.isHighlightEnabled() &&
390 item.isHighlightSupported() &&
391 item.getHighlightColor()
392 )
393 ) {
394 this.addTag( item.getName(), item.getLabel() );
395 } else {
396 this.removeTagByData( item.getName() );
397 }
398
399 this.setSavedQueryVisibility();
400
401 // Re-evaluate reset state
402 this.reevaluateResetRestoreState();
403 };
404
405 /**
406 * @inheritdoc
407 */
408 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.isAllowedData = function ( data ) {
409 return (
410 this.model.getItemByName( data ) &&
411 !this.isDuplicateData( data )
412 );
413 };
414
415 /**
416 * @inheritdoc
417 */
418 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onMenuChoose = function ( item ) {
419 this.controller.toggleFilterSelect( item.model.getName() );
420
421 // Select the tag if it exists, or reset selection otherwise
422 this.selectTag( this.getItemFromData( item.model.getName() ) );
423
424 this.focus();
425 };
426
427 /**
428 * Respond to highlightChange event
429 *
430 * @param {boolean} isHighlightEnabled Highlight is enabled
431 */
432 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelHighlightChange = function ( isHighlightEnabled ) {
433 var highlightedItems = this.model.getHighlightedItems();
434
435 if ( isHighlightEnabled ) {
436 // Add capsule widgets
437 highlightedItems.forEach( function ( filterItem ) {
438 this.addTag( filterItem.getName(), filterItem.getLabel() );
439 }.bind( this ) );
440 } else {
441 // Remove capsule widgets if they're not selected
442 highlightedItems.forEach( function ( filterItem ) {
443 if ( !filterItem.isSelected() ) {
444 this.removeTagByData( filterItem.getName() );
445 }
446 }.bind( this ) );
447 }
448 };
449
450 /**
451 * @inheritdoc
452 */
453 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onTagSelect = function ( tagItem ) {
454 var widget = this,
455 menuOption = this.menu.getItemFromModel( tagItem.getModel() ),
456 oldInputValue = this.input.getValue();
457
458 // Reset input
459 this.input.setValue( '' );
460
461 // Switch view
462 this.controller.switchView( tagItem.getView() );
463
464 // Parent method
465 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onTagSelect.call( this, tagItem );
466
467 this.menu.selectItem( menuOption );
468 this.selectTag( tagItem );
469
470 // Scroll to the item
471 if ( this.model.removeViewTriggers( oldInputValue ) ) {
472 // We're binding a 'once' to the itemVisibilityChange event
473 // so this happens when the menu is ready after the items
474 // are visible again, in case this is done right after the
475 // user filtered the results
476 this.getMenu().once(
477 'itemVisibilityChange',
478 function () { widget.scrollToTop( menuOption.$element ); }
479 );
480 } else {
481 this.scrollToTop( menuOption.$element );
482 }
483 };
484
485 /**
486 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
487 * If no items are given, reset selection from all.
488 *
489 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
490 * omit to deselect all
491 */
492 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.selectTag = function ( item ) {
493 var i, len, selected;
494
495 for ( i = 0, len = this.items.length; i < len; i++ ) {
496 selected = this.items[ i ] === item;
497 if ( this.items[ i ].isSelected() !== selected ) {
498 this.items[ i ].toggleSelected( selected );
499 }
500 }
501 };
502 /**
503 * @inheritdoc
504 */
505 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onTagRemove = function ( tagItem ) {
506 // Parent method
507 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onTagRemove.call( this, tagItem );
508
509 this.controller.clearFilter( tagItem.getName() );
510
511 tagItem.destroy();
512 };
513
514 /**
515 * Respond to click event on the reset button
516 */
517 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onResetButtonClick = function () {
518 if ( this.model.areCurrentFiltersEmpty() ) {
519 // Reset to default filters
520 this.controller.resetToDefaults();
521 } else {
522 // Reset to have no filters
523 this.controller.emptyFilters();
524 }
525 };
526
527 /**
528 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
529 */
530 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.reevaluateResetRestoreState = function () {
531 var defaultsAreEmpty = this.model.areDefaultFiltersEmpty(),
532 currFiltersAreEmpty = this.model.areCurrentFiltersEmpty(),
533 hideResetButton = currFiltersAreEmpty && defaultsAreEmpty;
534
535 this.resetButton.setIcon(
536 currFiltersAreEmpty ? 'history' : 'trash'
537 );
538
539 this.resetButton.setLabel(
540 currFiltersAreEmpty ? mw.msg( 'rcfilters-restore-default-filters' ) : ''
541 );
542 this.resetButton.setTitle(
543 currFiltersAreEmpty ? null : mw.msg( 'rcfilters-clear-all-filters' )
544 );
545
546 this.resetButton.toggle( !hideResetButton );
547 this.emptyFilterMessage.toggle( currFiltersAreEmpty );
548 };
549
550 /**
551 * @inheritdoc
552 */
553 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.createMenuWidget = function ( menuConfig ) {
554 return new mw.rcfilters.ui.MenuSelectWidget(
555 this.controller,
556 this.model,
557 $.extend( {
558 filterFromInput: true
559 }, menuConfig )
560 );
561 };
562
563 /**
564 * @inheritdoc
565 */
566 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.createTagItemWidget = function ( data ) {
567 var filterItem = this.model.getItemByName( data );
568
569 if ( filterItem ) {
570 return new mw.rcfilters.ui.FilterTagItemWidget(
571 this.controller,
572 filterItem,
573 {
574 $overlay: this.$overlay
575 }
576 );
577 }
578 };
579
580 /**
581 * Scroll the element to top within its container
582 *
583 * @private
584 * @param {jQuery} $element Element to position
585 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
586 * much space (in pixels) above the widget.
587 */
588 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.scrollToTop = function ( $element, marginFromTop ) {
589 var container = OO.ui.Element.static.getClosestScrollableContainer( $element[ 0 ], 'y' ),
590 pos = OO.ui.Element.static.getRelativePosition( $element, $( container ) ),
591 containerScrollTop = $( container ).is( 'body, html' ) ? 0 : $( container ).scrollTop();
592
593 // Scroll to item
594 $( container ).animate( {
595 scrollTop: containerScrollTop + pos.top - ( marginFromTop || 0 )
596 } );
597 };
598 }( mediaWiki ) );