RCFilters: Scroll widget to top when switching view
[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 this.currentView = this.model.getCurrentView();
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 this.saveQueryButton = new mw.rcfilters.ui.SaveFiltersPopupButtonWidget(
89 this.controller,
90 this.queriesModel
91 );
92
93 this.saveQueryButton.$element.on( 'mousedown', function ( e ) { e.stopPropagation(); } );
94
95 this.saveQueryButton.connect( this, {
96 click: 'onSaveQueryButtonClick',
97 saveCurrent: 'setSavedQueryVisibility'
98 } );
99
100 this.emptyFilterMessage = new OO.ui.LabelWidget( {
101 label: mw.msg( 'rcfilters-empty-filter' ),
102 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
103 } );
104 this.$content.append( this.emptyFilterMessage.$element );
105
106 // Events
107 this.resetButton.connect( this, { click: 'onResetButtonClick' } );
108 // Stop propagation for mousedown, so that the widget doesn't
109 // trigger the focus on the input and scrolls up when we click the reset button
110 this.resetButton.$element.on( 'mousedown', function ( e ) { e.stopPropagation(); } );
111 this.model.connect( this, {
112 initialize: 'onModelInitialize',
113 update: 'onModelUpdate',
114 itemUpdate: 'onModelItemUpdate',
115 highlightChange: 'onModelHighlightChange'
116 } );
117 this.input.connect( this, { change: 'onInputChange' } );
118 this.queriesModel.connect( this, { itemUpdate: 'onSavedQueriesItemUpdate' } );
119
120 // The filter list and button should appear side by side regardless of how
121 // wide the button is; the button also changes its width depending
122 // on language and its state, so the safest way to present both side
123 // by side is with a table layout
124 rcFiltersRow = $( '<div>' )
125 .addClass( 'mw-rcfilters-ui-row' )
126 .append(
127 this.$content
128 .addClass( 'mw-rcfilters-ui-cell' )
129 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
130 );
131
132 rcFiltersRow.append(
133 $( '<div>' )
134 .addClass( 'mw-rcfilters-ui-cell' )
135 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
136 .append( this.saveQueryButton.$element )
137 );
138
139 // Add a selector at the right of the input
140 this.viewsSelectWidget = new OO.ui.ButtonSelectWidget( {
141 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget' ],
142 items: [
143 new OO.ui.ButtonOptionWidget( {
144 framed: false,
145 data: 'namespaces',
146 icon: 'article',
147 title: mw.msg( 'rcfilters-view-namespaces-tooltip' )
148 } ),
149 new OO.ui.ButtonOptionWidget( {
150 framed: false,
151 data: 'tags',
152 icon: 'tag',
153 title: mw.msg( 'rcfilters-view-tags-tooltip' )
154 } )
155 ]
156 } );
157
158 // Rearrange the UI so the select widget is at the right of the input
159 this.$element.append(
160 $( '<div>' )
161 .addClass( 'mw-rcfilters-ui-table' )
162 .append(
163 $( '<div>' )
164 .addClass( 'mw-rcfilters-ui-row' )
165 .append(
166 $( '<div>' )
167 .addClass( 'mw-rcfilters-ui-cell' )
168 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-input' )
169 .append( this.input.$element ),
170 $( '<div>' )
171 .addClass( 'mw-rcfilters-ui-cell' )
172 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select' )
173 .append( this.viewsSelectWidget.$element )
174 )
175 )
176 );
177
178 // Event
179 this.viewsSelectWidget.connect( this, { choose: 'onViewsSelectWidgetChoose' } );
180
181 rcFiltersRow.append(
182 $( '<div>' )
183 .addClass( 'mw-rcfilters-ui-cell' )
184 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
185 .append( this.resetButton.$element )
186 );
187
188 // Build the content
189 $contentWrapper.append(
190 title.$element,
191 this.savedQueryTitle.$element,
192 $( '<div>' )
193 .addClass( 'mw-rcfilters-ui-table' )
194 .append(
195 rcFiltersRow
196 )
197 );
198
199 // Initialize
200 this.$handle.append( $contentWrapper );
201 this.emptyFilterMessage.toggle( this.isEmpty() );
202 this.savedQueryTitle.toggle( false );
203
204 this.$element
205 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
206
207 this.reevaluateResetRestoreState();
208 };
209
210 /* Initialization */
211
212 OO.inheritClass( mw.rcfilters.ui.FilterTagMultiselectWidget, OO.ui.MenuTagMultiselectWidget );
213
214 /* Methods */
215
216 /**
217 * Respond to view select widget choose event
218 *
219 * @param {OO.ui.ButtonOptionWidget} buttonOptionWidget Chosen widget
220 */
221 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onViewsSelectWidgetChoose = function ( buttonOptionWidget ) {
222 this.controller.switchView( buttonOptionWidget.getData() );
223 this.viewsSelectWidget.selectItem( null );
224 this.focus();
225 };
226
227 /**
228 * Respond to input change event
229 *
230 * @param {string} value Value of the input
231 */
232 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onInputChange = function ( value ) {
233 var view;
234
235 value = value.trim();
236
237 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().trim() === 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 * @inheritdoc
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().trim(),
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 if ( this.currentView !== view ) {
366 this.scrollToTop( this.$element );
367 this.currentView = view;
368 }
369 };
370
371 /**
372 * Set the visibility of the saved query button
373 */
374 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.setSavedQueryVisibility = function () {
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 * Respond to model itemUpdate event
393 *
394 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
395 */
396 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelItemUpdate = function ( item ) {
397 if ( item.getGroupModel().isHidden() ) {
398 return;
399 }
400
401 if (
402 item.isSelected() ||
403 (
404 this.model.isHighlightEnabled() &&
405 item.isHighlightSupported() &&
406 item.getHighlightColor()
407 )
408 ) {
409 this.addTag( item.getName(), item.getLabel() );
410 } else {
411 this.removeTagByData( item.getName() );
412 }
413
414 this.setSavedQueryVisibility();
415
416 // Re-evaluate reset state
417 this.reevaluateResetRestoreState();
418 };
419
420 /**
421 * @inheritdoc
422 */
423 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.isAllowedData = function ( data ) {
424 return (
425 this.model.getItemByName( data ) &&
426 !this.isDuplicateData( data )
427 );
428 };
429
430 /**
431 * @inheritdoc
432 */
433 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onMenuChoose = function ( item ) {
434 this.controller.toggleFilterSelect( item.model.getName() );
435
436 // Select the tag if it exists, or reset selection otherwise
437 this.selectTag( this.getItemFromData( item.model.getName() ) );
438
439 this.focus();
440 };
441
442 /**
443 * Respond to highlightChange event
444 *
445 * @param {boolean} isHighlightEnabled Highlight is enabled
446 */
447 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelHighlightChange = function ( isHighlightEnabled ) {
448 var highlightedItems = this.model.getHighlightedItems();
449
450 if ( isHighlightEnabled ) {
451 // Add capsule widgets
452 highlightedItems.forEach( function ( filterItem ) {
453 this.addTag( filterItem.getName(), filterItem.getLabel() );
454 }.bind( this ) );
455 } else {
456 // Remove capsule widgets if they're not selected
457 highlightedItems.forEach( function ( filterItem ) {
458 if ( !filterItem.isSelected() ) {
459 this.removeTagByData( filterItem.getName() );
460 }
461 }.bind( this ) );
462 }
463 };
464
465 /**
466 * @inheritdoc
467 */
468 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onTagSelect = function ( tagItem ) {
469 var widget = this,
470 menuOption = this.menu.getItemFromModel( tagItem.getModel() ),
471 oldInputValue = this.input.getValue().trim();
472
473 this.menu.setUserSelecting( true );
474
475 // Reset input
476 this.input.setValue( '' );
477
478 // Switch view
479 this.controller.switchView( tagItem.getView() );
480
481 // Parent method
482 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onTagSelect.call( this, tagItem );
483
484 this.menu.selectItem( menuOption );
485 this.selectTag( tagItem );
486
487 // Scroll to the item
488 if ( this.model.removeViewTriggers( oldInputValue ) ) {
489 // We're binding a 'once' to the itemVisibilityChange event
490 // so this happens when the menu is ready after the items
491 // are visible again, in case this is done right after the
492 // user filtered the results
493 this.getMenu().once(
494 'itemVisibilityChange',
495 function () {
496 widget.scrollToTop( menuOption.$element );
497 widget.menu.setUserSelecting( false );
498 }
499 );
500 } else {
501 this.scrollToTop( menuOption.$element );
502 this.menu.setUserSelecting( false );
503 }
504
505 };
506
507 /**
508 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
509 * If no items are given, reset selection from all.
510 *
511 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
512 * omit to deselect all
513 */
514 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.selectTag = function ( item ) {
515 var i, len, selected;
516
517 for ( i = 0, len = this.items.length; i < len; i++ ) {
518 selected = this.items[ i ] === item;
519 if ( this.items[ i ].isSelected() !== selected ) {
520 this.items[ i ].toggleSelected( selected );
521 }
522 }
523 };
524 /**
525 * @inheritdoc
526 */
527 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onTagRemove = function ( tagItem ) {
528 // Parent method
529 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onTagRemove.call( this, tagItem );
530
531 this.controller.clearFilter( tagItem.getName() );
532
533 tagItem.destroy();
534 };
535
536 /**
537 * Respond to click event on the reset button
538 */
539 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onResetButtonClick = function () {
540 if ( this.model.areCurrentFiltersEmpty() ) {
541 // Reset to default filters
542 this.controller.resetToDefaults();
543 } else {
544 // Reset to have no filters
545 this.controller.emptyFilters();
546 }
547 };
548
549 /**
550 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
551 */
552 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.reevaluateResetRestoreState = function () {
553 var defaultsAreEmpty = this.model.areDefaultFiltersEmpty(),
554 currFiltersAreEmpty = this.model.areCurrentFiltersEmpty(),
555 hideResetButton = currFiltersAreEmpty && defaultsAreEmpty;
556
557 this.resetButton.setIcon(
558 currFiltersAreEmpty ? 'history' : 'trash'
559 );
560
561 this.resetButton.setLabel(
562 currFiltersAreEmpty ? mw.msg( 'rcfilters-restore-default-filters' ) : ''
563 );
564 this.resetButton.setTitle(
565 currFiltersAreEmpty ? null : mw.msg( 'rcfilters-clear-all-filters' )
566 );
567
568 this.resetButton.toggle( !hideResetButton );
569 this.emptyFilterMessage.toggle( currFiltersAreEmpty );
570 };
571
572 /**
573 * @inheritdoc
574 */
575 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.createMenuWidget = function ( menuConfig ) {
576 return new mw.rcfilters.ui.MenuSelectWidget(
577 this.controller,
578 this.model,
579 $.extend( {
580 filterFromInput: true
581 }, menuConfig )
582 );
583 };
584
585 /**
586 * @inheritdoc
587 */
588 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.createTagItemWidget = function ( data ) {
589 var filterItem = this.model.getItemByName( data );
590
591 if ( filterItem ) {
592 return new mw.rcfilters.ui.FilterTagItemWidget(
593 this.controller,
594 filterItem,
595 {
596 $overlay: this.$overlay
597 }
598 );
599 }
600 };
601
602 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.emphasize = function () {
603 if (
604 !this.$handle.hasClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' )
605 ) {
606 this.$handle
607 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' )
608 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
609
610 setTimeout( function () {
611 this.$handle
612 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' );
613
614 setTimeout( function () {
615 this.$handle
616 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
617 }.bind( this ), 1000 );
618 }.bind( this ), 500 );
619
620 }
621 };
622 /**
623 * Scroll the element to top within its container
624 *
625 * @private
626 * @param {jQuery} $element Element to position
627 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
628 * much space (in pixels) above the widget.
629 */
630 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.scrollToTop = function ( $element, marginFromTop ) {
631 var container = OO.ui.Element.static.getClosestScrollableContainer( $element[ 0 ], 'y' ),
632 pos = OO.ui.Element.static.getRelativePosition( $element, $( container ) ),
633 containerScrollTop = $( container ).is( 'body, html' ) ? 0 : $( container ).scrollTop();
634
635 // Scroll to item
636 $( container ).animate( {
637 scrollTop: containerScrollTop + pos.top - ( marginFromTop || 0 )
638 } );
639 };
640 }( mediaWiki ) );