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