1 | // Copyright (c) Jupyter Development Team.
|
2 | // Distributed under the terms of the Modified BSD License.
|
3 | import { ArrayExt } from '@lumino/algorithm';
|
4 | import { DisposableDelegate } from '@lumino/disposable';
|
5 | import { Menu } from '@lumino/widgets';
|
6 | /**
|
7 | * Namespace for JupyterLabMenu interfaces
|
8 | */
|
9 | export var IRankedMenu;
|
10 | (function (IRankedMenu) {
|
11 | /**
|
12 | * Default menu item rank
|
13 | */
|
14 | IRankedMenu.DEFAULT_RANK = 100;
|
15 | })(IRankedMenu || (IRankedMenu = {}));
|
16 | /**
|
17 | * An extensible menu for JupyterLab application menus.
|
18 | */
|
19 | export class RankedMenu extends Menu {
|
20 | /**
|
21 | * Construct a new menu.
|
22 | *
|
23 | * @param options - Options for the lumino menu.
|
24 | */
|
25 | constructor(options) {
|
26 | var _a;
|
27 | super(options);
|
28 | this._ranks = [];
|
29 | this._rank = options.rank;
|
30 | this._includeSeparators = (_a = options.includeSeparators) !== null && _a !== void 0 ? _a : true;
|
31 | }
|
32 | /**
|
33 | * The underlying Lumino menu.
|
34 | *
|
35 | * @deprecated since v3.1
|
36 | * RankMenu inherits from Menu since v3.1
|
37 | */
|
38 | get menu() {
|
39 | return this;
|
40 | }
|
41 | /**
|
42 | * Menu rank.
|
43 | */
|
44 | get rank() {
|
45 | return this._rank;
|
46 | }
|
47 | /**
|
48 | * Add a group of menu items specific to a particular
|
49 | * plugin.
|
50 | *
|
51 | * The rank can be set for all items in the group using the
|
52 | * function argument or per item.
|
53 | *
|
54 | * @param items - the list of menu items to add.
|
55 | * @param rank - the default rank in the menu in which to insert the group.
|
56 | * @returns Disposable of the group
|
57 | */
|
58 | addGroup(items, rank) {
|
59 | if (items.length === 0) {
|
60 | return new DisposableDelegate(() => void 0);
|
61 | }
|
62 | const defaultRank = rank !== null && rank !== void 0 ? rank : IRankedMenu.DEFAULT_RANK;
|
63 | const sortedItems = items
|
64 | .map(item => {
|
65 | var _a;
|
66 | return Object.assign(Object.assign({}, item), { rank: (_a = item.rank) !== null && _a !== void 0 ? _a : defaultRank });
|
67 | })
|
68 | .sort((a, b) => a.rank - b.rank);
|
69 | // Insert the plugin group into the menu.
|
70 | let insertIndex = this._ranks.findIndex(rank => sortedItems[0].rank < rank);
|
71 | if (insertIndex < 0) {
|
72 | insertIndex = this._ranks.length; // Insert at the end of the menu
|
73 | }
|
74 | // Keep an array of the menu items that have been created.
|
75 | const added = [];
|
76 | // Insert a separator before the group.
|
77 | // Phosphor takes care of superfluous leading,
|
78 | // trailing, and duplicate separators.
|
79 | if (this._includeSeparators) {
|
80 | added.push(this.insertItem(insertIndex++, { type: 'separator', rank: defaultRank }));
|
81 | }
|
82 | // Insert the group.
|
83 | added.push(...sortedItems.map(item => {
|
84 | return this.insertItem(insertIndex++, item);
|
85 | }));
|
86 | // Insert a separator after the group.
|
87 | if (this._includeSeparators) {
|
88 | added.push(this.insertItem(insertIndex++, { type: 'separator', rank: defaultRank }));
|
89 | }
|
90 | return new DisposableDelegate(() => {
|
91 | added.forEach(i => i.dispose());
|
92 | });
|
93 | }
|
94 | /**
|
95 | * Add a menu item to the end of the menu.
|
96 | *
|
97 | * @param options - The options for creating the menu item.
|
98 | *
|
99 | * @returns The menu item added to the menu.
|
100 | */
|
101 | addItem(options) {
|
102 | let insertIndex = -1;
|
103 | if (options.rank) {
|
104 | insertIndex = this._ranks.findIndex(rank => options.rank < rank);
|
105 | }
|
106 | if (insertIndex < 0) {
|
107 | insertIndex = this._ranks.length; // Insert at the end of the menu
|
108 | }
|
109 | return this.insertItem(insertIndex, options);
|
110 | }
|
111 | /**
|
112 | * Remove all menu items from the menu.
|
113 | */
|
114 | clearItems() {
|
115 | this._ranks.length = 0;
|
116 | super.clearItems();
|
117 | }
|
118 | /**
|
119 | * Dispose of the resources held by the menu.
|
120 | */
|
121 | dispose() {
|
122 | this._ranks.length = 0;
|
123 | super.dispose();
|
124 | }
|
125 | /**
|
126 | * Get the rank of the item at index.
|
127 | *
|
128 | * @param index Item index.
|
129 | * @returns Rank of the item.
|
130 | */
|
131 | getRankAt(index) {
|
132 | return this._ranks[index];
|
133 | }
|
134 | /**
|
135 | * Insert a menu item into the menu at the specified index.
|
136 | *
|
137 | * @param index - The index at which to insert the item.
|
138 | *
|
139 | * @param options - The options for creating the menu item.
|
140 | *
|
141 | * @returns The menu item added to the menu.
|
142 | *
|
143 | * #### Notes
|
144 | * The index will be clamped to the bounds of the items.
|
145 | */
|
146 | insertItem(index, options) {
|
147 | var _a, _b;
|
148 | const clampedIndex = Math.max(0, Math.min(index, this._ranks.length));
|
149 | ArrayExt.insert(this._ranks, clampedIndex, (_a = options.rank) !== null && _a !== void 0 ? _a : Math.max(IRankedMenu.DEFAULT_RANK, (_b = this._ranks[this._ranks.length - 1]) !== null && _b !== void 0 ? _b : IRankedMenu.DEFAULT_RANK));
|
150 | const item = super.insertItem(clampedIndex, options);
|
151 | return new DisposableMenuItem(item, this);
|
152 | }
|
153 | /**
|
154 | * Remove the item at a given index from the menu.
|
155 | *
|
156 | * @param index - The index of the item to remove.
|
157 | *
|
158 | * #### Notes
|
159 | * This is a no-op if the index is out of range.
|
160 | */
|
161 | removeItemAt(index) {
|
162 | ArrayExt.removeAt(this._ranks, index);
|
163 | super.removeItemAt(index);
|
164 | }
|
165 | }
|
166 | /**
|
167 | * Disposable Menu Item
|
168 | */
|
169 | class DisposableMenuItem {
|
170 | /**
|
171 | * Create a disposable menu item from an item and the menu it belongs to
|
172 | *
|
173 | * @param item Menu item
|
174 | * @param menu Menu
|
175 | */
|
176 | constructor(item, menu) {
|
177 | this._item = item;
|
178 | this._menu = menu;
|
179 | // dispose this item if the parent menu is disposed
|
180 | const dispose = (menu) => {
|
181 | menu.disposed.disconnect(dispose);
|
182 | this.dispose();
|
183 | };
|
184 | this._menu.disposed.connect(dispose);
|
185 | }
|
186 | /**
|
187 | * Whether the menu item is disposed or not.
|
188 | */
|
189 | get isDisposed() {
|
190 | return this._isDisposed;
|
191 | }
|
192 | /**
|
193 | * The type of the menu item.
|
194 | */
|
195 | get type() {
|
196 | return this._item.type;
|
197 | }
|
198 | /**
|
199 | * The command to execute when the item is triggered.
|
200 | */
|
201 | get command() {
|
202 | return this._item.command;
|
203 | }
|
204 | /**
|
205 | * The arguments for the command.
|
206 | */
|
207 | get args() {
|
208 | return this._item.args;
|
209 | }
|
210 | /**
|
211 | * The submenu for a `'submenu'` type item.
|
212 | */
|
213 | get submenu() {
|
214 | return this._item.submenu;
|
215 | }
|
216 | /**
|
217 | * The display label for the menu item.
|
218 | */
|
219 | get label() {
|
220 | return this._item.label;
|
221 | }
|
222 | /**
|
223 | * The mnemonic index for the menu item.
|
224 | */
|
225 | get mnemonic() {
|
226 | return this._item.mnemonic;
|
227 | }
|
228 | /**
|
229 | * The icon renderer for the menu item.
|
230 | */
|
231 | get icon() {
|
232 | return this._item.icon;
|
233 | }
|
234 | /**
|
235 | * The icon class for the menu item.
|
236 | */
|
237 | get iconClass() {
|
238 | return this._item.iconClass;
|
239 | }
|
240 | /**
|
241 | * The icon label for the menu item.
|
242 | */
|
243 | get iconLabel() {
|
244 | return this._item.iconLabel;
|
245 | }
|
246 | /**
|
247 | * The display caption for the menu item.
|
248 | */
|
249 | get caption() {
|
250 | return this._item.caption;
|
251 | }
|
252 | /**
|
253 | * The extra class name for the menu item.
|
254 | */
|
255 | get className() {
|
256 | return this._item.className;
|
257 | }
|
258 | /**
|
259 | * The dataset for the menu item.
|
260 | */
|
261 | get dataset() {
|
262 | return this._item.dataset;
|
263 | }
|
264 | /**
|
265 | * Whether the menu item is enabled.
|
266 | */
|
267 | get isEnabled() {
|
268 | return this._item.isEnabled;
|
269 | }
|
270 | /**
|
271 | * Whether the menu item is toggled.
|
272 | */
|
273 | get isToggled() {
|
274 | return this._item.isToggled;
|
275 | }
|
276 | /**
|
277 | * Whether the menu item is visible.
|
278 | */
|
279 | get isVisible() {
|
280 | return this.isVisible;
|
281 | }
|
282 | /**
|
283 | * The key binding for the menu item.
|
284 | */
|
285 | get keyBinding() {
|
286 | return this._item.keyBinding;
|
287 | }
|
288 | /**
|
289 | * Dispose the menu item by removing it from its menu.
|
290 | */
|
291 | dispose() {
|
292 | this._isDisposed = true;
|
293 | if (this._menu.isDisposed) {
|
294 | // Bail early
|
295 | return;
|
296 | }
|
297 | this._menu.removeItem(this._item);
|
298 | }
|
299 | }
|
300 | //# sourceMappingURL=menu.js.map |
\ | No newline at end of file |