Merge "RCFilters: Have the model accept multiple views"
[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 // 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: 'search',
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 if ( areSavedQueriesEnabled ) {
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
101 this.emptyFilterMessage = new OO.ui.LabelWidget( {
102 label: mw.msg( 'rcfilters-empty-filter' ),
103 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
104 } );
105 this.$content.append( this.emptyFilterMessage.$element );
106
107 // Events
108 this.resetButton.connect( this, { click: 'onResetButtonClick' } );
109 // Stop propagation for mousedown, so that the widget doesn't
110 // trigger the focus on the input and scrolls up when we click the reset button
111 this.resetButton.$element.on( 'mousedown', function ( e ) { e.stopPropagation(); } );
112 this.model.connect( this, {
113 initialize: 'onModelInitialize',
114 update: 'onModelUpdate',
115 itemUpdate: 'onModelItemUpdate',
116 highlightChange: 'onModelHighlightChange'
117 } );
118 this.input.connect( this, { change: 'onInputChange' } );
119 this.queriesModel.connect( this, { itemUpdate: 'onSavedQueriesItemUpdate' } );
120
121 // The filter list and button should appear side by side regardless of how
122 // wide the button is; the button also changes its width depending
123 // on language and its state, so the safest way to present both side
124 // by side is with a table layout
125 rcFiltersRow = $( '<div>' )
126 .addClass( 'mw-rcfilters-ui-row' )
127 .append(
128 this.$content
129 .addClass( 'mw-rcfilters-ui-cell' )
130 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
131 );
132
133 if ( areSavedQueriesEnabled ) {
134 rcFiltersRow.append(
135 $( '<div>' )
136 .addClass( 'mw-rcfilters-ui-cell' )
137 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
138 .append( this.saveQueryButton.$element )
139 );
140 }
141
142 if ( mw.config.get( 'wgStructuredChangeFiltersEnableExperimentalViews' ) ) {
143 // Add a selector at the right of the input
144 this.viewsSelectWidget = new OO.ui.ButtonSelectWidget( {
145 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget' ],
146 items: [
147 new OO.ui.ButtonOptionWidget( {
148 data: 'namespaces',
149 icon: 'article',
150 title: mw.msg( 'namespaces' )
151 } ),
152 new OO.ui.ButtonOptionWidget( {
153 data: 'tags',
154 icon: 'tag',
155 title: mw.msg( 'rcfilters-view-tags' )
156 } )
157 ]
158 } );
159
160 // Rearrange the UI so the select widget is at the right of the input
161 this.$element.append(
162 $( '<div>' )
163 .addClass( 'mw-rcfilters-ui-table' )
164 .append(
165 $( '<div>' )
166 .addClass( 'mw-rcfilters-ui-row' )
167 .append(
168 $( '<div>' )
169 .addClass( 'mw-rcfilters-ui-cell' )
170 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-input' )
171 .append( this.input.$element ),
172 $( '<div>' )
173 .addClass( 'mw-rcfilters-ui-cell' )
174 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select' )
175 .append( this.viewsSelectWidget.$element )
176 )
177 )
178 );
179
180 // Event
181 this.viewsSelectWidget.connect( this, { choose: 'onViewsSelectWidgetChoose' } );
182 }
183
184 rcFiltersRow.append(
185 $( '<div>' )
186 .addClass( 'mw-rcfilters-ui-cell' )
187 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
188 .append( this.resetButton.$element )
189 );
190
191 // Build the content
192 $contentWrapper.append(
193 title.$element,
194 this.savedQueryTitle.$element,
195 $( '<div>' )
196 .addClass( 'mw-rcfilters-ui-table' )
197 .append(
198 rcFiltersRow
199 )
200 );
201
202 // Initialize
203 this.$handle.append( $contentWrapper );
204 this.emptyFilterMessage.toggle( this.isEmpty() );
205 this.savedQueryTitle.toggle( false );
206
207 this.$element
208 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
209
210 this.reevaluateResetRestoreState();
211 };
212
213 /* Initialization */
214
215 OO.inheritClass( mw.rcfilters.ui.FilterTagMultiselectWidget, OO.ui.MenuTagMultiselectWidget );
216
217 /* Methods */
218
219 /**
220 * Respond to view select widget choose event
221 *
222 * @param {OO.ui.ButtonOptionWidget} buttonOptionWidget Chosen widget
223 */
224 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onViewsSelectWidgetChoose = function ( buttonOptionWidget ) {
225 this.controller.switchView( buttonOptionWidget.getData() );
226 this.viewsSelectWidget.selectItem( null );
227 this.focus();
228 };
229
230 /**
231 * Respond to input change event
232 *
233 * @param {string} value Value of the input
234 */
235 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onInputChange = function ( value ) {
236 var 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() === this.model.getViewTrigger( this.model.getCurrentView() )
285 ) {
286 // Clear the input
287 this.input.setValue( '' );
288 }
289 }
290 };
291
292 /**
293 * @inheritdoc
294 */
295 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onInputFocus = function () {
296 // Parent
297 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onInputFocus.call( this );
298
299 // Scroll to top
300 this.scrollToTop( this.$element );
301 };
302
303 /**
304 * @inheritdoc
305 */
306 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.doInputEscape = function () {
307 // Parent
308 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.doInputEscape.call( this );
309
310 // Blur the input
311 this.input.$input.blur();
312 };
313
314 /**
315 * @inheridoc
316 */
317 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onChangeTags = function () {
318 // Parent method
319 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onChangeTags.call( this );
320
321 this.emptyFilterMessage.toggle( this.isEmpty() );
322 };
323
324 /**
325 * Respond to model initialize event
326 */
327 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelInitialize = function () {
328 this.setSavedQueryVisibility();
329 };
330
331 /**
332 * Respond to model update event
333 */
334 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelUpdate = function () {
335 this.updateElementsForView();
336 };
337
338 /**
339 * Update the elements in the widget to the current view
340 */
341 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.updateElementsForView = function () {
342 var view = this.model.getCurrentView(),
343 inputValue = this.input.getValue(),
344 inputView = this.model.getViewByTrigger( inputValue.substr( 0, 1 ) );
345
346 if ( inputView !== 'default' ) {
347 // We have a prefix already, remove it
348 inputValue = inputValue.substr( 1 );
349 }
350
351 if ( inputView !== view ) {
352 // Add the correct prefix
353 inputValue = this.model.getViewTrigger( view ) + inputValue;
354 }
355
356 // Update input
357 this.input.setValue( inputValue );
358 };
359
360 /**
361 * Set the visibility of the saved query button
362 */
363 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.setSavedQueryVisibility = function () {
364 if ( this.areSavedQueriesEnabled ) {
365 this.matchingQuery = this.controller.findQueryMatchingCurrentState();
366
367 this.savedQueryTitle.setLabel(
368 this.matchingQuery ? this.matchingQuery.getLabel() : ''
369 );
370 this.savedQueryTitle.toggle( !!this.matchingQuery );
371 this.saveQueryButton.toggle(
372 !this.isEmpty() &&
373 !this.matchingQuery
374 );
375 }
376 };
377
378 /**
379 * Respond to model itemUpdate event
380 *
381 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
382 */
383 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelItemUpdate = function ( item ) {
384 if (
385 item.isSelected() ||
386 (
387 this.model.isHighlightEnabled() &&
388 item.isHighlightSupported() &&
389 item.getHighlightColor()
390 )
391 ) {
392 this.addTag( item.getName(), item.getLabel() );
393 } else {
394 this.removeTagByData( item.getName() );
395 }
396
397 this.setSavedQueryVisibility();
398
399 // Re-evaluate reset state
400 this.reevaluateResetRestoreState();
401 };
402
403 /**
404 * @inheritdoc
405 */
406 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.isAllowedData = function ( data ) {
407 return (
408 this.model.getItemByName( data ) &&
409 !this.isDuplicateData( data )
410 );
411 };
412
413 /**
414 * @inheritdoc
415 */
416 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onMenuChoose = function ( item ) {
417 this.controller.toggleFilterSelect( item.model.getName() );
418
419 // Select the tag if it exists, or reset selection otherwise
420 this.selectTag( this.getItemFromData( item.model.getName() ) );
421
422 this.focus();
423 };
424
425 /**
426 * Respond to highlightChange event
427 *
428 * @param {boolean} isHighlightEnabled Highlight is enabled
429 */
430 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelHighlightChange = function ( isHighlightEnabled ) {
431 var highlightedItems = this.model.getHighlightedItems();
432
433 if ( isHighlightEnabled ) {
434 // Add capsule widgets
435 highlightedItems.forEach( function ( filterItem ) {
436 this.addTag( filterItem.getName(), filterItem.getLabel() );
437 }.bind( this ) );
438 } else {
439 // Remove capsule widgets if they're not selected
440 highlightedItems.forEach( function ( filterItem ) {
441 if ( !filterItem.isSelected() ) {
442 this.removeTagByData( filterItem.getName() );
443 }
444 }.bind( this ) );
445 }
446 };
447
448 /**
449 * @inheritdoc
450 */
451 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onTagSelect = function ( tagItem ) {
452 var widget = this,
453 menuOption = this.menu.getItemFromModel( tagItem.getModel() ),
454 oldInputValue = this.input.getValue();
455
456 // Reset input
457 this.input.setValue( '' );
458
459 // Switch view
460 this.controller.switchView( tagItem.getView() );
461
462 // Parent method
463 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onTagSelect.call( this, tagItem );
464
465 this.menu.selectItem( menuOption );
466 this.selectTag( tagItem );
467
468 // Scroll to the item
469 if ( this.model.removeViewTriggers( oldInputValue ) ) {
470 // We're binding a 'once' to the itemVisibilityChange event
471 // so this happens when the menu is ready after the items
472 // are visible again, in case this is done right after the
473 // user filtered the results
474 this.getMenu().once(
475 'itemVisibilityChange',
476 function () { widget.scrollToTop( menuOption.$element ); }
477 );
478 } else {
479 this.scrollToTop( menuOption.$element );
480 }
481 };
482
483 /**
484 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
485 * If no items are given, reset selection from all.
486 *
487 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
488 * omit to deselect all
489 */
490 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.selectTag = function ( item ) {
491 var i, len, selected;
492
493 for ( i = 0, len = this.items.length; i < len; i++ ) {
494 selected = this.items[ i ] === item;
495 if ( this.items[ i ].isSelected() !== selected ) {
496 this.items[ i ].toggleSelected( selected );
497 }
498 }
499 };
500 /**
501 * @inheritdoc
502 */
503 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onTagRemove = function ( tagItem ) {
504 // Parent method
505 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onTagRemove.call( this, tagItem );
506
507 this.controller.clearFilter( tagItem.getName() );
508
509 tagItem.destroy();
510 };
511
512 /**
513 * Respond to click event on the reset button
514 */
515 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onResetButtonClick = function () {
516 if ( this.model.areCurrentFiltersEmpty() ) {
517 // Reset to default filters
518 this.controller.resetToDefaults();
519 } else {
520 // Reset to have no filters
521 this.controller.emptyFilters();
522 }
523 };
524
525 /**
526 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
527 */
528 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.reevaluateResetRestoreState = function () {
529 var defaultsAreEmpty = this.model.areDefaultFiltersEmpty(),
530 currFiltersAreEmpty = this.model.areCurrentFiltersEmpty(),
531 hideResetButton = currFiltersAreEmpty && defaultsAreEmpty;
532
533 this.resetButton.setIcon(
534 currFiltersAreEmpty ? 'history' : 'trash'
535 );
536
537 this.resetButton.setLabel(
538 currFiltersAreEmpty ? mw.msg( 'rcfilters-restore-default-filters' ) : ''
539 );
540 this.resetButton.setTitle(
541 currFiltersAreEmpty ? null : mw.msg( 'rcfilters-clear-all-filters' )
542 );
543
544 this.resetButton.toggle( !hideResetButton );
545 this.emptyFilterMessage.toggle( currFiltersAreEmpty );
546 };
547
548 /**
549 * @inheritdoc
550 */
551 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.createMenuWidget = function ( menuConfig ) {
552 return new mw.rcfilters.ui.MenuSelectWidget(
553 this.controller,
554 this.model,
555 $.extend( {
556 filterFromInput: true
557 }, menuConfig )
558 );
559 };
560
561 /**
562 * @inheritdoc
563 */
564 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.createTagItemWidget = function ( data ) {
565 var filterItem = this.model.getItemByName( data );
566
567 if ( filterItem ) {
568 return new mw.rcfilters.ui.FilterTagItemWidget(
569 this.controller,
570 filterItem,
571 {
572 $overlay: this.$overlay
573 }
574 );
575 }
576 };
577
578 /**
579 * Scroll the element to top within its container
580 *
581 * @private
582 * @param {jQuery} $element Element to position
583 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
584 * much space (in pixels) above the widget.
585 */
586 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.scrollToTop = function ( $element, marginFromTop ) {
587 var container = OO.ui.Element.static.getClosestScrollableContainer( $element[ 0 ], 'y' ),
588 pos = OO.ui.Element.static.getRelativePosition( $element, $( container ) ),
589 containerScrollTop = $( container ).is( 'body, html' ) ? 0 : $( container ).scrollTop();
590
591 // Scroll to item
592 $( container ).animate( {
593 scrollTop: containerScrollTop + pos.top - ( marginFromTop || 0 )
594 } );
595 };
596 }( mediaWiki ) );