Merge "Fix Special:ListUsers "subpage parameter" handling"
[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 disabled: !mw.config.get( 'wgStructuredChangeFiltersEnableExperimentalViews' ),
52 sticky: false,
53 // View select menu, appears on default view only
54 $element: $( '<div>' )
55 .append( new mw.rcfilters.ui.ViewSwitchWidget( this.controller, this.model ).$element ),
56 views: [ 'default' ]
57 },
58 {
59 name: 'feedback',
60 // Feedback footer, appears on all views
61 $element: $( '<div>' )
62 .append(
63 new OO.ui.ButtonWidget( {
64 framed: false,
65 icon: 'feedback',
66 flags: [ 'progressive' ],
67 label: mw.msg( 'rcfilters-filterlist-feedbacklink' ),
68 href: 'https://www.mediawiki.org/wiki/Help_talk:New_filters_for_edit_review'
69 } ).$element
70 )
71 }
72 ]
73 },
74 input: {
75 icon: 'menu',
76 placeholder: mw.msg( 'rcfilters-search-placeholder' )
77 }
78 }, config ) );
79
80 this.savedQueryTitle = new OO.ui.LabelWidget( {
81 label: '',
82 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-savedQueryTitle' ]
83 } );
84
85 this.resetButton = new OO.ui.ButtonWidget( {
86 framed: false,
87 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-resetButton' ]
88 } );
89
90 if ( areSavedQueriesEnabled ) {
91 this.saveQueryButton = new mw.rcfilters.ui.SaveFiltersPopupButtonWidget(
92 this.controller,
93 this.queriesModel
94 );
95
96 this.saveQueryButton.$element.on( 'mousedown', function ( e ) { e.stopPropagation(); } );
97
98 this.saveQueryButton.connect( this, {
99 click: 'onSaveQueryButtonClick',
100 saveCurrent: 'setSavedQueryVisibility'
101 } );
102 }
103
104 this.emptyFilterMessage = new OO.ui.LabelWidget( {
105 label: mw.msg( 'rcfilters-empty-filter' ),
106 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
107 } );
108 this.$content.append( this.emptyFilterMessage.$element );
109
110 // Events
111 this.resetButton.connect( this, { click: 'onResetButtonClick' } );
112 // Stop propagation for mousedown, so that the widget doesn't
113 // trigger the focus on the input and scrolls up when we click the reset button
114 this.resetButton.$element.on( 'mousedown', function ( e ) { e.stopPropagation(); } );
115 this.model.connect( this, {
116 initialize: 'onModelInitialize',
117 update: 'onModelUpdate',
118 itemUpdate: 'onModelItemUpdate',
119 highlightChange: 'onModelHighlightChange'
120 } );
121 this.input.connect( this, { change: 'onInputChange' } );
122 this.queriesModel.connect( this, { itemUpdate: 'onSavedQueriesItemUpdate' } );
123
124 // The filter list and button should appear side by side regardless of how
125 // wide the button is; the button also changes its width depending
126 // on language and its state, so the safest way to present both side
127 // by side is with a table layout
128 rcFiltersRow = $( '<div>' )
129 .addClass( 'mw-rcfilters-ui-row' )
130 .append(
131 this.$content
132 .addClass( 'mw-rcfilters-ui-cell' )
133 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
134 );
135
136 if ( areSavedQueriesEnabled ) {
137 rcFiltersRow.append(
138 $( '<div>' )
139 .addClass( 'mw-rcfilters-ui-cell' )
140 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
141 .append( this.saveQueryButton.$element )
142 );
143 }
144
145 if ( mw.config.get( 'wgStructuredChangeFiltersEnableExperimentalViews' ) ) {
146 // Add a selector at the right of the input
147 this.viewsSelectWidget = new OO.ui.ButtonSelectWidget( {
148 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget' ],
149 items: [
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
189 rcFiltersRow.append(
190 $( '<div>' )
191 .addClass( 'mw-rcfilters-ui-cell' )
192 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
193 .append( this.resetButton.$element )
194 );
195
196 // Build the content
197 $contentWrapper.append(
198 title.$element,
199 this.savedQueryTitle.$element,
200 $( '<div>' )
201 .addClass( 'mw-rcfilters-ui-table' )
202 .append(
203 rcFiltersRow
204 )
205 );
206
207 // Initialize
208 this.$handle.append( $contentWrapper );
209 this.emptyFilterMessage.toggle( this.isEmpty() );
210 this.savedQueryTitle.toggle( false );
211
212 this.$element
213 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
214
215 this.reevaluateResetRestoreState();
216 };
217
218 /* Initialization */
219
220 OO.inheritClass( mw.rcfilters.ui.FilterTagMultiselectWidget, OO.ui.MenuTagMultiselectWidget );
221
222 /* Methods */
223
224 /**
225 * Respond to view select widget choose event
226 *
227 * @param {OO.ui.ButtonOptionWidget} buttonOptionWidget Chosen widget
228 */
229 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onViewsSelectWidgetChoose = function ( buttonOptionWidget ) {
230 this.controller.switchView( buttonOptionWidget.getData() );
231 this.viewsSelectWidget.selectItem( null );
232 this.focus();
233 };
234
235 /**
236 * Respond to input change event
237 *
238 * @param {string} value Value of the input
239 */
240 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onInputChange = function ( value ) {
241 var view = this.model.getViewByTrigger( value.substr( 0, 1 ) );
242
243 this.controller.switchView( view );
244 };
245 /**
246 * Respond to query button click
247 */
248 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onSaveQueryButtonClick = function () {
249 this.getMenu().toggle( false );
250 };
251
252 /**
253 * Respond to save query item change. Mainly this is done to update the label in case
254 * a query item has been edited
255 *
256 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
257 */
258 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onSavedQueriesItemUpdate = function ( item ) {
259 if ( this.matchingQuery === item ) {
260 // This means we just edited the item that is currently matched
261 this.savedQueryTitle.setLabel( item.getLabel() );
262 }
263 };
264
265 /**
266 * Respond to menu toggle
267 *
268 * @param {boolean} isVisible Menu is visible
269 */
270 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onMenuToggle = function ( isVisible ) {
271 // Parent
272 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onMenuToggle.call( this );
273
274 if ( isVisible ) {
275 mw.hook( 'RcFilters.popup.open' ).fire();
276
277 if ( !this.getMenu().getSelectedItem() ) {
278 // If there are no selected items, scroll menu to top
279 // This has to be in a setTimeout so the menu has time
280 // to be positioned and fixed
281 setTimeout( function () { this.getMenu().scrollToTop(); }.bind( this ), 0 );
282 }
283 } else {
284 // Clear selection
285 this.selectTag( null );
286
287 // Clear input if the only thing in the input is the prefix
288 if (
289 this.input.getValue() === this.model.getViewTrigger( this.model.getCurrentView() )
290 ) {
291 // Clear the input
292 this.input.setValue( '' );
293 }
294
295 // Log filter grouping
296 this.controller.trackFilterGroupings( 'filtermenu' );
297 }
298
299 this.input.setIcon( isVisible ? 'search' : 'menu' );
300 };
301
302 /**
303 * @inheritdoc
304 */
305 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onInputFocus = function () {
306 // Parent
307 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onInputFocus.call( this );
308
309 // Scroll to top
310 this.scrollToTop( this.$element );
311 };
312
313 /**
314 * @inheritdoc
315 */
316 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.doInputEscape = function () {
317 // Parent
318 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.doInputEscape.call( this );
319
320 // Blur the input
321 this.input.$input.blur();
322 };
323
324 /**
325 * @inheritdoc
326 */
327 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onChangeTags = function () {
328 // Parent method
329 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onChangeTags.call( this );
330
331 this.emptyFilterMessage.toggle( this.isEmpty() );
332 };
333
334 /**
335 * Respond to model initialize event
336 */
337 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelInitialize = function () {
338 this.setSavedQueryVisibility();
339 };
340
341 /**
342 * Respond to model update event
343 */
344 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelUpdate = function () {
345 this.updateElementsForView();
346 };
347
348 /**
349 * Update the elements in the widget to the current view
350 */
351 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.updateElementsForView = function () {
352 var view = this.model.getCurrentView(),
353 inputValue = this.input.getValue(),
354 inputView = this.model.getViewByTrigger( inputValue.substr( 0, 1 ) );
355
356 if ( inputView !== 'default' ) {
357 // We have a prefix already, remove it
358 inputValue = inputValue.substr( 1 );
359 }
360
361 if ( inputView !== view ) {
362 // Add the correct prefix
363 inputValue = this.model.getViewTrigger( view ) + inputValue;
364 }
365
366 // Update input
367 this.input.setValue( inputValue );
368 };
369
370 /**
371 * Set the visibility of the saved query button
372 */
373 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.setSavedQueryVisibility = function () {
374 if ( this.areSavedQueriesEnabled ) {
375 this.matchingQuery = this.controller.findQueryMatchingCurrentState();
376
377 this.savedQueryTitle.setLabel(
378 this.matchingQuery ? this.matchingQuery.getLabel() : ''
379 );
380 this.savedQueryTitle.toggle( !!this.matchingQuery );
381 this.saveQueryButton.toggle(
382 !this.isEmpty() &&
383 !this.matchingQuery
384 );
385
386 if ( this.matchingQuery ) {
387 this.emphasize();
388 }
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();
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 ) );