Merge "rdbms: add IDatabase::lockForUpdate() convenience method"
[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.menuInitialized = false;
36 this.$overlay = config.$overlay || this.$element;
37 this.$body = $( '<div>' ).addClass( 'mw-rcfilters-ui-menuSelectWidget-body' );
38 this.footers = [];
39
40 // Parent
41 mw.rcfilters.ui.MenuSelectWidget.parent.call( this, $.extend( config, {
42 $autoCloseIgnore: this.$overlay,
43 width: 650,
44 // Our filtering is done through the model
45 filterFromInput: false
46 } ) );
47 this.setGroupElement(
48 $( '<div>' )
49 .addClass( 'mw-rcfilters-ui-menuSelectWidget-group' )
50 );
51 this.setClippableElement( this.$body );
52 this.setClippableContainer( this.$element );
53
54 header = new mw.rcfilters.ui.FilterMenuHeaderWidget(
55 this.controller,
56 this.model,
57 {
58 $overlay: this.$overlay
59 }
60 );
61
62 this.noResults = new OO.ui.LabelWidget( {
63 label: mw.msg( 'rcfilters-filterlist-noresults' ),
64 classes: [ 'mw-rcfilters-ui-menuSelectWidget-noresults' ]
65 } );
66
67 // Events
68 this.model.connect( this, {
69 initialize: 'onModelInitialize',
70 searchChange: 'onModelSearchChange'
71 } );
72
73 // Initialization
74 this.$element
75 .addClass( 'mw-rcfilters-ui-menuSelectWidget' )
76 .append( header.$element )
77 .append(
78 this.$body
79 .append( this.$group, this.noResults.$element )
80 );
81
82 // Append all footers; we will control their visibility
83 // based on view
84 config.footers = config.footers || [];
85 config.footers.forEach( function ( footerData ) {
86 var isSticky = footerData.sticky === undefined ? true : !!footerData.sticky,
87 adjustedData = {
88 // Wrap the element with our own footer wrapper
89 $element: $( '<div>' )
90 .addClass( 'mw-rcfilters-ui-menuSelectWidget-footer' )
91 .addClass( 'mw-rcfilters-ui-menuSelectWidget-footer-' + footerData.name )
92 .append( footerData.$element ),
93 views: footerData.views
94 };
95
96 if ( !footerData.disabled ) {
97 this.footers.push( adjustedData );
98
99 if ( isSticky ) {
100 this.$element.append( adjustedData.$element );
101 } else {
102 this.$body.append( adjustedData.$element );
103 }
104 }
105 }.bind( this ) );
106
107 // Switch to the correct view
108 this.updateView();
109 };
110
111 /* Initialize */
112
113 OO.inheritClass( mw.rcfilters.ui.MenuSelectWidget, OO.ui.MenuSelectWidget );
114
115 /* Events */
116
117 /* Methods */
118 mw.rcfilters.ui.MenuSelectWidget.prototype.onModelSearchChange = function () {
119 this.updateView();
120 };
121
122 /**
123 * @inheritdoc
124 */
125 mw.rcfilters.ui.MenuSelectWidget.prototype.toggle = function ( show ) {
126 this.lazyMenuCreation();
127 mw.rcfilters.ui.MenuSelectWidget.parent.prototype.toggle.call( this, show );
128 // Always open this menu downwards. FilterTagMultiselectWidget scrolls it into view.
129 this.setVerticalPosition( 'below' );
130 };
131
132 /**
133 * lazy creation of the menu
134 */
135 mw.rcfilters.ui.MenuSelectWidget.prototype.lazyMenuCreation = function () {
136 var widget = this,
137 items = [],
138 viewGroupCount = {},
139 groups = this.model.getFilterGroups();
140
141 if ( this.menuInitialized ) {
142 return;
143 }
144
145 this.menuInitialized = true;
146
147 // Create shared popup for highlight buttons
148 this.highlightPopup = new mw.rcfilters.ui.HighlightPopupWidget( this.controller );
149 this.$overlay.append( this.highlightPopup.$element );
150
151 // Count groups per view
152 $.each( groups, function ( groupName, groupModel ) {
153 if ( !groupModel.isHidden() ) {
154 viewGroupCount[ groupModel.getView() ] = viewGroupCount[ groupModel.getView() ] || 0;
155 viewGroupCount[ groupModel.getView() ]++;
156 }
157 } );
158
159 $.each( groups, function ( groupName, groupModel ) {
160 var currentItems = [],
161 view = groupModel.getView();
162
163 if ( !groupModel.isHidden() ) {
164 if ( viewGroupCount[ view ] > 1 ) {
165 // Only add a section header if there is more than
166 // one group
167 currentItems.push(
168 // Group section
169 new mw.rcfilters.ui.FilterMenuSectionOptionWidget(
170 widget.controller,
171 groupModel,
172 {
173 $overlay: widget.$overlay
174 }
175 )
176 );
177 }
178
179 // Add items
180 widget.model.getGroupFilters( groupName ).forEach( function ( filterItem ) {
181 currentItems.push(
182 new mw.rcfilters.ui.FilterMenuOptionWidget(
183 widget.controller,
184 widget.model,
185 widget.model.getInvertModel(),
186 filterItem,
187 widget.highlightPopup,
188 {
189 $overlay: widget.$overlay
190 }
191 )
192 );
193 } );
194
195 // Cache the items per view, so we can switch between them
196 // without rebuilding the widgets each time
197 widget.views[ view ] = widget.views[ view ] || [];
198 widget.views[ view ] = widget.views[ view ].concat( currentItems );
199 items = items.concat( currentItems );
200 }
201 } );
202
203 this.addItems( items );
204 this.updateView();
205 };
206
207 /**
208 * Respond to model initialize event. Populate the menu from the model
209 */
210 mw.rcfilters.ui.MenuSelectWidget.prototype.onModelInitialize = function () {
211 this.menuInitialized = false;
212 // Set timeout for the menu to lazy build.
213 setTimeout( this.lazyMenuCreation.bind( this ) );
214 };
215
216 /**
217 * Update view
218 */
219 mw.rcfilters.ui.MenuSelectWidget.prototype.updateView = function () {
220 var viewName = this.model.getCurrentView();
221
222 if ( this.views[ viewName ] && this.currentView !== viewName ) {
223 this.updateFooterVisibility( viewName );
224
225 this.$element
226 .data( 'view', viewName )
227 .removeClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + this.currentView )
228 .addClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + viewName );
229
230 this.currentView = viewName;
231 this.scrollToTop();
232 }
233
234 this.postProcessItems();
235 this.clip();
236 };
237
238 /**
239 * Go over the available footers and decide which should be visible
240 * for this view
241 *
242 * @param {string} [currentView] Current view
243 */
244 mw.rcfilters.ui.MenuSelectWidget.prototype.updateFooterVisibility = function ( currentView ) {
245 currentView = currentView || this.model.getCurrentView();
246
247 this.footers.forEach( function ( data ) {
248 data.$element.toggle(
249 // This footer should only be shown if it is configured
250 // for all views or for this specific view
251 !data.views || data.views.length === 0 || data.views.indexOf( currentView ) > -1
252 );
253 } );
254 };
255
256 /**
257 * Post-process items after the visibility changed. Make sure
258 * that we always have an item selected, and that the no-results
259 * widget appears if the menu is empty.
260 */
261 mw.rcfilters.ui.MenuSelectWidget.prototype.postProcessItems = function () {
262 var i,
263 itemWasSelected = false,
264 items = this.getItems();
265
266 // If we are not already selecting an item, always make sure
267 // that the top item is selected
268 if ( !this.userSelecting ) {
269 // Select the first item in the list
270 for ( i = 0; i < items.length; i++ ) {
271 if (
272 !( items[ i ] instanceof OO.ui.MenuSectionOptionWidget ) &&
273 items[ i ].isVisible()
274 ) {
275 itemWasSelected = true;
276 this.selectItem( items[ i ] );
277 break;
278 }
279 }
280
281 if ( !itemWasSelected ) {
282 this.selectItem( null );
283 }
284 }
285
286 this.noResults.toggle( !this.getItems().some( function ( item ) {
287 return item.isVisible();
288 } ) );
289 };
290
291 /**
292 * Get the option widget that matches the model given
293 *
294 * @param {mw.rcfilters.dm.ItemModel} model Item model
295 * @return {mw.rcfilters.ui.ItemMenuOptionWidget} Option widget
296 */
297 mw.rcfilters.ui.MenuSelectWidget.prototype.getItemFromModel = function ( model ) {
298 this.lazyMenuCreation();
299 return this.views[ model.getGroupModel().getView() ].filter( function ( item ) {
300 return item.getName() === model.getName();
301 } )[ 0 ];
302 };
303
304 /**
305 * @inheritdoc
306 */
307 mw.rcfilters.ui.MenuSelectWidget.prototype.onKeyDown = function ( e ) {
308 var nextItem,
309 currentItem = this.findHighlightedItem() || this.findSelectedItem();
310
311 // Call parent
312 mw.rcfilters.ui.MenuSelectWidget.parent.prototype.onKeyDown.call( this, e );
313
314 // We want to select the item on arrow movement
315 // rather than just highlight it, like the menu
316 // does by default
317 if ( !this.isDisabled() && this.isVisible() ) {
318 switch ( e.keyCode ) {
319 case OO.ui.Keys.UP:
320 case OO.ui.Keys.LEFT:
321 // Get the next item
322 nextItem = this.findRelativeSelectableItem( currentItem, -1 );
323 break;
324 case OO.ui.Keys.DOWN:
325 case OO.ui.Keys.RIGHT:
326 // Get the next item
327 nextItem = this.findRelativeSelectableItem( currentItem, 1 );
328 break;
329 }
330
331 nextItem = nextItem && nextItem.constructor.static.selectable ?
332 nextItem : null;
333
334 // Select the next item
335 this.selectItem( nextItem );
336 }
337 };
338
339 /**
340 * Scroll to the top of the menu
341 */
342 mw.rcfilters.ui.MenuSelectWidget.prototype.scrollToTop = function () {
343 this.$body.scrollTop( 0 );
344 };
345
346 /**
347 * Set whether the user is currently selecting an item.
348 * This is important when the user selects an item that is in between
349 * different views, and makes sure we do not re-select a different
350 * item (like the item on top) when this is happening.
351 *
352 * @param {boolean} isSelecting User is selecting
353 */
354 mw.rcfilters.ui.MenuSelectWidget.prototype.setUserSelecting = function ( isSelecting ) {
355 this.userSelecting = !!isSelecting;
356 };
357 }( mediaWiki ) );