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