summaryrefslogtreecommitdiff
path: root/www/wiki/resources/src/mediawiki.rcfilters/ui/mw.rcfilters.ui.MenuSelectWidget.js
blob: d968f0c486d0c922ecacd4ea359acbb35e9566aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
( function ( mw ) {
	/**
	 * A floating menu widget for the filter list
	 *
	 * @extends OO.ui.MenuSelectWidget
	 *
	 * @constructor
	 * @param {mw.rcfilters.Controller} controller Controller
	 * @param {mw.rcfilters.dm.FiltersViewModel} model View model
	 * @param {Object} [config] Configuration object
	 * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
	 * @cfg {Object[]} [footers] An array of objects defining the footers for
	 *  this menu, with a definition whether they appear per specific views.
	 *  The expected structure is:
	 *  [
	 *     {
	 *        name: {string} A unique name for the footer object
	 *        $element: {jQuery} A jQuery object for the content of the footer
	 *        views: {string[]} Optional. An array stating which views this footer is
	 *               active on. Use null or omit to display this on all views.
	 *     }
	 *  ]
	 */
	mw.rcfilters.ui.MenuSelectWidget = function MwRcfiltersUiMenuSelectWidget( controller, model, config ) {
		var header;

		config = config || {};

		this.controller = controller;
		this.model = model;
		this.currentView = '';
		this.views = {};
		this.userSelecting = false;

		this.menuInitialized = false;
		this.$overlay = config.$overlay || this.$element;
		this.$body = $( '<div>' ).addClass( 'mw-rcfilters-ui-menuSelectWidget-body' );
		this.footers = [];

		// Parent
		mw.rcfilters.ui.MenuSelectWidget.parent.call( this, $.extend( config, {
			$autoCloseIgnore: this.$overlay,
			width: 650,
			// Our filtering is done through the model
			filterFromInput: false
		} ) );
		this.setGroupElement(
			$( '<div>' )
				.addClass( 'mw-rcfilters-ui-menuSelectWidget-group' )
		);
		this.setClippableElement( this.$body );
		this.setClippableContainer( this.$element );

		header = new mw.rcfilters.ui.FilterMenuHeaderWidget(
			this.controller,
			this.model,
			{
				$overlay: this.$overlay
			}
		);

		this.noResults = new OO.ui.LabelWidget( {
			label: mw.msg( 'rcfilters-filterlist-noresults' ),
			classes: [ 'mw-rcfilters-ui-menuSelectWidget-noresults' ]
		} );

		// Events
		this.model.connect( this, {
			initialize: 'onModelInitialize',
			searchChange: 'onModelSearchChange'
		} );

		// Initialization
		this.$element
			.addClass( 'mw-rcfilters-ui-menuSelectWidget' )
			.append( header.$element )
			.append(
				this.$body
					.append( this.$group, this.noResults.$element )
			);

		// Append all footers; we will control their visibility
		// based on view
		config.footers = config.footers || [];
		config.footers.forEach( function ( footerData ) {
			var isSticky = footerData.sticky === undefined ? true : !!footerData.sticky,
				adjustedData = {
					// Wrap the element with our own footer wrapper
					$element: $( '<div>' )
						.addClass( 'mw-rcfilters-ui-menuSelectWidget-footer' )
						.addClass( 'mw-rcfilters-ui-menuSelectWidget-footer-' + footerData.name )
						.append( footerData.$element ),
					views: footerData.views
				};

			if ( !footerData.disabled ) {
				this.footers.push( adjustedData );

				if ( isSticky ) {
					this.$element.append( adjustedData.$element );
				} else {
					this.$body.append( adjustedData.$element );
				}
			}
		}.bind( this ) );

		// Switch to the correct view
		this.updateView();
	};

	/* Initialize */

	OO.inheritClass( mw.rcfilters.ui.MenuSelectWidget, OO.ui.MenuSelectWidget );

	/* Events */

	/* Methods */
	mw.rcfilters.ui.MenuSelectWidget.prototype.onModelSearchChange = function () {
		this.updateView();
	};

	/**
	 * @inheritdoc
	 */
	mw.rcfilters.ui.MenuSelectWidget.prototype.toggle = function ( show ) {
		this.lazyMenuCreation();
		mw.rcfilters.ui.MenuSelectWidget.parent.prototype.toggle.call( this, show );
		// Always open this menu downwards. FilterTagMultiselectWidget scrolls it into view.
		this.setVerticalPosition( 'below' );
	};

	/**
	 * lazy creation of the menu
	 */
	mw.rcfilters.ui.MenuSelectWidget.prototype.lazyMenuCreation = function () {
		var widget = this,
			items = [],
			viewGroupCount = {},
			groups = this.model.getFilterGroups();

		if ( this.menuInitialized ) {
			return;
		}

		this.menuInitialized = true;

		// Count groups per view
		$.each( groups, function ( groupName, groupModel ) {
			if ( !groupModel.isHidden() ) {
				viewGroupCount[ groupModel.getView() ] = viewGroupCount[ groupModel.getView() ] || 0;
				viewGroupCount[ groupModel.getView() ]++;
			}
		} );

		$.each( groups, function ( groupName, groupModel ) {
			var currentItems = [],
				view = groupModel.getView();

			if ( !groupModel.isHidden() ) {
				if ( viewGroupCount[ view ] > 1 ) {
					// Only add a section header if there is more than
					// one group
					currentItems.push(
						// Group section
						new mw.rcfilters.ui.FilterMenuSectionOptionWidget(
							widget.controller,
							groupModel,
							{
								$overlay: widget.$overlay
							}
						)
					);
				}

				// Add items
				widget.model.getGroupFilters( groupName ).forEach( function ( filterItem ) {
					currentItems.push(
						new mw.rcfilters.ui.FilterMenuOptionWidget(
							widget.controller,
							widget.model,
							widget.model.getInvertModel(),
							filterItem,
							{
								$overlay: widget.$overlay
							}
						)
					);
				} );

				// Cache the items per view, so we can switch between them
				// without rebuilding the widgets each time
				widget.views[ view ] = widget.views[ view ] || [];
				widget.views[ view ] = widget.views[ view ].concat( currentItems );
				items = items.concat( currentItems );
			}
		} );

		this.addItems( items );
		this.updateView();
	};

	/**
	 * Respond to model initialize event. Populate the menu from the model
	 */
	mw.rcfilters.ui.MenuSelectWidget.prototype.onModelInitialize = function () {
		this.menuInitialized = false;
	};

	/**
	 * Update view
	 */
	mw.rcfilters.ui.MenuSelectWidget.prototype.updateView = function () {
		var viewName = this.model.getCurrentView();

		if ( this.views[ viewName ] && this.currentView !== viewName ) {
			this.updateFooterVisibility( viewName );

			this.$element
				.data( 'view', viewName )
				.removeClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + this.currentView )
				.addClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + viewName );

			this.currentView = viewName;
			this.scrollToTop();
		}

		this.postProcessItems();
		this.clip();
	};

	/**
	 * Go over the available footers and decide which should be visible
	 * for this view
	 *
	 * @param {string} [currentView] Current view
	 */
	mw.rcfilters.ui.MenuSelectWidget.prototype.updateFooterVisibility = function ( currentView ) {
		currentView = currentView || this.model.getCurrentView();

		this.footers.forEach( function ( data ) {
			data.$element.toggle(
				// This footer should only be shown if it is configured
				// for all views or for this specific view
				!data.views || data.views.length === 0 || data.views.indexOf( currentView ) > -1
			);
		} );
	};

	/**
	 * Post-process items after the visibility changed. Make sure
	 * that we always have an item selected, and that the no-results
	 * widget appears if the menu is empty.
	 */
	mw.rcfilters.ui.MenuSelectWidget.prototype.postProcessItems = function () {
		var i,
			itemWasSelected = false,
			items = this.getItems();

		// If we are not already selecting an item, always make sure
		// that the top item is selected
		if ( !this.userSelecting ) {
			// Select the first item in the list
			for ( i = 0; i < items.length; i++ ) {
				if (
					!( items[ i ] instanceof OO.ui.MenuSectionOptionWidget ) &&
					items[ i ].isVisible()
				) {
					itemWasSelected = true;
					this.selectItem( items[ i ] );
					break;
				}
			}

			if ( !itemWasSelected ) {
				this.selectItem( null );
			}
		}

		this.noResults.toggle( !this.getItems().some( function ( item ) {
			return item.isVisible();
		} ) );
	};

	/**
	 * Get the option widget that matches the model given
	 *
	 * @param {mw.rcfilters.dm.ItemModel} model Item model
	 * @return {mw.rcfilters.ui.ItemMenuOptionWidget} Option widget
	 */
	mw.rcfilters.ui.MenuSelectWidget.prototype.getItemFromModel = function ( model ) {
		this.lazyMenuCreation();
		return this.views[ model.getGroupModel().getView() ].filter( function ( item ) {
			return item.getName() === model.getName();
		} )[ 0 ];
	};

	/**
	 * @inheritdoc
	 */
	mw.rcfilters.ui.MenuSelectWidget.prototype.onKeyDown = function ( e ) {
		var nextItem,
			currentItem = this.findHighlightedItem() || this.findSelectedItem();

		// Call parent
		mw.rcfilters.ui.MenuSelectWidget.parent.prototype.onKeyDown.call( this, e );

		// We want to select the item on arrow movement
		// rather than just highlight it, like the menu
		// does by default
		if ( !this.isDisabled() && this.isVisible() ) {
			switch ( e.keyCode ) {
				case OO.ui.Keys.UP:
				case OO.ui.Keys.LEFT:
					// Get the next item
					nextItem = this.findRelativeSelectableItem( currentItem, -1 );
					break;
				case OO.ui.Keys.DOWN:
				case OO.ui.Keys.RIGHT:
					// Get the next item
					nextItem = this.findRelativeSelectableItem( currentItem, 1 );
					break;
			}

			nextItem = nextItem && nextItem.constructor.static.selectable ?
				nextItem : null;

			// Select the next item
			this.selectItem( nextItem );
		}
	};

	/**
	 * Scroll to the top of the menu
	 */
	mw.rcfilters.ui.MenuSelectWidget.prototype.scrollToTop = function () {
		this.$body.scrollTop( 0 );
	};

	/**
	 * Set whether the user is currently selecting an item.
	 * This is important when the user selects an item that is in between
	 * different views, and makes sure we do not re-select a different
	 * item (like the item on top) when this is happening.
	 *
	 * @param {boolean} isSelecting User is selecting
	 */
	mw.rcfilters.ui.MenuSelectWidget.prototype.setUserSelecting = function ( isSelecting ) {
		this.userSelecting = !!isSelecting;
	};
}( mediaWiki ) );