1 |
|
2 |
|
3 |
|
4 |
|
5 | import { Text } from '@jupyterlab/coreutils';
|
6 | import { LabIcon } from '@jupyterlab/ui-components';
|
7 | import { JSONExt } from '@lumino/coreutils';
|
8 |
|
9 |
|
10 |
|
11 | export var MenuFactory;
|
12 | (function (MenuFactory) {
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
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 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function dataToMenu(item, menuFactory) {
|
36 | var _a, _b;
|
37 | const menu = menuFactory(item);
|
38 | menu.id = item.id;
|
39 |
|
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 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | function addContextItem(item, menu, menuFactory) {
|
62 | const { submenu, ...newItem } = item;
|
63 |
|
64 | menu.addItem({
|
65 | ...newItem,
|
66 | submenu: submenu ? dataToMenu(submenu, menuFactory) : null
|
67 | });
|
68 | }
|
69 | MenuFactory.addContextItem = addContextItem;
|
70 | |
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | function addItem(item, menu, menuFactory) {
|
78 | const { submenu, ...newItem } = item;
|
79 |
|
80 | menu.addItem({
|
81 | ...newItem,
|
82 | submenu: submenu ? dataToMenu(submenu, menuFactory) : null
|
83 | });
|
84 | }
|
85 | |
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
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 |
|
\ | No newline at end of file |