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