1 |
|
2 |
|
3 | import { IRankedMenu, MenuSvg, RankedMenu } from '@jupyterlab/ui-components';
|
4 | import { ArrayExt } from '@lumino/algorithm';
|
5 | import { MenuBar } from '@lumino/widgets';
|
6 | import { EditMenu } from './edit';
|
7 | import { FileMenu } from './file';
|
8 | import { HelpMenu } from './help';
|
9 | import { KernelMenu } from './kernel';
|
10 | import { RunMenu } from './run';
|
11 | import { SettingsMenu } from './settings';
|
12 | import { TabsMenu } from './tabs';
|
13 | import { ViewMenu } from './view';
|
14 |
|
15 |
|
16 |
|
17 | export class MainMenu extends MenuBar {
|
18 | |
19 |
|
20 |
|
21 | constructor(commands) {
|
22 | let options = { forceItemsPosition: { forceX: false, forceY: true } };
|
23 | super(options);
|
24 | this._items = [];
|
25 | this._commands = commands;
|
26 | }
|
27 | |
28 |
|
29 |
|
30 | get editMenu() {
|
31 | if (!this._editMenu) {
|
32 | this._editMenu = new EditMenu({
|
33 | commands: this._commands,
|
34 | rank: 2,
|
35 | renderer: MenuSvg.defaultRenderer
|
36 | });
|
37 | }
|
38 | return this._editMenu;
|
39 | }
|
40 | |
41 |
|
42 |
|
43 | get fileMenu() {
|
44 | if (!this._fileMenu) {
|
45 | this._fileMenu = new FileMenu({
|
46 | commands: this._commands,
|
47 | rank: 1,
|
48 | renderer: MenuSvg.defaultRenderer
|
49 | });
|
50 | }
|
51 | return this._fileMenu;
|
52 | }
|
53 | |
54 |
|
55 |
|
56 | get helpMenu() {
|
57 | if (!this._helpMenu) {
|
58 | this._helpMenu = new HelpMenu({
|
59 | commands: this._commands,
|
60 | rank: 1000,
|
61 | renderer: MenuSvg.defaultRenderer
|
62 | });
|
63 | }
|
64 | return this._helpMenu;
|
65 | }
|
66 | |
67 |
|
68 |
|
69 | get kernelMenu() {
|
70 | if (!this._kernelMenu) {
|
71 | this._kernelMenu = new KernelMenu({
|
72 | commands: this._commands,
|
73 | rank: 5,
|
74 | renderer: MenuSvg.defaultRenderer
|
75 | });
|
76 | }
|
77 | return this._kernelMenu;
|
78 | }
|
79 | |
80 |
|
81 |
|
82 | get runMenu() {
|
83 | if (!this._runMenu) {
|
84 | this._runMenu = new RunMenu({
|
85 | commands: this._commands,
|
86 | rank: 4,
|
87 | renderer: MenuSvg.defaultRenderer
|
88 | });
|
89 | }
|
90 | return this._runMenu;
|
91 | }
|
92 | |
93 |
|
94 |
|
95 | get settingsMenu() {
|
96 | if (!this._settingsMenu) {
|
97 | this._settingsMenu = new SettingsMenu({
|
98 | commands: this._commands,
|
99 | rank: 999,
|
100 | renderer: MenuSvg.defaultRenderer
|
101 | });
|
102 | }
|
103 | return this._settingsMenu;
|
104 | }
|
105 | |
106 |
|
107 |
|
108 | get viewMenu() {
|
109 | if (!this._viewMenu) {
|
110 | this._viewMenu = new ViewMenu({
|
111 | commands: this._commands,
|
112 | rank: 3,
|
113 | renderer: MenuSvg.defaultRenderer
|
114 | });
|
115 | }
|
116 | return this._viewMenu;
|
117 | }
|
118 | |
119 |
|
120 |
|
121 | get tabsMenu() {
|
122 | if (!this._tabsMenu) {
|
123 | this._tabsMenu = new TabsMenu({
|
124 | commands: this._commands,
|
125 | rank: 500,
|
126 | renderer: MenuSvg.defaultRenderer
|
127 | });
|
128 | }
|
129 | return this._tabsMenu;
|
130 | }
|
131 | |
132 |
|
133 |
|
134 | addMenu(menu, update = true, options = {}) {
|
135 | if (ArrayExt.firstIndexOf(this.menus, menu) > -1) {
|
136 | return;
|
137 | }
|
138 |
|
139 | MenuSvg.overrideDefaultRenderer(menu);
|
140 | const rank = 'rank' in options
|
141 | ? options.rank
|
142 | : 'rank' in menu
|
143 | ? menu.rank
|
144 | : IRankedMenu.DEFAULT_RANK;
|
145 | const rankItem = { menu, rank };
|
146 | const index = ArrayExt.upperBound(this._items, rankItem, Private.itemCmp);
|
147 |
|
148 | menu.disposed.connect(this._onMenuDisposed, this);
|
149 | ArrayExt.insert(this._items, index, rankItem);
|
150 | |
151 |
|
152 |
|
153 | this.insertMenu(index, menu);
|
154 |
|
155 | switch (menu.id) {
|
156 | case 'jp-mainmenu-file':
|
157 | if (!this._fileMenu && menu instanceof FileMenu) {
|
158 | this._fileMenu = menu;
|
159 | }
|
160 | break;
|
161 | case 'jp-mainmenu-edit':
|
162 | if (!this._editMenu && menu instanceof EditMenu) {
|
163 | this._editMenu = menu;
|
164 | }
|
165 | break;
|
166 | case 'jp-mainmenu-view':
|
167 | if (!this._viewMenu && menu instanceof ViewMenu) {
|
168 | this._viewMenu = menu;
|
169 | }
|
170 | break;
|
171 | case 'jp-mainmenu-run':
|
172 | if (!this._runMenu && menu instanceof RunMenu) {
|
173 | this._runMenu = menu;
|
174 | }
|
175 | break;
|
176 | case 'jp-mainmenu-kernel':
|
177 | if (!this._kernelMenu && menu instanceof KernelMenu) {
|
178 | this._kernelMenu = menu;
|
179 | }
|
180 | break;
|
181 | case 'jp-mainmenu-tabs':
|
182 | if (!this._tabsMenu && menu instanceof TabsMenu) {
|
183 | this._tabsMenu = menu;
|
184 | }
|
185 | break;
|
186 | case 'jp-mainmenu-settings':
|
187 | if (!this._settingsMenu && menu instanceof SettingsMenu) {
|
188 | this._settingsMenu = menu;
|
189 | }
|
190 | break;
|
191 | case 'jp-mainmenu-help':
|
192 | if (!this._helpMenu && menu instanceof HelpMenu) {
|
193 | this._helpMenu = menu;
|
194 | }
|
195 | break;
|
196 | }
|
197 | }
|
198 | |
199 |
|
200 |
|
201 | dispose() {
|
202 | var _a, _b, _c, _d, _e, _f, _g, _h;
|
203 | (_a = this._editMenu) === null || _a === void 0 ? void 0 : _a.dispose();
|
204 | (_b = this._fileMenu) === null || _b === void 0 ? void 0 : _b.dispose();
|
205 | (_c = this._helpMenu) === null || _c === void 0 ? void 0 : _c.dispose();
|
206 | (_d = this._kernelMenu) === null || _d === void 0 ? void 0 : _d.dispose();
|
207 | (_e = this._runMenu) === null || _e === void 0 ? void 0 : _e.dispose();
|
208 | (_f = this._settingsMenu) === null || _f === void 0 ? void 0 : _f.dispose();
|
209 | (_g = this._viewMenu) === null || _g === void 0 ? void 0 : _g.dispose();
|
210 | (_h = this._tabsMenu) === null || _h === void 0 ? void 0 : _h.dispose();
|
211 | super.dispose();
|
212 | }
|
213 | |
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | static generateMenu(commands, options, trans) {
|
221 | let menu;
|
222 | const { id, label, rank } = options;
|
223 | switch (id) {
|
224 | case 'jp-mainmenu-file':
|
225 | menu = new FileMenu({
|
226 | commands,
|
227 | rank,
|
228 | renderer: MenuSvg.defaultRenderer
|
229 | });
|
230 | break;
|
231 | case 'jp-mainmenu-edit':
|
232 | menu = new EditMenu({
|
233 | commands,
|
234 | rank,
|
235 | renderer: MenuSvg.defaultRenderer
|
236 | });
|
237 | break;
|
238 | case 'jp-mainmenu-view':
|
239 | menu = new ViewMenu({
|
240 | commands,
|
241 | rank,
|
242 | renderer: MenuSvg.defaultRenderer
|
243 | });
|
244 | break;
|
245 | case 'jp-mainmenu-run':
|
246 | menu = new RunMenu({
|
247 | commands,
|
248 | rank,
|
249 | renderer: MenuSvg.defaultRenderer
|
250 | });
|
251 | break;
|
252 | case 'jp-mainmenu-kernel':
|
253 | menu = new KernelMenu({
|
254 | commands,
|
255 | rank,
|
256 | renderer: MenuSvg.defaultRenderer
|
257 | });
|
258 | break;
|
259 | case 'jp-mainmenu-tabs':
|
260 | menu = new TabsMenu({
|
261 | commands,
|
262 | rank,
|
263 | renderer: MenuSvg.defaultRenderer
|
264 | });
|
265 | break;
|
266 | case 'jp-mainmenu-settings':
|
267 | menu = new SettingsMenu({
|
268 | commands,
|
269 | rank,
|
270 | renderer: MenuSvg.defaultRenderer
|
271 | });
|
272 | break;
|
273 | case 'jp-mainmenu-help':
|
274 | menu = new HelpMenu({
|
275 | commands,
|
276 | rank,
|
277 | renderer: MenuSvg.defaultRenderer
|
278 | });
|
279 | break;
|
280 | default:
|
281 | menu = new RankedMenu({
|
282 | commands,
|
283 | rank,
|
284 | renderer: MenuSvg.defaultRenderer
|
285 | });
|
286 | }
|
287 | if (label) {
|
288 | menu.title.label = trans._p('menu', label);
|
289 | }
|
290 | return menu;
|
291 | }
|
292 | |
293 |
|
294 |
|
295 | _onMenuDisposed(menu) {
|
296 | this.removeMenu(menu);
|
297 | const index = ArrayExt.findFirstIndex(this._items, item => item.menu === menu);
|
298 | if (index !== -1) {
|
299 | ArrayExt.removeAt(this._items, index);
|
300 | }
|
301 | }
|
302 | }
|
303 |
|
304 |
|
305 |
|
306 | var Private;
|
307 | (function (Private) {
|
308 | |
309 |
|
310 |
|
311 | function itemCmp(first, second) {
|
312 | return first.rank - second.rank;
|
313 | }
|
314 | Private.itemCmp = itemCmp;
|
315 | })(Private || (Private = {}));
|
316 |
|
\ | No newline at end of file |