Merge "User: Avoid deprecated Linker::link()"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.MenuSelectWidget.js
1 ( function ( mw ) {
2 /**
3 * A floating menu widget for the filter list
4 *
5 * @extends OO.ui.MenuSelectWidget
6 *
7 * @constructor
8 * @param {mw.rcfilters.Controller} controller Controller
9 * @param {mw.rcfilters.dm.FiltersViewModel} model View model
10 * @param {Object} [config] Configuration object
11 * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
12 * @cfg {Object[]} [footers] An array of objects defining the footers for
13 * this menu, with a definition whether they appear per specific views.
14 * The expected structure is:
15 * [
16 * {
17 * name: {string} A unique name for the footer object
18 * $element: {jQuery} A jQuery object for the content of the footer
19 * views: {string[]} Optional. An array stating which views this footer is
20 * active on. Use null or omit to display this on all views.
21 * }
22 * ]
23 */
24 mw.rcfilters.ui.MenuSelectWidget = function MwRcfiltersUiMenuSelectWidget( controller, model, config ) {
25 var header;
26
27 config = config || {};
28
29 this.controller = controller;
30 this.model = model;
31 this.currentView = '';
32 this.views = {};
33 this.userSelecting = false;
34
35 this.inputValue = '';
36 this.$overlay = config.$overlay || this.$element;
37 this.$body = $( '<div>' )
38 .addClass( 'mw-rcfilters-ui-menuSelectWidget-body' );
39 this.footers = [];
40
41 // Parent
42 mw.rcfilters.ui.MenuSelectWidget.parent.call( this, $.extend( {
43 $autoCloseIgnore: this.$overlay,
44 width: 650
45 }, config ) );
46 this.setGroupElement(
47 $( '<div>' )
48 .addClass( 'mw-rcfilters-ui-menuSelectWidget-group' )
49 );
50 this.setClippableElement( this.$body );
51 this.setClippableContainer( this.$element );
52
53 header = new mw.rcfilters.ui.FilterMenuHeaderWidget(
54 this.controller,
55 this.model,
56 {
57 $overlay: this.$overlay
58 }
59 );
60
61 this.noResults = new OO.ui.LabelWidget( {
62 label: mw.msg( 'rcfilters-filterlist-noresults' ),
63 classes: [ 'mw-rcfilters-ui-menuSelectWidget-noresults' ]
64 } );
65
66 // Events
67 this.model.connect( this, {
68 update: 'onModelUpdate',
69 initialize: 'onModelInitialize'
70 } );
71
72 // Initialization
73 this.$element
74 .addClass( 'mw-rcfilters-ui-menuSelectWidget' )
75 .append( header.$element )
76 .append(
77 this.$body
78 .append( this.$group, this.noResults.$element )
79 );
80
81 // Append all footers; we will control their visibility
82 // based on view
83 config.footers = config.footers || [];
84 config.footers.forEach( function ( footerData ) {
85 var isSticky = footerData.sticky === undefined ? true : !!footerData.sticky,
86 adjustedData = {
87 // Wrap the element with our own footer wrapper
88 $element: $( '<div>' )
89 .addClass( 'mw-rcfilters-ui-menuSelectWidget-footer' )
90 .addClass( 'mw-rcfilters-ui-menuSelectWidget-footer-' + footerData.name )
91 .append( footerData.$element ),
92 views: footerData.views
93 };
94
95 this.footers.push( adjustedData );
96
97 if ( isSticky ) {
98 this.$element.append( adjustedData.$element );
99 } else {
100 this.$body.append( adjustedData.$element );
101 }
102 }.bind( this ) );
103
104 // Switch to the correct view
105 this.switchView( this.model.getCurrentView() );
106 };
107
108 /* Initialize */
109
110 OO.inheritClass( mw.rcfilters.ui.MenuSelectWidget, OO.ui.MenuSelectWidget );
111
112 /* Events */
113
114 /**
115 * @event itemVisibilityChange
116 *
117 * Item visibility has changed
118 */
119
120 /* Methods */
121
122 /**
123 * Respond to model update event
124 */
125 mw.rcfilters.ui.MenuSelectWidget.prototype.onModelUpdate = function () {
126 // Change view
127 this.switchView( this.model.getCurrentView() );
128 };
129
130 /**
131 * Respond to model initialize event. Populate the menu from the model
132 */
133 mw.rcfilters.ui.MenuSelectWidget.prototype.onModelInitialize = function () {
134 var widget = this,
135 viewGroupCount = {},
136 groups = this.model.getFilterGroups();
137
138 // Reset
139 this.clearItems();
140
141 // Count groups per view
142 $.each( groups, function ( groupName, groupModel ) {
143 if ( !groupModel.isHidden() ) {
144 viewGroupCount[ groupModel.getView() ] = viewGroupCount[ groupModel.getView() ] || 0;
145 viewGroupCount[ groupModel.getView() ]++;
146 }
147 } );
148
149 $.each( groups, function ( groupName, groupModel ) {
150 var currentItems = [],
151 view = groupModel.getView();
152
153 if ( !groupModel.isHidden() ) {
154 if ( viewGroupCount[ view ] > 1 ) {
155 // Only add a section header if there is more than
156 // one group
157 currentItems.push(
158 // Group section
159 new mw.rcfilters.ui.FilterMenuSectionOptionWidget(
160 widget.controller,
161 groupModel,
162 {
163 $overlay: widget.$overlay
164 }
165 )
166 );
167 }
168
169 // Add items
170 widget.model.getGroupFilters( groupName ).forEach( function ( filterItem ) {
171 currentItems.push(
172 new mw.rcfilters.ui.FilterMenuOptionWidget(
173 widget.controller,
174 filterItem,
175 {
176 $overlay: widget.$overlay
177 }
178 )
179 );
180 } );
181
182 // Cache the items per view, so we can switch between them
183 // without rebuilding the widgets each time
184 widget.views[ view ] = widget.views[ view ] || [];
185 widget.views[ view ] = widget.views[ view ].concat( currentItems );
186 }
187 } );
188
189 this.switchView( this.model.getCurrentView() );
190 };
191
192 /**
193 * Switch view
194 *
195 * @param {string} [viewName] View name. If not given, default is used.
196 */
197 mw.rcfilters.ui.MenuSelectWidget.prototype.switchView = function ( viewName ) {
198 viewName = viewName || 'default';
199
200 if ( this.views[ viewName ] && this.currentView !== viewName ) {
201 this.clearItems();
202 this.addItems( this.views[ viewName ] );
203 this.updateFooterVisibility( viewName );
204
205 this.$element
206 .data( 'view', viewName )
207 .removeClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + this.currentView )
208 .addClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + viewName );
209
210 this.currentView = viewName;
211 this.clip();
212 }
213 };
214
215 /**
216 * Go over the available footers and decide which should be visible
217 * for this view
218 *
219 * @param {string} [currentView] Current view
220 */
221 mw.rcfilters.ui.MenuSelectWidget.prototype.updateFooterVisibility = function ( currentView ) {
222 currentView = currentView || this.model.getCurrentView();
223
224 this.footers.forEach( function ( data ) {
225 data.$element.toggle(
226 // This footer should only be shown if it is configured
227 // for all views or for this specific view
228 !data.views || data.views.length === 0 || data.views.indexOf( currentView ) > -1
229 );
230 } );
231 };
232
233 /**
234 * @fires itemVisibilityChange
235 * @inheritdoc
236 */
237 mw.rcfilters.ui.MenuSelectWidget.prototype.updateItemVisibility = function () {
238 var i,
239 itemWasSelected = false,
240 inputVal = this.$input.val(),
241 items = this.getItems();
242
243 // Since the method hides/shows items, we don't want to
244 // call it unless the input actually changed
245 if (
246 !this.userSelecting &&
247 this.inputValue !== inputVal
248 ) {
249 // Parent method
250 mw.rcfilters.ui.MenuSelectWidget.parent.prototype.updateItemVisibility.call( this );
251
252 // Select the first item in the list
253 for ( i = 0; i < items.length; i++ ) {
254 if (
255 !( items[ i ] instanceof OO.ui.MenuSectionOptionWidget ) &&
256 items[ i ].isVisible()
257 ) {
258 itemWasSelected = true;
259 this.selectItem( items[ i ] );
260 break;
261 }
262 }
263
264 if ( !itemWasSelected ) {
265 this.selectItem( null );
266 }
267
268 // Cache value
269 this.inputValue = inputVal;
270
271 this.emit( 'itemVisibilityChange' );
272 }
273 };
274
275 /**
276 * Get the option widget that matches the model given
277 *
278 * @param {mw.rcfilters.dm.ItemModel} model Item model
279 * @return {mw.rcfilters.ui.ItemMenuOptionWidget} Option widget
280 */
281 mw.rcfilters.ui.MenuSelectWidget.prototype.getItemFromModel = function ( model ) {
282 return this.views[ model.getGroupModel().getView() ].filter( function ( item ) {
283 return item.getName() === model.getName();
284 } )[ 0 ];
285 };
286
287 /**
288 * Override the item matcher to use the model's match process
289 *
290 * @inheritdoc
291 */
292 mw.rcfilters.ui.MenuSelectWidget.prototype.getItemMatcher = function ( s ) {
293 var results = this.model.findMatches( s, true );
294
295 return function ( item ) {
296 return results.indexOf( item.getModel() ) > -1;
297 };
298 };
299
300 /**
301 * @inheritdoc
302 */
303 mw.rcfilters.ui.MenuSelectWidget.prototype.onKeyDown = function ( e ) {
304 var nextItem,
305 currentItem = this.getHighlightedItem() || this.getSelectedItem();
306
307 // Call parent
308 mw.rcfilters.ui.MenuSelectWidget.parent.prototype.onKeyDown.call( this, e );
309
310 // We want to select the item on arrow movement
311 // rather than just highlight it, like the menu
312 // does by default
313 if ( !this.isDisabled() && this.isVisible() ) {
314 switch ( e.keyCode ) {
315 case OO.ui.Keys.UP:
316 case OO.ui.Keys.LEFT:
317 // Get the next item
318 nextItem = this.getRelativeSelectableItem( currentItem, -1 );
319 break;
320 case OO.ui.Keys.DOWN:
321 case OO.ui.Keys.RIGHT:
322 // Get the next item
323 nextItem = this.getRelativeSelectableItem( currentItem, 1 );
324 break;
325 }
326
327 nextItem = nextItem && nextItem.constructor.static.selectable ?
328 nextItem : null;
329
330 // Select the next item
331 this.selectItem( nextItem );
332 }
333 };
334
335 /**
336 * Scroll to the top of the menu
337 */
338 mw.rcfilters.ui.MenuSelectWidget.prototype.scrollToTop = function () {
339 this.$body.scrollTop( 0 );
340 };
341
342 /**
343 * Set whether the user is currently selecting an item.
344 * This is important when the user selects an item that is in between
345 * different views, and makes sure we do not re-select a different
346 * item (like the item on top) when this is happening.
347 *
348 * @param {boolean} isSelecting User is selecting
349 */
350 mw.rcfilters.ui.MenuSelectWidget.prototype.setUserSelecting = function ( isSelecting ) {
351 this.userSelecting = !!isSelecting;
352 };
353 }( mediaWiki ) );