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