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