Debian Codegraph @

resources/src/mediawiki.rcfilters/ui/MenuSelectWidget.js

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