Merge "RCFilters UI: Prevent label from stealing focus on click"
[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 }
141 };
142
143 /**
144 * @inheritdoc
145 */
146 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onInputFocus = function () {
147 // Parent
148 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onInputFocus.call( this );
149
150 // Scroll to top
151 this.scrollToTop( this.$element );
152 };
153
154 /**
155 * @inheridoc
156 */
157 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onChangeTags = function () {
158 // Parent method
159 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onChangeTags.call( this );
160
161 this.emptyFilterMessage.toggle( this.isEmpty() );
162 };
163
164 /**
165 * Respond to model initialize event
166 */
167 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelInitialize = function () {
168 this.populateFromModel();
169 };
170
171 /**
172 * Respond to model itemUpdate event
173 *
174 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
175 */
176 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelItemUpdate = function ( item ) {
177 if (
178 item.isSelected() ||
179 (
180 this.model.isHighlightEnabled() &&
181 item.isHighlightSupported() &&
182 item.getHighlightColor()
183 )
184 ) {
185 this.addTag( item.getName(), item.getLabel() );
186 } else {
187 this.removeTagByData( item.getName() );
188 }
189
190 // Re-evaluate reset state
191 this.reevaluateResetRestoreState();
192 };
193
194 /**
195 * @inheritdoc
196 */
197 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.isAllowedData = function ( data ) {
198 return (
199 this.menu.getItemFromData( data ) &&
200 !this.isDuplicateData( data )
201 );
202 };
203
204 /**
205 * @inheritdoc
206 */
207 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onMenuChoose = function ( item ) {
208 this.controller.toggleFilterSelect( item.model.getName() );
209
210 this.focus();
211 };
212
213 /**
214 * Respond to highlightChange event
215 *
216 * @param {boolean} isHighlightEnabled Highlight is enabled
217 */
218 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelHighlightChange = function ( isHighlightEnabled ) {
219 var highlightedItems = this.model.getHighlightedItems();
220
221 if ( isHighlightEnabled ) {
222 // Add capsule widgets
223 highlightedItems.forEach( function ( filterItem ) {
224 this.addTag( filterItem.getName(), filterItem.getLabel() );
225 }.bind( this ) );
226 } else {
227 // Remove capsule widgets if they're not selected
228 highlightedItems.forEach( function ( filterItem ) {
229 if ( !filterItem.isSelected() ) {
230 this.removeTagByData( filterItem.getName() );
231 }
232 }.bind( this ) );
233 }
234 };
235
236 /**
237 * @inheritdoc
238 */
239 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onTagSelect = function ( tagItem ) {
240 var widget = this,
241 menuOption = this.menu.getItemFromData( tagItem.getData() ),
242 oldInputValue = this.input.getValue();
243
244 // Reset input
245 this.input.setValue( '' );
246
247 // Parent method
248 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onTagSelect.call( this, tagItem );
249
250 this.menu.selectItem( menuOption );
251
252 // Scroll to the item
253 if ( oldInputValue ) {
254 // We're binding a 'once' to the itemVisibilityChange event
255 // so this happens when the menu is ready after the items
256 // are visible again, in case this is done right after the
257 // user filtered the results
258 this.getMenu().once(
259 'itemVisibilityChange',
260 function () { widget.scrollToTop( menuOption.$element ); }
261 );
262 } else {
263 this.scrollToTop( menuOption.$element );
264 }
265 };
266
267 /**
268 * @inheritdoc
269 */
270 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onTagRemove = function ( tagItem ) {
271 // Parent method
272 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onTagRemove.call( this, tagItem );
273
274 this.controller.clearFilter( tagItem.getName() );
275
276 tagItem.destroy();
277 };
278
279 /**
280 * Respond to click event on the reset button
281 */
282 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onResetButtonClick = function () {
283 if ( this.model.areCurrentFiltersEmpty() ) {
284 // Reset to default filters
285 this.controller.resetToDefaults();
286 } else {
287 // Reset to have no filters
288 this.controller.emptyFilters();
289 }
290 };
291
292 /**
293 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
294 */
295 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.reevaluateResetRestoreState = function () {
296 var defaultsAreEmpty = this.model.areDefaultFiltersEmpty(),
297 currFiltersAreEmpty = this.model.areCurrentFiltersEmpty(),
298 hideResetButton = currFiltersAreEmpty && defaultsAreEmpty;
299
300 this.resetButton.setIcon(
301 currFiltersAreEmpty ? 'history' : 'trash'
302 );
303
304 this.resetButton.setLabel(
305 currFiltersAreEmpty ? mw.msg( 'rcfilters-restore-default-filters' ) : ''
306 );
307 this.resetButton.setTitle(
308 currFiltersAreEmpty ? null : mw.msg( 'rcfilters-clear-all-filters' )
309 );
310
311 this.resetButton.toggle( !hideResetButton );
312 this.emptyFilterMessage.toggle( currFiltersAreEmpty );
313 };
314
315 /**
316 * @inheritdoc
317 */
318 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.createMenuWidget = function ( menuConfig ) {
319 return new mw.rcfilters.ui.FilterFloatingMenuSelectWidget(
320 this.controller,
321 this.model,
322 $.extend( {
323 filterFromInput: true
324 }, menuConfig )
325 );
326 };
327
328 /**
329 * Populate the menu from the model
330 */
331 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.populateFromModel = function () {
332 var widget = this,
333 items = [];
334
335 // Reset
336 this.getMenu().clearItems();
337
338 $.each( this.model.getFilterGroups(), function ( groupName, groupModel ) {
339 items.push(
340 // Group section
341 new mw.rcfilters.ui.FilterMenuSectionOptionWidget(
342 widget.controller,
343 groupModel,
344 {
345 $overlay: widget.$overlay
346 }
347 )
348 );
349
350 // Add items
351 widget.model.getGroupFilters( groupName ).forEach( function ( filterItem ) {
352 items.push(
353 new mw.rcfilters.ui.FilterMenuOptionWidget(
354 widget.controller,
355 filterItem,
356 {
357 $overlay: widget.$overlay
358 }
359 )
360 );
361 } );
362 } );
363
364 // Add all items to the menu
365 this.getMenu().addItems( items );
366 };
367
368 /**
369 * @inheritdoc
370 */
371 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.createTagItemWidget = function ( data ) {
372 var filterItem = this.model.getItemByName( data );
373
374 if ( filterItem ) {
375 return new mw.rcfilters.ui.FilterTagItemWidget(
376 this.controller,
377 filterItem,
378 {
379 $overlay: this.$overlay
380 }
381 );
382 }
383 };
384
385 /**
386 * Scroll the element to top within its container
387 *
388 * @private
389 * @param {jQuery} $element Element to position
390 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
391 * much space (in pixels) above the widget.
392 */
393 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.scrollToTop = function ( $element, marginFromTop ) {
394 var container = OO.ui.Element.static.getClosestScrollableContainer( $element[ 0 ], 'y' ),
395 pos = OO.ui.Element.static.getRelativePosition( $element, $( container ) ),
396 containerScrollTop = $( container ).is( 'body, html' ) ? 0 : $( container ).scrollTop();
397
398 // Scroll to item
399 $( container ).animate( {
400 scrollTop: containerScrollTop + pos.top - ( marginFromTop || 0 )
401 } );
402 };
403 }( mediaWiki ) );