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