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