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