UNPKG

6 kBJavaScriptView Raw
1/*
2 * Copyright (c) Jupyter Development Team.
3 * Distributed under the terms of the Modified BSD License.
4 */
5import { Text } from '@jupyterlab/coreutils';
6import { LabIcon } from '@jupyterlab/ui-components';
7import { JSONExt } from '@lumino/coreutils';
8/**
9 * Helper functions to build a menu from the settings
10 */
11export var MenuFactory;
12(function (MenuFactory) {
13 /**
14 * Create menus from their description
15 *
16 * @param data Menubar description
17 * @param menuFactory Factory for empty menu
18 */
19 function createMenus(data, menuFactory) {
20 return data
21 .filter(item => !item.disabled)
22 .sort((a, b) => { var _a, _b; return ((_a = a.rank) !== null && _a !== void 0 ? _a : Infinity) - ((_b = b.rank) !== null && _b !== void 0 ? _b : Infinity); })
23 .map(menuItem => {
24 return dataToMenu(menuItem, menuFactory);
25 });
26 }
27 MenuFactory.createMenus = createMenus;
28 /**
29 * Convert a menu description in a JupyterLabMenu object
30 *
31 * @param item Menu description
32 * @param menuFactory Empty menu factory
33 * @returns The menu widget
34 */
35 function dataToMenu(item, menuFactory) {
36 var _a, _b;
37 const menu = menuFactory(item);
38 menu.id = item.id;
39 // Set the label in case the menu factory did not.
40 if (!menu.title.label) {
41 menu.title.label = (_a = item.label) !== null && _a !== void 0 ? _a : Text.titleCase(menu.id.trim());
42 }
43 if (item.icon) {
44 menu.title.icon = LabIcon.resolve({ icon: item.icon });
45 }
46 if (item.mnemonic !== undefined) {
47 menu.title.mnemonic = item.mnemonic;
48 }
49 (_b = item.items) === null || _b === void 0 ? void 0 : _b.filter(item => !item.disabled).sort((a, b) => { var _a, _b; return ((_a = a.rank) !== null && _a !== void 0 ? _a : Infinity) - ((_b = b.rank) !== null && _b !== void 0 ? _b : Infinity); }).map(item => {
50 addItem(item, menu, menuFactory);
51 });
52 return menu;
53 }
54 /**
55 * Convert an item description in a context menu item object
56 *
57 * @param item Context menu item
58 * @param menu Context menu to populate
59 * @param menuFactory Empty menu factory
60 */
61 function addContextItem(item, menu, menuFactory) {
62 const { submenu, ...newItem } = item;
63 // Commands may not have been registered yet; so we don't force it to exist
64 menu.addItem({
65 ...newItem,
66 submenu: submenu ? dataToMenu(submenu, menuFactory) : null
67 });
68 }
69 MenuFactory.addContextItem = addContextItem;
70 /**
71 * Convert an item description in a menu item object
72 *
73 * @param item Menu item
74 * @param menu Menu to populate
75 * @param menuFactory Empty menu factory
76 */
77 function addItem(item, menu, menuFactory) {
78 const { submenu, ...newItem } = item;
79 // Commands may not have been registered yet; so we don't force it to exist
80 menu.addItem({
81 ...newItem,
82 submenu: submenu ? dataToMenu(submenu, menuFactory) : null
83 });
84 }
85 /**
86 * Update an existing list of menu and returns
87 * the new elements.
88 *
89 * #### Note
90 * New elements are added to the current menu list.
91 *
92 * @param menus Current menus
93 * @param data New description to take into account
94 * @param menuFactory Empty menu factory
95 * @returns Newly created menus
96 */
97 function updateMenus(menus, data, menuFactory) {
98 const newMenus = [];
99 data.forEach(item => {
100 const menu = menus.find(menu => menu.id === item.id);
101 if (menu) {
102 mergeMenus(item, menu, menuFactory);
103 }
104 else {
105 if (!item.disabled) {
106 newMenus.push(dataToMenu(item, menuFactory));
107 }
108 }
109 });
110 menus.push(...newMenus);
111 return newMenus;
112 }
113 MenuFactory.updateMenus = updateMenus;
114 function mergeMenus(item, menu, menuFactory) {
115 var _a;
116 if (item.disabled) {
117 menu.dispose();
118 }
119 else {
120 (_a = item.items) === null || _a === void 0 ? void 0 : _a.forEach(entry => {
121 var _a, _b;
122 const existingItem = menu === null || menu === void 0 ? void 0 : menu.items.find((i, idx) => {
123 var _a, _b, _c;
124 return i.type === entry.type &&
125 i.command === ((_a = entry.command) !== null && _a !== void 0 ? _a : '') &&
126 ((_b = i.submenu) === null || _b === void 0 ? void 0 : _b.id) === ((_c = entry.submenu) === null || _c === void 0 ? void 0 : _c.id);
127 });
128 if (existingItem && entry.type !== 'separator') {
129 if (entry.disabled) {
130 menu.removeItem(existingItem);
131 }
132 else {
133 switch ((_a = entry.type) !== null && _a !== void 0 ? _a : 'command') {
134 case 'command':
135 if (entry.command) {
136 if (!JSONExt.deepEqual(existingItem.args, (_b = entry.args) !== null && _b !== void 0 ? _b : {})) {
137 addItem(entry, menu, menuFactory);
138 }
139 }
140 break;
141 case 'submenu':
142 if (entry.submenu) {
143 mergeMenus(entry.submenu, existingItem.submenu, menuFactory);
144 }
145 }
146 }
147 }
148 else {
149 addItem(entry, menu, menuFactory);
150 }
151 });
152 }
153 }
154})(MenuFactory || (MenuFactory = {}));
155//# sourceMappingURL=menufactory.js.map
\No newline at end of file