-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmenu-service.js
More file actions
611 lines (536 loc) · 22.3 KB
/
menu-service.js
File metadata and controls
611 lines (536 loc) · 22.3 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
import Service from '@ember/service';
import Evented from '@ember/object/evented';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { dasherize } from '@ember/string';
import { A, isArray } from '@ember/array';
import MenuItem from '../../contracts/menu-item';
import MenuPanel from '../../contracts/menu-panel';
/**
* MenuManagerService
*
* Manages all menu items and panels in the application.
* Uses RegistryService for storage, providing cross-engine access.
*
* @class MenuService
* @extends Service
*/
export default class MenuService extends Service.extend(Evented) {
@service('universe/registry-service') registry;
@service universe;
/**
* Reference to the root Ember Application Instance.
* Used for registering components/services to the application container
* for cross-engine sharing.
*/
@tracked applicationInstance = null;
/**
* Set the application instance (for consistency with other services)
*
* @method setApplicationInstance
* @param {Application} application The root application instance
*/
setApplicationInstance(application) {
this.applicationInstance = application;
}
/**
* Wrap an onClick handler to automatically pass menuItem and universe as parameters
*
* @private
* @method #wrapOnClickHandler
* @param {Function} onClick The original onClick function
* @param {Object} menuItem The menu item object
* @returns {Function} Wrapped onClick function
*/
#wrapOnClickHandler(onClick, menuItem) {
if (typeof onClick !== 'function') {
return onClick;
}
const universe = this.universe;
return function () {
return onClick(menuItem, universe);
};
}
/**
* Normalize a menu item input to a plain object
*
* @private
* @method #normalizeMenuItem
* @param {MenuItem|String|Object} input MenuItem instance, title, or object
* @param {String} route Optional route
* @param {Object} options Optional options
* @returns {Object} Normalized menu item object
*/
#normalizeMenuItem(input, route = null, options = {}) {
let menuItemObj;
if (input instanceof MenuItem) {
menuItemObj = input.toObject();
} else if (typeof input === 'object' && input !== null && !input.title) {
menuItemObj = input;
} else if (typeof input === 'string') {
const menuItem = new MenuItem(input, route);
// Apply options
Object.keys(options).forEach((key) => {
if (key === 'icon') menuItem.withIcon(options[key]);
else if (key === 'priority') menuItem.withPriority(options[key]);
else if (key === 'component') menuItem.withComponent(options[key]);
else if (key === 'slug') menuItem.withSlug(options[key]);
else if (key === 'section') menuItem.inSection(options[key]);
else if (key === 'index') menuItem.atIndex(options[key]);
else if (key === 'type') menuItem.withType(options[key]);
else if (key === 'wrapperClass') menuItem.withWrapperClass(options[key]);
else if (key === 'queryParams') menuItem.withQueryParams(options[key]);
else if (key === 'onClick') menuItem.onClick(options[key]);
else menuItem.setOption(key, options[key]);
});
menuItemObj = menuItem.toObject();
} else {
menuItemObj = input;
}
// Wrap onClick handler to automatically pass menuItem and universe
if (menuItemObj && typeof menuItemObj.onClick === 'function') {
menuItemObj.onClick = this.#wrapOnClickHandler(menuItemObj.onClick, menuItemObj);
}
return menuItemObj;
}
/**
* Normalize a menu panel input to a plain object
*
* @private
* @method #normalizeMenuPanel
* @param {MenuPanel|String|Object} input MenuPanel instance, title, or object
* @param {Array} items Optional items
* @param {Object} options Optional options
* @returns {Object} Normalized menu panel object
*/
#normalizeMenuPanel(input, items = [], options = {}) {
if (input instanceof MenuPanel) {
return input.toObject();
}
if (typeof input === 'object' && input !== null && !input.title) {
return input;
}
if (typeof input === 'string') {
const panel = new MenuPanel(input, items);
if (options.slug) panel.withSlug(options.slug);
if (options.icon) panel.withIcon(options.icon);
if (options.priority) panel.withPriority(options.priority);
return panel.toObject();
}
return input;
}
// ============================================================================
// Registration Methods
// ============================================================================
/**
* Register a header menu item
*
* @method registerHeaderMenuItem
* @param {MenuItem|String} menuItemOrTitle MenuItem instance or title
* @param {String} route Optional route (if first param is string)
* @param {Object} options Optional options (if first param is string)
*/
registerHeaderMenuItem(itemOrTitle, route = null, options = {}) {
const menuItem = this.#normalizeMenuItem(itemOrTitle, route, options);
this.registry.register('header', 'menu-item', menuItem.slug, menuItem);
// Auto-register each shortcut as a first-class header menu item so that
// they appear in the customiser's "All Extensions" list and can be found
// by id in allItems when pinned to the bar.
if (isArray(menuItem.shortcuts)) {
for (const sc of menuItem.shortcuts) {
const scId = sc.id ?? dasherize(menuItem.id + '-sc-' + sc.title);
const scSlug = sc.slug ?? scId;
// Build a first-class item that supports the full MenuItem
// property surface. Each property falls back to the parent's
// value so shortcuts inherit sensible defaults without the
// consumer having to repeat them.
const scItem = {
// ── Identity ──────────────────────────────────────────────
id: scId,
slug: scSlug,
title: sc.title,
text: sc.text ?? sc.title,
label: sc.label ?? sc.title,
view: sc.view ?? scId,
// ── Routing ───────────────────────────────────────────────
route: sc.route ?? menuItem.route,
section: sc.section ?? null,
queryParams: sc.queryParams ?? {},
routeParams: sc.routeParams ?? [],
// ── Icons (full surface) ──────────────────────────────────
icon: sc.icon ?? menuItem.icon,
iconPrefix: sc.iconPrefix ?? menuItem.iconPrefix,
iconSize: sc.iconSize ?? menuItem.iconSize ?? null,
iconClass: sc.iconClass ?? menuItem.iconClass ?? null,
iconComponent: sc.iconComponent ?? null,
iconComponentOptions: sc.iconComponentOptions ?? {},
// ── Metadata ──────────────────────────────────────────────
description: sc.description ?? null,
// Shortcuts inherit parent tags so they surface under the
// same search terms; shortcut-specific tags take precedence.
tags: isArray(sc.tags) ? sc.tags : isArray(menuItem.tags) ? menuItem.tags : null,
// ── Behaviour ─────────────────────────────────────────────
onClick: sc.onClick ?? null,
disabled: sc.disabled ?? false,
type: sc.type ?? 'default',
buttonType: sc.buttonType ?? null,
// ── Styling ───────────────────────────────────────────────
class: sc.class ?? null,
inlineClass: sc.inlineClass ?? null,
wrapperClass: sc.wrapperClass ?? null,
// ── Internal flags ────────────────────────────────────────
_isShortcut: true,
_parentTitle: menuItem.title,
_parentId: menuItem.id,
priority: (menuItem.priority ?? 0) + 1,
_isMenuItem: true,
};
this.registry.register('header', 'menu-item', scSlug, scItem);
this.trigger('menuItem.registered', scItem, 'header');
}
}
// Trigger event for backward compatibility
this.trigger('menuItem.registered', menuItem, 'header');
}
/**
* Register an admin menu item
*
* @method registerAdminMenuItem
* @param {MenuItem|String} itemOrTitle MenuItem instance or title
* @param {String} route Optional route (if first param is string)
* @param {Object} options Optional options (if first param is string)
*/
registerAdminMenuItem(itemOrTitle, route = null, options = {}) {
const menuItem = this.#normalizeMenuItem(itemOrTitle, route, options);
this.registry.register('console:admin', 'menu-item', menuItem.slug, menuItem);
// Trigger event for backward compatibility
this.trigger('menuItem.registered', menuItem, 'console:admin');
}
/**
* Register an organization menu item
*
* @method registerOrganizationMenuItem
* @param {MenuItem|String} menuItemOrTitle MenuItem instance or title
* @param {Object} options Optional options
*/
registerOrganizationMenuItem(menuItemOrTitle, options = {}) {
const menuItem = this.#normalizeMenuItem(menuItemOrTitle, options.route || 'console.virtual', options);
if (!menuItem.section) {
menuItem.section = 'settings';
}
this.registry.register('console:account', 'menu-item', `organization:${menuItem.slug}`, menuItem);
}
/**
* Register a user menu item
*
* @method registerUserMenuItem
* @param {MenuItem|String} menuItemOrTitle MenuItem instance or title
* @param {Object} options Optional options
*/
registerUserMenuItem(menuItemOrTitle, options = {}) {
const menuItem = this.#normalizeMenuItem(menuItemOrTitle, options.route || 'console.virtual', options);
if (!menuItem.section) {
menuItem.section = 'account';
}
this.registry.register('console:account', 'menu-item', `user:${menuItem.slug}`, menuItem);
}
/**
* Register an admin menu panel
*
* @method registerAdminMenuPanel
* @param {MenuPanel|String} panelOrTitle MenuPanel instance or title
* @param {Array} items Optional items array (if first param is string)
* @param {Object} options Optional options (if first param is string)
*/
registerAdminMenuPanel(panelOrTitle, items = [], options = {}) {
const panel = this.#normalizeMenuPanel(panelOrTitle, items, options);
this.registry.register('console:admin', 'menu-panel', panel.slug, panel);
// The PDF states: "Additionally registering menu panels should also register there items."
// We assume the items are passed in the panel object or items array.
if (panel.items && panel.items.length) {
panel.items = panel.items.map((item) => {
const menuItem = this.#normalizeMenuItem(item);
// CRITICAL: Original behavior for panel items:
// - slug = panel slug (e.g., 'fleet-ops') ← Used in URL
// - view = item slug (e.g., 'navigator-app') ← Used in query param
// - section = null (not used for panel items)
// Result: /admin/fleet-ops?view=navigator-app
const itemSlug = menuItem.slug; // Save the original item slug
menuItem.slug = panel.slug; // Set slug to panel slug for URL
menuItem.view = itemSlug; // Set view to item slug for query param
menuItem.section = null; // Panel items don't use section
// Mark as panel item to prevent duplication in main menu
menuItem._isPanelItem = true;
menuItem._panelSlug = panel.slug;
// Register with the item slug as key (for lookup)
this.registry.register('console:admin', 'menu-item', itemSlug, menuItem);
// Trigger event for backward compatibility
this.trigger('menuItem.registered', menuItem, 'console:admin');
// Return the modified menu item so panel.items gets updated
return menuItem;
});
}
// Trigger event for backward compatibility
this.trigger('menuPanel.registered', panel, 'console:admin');
}
/**
* Register a settings menu item
*
* @method registerSettingsMenuItem
* @param {MenuItem|String} menuItemOrTitle MenuItem instance or title
* @param {Object} options Optional options
*/
registerSettingsMenuItem(menuItemOrTitle, options = {}) {
const menuItem = this.#normalizeMenuItem(menuItemOrTitle, options.route || 'console.settings.virtual', options);
this.registry.register('console:settings', 'menu-item', menuItem.slug, menuItem);
}
/**
* Register a menu item to a custom registry
*
* Supports two patterns:
* 1. Original: registerMenuItem(registryName, title, options)
* 2. New: registerMenuItem(registryName, menuItemInstance)
*
* @method registerMenuItem
* @param {String} registryName Registry name (e.g., 'auth:login', 'engine:fleet-ops')
* @param {String|MenuItem} titleOrMenuItem Menu item title string or MenuItem instance
* @param {Object} options Optional options (only used with title string)
*/
registerMenuItem(registryName, titleOrMenuItem, options = {}) {
let menuItem;
// Normalize the menu item first (handles both MenuItem instances and string titles)
if (titleOrMenuItem instanceof MenuItem) {
menuItem = this.#normalizeMenuItem(titleOrMenuItem);
} else {
// Original pattern: title string + options
const title = titleOrMenuItem;
const route = options.route || `console.${dasherize(registryName)}.virtual`;
// Set defaults matching original behavior
const slug = options.slug || '~';
menuItem = this.#normalizeMenuItem(title, route, {
...options,
slug,
});
}
// Apply finalView normalization consistently for ALL menu items
// If slug === view, set view to null to prevent redundant query params
// This matches the legacy behavior: const finalView = (slug === view) ? null : view;
if (menuItem.slug && menuItem.view && menuItem.slug === menuItem.view) {
menuItem.view = null;
}
// Register the menu item
this.registry.register(registryName, 'menu-item', menuItem.slug || menuItem.title, menuItem);
// Trigger event
this.trigger('menuItem.registered', menuItem, registryName);
}
// ============================================================================
// Getter Methods (Improved DX)
// ============================================================================
/**
* Get menu items from a registry
*
* @method getMenuItems
* @param {String} registryName Registry name (e.g., 'engine:fleet-ops')
* @returns {Array} Menu items
*/
getMenuItems(registryName) {
return this.registry.getRegistry(registryName, 'menu-item');
}
/**
* Get menu panels from a registry
*
* @method getMenuPanels
* @param {String} registryName Registry name (e.g., 'engine:fleet-ops')
* @returns {Array} Menu panels
*/
getMenuPanels(registryName) {
return this.registry.getRegistry(registryName, 'menu-panel');
}
/**
* Lookup a menu item from a registry
*
* @method lookupMenuItem
* @param {String} registryName Registry name
* @param {String} slug Menu item slug
* @param {String} view Optional view
* @param {String} section Optional section
* @returns {Object|null} Menu item or null
*/
lookupMenuItem(registryName, slug, view = null, section = null) {
const items = this.getMenuItems(registryName);
return items.find((item) => {
const slugMatch = item.slug === slug;
const viewMatch = !view || item.view === view;
const sectionMatch = !section || item.section === section;
return slugMatch && viewMatch && sectionMatch;
});
}
/**
* Alias for lookupMenuItem
*
* @method getMenuItem
* @param {String} registryName Registry name
* @param {String} slug Menu item slug
* @param {String} view Optional view
* @param {String} section Optional section
* @returns {Object|null} Menu item or null
*/
getMenuItem(registryName, slug, view = null, section = null) {
return this.lookupMenuItem(registryName, slug, view, section);
}
/**
* Get header menu items
*
* @method getHeaderMenuItems
* @returns {Array} Header menu items sorted by priority
*/
getHeaderMenuItems() {
const items = this.registry.getRegistry('header', 'menu-item');
return A(items).sortBy('priority');
}
/**
* Get organization menu items
*
* @method getOrganizationMenuItems
* @returns {Array} Organization menu items
*/
getOrganizationMenuItems() {
return this.registry.getRegistry('console:account', 'menu-item');
}
/**
* Get user menu items
*
* @method getUserMenuItems
* @returns {Array} User menu items
*/
getUserMenuItems() {
return this.registry.getRegistry('console:account', 'menu-item');
}
/**
* Get admin menu panels
*
* @method getAdminMenuPanels
* @returns {Array} Admin panels sorted by priority
*/
getAdminMenuPanels() {
const panels = this.registry.getRegistry('console:admin', 'menu-panel');
return A(panels).sortBy('priority');
}
/**
* Alias for getAdminMenuPanels
*
* @method getAdminPanels
* @returns {Array} Admin panels
*/
getAdminPanels() {
return this.getAdminMenuPanels();
}
/**
* Get admin menu items
* Excludes items that belong to panels (to prevent duplication)
*
* @method getAdminMenuItems
* @returns {Array} Admin menu items (excluding panel items)
*/
getAdminMenuItems() {
const items = this.registry.getRegistry('console:admin', 'menu-item');
// Filter out panel items to prevent duplication in the UI
return items.filter((item) => !item._isPanelItem);
}
/**
* Get menu items from a specific panel
*
* @method getMenuItemsFromPanel
* @param {String} panelSlug Panel slug
* @returns {Array} Menu items belonging to the panel
*/
getMenuItemsFromPanel(panelSlug) {
const items = this.registry.getRegistry('console:admin', 'menu-item');
return items.filter((item) => item._panelSlug === panelSlug);
}
/**
* Get settings menu items
*
* @method getSettingsMenuItems
* @returns {Array} Settings menu items
*/
getSettingsMenuItems() {
return this.registry.getRegistry('console:settings', 'menu-item');
}
/**
* Get settings menu panels
*
* @method getSettingsMenuPanels
* @returns {Array} Settings menu panels
*/
getSettingsMenuPanels() {
const panels = this.registry.getRegistry('console:settings', 'menu-panel');
return A(panels).sortBy('priority');
}
// ============================================================================
// Computed Getters (for template access)
// ============================================================================
/**
* Get header menu items (computed getter)
*
* @computed headerMenuItems
* @returns {Array} Header menu items
*/
get headerMenuItems() {
return this.getHeaderMenuItems();
}
/**
* Get organization menu items (computed getter)
*
* @computed organizationMenuItems
* @returns {Array} Organization menu items
*/
get organizationMenuItems() {
return this.getOrganizationMenuItems();
}
/**
* Get user menu items (computed getter)
*
* @computed userMenuItems
* @returns {Array} User menu items
*/
get userMenuItems() {
return this.getUserMenuItems();
}
/**
* Get admin menu items (computed getter)
*
* @computed adminMenuItems
* @returns {Array} Admin menu items
*/
get adminMenuItems() {
return this.getAdminMenuItems();
}
/**
* Get admin menu panels (computed getter)
*
* @computed adminMenuPanels
* @returns {Array} Admin menu panels
*/
get adminMenuPanels() {
return this.getAdminMenuPanels();
}
/**
* Get settings menu items (computed getter)
*
* @computed settingsMenuItems
* @returns {Array} Settings menu items
*/
get settingsMenuItems() {
return this.getSettingsMenuItems();
}
/**
* Get settings menu panels (computed getter)
*
* @computed settingsMenuPanels
* @returns {Array} Settings menu panels
*/
get settingsMenuPanels() {
return this.getSettingsMenuPanels();
}
}