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