c4d5877d83439ea1d2ae7f967de5c703f6eb3c00
[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;
242
243 value = value.trim();
244
245 view = this.model.getViewByTrigger( value.substr( 0, 1 ) );
246
247 this.controller.switchView( view );
248 };
249 /**
250 * Respond to query button click
251 */
252 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onSaveQueryButtonClick = function () {
253 this.getMenu().toggle( false );
254 };
255
256 /**
257 * Respond to save query item change. Mainly this is done to update the label in case
258 * a query item has been edited
259 *
260 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
261 */
262 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onSavedQueriesItemUpdate = function ( item ) {
263 if ( this.matchingQuery === item ) {
264 // This means we just edited the item that is currently matched
265 this.savedQueryTitle.setLabel( item.getLabel() );
266 }
267 };
268
269 /**
270 * Respond to menu toggle
271 *
272 * @param {boolean} isVisible Menu is visible
273 */
274 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onMenuToggle = function ( isVisible ) {
275 // Parent
276 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onMenuToggle.call( this );
277
278 if ( isVisible ) {
279 mw.hook( 'RcFilters.popup.open' ).fire();
280
281 if ( !this.getMenu().getSelectedItem() ) {
282 // If there are no selected items, scroll menu to top
283 // This has to be in a setTimeout so the menu has time
284 // to be positioned and fixed
285 setTimeout( function () { this.getMenu().scrollToTop(); }.bind( this ), 0 );
286 }
287 } else {
288 // Clear selection
289 this.selectTag( null );
290
291 // Clear input if the only thing in the input is the prefix
292 if (
293 this.input.getValue().trim() === this.model.getViewTrigger( this.model.getCurrentView() )
294 ) {
295 // Clear the input
296 this.input.setValue( '' );
297 }
298
299 // Log filter grouping
300 this.controller.trackFilterGroupings( 'filtermenu' );
301 }
302
303 this.input.setIcon( isVisible ? 'search' : 'menu' );
304 };
305
306 /**
307 * @inheritdoc
308 */
309 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onInputFocus = function () {
310 // Parent
311 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onInputFocus.call( this );
312
313 // Scroll to top
314 this.scrollToTop( this.$element );
315 };
316
317 /**
318 * @inheritdoc
319 */
320 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.doInputEscape = function () {
321 // Parent
322 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.doInputEscape.call( this );
323
324 // Blur the input
325 this.input.$input.blur();
326 };
327
328 /**
329 * @inheritdoc
330 */
331 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onChangeTags = function () {
332 // Parent method
333 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onChangeTags.call( this );
334
335 this.emptyFilterMessage.toggle( this.isEmpty() );
336 };
337
338 /**
339 * Respond to model initialize event
340 */
341 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelInitialize = function () {
342 this.setSavedQueryVisibility();
343 };
344
345 /**
346 * Respond to model update event
347 */
348 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelUpdate = function () {
349 this.updateElementsForView();
350 };
351
352 /**
353 * Update the elements in the widget to the current view
354 */
355 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.updateElementsForView = function () {
356 var view = this.model.getCurrentView(),
357 inputValue = this.input.getValue().trim(),
358 inputView = this.model.getViewByTrigger( inputValue.substr( 0, 1 ) );
359
360 if ( inputView !== 'default' ) {
361 // We have a prefix already, remove it
362 inputValue = inputValue.substr( 1 );
363 }
364
365 if ( inputView !== view ) {
366 // Add the correct prefix
367 inputValue = this.model.getViewTrigger( view ) + inputValue;
368 }
369
370 // Update input
371 this.input.setValue( inputValue );
372 };
373
374 /**
375 * Set the visibility of the saved query button
376 */
377 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.setSavedQueryVisibility = function () {
378 if ( this.areSavedQueriesEnabled ) {
379 this.matchingQuery = this.controller.findQueryMatchingCurrentState();
380
381 this.savedQueryTitle.setLabel(
382 this.matchingQuery ? this.matchingQuery.getLabel() : ''
383 );
384 this.savedQueryTitle.toggle( !!this.matchingQuery );
385 this.saveQueryButton.toggle(
386 !this.isEmpty() &&
387 !this.matchingQuery
388 );
389
390 if ( this.matchingQuery ) {
391 this.emphasize();
392 }
393 }
394 };
395
396 /**
397 * Respond to model itemUpdate event
398 *
399 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
400 */
401 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelItemUpdate = function ( item ) {
402 if ( item.getGroupModel().isHidden() ) {
403 return;
404 }
405
406 if (
407 item.isSelected() ||
408 (
409 this.model.isHighlightEnabled() &&
410 item.isHighlightSupported() &&
411 item.getHighlightColor()
412 )
413 ) {
414 this.addTag( item.getName(), item.getLabel() );
415 } else {
416 this.removeTagByData( item.getName() );
417 }
418
419 this.setSavedQueryVisibility();
420
421 // Re-evaluate reset state
422 this.reevaluateResetRestoreState();
423 };
424
425 /**
426 * @inheritdoc
427 */
428 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.isAllowedData = function ( data ) {
429 return (
430 this.model.getItemByName( data ) &&
431 !this.isDuplicateData( data )
432 );
433 };
434
435 /**
436 * @inheritdoc
437 */
438 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onMenuChoose = function ( item ) {
439 this.controller.toggleFilterSelect( item.model.getName() );
440
441 // Select the tag if it exists, or reset selection otherwise
442 this.selectTag( this.getItemFromData( item.model.getName() ) );
443
444 this.focus();
445 };
446
447 /**
448 * Respond to highlightChange event
449 *
450 * @param {boolean} isHighlightEnabled Highlight is enabled
451 */
452 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelHighlightChange = function ( isHighlightEnabled ) {
453 var highlightedItems = this.model.getHighlightedItems();
454
455 if ( isHighlightEnabled ) {
456 // Add capsule widgets
457 highlightedItems.forEach( function ( filterItem ) {
458 this.addTag( filterItem.getName(), filterItem.getLabel() );
459 }.bind( this ) );
460 } else {
461 // Remove capsule widgets if they're not selected
462 highlightedItems.forEach( function ( filterItem ) {
463 if ( !filterItem.isSelected() ) {
464 this.removeTagByData( filterItem.getName() );
465 }
466 }.bind( this ) );
467 }
468 };
469
470 /**
471 * @inheritdoc
472 */
473 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onTagSelect = function ( tagItem ) {
474 var widget = this,
475 menuOption = this.menu.getItemFromModel( tagItem.getModel() ),
476 oldInputValue = this.input.getValue().trim();
477
478 this.menu.setUserSelecting( true );
479
480 // Reset input
481 this.input.setValue( '' );
482
483 // Switch view
484 this.controller.switchView( tagItem.getView() );
485
486 // Parent method
487 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onTagSelect.call( this, tagItem );
488
489 this.menu.selectItem( menuOption );
490 this.selectTag( tagItem );
491
492 // Scroll to the item
493 if ( this.model.removeViewTriggers( oldInputValue ) ) {
494 // We're binding a 'once' to the itemVisibilityChange event
495 // so this happens when the menu is ready after the items
496 // are visible again, in case this is done right after the
497 // user filtered the results
498 this.getMenu().once(
499 'itemVisibilityChange',
500 function () {
501 widget.scrollToTop( menuOption.$element );
502 widget.menu.setUserSelecting( false );
503 }
504 );
505 } else {
506 this.scrollToTop( menuOption.$element );
507 this.menu.setUserSelecting( false );
508 }
509
510 };
511
512 /**
513 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
514 * If no items are given, reset selection from all.
515 *
516 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
517 * omit to deselect all
518 */
519 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.selectTag = function ( item ) {
520 var i, len, selected;
521
522 for ( i = 0, len = this.items.length; i < len; i++ ) {
523 selected = this.items[ i ] === item;
524 if ( this.items[ i ].isSelected() !== selected ) {
525 this.items[ i ].toggleSelected( selected );
526 }
527 }
528 };
529 /**
530 * @inheritdoc
531 */
532 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onTagRemove = function ( tagItem ) {
533 // Parent method
534 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onTagRemove.call( this, tagItem );
535
536 this.controller.clearFilter( tagItem.getName() );
537
538 tagItem.destroy();
539 };
540
541 /**
542 * Respond to click event on the reset button
543 */
544 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onResetButtonClick = function () {
545 if ( this.model.areCurrentFiltersEmpty() ) {
546 // Reset to default filters
547 this.controller.resetToDefaults();
548 } else {
549 // Reset to have no filters
550 this.controller.emptyFilters();
551 }
552 };
553
554 /**
555 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
556 */
557 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.reevaluateResetRestoreState = function () {
558 var defaultsAreEmpty = this.model.areDefaultFiltersEmpty(),
559 currFiltersAreEmpty = this.model.areCurrentFiltersEmpty(),
560 hideResetButton = currFiltersAreEmpty && defaultsAreEmpty;
561
562 this.resetButton.setIcon(
563 currFiltersAreEmpty ? 'history' : 'trash'
564 );
565
566 this.resetButton.setLabel(
567 currFiltersAreEmpty ? mw.msg( 'rcfilters-restore-default-filters' ) : ''
568 );
569 this.resetButton.setTitle(
570 currFiltersAreEmpty ? null : mw.msg( 'rcfilters-clear-all-filters' )
571 );
572
573 this.resetButton.toggle( !hideResetButton );
574 this.emptyFilterMessage.toggle( currFiltersAreEmpty );
575 };
576
577 /**
578 * @inheritdoc
579 */
580 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.createMenuWidget = function ( menuConfig ) {
581 return new mw.rcfilters.ui.MenuSelectWidget(
582 this.controller,
583 this.model,
584 $.extend( {
585 filterFromInput: true
586 }, menuConfig )
587 );
588 };
589
590 /**
591 * @inheritdoc
592 */
593 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.createTagItemWidget = function ( data ) {
594 var filterItem = this.model.getItemByName( data );
595
596 if ( filterItem ) {
597 return new mw.rcfilters.ui.FilterTagItemWidget(
598 this.controller,
599 filterItem,
600 {
601 $overlay: this.$overlay
602 }
603 );
604 }
605 };
606
607 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.emphasize = function () {
608 if (
609 !this.$handle.hasClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' )
610 ) {
611 this.$handle
612 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' )
613 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
614
615 setTimeout( function () {
616 this.$handle
617 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' );
618
619 setTimeout( function () {
620 this.$handle
621 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
622 }.bind( this ), 1000 );
623 }.bind( this ), 500 );
624
625 }
626 };
627 /**
628 * Scroll the element to top within its container
629 *
630 * @private
631 * @param {jQuery} $element Element to position
632 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
633 * much space (in pixels) above the widget.
634 */
635 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.scrollToTop = function ( $element, marginFromTop ) {
636 var container = OO.ui.Element.static.getClosestScrollableContainer( $element[ 0 ], 'y' ),
637 pos = OO.ui.Element.static.getRelativePosition( $element, $( container ) ),
638 containerScrollTop = $( container ).is( 'body, html' ) ? 0 : $( container ).scrollTop();
639
640 // Scroll to item
641 $( container ).animate( {
642 scrollTop: containerScrollTop + pos.top - ( marginFromTop || 0 )
643 } );
644 };
645 }( mediaWiki ) );