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