Merge "Replace some usages of Linker::link with LinkRenderer"
[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 adjustedData = {
86 // Wrap the element with our own footer wrapper
87 $element: $( '<div>' )
88 .addClass( 'mw-rcfilters-ui-menuSelectWidget-footer' )
89 .addClass( 'mw-rcfilters-ui-menuSelectWidget-footer-' + footerData.name )
90 .append( footerData.$element ),
91 views: footerData.views
92 };
93
94 this.footers.push( adjustedData );
95 this.$element.append( adjustedData.$element );
96 }.bind( this ) );
97
98 // Switch to the correct view
99 this.switchView( this.model.getCurrentView() );
100 };
101
102 /* Initialize */
103
104 OO.inheritClass( mw.rcfilters.ui.MenuSelectWidget, OO.ui.MenuSelectWidget );
105
106 /* Events */
107
108 /**
109 * @event itemVisibilityChange
110 *
111 * Item visibility has changed
112 */
113
114 /* Methods */
115
116 /**
117 * Respond to model update event
118 */
119 mw.rcfilters.ui.MenuSelectWidget.prototype.onModelUpdate = function () {
120 // Change view
121 this.switchView( this.model.getCurrentView() );
122 };
123
124 /**
125 * Respond to model initialize event. Populate the menu from the model
126 */
127 mw.rcfilters.ui.MenuSelectWidget.prototype.onModelInitialize = function () {
128 var widget = this,
129 viewGroupCount = {},
130 groups = this.model.getFilterGroups();
131
132 // Reset
133 this.clearItems();
134
135 // Count groups per view
136 $.each( groups, function ( groupName, groupModel ) {
137 viewGroupCount[ groupModel.getView() ] = viewGroupCount[ groupModel.getView() ] || 0;
138 viewGroupCount[ groupModel.getView() ]++;
139 } );
140
141 $.each( groups, function ( groupName, groupModel ) {
142 var currentItems = [],
143 view = groupModel.getView();
144
145 if ( viewGroupCount[ view ] > 1 ) {
146 // Only add a section header if there is more than
147 // one group
148 currentItems.push(
149 // Group section
150 new mw.rcfilters.ui.FilterMenuSectionOptionWidget(
151 widget.controller,
152 groupModel,
153 {
154 $overlay: widget.$overlay
155 }
156 )
157 );
158 }
159
160 // Add items
161 widget.model.getGroupFilters( groupName ).forEach( function ( filterItem ) {
162 currentItems.push(
163 new mw.rcfilters.ui.FilterMenuOptionWidget(
164 widget.controller,
165 filterItem,
166 {
167 $overlay: widget.$overlay
168 }
169 )
170 );
171 } );
172
173 // Cache the items per view, so we can switch between them
174 // without rebuilding the widgets each time
175 widget.views[ view ] = widget.views[ view ] || [];
176 widget.views[ view ] = widget.views[ view ].concat( currentItems );
177 } );
178
179 this.switchView( this.model.getCurrentView() );
180 };
181
182 /**
183 * Switch view
184 *
185 * @param {string} [viewName] View name. If not given, default is used.
186 */
187 mw.rcfilters.ui.MenuSelectWidget.prototype.switchView = function ( viewName ) {
188 viewName = viewName || 'default';
189
190 if ( this.views[ viewName ] && this.currentView !== viewName ) {
191 this.clearItems();
192 this.addItems( this.views[ viewName ] );
193 this.updateFooterVisibility( viewName );
194
195 this.$element
196 .data( 'view', viewName )
197 .removeClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + this.currentView )
198 .addClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + viewName );
199
200 this.currentView = viewName;
201 this.clip();
202 }
203 };
204
205 /**
206 * Go over the available footers and decide which should be visible
207 * for this view
208 *
209 * @param {string} [currentView] Current view
210 */
211 mw.rcfilters.ui.MenuSelectWidget.prototype.updateFooterVisibility = function ( currentView ) {
212 currentView = currentView || this.model.getCurrentView();
213
214 this.footers.forEach( function ( data ) {
215 data.$element.toggle(
216 // This footer should only be shown if it is configured
217 // for all views or for this specific view
218 !data.views || data.views.length === 0 || data.views.indexOf( currentView ) > -1
219 );
220 } );
221 };
222
223 /**
224 * @fires itemVisibilityChange
225 * @inheritdoc
226 */
227 mw.rcfilters.ui.MenuSelectWidget.prototype.updateItemVisibility = function () {
228 var i,
229 itemWasSelected = false,
230 inputVal = this.$input.val(),
231 items = this.getItems();
232
233 // Since the method hides/shows items, we don't want to
234 // call it unless the input actually changed
235 if (
236 !this.userSelecting &&
237 this.inputValue !== inputVal
238 ) {
239 // Parent method
240 mw.rcfilters.ui.MenuSelectWidget.parent.prototype.updateItemVisibility.call( this );
241
242 // Select the first item in the list
243 for ( i = 0; i < items.length; i++ ) {
244 if (
245 !( items[ i ] instanceof OO.ui.MenuSectionOptionWidget ) &&
246 items[ i ].isVisible()
247 ) {
248 itemWasSelected = true;
249 this.selectItem( items[ i ] );
250 break;
251 }
252 }
253
254 if ( !itemWasSelected ) {
255 this.selectItem( null );
256 }
257
258 // Cache value
259 this.inputValue = inputVal;
260
261 this.emit( 'itemVisibilityChange' );
262 }
263 };
264
265 /**
266 * Get the option widget that matches the model given
267 *
268 * @param {mw.rcfilters.dm.ItemModel} model Item model
269 * @return {mw.rcfilters.ui.ItemMenuOptionWidget} Option widget
270 */
271 mw.rcfilters.ui.MenuSelectWidget.prototype.getItemFromModel = function ( model ) {
272 return this.views[ model.getGroupModel().getView() ].filter( function ( item ) {
273 return item.getName() === model.getName();
274 } )[ 0 ];
275 };
276
277 /**
278 * Override the item matcher to use the model's match process
279 *
280 * @inheritdoc
281 */
282 mw.rcfilters.ui.MenuSelectWidget.prototype.getItemMatcher = function ( s ) {
283 var results = this.model.findMatches( s, true );
284
285 return function ( item ) {
286 return results.indexOf( item.getModel() ) > -1;
287 };
288 };
289
290 /**
291 * @inheritdoc
292 */
293 mw.rcfilters.ui.MenuSelectWidget.prototype.onKeyDown = function ( e ) {
294 var nextItem,
295 currentItem = this.getHighlightedItem() || this.getSelectedItem();
296
297 // Call parent
298 mw.rcfilters.ui.MenuSelectWidget.parent.prototype.onKeyDown.call( this, e );
299
300 // We want to select the item on arrow movement
301 // rather than just highlight it, like the menu
302 // does by default
303 if ( !this.isDisabled() && this.isVisible() ) {
304 switch ( e.keyCode ) {
305 case OO.ui.Keys.UP:
306 case OO.ui.Keys.LEFT:
307 // Get the next item
308 nextItem = this.getRelativeSelectableItem( currentItem, -1 );
309 break;
310 case OO.ui.Keys.DOWN:
311 case OO.ui.Keys.RIGHT:
312 // Get the next item
313 nextItem = this.getRelativeSelectableItem( currentItem, 1 );
314 break;
315 }
316
317 nextItem = nextItem && nextItem.constructor.static.selectable ?
318 nextItem : null;
319
320 // Select the next item
321 this.selectItem( nextItem );
322 }
323 };
324
325 /**
326 * Scroll to the top of the menu
327 */
328 mw.rcfilters.ui.MenuSelectWidget.prototype.scrollToTop = function () {
329 this.$body.scrollTop( 0 );
330 };
331
332 /**
333 * Set whether the user is currently selecting an item.
334 * This is important when the user selects an item that is in between
335 * different views, and makes sure we do not re-select a different
336 * item (like the item on top) when this is happening.
337 *
338 * @param {boolean} isSelecting User is selecting
339 */
340 mw.rcfilters.ui.MenuSelectWidget.prototype.setUserSelecting = function ( isSelecting ) {
341 this.userSelecting = !!isSelecting;
342 };
343 }( mediaWiki ) );