1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | import { ArrayExt } from '@lumino/algorithm';
|
11 |
|
12 | import { CommandRegistry } from '@lumino/commands';
|
13 |
|
14 | import { JSONExt, ReadonlyJSONObject } from '@lumino/coreutils';
|
15 |
|
16 | import { ElementExt } from '@lumino/domutils';
|
17 |
|
18 | import { getKeyboardLayout } from '@lumino/keyboard';
|
19 |
|
20 | import { Message, MessageLoop } from '@lumino/messaging';
|
21 |
|
22 | import { ISignal, Signal } from '@lumino/signaling';
|
23 |
|
24 | import {
|
25 | ARIAAttrNames,
|
26 | ElementARIAAttrs,
|
27 | ElementDataset,
|
28 | h,
|
29 | VirtualDOM,
|
30 | VirtualElement
|
31 | } from '@lumino/virtualdom';
|
32 |
|
33 | import { Widget } from './widget';
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | export class Menu extends Widget {
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 | constructor(options: Menu.IOptions) {
|
45 | super({ node: Private.createNode() });
|
46 | this.addClass('lm-Menu');
|
47 |
|
48 | this.addClass('p-Menu');
|
49 |
|
50 | this.setFlag(Widget.Flag.DisallowLayout);
|
51 | this.commands = options.commands;
|
52 | this.renderer = options.renderer || Menu.defaultRenderer;
|
53 | }
|
54 |
|
55 | |
56 |
|
57 |
|
58 | dispose(): void {
|
59 | this.close();
|
60 | this._items.length = 0;
|
61 | super.dispose();
|
62 | }
|
63 |
|
64 | |
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | get aboutToClose(): ISignal<this, void> {
|
75 | return this._aboutToClose;
|
76 | }
|
77 |
|
78 | |
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | get menuRequested(): ISignal<this, 'next' | 'previous'> {
|
91 | return this._menuRequested;
|
92 | }
|
93 |
|
94 | |
95 |
|
96 |
|
97 | readonly commands: CommandRegistry;
|
98 |
|
99 | |
100 |
|
101 |
|
102 | readonly renderer: Menu.IRenderer;
|
103 |
|
104 | |
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | get parentMenu(): Menu | null {
|
111 | return this._parentMenu;
|
112 | }
|
113 |
|
114 | |
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | get childMenu(): Menu | null {
|
121 | return this._childMenu;
|
122 | }
|
123 |
|
124 | |
125 |
|
126 |
|
127 | get rootMenu(): Menu {
|
128 |
|
129 | let menu: Menu = this;
|
130 | while (menu._parentMenu) {
|
131 | menu = menu._parentMenu;
|
132 | }
|
133 | return menu;
|
134 | }
|
135 |
|
136 | |
137 |
|
138 |
|
139 | get leafMenu(): Menu {
|
140 |
|
141 | let menu: Menu = this;
|
142 | while (menu._childMenu) {
|
143 | menu = menu._childMenu;
|
144 | }
|
145 | return menu;
|
146 | }
|
147 |
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | get contentNode(): HTMLUListElement {
|
157 | return this.node.getElementsByClassName(
|
158 | 'lm-Menu-content'
|
159 | )[0] as HTMLUListElement;
|
160 | }
|
161 |
|
162 | |
163 |
|
164 |
|
165 | get activeItem(): Menu.IItem | null {
|
166 | return this._items[this._activeIndex] || null;
|
167 | }
|
168 |
|
169 | |
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | set activeItem(value: Menu.IItem | null) {
|
176 | this.activeIndex = value ? this._items.indexOf(value) : -1;
|
177 | }
|
178 |
|
179 | |
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | get activeIndex(): number {
|
186 | return this._activeIndex;
|
187 | }
|
188 |
|
189 | |
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | set activeIndex(value: number) {
|
196 |
|
197 | if (value < 0 || value >= this._items.length) {
|
198 | value = -1;
|
199 | }
|
200 |
|
201 |
|
202 | if (value !== -1 && !Private.canActivate(this._items[value])) {
|
203 | value = -1;
|
204 | }
|
205 |
|
206 |
|
207 | if (this._activeIndex === value) {
|
208 | return;
|
209 | }
|
210 |
|
211 |
|
212 | this._activeIndex = value;
|
213 |
|
214 |
|
215 | if (
|
216 | this._activeIndex >= 0 &&
|
217 | this.contentNode.childNodes[this._activeIndex]
|
218 | ) {
|
219 | (this.contentNode.childNodes[this._activeIndex] as HTMLElement).focus();
|
220 | }
|
221 |
|
222 |
|
223 | this.update();
|
224 | }
|
225 |
|
226 | |
227 |
|
228 |
|
229 | get items(): ReadonlyArray<Menu.IItem> {
|
230 | return this._items;
|
231 | }
|
232 |
|
233 | |
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 | activateNextItem(): void {
|
240 | let n = this._items.length;
|
241 | let ai = this._activeIndex;
|
242 | let start = ai < n - 1 ? ai + 1 : 0;
|
243 | let stop = start === 0 ? n - 1 : start - 1;
|
244 | this.activeIndex = ArrayExt.findFirstIndex(
|
245 | this._items,
|
246 | Private.canActivate,
|
247 | start,
|
248 | stop
|
249 | );
|
250 | }
|
251 |
|
252 | |
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | activatePreviousItem(): void {
|
259 | let n = this._items.length;
|
260 | let ai = this._activeIndex;
|
261 | let start = ai <= 0 ? n - 1 : ai - 1;
|
262 | let stop = start === n - 1 ? 0 : start + 1;
|
263 | this.activeIndex = ArrayExt.findLastIndex(
|
264 | this._items,
|
265 | Private.canActivate,
|
266 | start,
|
267 | stop
|
268 | );
|
269 | }
|
270 |
|
271 | |
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 | triggerActiveItem(): void {
|
285 |
|
286 | if (!this.isAttached) {
|
287 | return;
|
288 | }
|
289 |
|
290 |
|
291 | let item = this.activeItem;
|
292 | if (!item) {
|
293 | return;
|
294 | }
|
295 |
|
296 |
|
297 | this._cancelOpenTimer();
|
298 | this._cancelCloseTimer();
|
299 |
|
300 |
|
301 | if (item.type === 'submenu') {
|
302 | this._openChildMenu(true);
|
303 | return;
|
304 | }
|
305 |
|
306 |
|
307 | this.rootMenu.close();
|
308 |
|
309 |
|
310 | let { command, args } = item;
|
311 | if (this.commands.isEnabled(command, args)) {
|
312 | this.commands.execute(command, args);
|
313 | } else {
|
314 | console.log(`Command '${command}' is disabled.`);
|
315 | }
|
316 | }
|
317 |
|
318 | |
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 | addItem(options: Menu.IItemOptions): Menu.IItem {
|
326 | return this.insertItem(this._items.length, options);
|
327 | }
|
328 |
|
329 | |
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 | insertItem(index: number, options: Menu.IItemOptions): Menu.IItem {
|
342 |
|
343 | if (this.isAttached) {
|
344 | this.close();
|
345 | }
|
346 |
|
347 |
|
348 | this.activeIndex = -1;
|
349 |
|
350 |
|
351 | let i = Math.max(0, Math.min(index, this._items.length));
|
352 |
|
353 |
|
354 | let item = Private.createItem(this, options);
|
355 |
|
356 |
|
357 | ArrayExt.insert(this._items, i, item);
|
358 |
|
359 |
|
360 | this.update();
|
361 |
|
362 |
|
363 | return item;
|
364 | }
|
365 |
|
366 | |
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 | removeItem(item: Menu.IItem): void {
|
375 | this.removeItemAt(this._items.indexOf(item));
|
376 | }
|
377 |
|
378 | |
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 | removeItemAt(index: number): void {
|
387 |
|
388 | if (this.isAttached) {
|
389 | this.close();
|
390 | }
|
391 |
|
392 |
|
393 | this.activeIndex = -1;
|
394 |
|
395 |
|
396 | let item = ArrayExt.removeAt(this._items, index);
|
397 |
|
398 |
|
399 | if (!item) {
|
400 | return;
|
401 | }
|
402 |
|
403 |
|
404 | this.update();
|
405 | }
|
406 |
|
407 | |
408 |
|
409 |
|
410 | clearItems(): void {
|
411 |
|
412 | if (this.isAttached) {
|
413 | this.close();
|
414 | }
|
415 |
|
416 |
|
417 | this.activeIndex = -1;
|
418 |
|
419 |
|
420 | if (this._items.length === 0) {
|
421 | return;
|
422 | }
|
423 |
|
424 |
|
425 | this._items.length = 0;
|
426 |
|
427 |
|
428 | this.update();
|
429 | }
|
430 |
|
431 | |
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 | open(x: number, y: number, options: Menu.IOpenOptions = {}): void {
|
448 |
|
449 | if (this.isAttached) {
|
450 | return;
|
451 | }
|
452 |
|
453 |
|
454 | let forceX = options.forceX || false;
|
455 | let forceY = options.forceY || false;
|
456 |
|
457 |
|
458 | Private.openRootMenu(this, x, y, forceX, forceY);
|
459 |
|
460 |
|
461 | this.activate();
|
462 | }
|
463 |
|
464 | |
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 | handleEvent(event: Event): void {
|
475 | switch (event.type) {
|
476 | case 'keydown':
|
477 | this._evtKeyDown(event as KeyboardEvent);
|
478 | break;
|
479 | case 'mouseup':
|
480 | this._evtMouseUp(event as MouseEvent);
|
481 | break;
|
482 | case 'mousemove':
|
483 | this._evtMouseMove(event as MouseEvent);
|
484 | break;
|
485 | case 'mouseenter':
|
486 | this._evtMouseEnter(event as MouseEvent);
|
487 | break;
|
488 | case 'mouseleave':
|
489 | this._evtMouseLeave(event as MouseEvent);
|
490 | break;
|
491 | case 'mousedown':
|
492 | this._evtMouseDown(event as MouseEvent);
|
493 | break;
|
494 | case 'contextmenu':
|
495 | event.preventDefault();
|
496 | event.stopPropagation();
|
497 | break;
|
498 | }
|
499 | }
|
500 |
|
501 | |
502 |
|
503 |
|
504 | protected onBeforeAttach(msg: Message): void {
|
505 | this.node.addEventListener('keydown', this);
|
506 | this.node.addEventListener('mouseup', this);
|
507 | this.node.addEventListener('mousemove', this);
|
508 | this.node.addEventListener('mouseenter', this);
|
509 | this.node.addEventListener('mouseleave', this);
|
510 | this.node.addEventListener('contextmenu', this);
|
511 | document.addEventListener('mousedown', this, true);
|
512 | }
|
513 |
|
514 | |
515 |
|
516 |
|
517 | protected onAfterDetach(msg: Message): void {
|
518 | this.node.removeEventListener('keydown', this);
|
519 | this.node.removeEventListener('mouseup', this);
|
520 | this.node.removeEventListener('mousemove', this);
|
521 | this.node.removeEventListener('mouseenter', this);
|
522 | this.node.removeEventListener('mouseleave', this);
|
523 | this.node.removeEventListener('contextmenu', this);
|
524 | document.removeEventListener('mousedown', this, true);
|
525 | }
|
526 |
|
527 | |
528 |
|
529 |
|
530 | protected onActivateRequest(msg: Message): void {
|
531 | if (this.isAttached) {
|
532 | this.node.focus();
|
533 | }
|
534 | }
|
535 |
|
536 | |
537 |
|
538 |
|
539 | protected onUpdateRequest(msg: Message): void {
|
540 | let items = this._items;
|
541 | let renderer = this.renderer;
|
542 | let activeIndex = this._activeIndex;
|
543 | let collapsedFlags = Private.computeCollapsed(items);
|
544 | let content = new Array<VirtualElement>(items.length);
|
545 | for (let i = 0, n = items.length; i < n; ++i) {
|
546 | let item = items[i];
|
547 | let active = i === activeIndex;
|
548 | let collapsed = collapsedFlags[i];
|
549 | content[i] = renderer.renderItem({
|
550 | item,
|
551 | active,
|
552 | collapsed,
|
553 | onfocus: () => {
|
554 | this.activeIndex = i;
|
555 | }
|
556 | });
|
557 | }
|
558 | VirtualDOM.render(content, this.contentNode);
|
559 | }
|
560 |
|
561 | |
562 |
|
563 |
|
564 | protected onCloseRequest(msg: Message): void {
|
565 |
|
566 | this._cancelOpenTimer();
|
567 | this._cancelCloseTimer();
|
568 |
|
569 |
|
570 | this.activeIndex = -1;
|
571 |
|
572 |
|
573 | let childMenu = this._childMenu;
|
574 | if (childMenu) {
|
575 | this._childIndex = -1;
|
576 | this._childMenu = null;
|
577 | childMenu._parentMenu = null;
|
578 | childMenu.close();
|
579 | }
|
580 |
|
581 |
|
582 | let parentMenu = this._parentMenu;
|
583 | if (parentMenu) {
|
584 | this._parentMenu = null;
|
585 | parentMenu._childIndex = -1;
|
586 | parentMenu._childMenu = null;
|
587 | parentMenu.activate();
|
588 | }
|
589 |
|
590 |
|
591 | if (this.isAttached) {
|
592 | this._aboutToClose.emit(undefined);
|
593 | }
|
594 |
|
595 |
|
596 | super.onCloseRequest(msg);
|
597 | }
|
598 |
|
599 | |
600 |
|
601 |
|
602 |
|
603 |
|
604 |
|
605 | private _evtKeyDown(event: KeyboardEvent): void {
|
606 |
|
607 | event.preventDefault();
|
608 | event.stopPropagation();
|
609 |
|
610 |
|
611 | let kc = event.keyCode;
|
612 |
|
613 |
|
614 | if (kc === 13) {
|
615 | this.triggerActiveItem();
|
616 | return;
|
617 | }
|
618 |
|
619 |
|
620 | if (kc === 27) {
|
621 | this.close();
|
622 | return;
|
623 | }
|
624 |
|
625 |
|
626 | if (kc === 37) {
|
627 | if (this._parentMenu) {
|
628 | this.close();
|
629 | } else {
|
630 | this._menuRequested.emit('previous');
|
631 | }
|
632 | return;
|
633 | }
|
634 |
|
635 |
|
636 | if (kc === 38) {
|
637 | this.activatePreviousItem();
|
638 | return;
|
639 | }
|
640 |
|
641 |
|
642 | if (kc === 39) {
|
643 | let item = this.activeItem;
|
644 | if (item && item.type === 'submenu') {
|
645 | this.triggerActiveItem();
|
646 | } else {
|
647 | this.rootMenu._menuRequested.emit('next');
|
648 | }
|
649 | return;
|
650 | }
|
651 |
|
652 |
|
653 | if (kc === 40) {
|
654 | this.activateNextItem();
|
655 | return;
|
656 | }
|
657 |
|
658 |
|
659 | let key = getKeyboardLayout().keyForKeydownEvent(event);
|
660 |
|
661 |
|
662 | if (!key) {
|
663 | return;
|
664 | }
|
665 |
|
666 |
|
667 | let start = this._activeIndex + 1;
|
668 | let result = Private.findMnemonic(this._items, key, start);
|
669 |
|
670 |
|
671 |
|
672 |
|
673 |
|
674 | if (result.index !== -1 && !result.multiple) {
|
675 | this.activeIndex = result.index;
|
676 | this.triggerActiveItem();
|
677 | } else if (result.index !== -1) {
|
678 | this.activeIndex = result.index;
|
679 | } else if (result.auto !== -1) {
|
680 | this.activeIndex = result.auto;
|
681 | }
|
682 | }
|
683 |
|
684 | |
685 |
|
686 |
|
687 |
|
688 |
|
689 |
|
690 | private _evtMouseUp(event: MouseEvent): void {
|
691 | if (event.button !== 0) {
|
692 | return;
|
693 | }
|
694 | event.preventDefault();
|
695 | event.stopPropagation();
|
696 | this.triggerActiveItem();
|
697 | }
|
698 |
|
699 | |
700 |
|
701 |
|
702 |
|
703 |
|
704 |
|
705 | private _evtMouseMove(event: MouseEvent): void {
|
706 |
|
707 | let index = ArrayExt.findFirstIndex(this.contentNode.children, node => {
|
708 | return ElementExt.hitTest(node, event.clientX, event.clientY);
|
709 | });
|
710 |
|
711 |
|
712 | if (index === this._activeIndex) {
|
713 | return;
|
714 | }
|
715 |
|
716 |
|
717 | this.activeIndex = index;
|
718 | index = this.activeIndex;
|
719 |
|
720 |
|
721 | if (index === this._childIndex) {
|
722 | this._cancelOpenTimer();
|
723 | this._cancelCloseTimer();
|
724 | return;
|
725 | }
|
726 |
|
727 |
|
728 | if (this._childIndex !== -1) {
|
729 | this._startCloseTimer();
|
730 | }
|
731 |
|
732 |
|
733 | this._cancelOpenTimer();
|
734 |
|
735 |
|
736 | let item = this.activeItem;
|
737 | if (!item || item.type !== 'submenu' || !item.submenu) {
|
738 | return;
|
739 | }
|
740 |
|
741 |
|
742 | this._startOpenTimer();
|
743 | }
|
744 |
|
745 | |
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 | private _evtMouseEnter(event: MouseEvent): void {
|
752 |
|
753 | for (let menu = this._parentMenu; menu; menu = menu._parentMenu) {
|
754 | menu._cancelOpenTimer();
|
755 | menu._cancelCloseTimer();
|
756 | menu.activeIndex = menu._childIndex;
|
757 | }
|
758 | }
|
759 |
|
760 | |
761 |
|
762 |
|
763 |
|
764 |
|
765 |
|
766 | private _evtMouseLeave(event: MouseEvent): void {
|
767 |
|
768 | this._cancelOpenTimer();
|
769 |
|
770 |
|
771 | if (!this._childMenu) {
|
772 | this.activeIndex = -1;
|
773 | return;
|
774 | }
|
775 |
|
776 |
|
777 | let { clientX, clientY } = event;
|
778 | if (ElementExt.hitTest(this._childMenu.node, clientX, clientY)) {
|
779 | this._cancelCloseTimer();
|
780 | return;
|
781 | }
|
782 |
|
783 |
|
784 | this.activeIndex = -1;
|
785 | this._startCloseTimer();
|
786 | }
|
787 |
|
788 | |
789 |
|
790 |
|
791 |
|
792 |
|
793 |
|
794 | private _evtMouseDown(event: MouseEvent): void {
|
795 |
|
796 | if (this._parentMenu) {
|
797 | return;
|
798 | }
|
799 |
|
800 |
|
801 |
|
802 |
|
803 |
|
804 | if (Private.hitTestMenus(this, event.clientX, event.clientY)) {
|
805 | event.preventDefault();
|
806 | event.stopPropagation();
|
807 | } else {
|
808 | this.close();
|
809 | }
|
810 | }
|
811 |
|
812 | |
813 |
|
814 |
|
815 |
|
816 |
|
817 |
|
818 | private _openChildMenu(activateFirst = false): void {
|
819 |
|
820 | let item = this.activeItem;
|
821 | if (!item || item.type !== 'submenu' || !item.submenu) {
|
822 | this._closeChildMenu();
|
823 | return;
|
824 | }
|
825 |
|
826 |
|
827 | let submenu = item.submenu;
|
828 | if (submenu === this._childMenu) {
|
829 | return;
|
830 | }
|
831 |
|
832 |
|
833 | this._closeChildMenu();
|
834 |
|
835 |
|
836 | this._childMenu = submenu;
|
837 | this._childIndex = this._activeIndex;
|
838 |
|
839 |
|
840 | submenu._parentMenu = this;
|
841 |
|
842 |
|
843 | MessageLoop.sendMessage(this, Widget.Msg.UpdateRequest);
|
844 | let itemNode = this.contentNode.children[this._activeIndex];
|
845 |
|
846 |
|
847 | Private.openSubmenu(submenu, itemNode as HTMLElement);
|
848 |
|
849 |
|
850 | if (activateFirst) {
|
851 | submenu.activeIndex = -1;
|
852 | submenu.activateNextItem();
|
853 | }
|
854 |
|
855 |
|
856 | submenu.activate();
|
857 | }
|
858 |
|
859 | |
860 |
|
861 |
|
862 |
|
863 |
|
864 | private _closeChildMenu(): void {
|
865 | if (this._childMenu) {
|
866 | this._childMenu.close();
|
867 | }
|
868 | }
|
869 |
|
870 | |
871 |
|
872 |
|
873 | private _startOpenTimer(): void {
|
874 | if (this._openTimerID === 0) {
|
875 | this._openTimerID = window.setTimeout(() => {
|
876 | this._openTimerID = 0;
|
877 | this._openChildMenu();
|
878 | }, Private.TIMER_DELAY);
|
879 | }
|
880 | }
|
881 |
|
882 | |
883 |
|
884 |
|
885 | private _startCloseTimer(): void {
|
886 | if (this._closeTimerID === 0) {
|
887 | this._closeTimerID = window.setTimeout(() => {
|
888 | this._closeTimerID = 0;
|
889 | this._closeChildMenu();
|
890 | }, Private.TIMER_DELAY);
|
891 | }
|
892 | }
|
893 |
|
894 | |
895 |
|
896 |
|
897 | private _cancelOpenTimer(): void {
|
898 | if (this._openTimerID !== 0) {
|
899 | clearTimeout(this._openTimerID);
|
900 | this._openTimerID = 0;
|
901 | }
|
902 | }
|
903 |
|
904 | |
905 |
|
906 |
|
907 | private _cancelCloseTimer(): void {
|
908 | if (this._closeTimerID !== 0) {
|
909 | clearTimeout(this._closeTimerID);
|
910 | this._closeTimerID = 0;
|
911 | }
|
912 | }
|
913 |
|
914 | private _childIndex = -1;
|
915 | private _activeIndex = -1;
|
916 | private _openTimerID = 0;
|
917 | private _closeTimerID = 0;
|
918 | private _items: Menu.IItem[] = [];
|
919 | private _childMenu: Menu | null = null;
|
920 | private _parentMenu: Menu | null = null;
|
921 | private _aboutToClose = new Signal<this, void>(this);
|
922 | private _menuRequested = new Signal<this, 'next' | 'previous'>(this);
|
923 | }
|
924 |
|
925 |
|
926 |
|
927 |
|
928 | export namespace Menu {
|
929 | |
930 |
|
931 |
|
932 | export interface IOptions {
|
933 | |
934 |
|
935 |
|
936 | commands: CommandRegistry;
|
937 |
|
938 | |
939 |
|
940 |
|
941 |
|
942 |
|
943 | renderer?: IRenderer;
|
944 | }
|
945 |
|
946 | |
947 |
|
948 |
|
949 | export interface IOpenOptions {
|
950 | |
951 |
|
952 |
|
953 |
|
954 |
|
955 |
|
956 |
|
957 |
|
958 | forceX?: boolean;
|
959 |
|
960 | |
961 |
|
962 |
|
963 |
|
964 |
|
965 |
|
966 |
|
967 |
|
968 | forceY?: boolean;
|
969 | }
|
970 |
|
971 | |
972 |
|
973 |
|
974 | export type ItemType = 'command' | 'submenu' | 'separator';
|
975 |
|
976 | |
977 |
|
978 |
|
979 | export interface IItemOptions {
|
980 | |
981 |
|
982 |
|
983 |
|
984 |
|
985 | type?: ItemType;
|
986 |
|
987 | |
988 |
|
989 |
|
990 |
|
991 |
|
992 | command?: string;
|
993 |
|
994 | |
995 |
|
996 |
|
997 |
|
998 |
|
999 | args?: ReadonlyJSONObject;
|
1000 |
|
1001 | |
1002 |
|
1003 |
|
1004 |
|
1005 |
|
1006 | submenu?: Menu | null;
|
1007 | }
|
1008 |
|
1009 | |
1010 |
|
1011 |
|
1012 |
|
1013 |
|
1014 |
|
1015 | export interface IItem {
|
1016 | |
1017 |
|
1018 |
|
1019 | readonly type: ItemType;
|
1020 |
|
1021 | |
1022 |
|
1023 |
|
1024 | readonly command: string;
|
1025 |
|
1026 | |
1027 |
|
1028 |
|
1029 | readonly args: ReadonlyJSONObject;
|
1030 |
|
1031 | |
1032 |
|
1033 |
|
1034 | readonly submenu: Menu | null;
|
1035 |
|
1036 | |
1037 |
|
1038 |
|
1039 | readonly label: string;
|
1040 |
|
1041 | |
1042 |
|
1043 |
|
1044 | readonly mnemonic: number;
|
1045 |
|
1046 | |
1047 |
|
1048 |
|
1049 | readonly icon:
|
1050 | | VirtualElement.IRenderer
|
1051 | | undefined
|
1052 |
|
1053 | | string ;
|
1054 |
|
1055 | |
1056 |
|
1057 |
|
1058 | readonly iconClass: string;
|
1059 |
|
1060 | |
1061 |
|
1062 |
|
1063 | readonly iconLabel: string;
|
1064 |
|
1065 | |
1066 |
|
1067 |
|
1068 | readonly caption: string;
|
1069 |
|
1070 | |
1071 |
|
1072 |
|
1073 | readonly className: string;
|
1074 |
|
1075 | |
1076 |
|
1077 |
|
1078 | readonly dataset: CommandRegistry.Dataset;
|
1079 |
|
1080 | |
1081 |
|
1082 |
|
1083 | readonly isEnabled: boolean;
|
1084 |
|
1085 | |
1086 |
|
1087 |
|
1088 | readonly isToggled: boolean;
|
1089 |
|
1090 | |
1091 |
|
1092 |
|
1093 | readonly isVisible: boolean;
|
1094 |
|
1095 | |
1096 |
|
1097 |
|
1098 | readonly keyBinding: CommandRegistry.IKeyBinding | null;
|
1099 | }
|
1100 |
|
1101 | |
1102 |
|
1103 |
|
1104 | export interface IRenderData {
|
1105 | |
1106 |
|
1107 |
|
1108 | readonly item: IItem;
|
1109 |
|
1110 | |
1111 |
|
1112 |
|
1113 | readonly active: boolean;
|
1114 |
|
1115 | |
1116 |
|
1117 |
|
1118 | readonly collapsed: boolean;
|
1119 |
|
1120 | |
1121 |
|
1122 |
|
1123 | readonly onfocus?: () => void;
|
1124 | }
|
1125 |
|
1126 | |
1127 |
|
1128 |
|
1129 | export interface IRenderer {
|
1130 | |
1131 |
|
1132 |
|
1133 |
|
1134 |
|
1135 |
|
1136 |
|
1137 | renderItem(data: IRenderData): VirtualElement;
|
1138 | }
|
1139 |
|
1140 | |
1141 |
|
1142 |
|
1143 |
|
1144 |
|
1145 |
|
1146 | export class Renderer implements IRenderer {
|
1147 | |
1148 |
|
1149 |
|
1150 |
|
1151 |
|
1152 |
|
1153 |
|
1154 | renderItem(data: IRenderData): VirtualElement {
|
1155 | let className = this.createItemClass(data);
|
1156 | let dataset = this.createItemDataset(data);
|
1157 | let aria = this.createItemARIA(data);
|
1158 | return h.li(
|
1159 | {
|
1160 | className,
|
1161 | dataset,
|
1162 | tabindex: '0',
|
1163 | onfocus: data.onfocus,
|
1164 | ...aria
|
1165 | },
|
1166 | this.renderIcon(data),
|
1167 | this.renderLabel(data),
|
1168 | this.renderShortcut(data),
|
1169 | this.renderSubmenu(data)
|
1170 | );
|
1171 | }
|
1172 |
|
1173 | |
1174 |
|
1175 |
|
1176 |
|
1177 |
|
1178 |
|
1179 |
|
1180 | renderIcon(data: IRenderData): VirtualElement {
|
1181 | let className = this.createIconClass(data);
|
1182 |
|
1183 |
|
1184 | if (typeof data.item.icon === 'string') {
|
1185 | return h.div({ className }, data.item.iconLabel);
|
1186 | }
|
1187 |
|
1188 |
|
1189 |
|
1190 | return h.div({ className }, data.item.icon!, data.item.iconLabel);
|
1191 | }
|
1192 |
|
1193 | |
1194 |
|
1195 |
|
1196 |
|
1197 |
|
1198 |
|
1199 |
|
1200 | renderLabel(data: IRenderData): VirtualElement {
|
1201 | let content = this.formatLabel(data);
|
1202 | return h.div(
|
1203 | {
|
1204 | className:
|
1205 | 'lm-Menu-itemLabel' +
|
1206 |
|
1207 | ' p-Menu-itemLabel'
|
1208 |
|
1209 | },
|
1210 | content
|
1211 | );
|
1212 | }
|
1213 |
|
1214 | |
1215 |
|
1216 |
|
1217 |
|
1218 |
|
1219 |
|
1220 |
|
1221 | renderShortcut(data: IRenderData): VirtualElement {
|
1222 | let content = this.formatShortcut(data);
|
1223 | return h.div(
|
1224 | {
|
1225 | className:
|
1226 | 'lm-Menu-itemShortcut' +
|
1227 |
|
1228 | ' p-Menu-itemShortcut'
|
1229 |
|
1230 | },
|
1231 | content
|
1232 | );
|
1233 | }
|
1234 |
|
1235 | |
1236 |
|
1237 |
|
1238 |
|
1239 |
|
1240 |
|
1241 |
|
1242 | renderSubmenu(data: IRenderData): VirtualElement {
|
1243 | return h.div({
|
1244 | className:
|
1245 | 'lm-Menu-itemSubmenuIcon' +
|
1246 |
|
1247 | ' p-Menu-itemSubmenuIcon'
|
1248 |
|
1249 | });
|
1250 | }
|
1251 |
|
1252 | |
1253 |
|
1254 |
|
1255 |
|
1256 |
|
1257 |
|
1258 |
|
1259 | createItemClass(data: IRenderData): string {
|
1260 |
|
1261 | let name = 'lm-Menu-item';
|
1262 |
|
1263 | name += ' p-Menu-item';
|
1264 |
|
1265 |
|
1266 |
|
1267 | if (!data.item.isEnabled) {
|
1268 | name += ' lm-mod-disabled';
|
1269 |
|
1270 | name += ' p-mod-disabled';
|
1271 |
|
1272 | }
|
1273 | if (data.item.isToggled) {
|
1274 | name += ' lm-mod-toggled';
|
1275 |
|
1276 | name += ' p-mod-toggled';
|
1277 |
|
1278 | }
|
1279 | if (!data.item.isVisible) {
|
1280 | name += ' lm-mod-hidden';
|
1281 |
|
1282 | name += ' p-mod-hidden';
|
1283 |
|
1284 | }
|
1285 | if (data.active) {
|
1286 | name += ' lm-mod-active';
|
1287 |
|
1288 | name += ' p-mod-active';
|
1289 |
|
1290 | }
|
1291 | if (data.collapsed) {
|
1292 | name += ' lm-mod-collapsed';
|
1293 |
|
1294 | name += ' p-mod-collapsed';
|
1295 |
|
1296 | }
|
1297 |
|
1298 |
|
1299 | let extra = data.item.className;
|
1300 | if (extra) {
|
1301 | name += ` ${extra}`;
|
1302 | }
|
1303 |
|
1304 |
|
1305 | return name;
|
1306 | }
|
1307 |
|
1308 | |
1309 |
|
1310 |
|
1311 |
|
1312 |
|
1313 |
|
1314 |
|
1315 | createItemDataset(data: IRenderData): ElementDataset {
|
1316 | let result: ElementDataset;
|
1317 | let { type, command, dataset } = data.item;
|
1318 | if (type === 'command') {
|
1319 | result = { ...dataset, type, command };
|
1320 | } else {
|
1321 | result = { ...dataset, type };
|
1322 | }
|
1323 | return result;
|
1324 | }
|
1325 |
|
1326 | |
1327 |
|
1328 |
|
1329 |
|
1330 |
|
1331 |
|
1332 |
|
1333 | createIconClass(data: IRenderData): string {
|
1334 | let name = 'lm-Menu-itemIcon';
|
1335 |
|
1336 | name += ' p-Menu-itemIcon';
|
1337 |
|
1338 | let extra = data.item.iconClass;
|
1339 | return extra ? `${name} ${extra}` : name;
|
1340 | }
|
1341 |
|
1342 | |
1343 |
|
1344 |
|
1345 |
|
1346 |
|
1347 |
|
1348 |
|
1349 | createItemARIA(data: IRenderData): ElementARIAAttrs {
|
1350 | let aria: { [T in ARIAAttrNames]?: string } = {};
|
1351 | switch (data.item.type) {
|
1352 | case 'separator':
|
1353 | aria.role = 'presentation';
|
1354 | break;
|
1355 | case 'submenu':
|
1356 | aria['aria-haspopup'] = 'true';
|
1357 | if (!data.item.isEnabled) {
|
1358 | aria['aria-disabled'] = 'true';
|
1359 | }
|
1360 | break;
|
1361 | default:
|
1362 | if (!data.item.isEnabled) {
|
1363 | aria['aria-disabled'] = 'true';
|
1364 | }
|
1365 | aria.role = 'menuitem';
|
1366 | }
|
1367 | return aria;
|
1368 | }
|
1369 |
|
1370 | |
1371 |
|
1372 |
|
1373 |
|
1374 |
|
1375 |
|
1376 |
|
1377 | formatLabel(data: IRenderData): h.Child {
|
1378 |
|
1379 | let { label, mnemonic } = data.item;
|
1380 |
|
1381 |
|
1382 | if (mnemonic < 0 || mnemonic >= label.length) {
|
1383 | return label;
|
1384 | }
|
1385 |
|
1386 |
|
1387 | let prefix = label.slice(0, mnemonic);
|
1388 | let suffix = label.slice(mnemonic + 1);
|
1389 | let char = label[mnemonic];
|
1390 |
|
1391 |
|
1392 | let span = h.span(
|
1393 | {
|
1394 | className:
|
1395 | 'lm-Menu-itemMnemonic' +
|
1396 |
|
1397 | ' p-Menu-itemMnemonic'
|
1398 |
|
1399 | },
|
1400 | char
|
1401 | );
|
1402 |
|
1403 |
|
1404 | return [prefix, span, suffix];
|
1405 | }
|
1406 |
|
1407 | |
1408 |
|
1409 |
|
1410 |
|
1411 |
|
1412 |
|
1413 |
|
1414 | formatShortcut(data: IRenderData): h.Child {
|
1415 | let kb = data.item.keyBinding;
|
1416 | return kb
|
1417 | ? kb.keys.map(CommandRegistry.formatKeystroke).join(', ')
|
1418 | : null;
|
1419 | }
|
1420 | }
|
1421 |
|
1422 | |
1423 |
|
1424 |
|
1425 | export const defaultRenderer = new Renderer();
|
1426 | }
|
1427 |
|
1428 |
|
1429 |
|
1430 |
|
1431 | namespace Private {
|
1432 | |
1433 |
|
1434 |
|
1435 | export const TIMER_DELAY = 300;
|
1436 |
|
1437 | |
1438 |
|
1439 |
|
1440 | export const SUBMENU_OVERLAP = 3;
|
1441 |
|
1442 | |
1443 |
|
1444 |
|
1445 | export function createNode(): HTMLDivElement {
|
1446 | let node = document.createElement('div');
|
1447 | let content = document.createElement('ul');
|
1448 | content.className = 'lm-Menu-content';
|
1449 |
|
1450 | content.classList.add('p-Menu-content');
|
1451 |
|
1452 | node.appendChild(content);
|
1453 | content.setAttribute('role', 'menu');
|
1454 | node.tabIndex = 0;
|
1455 | return node;
|
1456 | }
|
1457 |
|
1458 | |
1459 |
|
1460 |
|
1461 | export function canActivate(item: Menu.IItem): boolean {
|
1462 | return item.type !== 'separator' && item.isEnabled && item.isVisible;
|
1463 | }
|
1464 |
|
1465 | |
1466 |
|
1467 |
|
1468 | export function createItem(
|
1469 | owner: Menu,
|
1470 | options: Menu.IItemOptions
|
1471 | ): Menu.IItem {
|
1472 | return new MenuItem(owner.commands, options);
|
1473 | }
|
1474 |
|
1475 | |
1476 |
|
1477 |
|
1478 | export function hitTestMenus(menu: Menu, x: number, y: number): boolean {
|
1479 | for (let temp: Menu | null = menu; temp; temp = temp.childMenu) {
|
1480 | if (ElementExt.hitTest(temp.node, x, y)) {
|
1481 | return true;
|
1482 | }
|
1483 | }
|
1484 | return false;
|
1485 | }
|
1486 |
|
1487 | |
1488 |
|
1489 |
|
1490 | export function computeCollapsed(
|
1491 | items: ReadonlyArray<Menu.IItem>
|
1492 | ): boolean[] {
|
1493 |
|
1494 | let result = new Array<boolean>(items.length);
|
1495 | ArrayExt.fill(result, false);
|
1496 |
|
1497 |
|
1498 | let k1 = 0;
|
1499 | let n = items.length;
|
1500 | for (; k1 < n; ++k1) {
|
1501 | let item = items[k1];
|
1502 | if (!item.isVisible) {
|
1503 | continue;
|
1504 | }
|
1505 | if (item.type !== 'separator') {
|
1506 | break;
|
1507 | }
|
1508 | result[k1] = true;
|
1509 | }
|
1510 |
|
1511 |
|
1512 | let k2 = n - 1;
|
1513 | for (; k2 >= 0; --k2) {
|
1514 | let item = items[k2];
|
1515 | if (!item.isVisible) {
|
1516 | continue;
|
1517 | }
|
1518 | if (item.type !== 'separator') {
|
1519 | break;
|
1520 | }
|
1521 | result[k2] = true;
|
1522 | }
|
1523 |
|
1524 |
|
1525 | let hide = false;
|
1526 | while (++k1 < k2) {
|
1527 | let item = items[k1];
|
1528 | if (!item.isVisible) {
|
1529 | continue;
|
1530 | }
|
1531 | if (item.type !== 'separator') {
|
1532 | hide = false;
|
1533 | } else if (hide) {
|
1534 | result[k1] = true;
|
1535 | } else {
|
1536 | hide = true;
|
1537 | }
|
1538 | }
|
1539 |
|
1540 |
|
1541 | return result;
|
1542 | }
|
1543 |
|
1544 | |
1545 |
|
1546 |
|
1547 | export function openRootMenu(
|
1548 | menu: Menu,
|
1549 | x: number,
|
1550 | y: number,
|
1551 | forceX: boolean,
|
1552 | forceY: boolean
|
1553 | ): void {
|
1554 |
|
1555 | MessageLoop.sendMessage(menu, Widget.Msg.UpdateRequest);
|
1556 |
|
1557 |
|
1558 | let px = window.pageXOffset;
|
1559 | let py = window.pageYOffset;
|
1560 | let cw = document.documentElement.clientWidth;
|
1561 | let ch = document.documentElement.clientHeight;
|
1562 |
|
1563 |
|
1564 | let maxHeight = ch - (forceY ? y : 0);
|
1565 |
|
1566 |
|
1567 | let node = menu.node;
|
1568 | let style = node.style;
|
1569 |
|
1570 |
|
1571 | style.top = '';
|
1572 | style.left = '';
|
1573 | style.width = '';
|
1574 | style.height = '';
|
1575 | style.visibility = 'hidden';
|
1576 | style.maxHeight = `${maxHeight}px`;
|
1577 |
|
1578 |
|
1579 | Widget.attach(menu, document.body);
|
1580 |
|
1581 |
|
1582 | let { width, height } = node.getBoundingClientRect();
|
1583 |
|
1584 |
|
1585 | if (!forceX && x + width > px + cw) {
|
1586 | x = px + cw - width;
|
1587 | }
|
1588 |
|
1589 |
|
1590 | if (!forceY && y + height > py + ch) {
|
1591 | if (y > py + ch) {
|
1592 | y = py + ch - height;
|
1593 | } else {
|
1594 | y = y - height;
|
1595 | }
|
1596 | }
|
1597 |
|
1598 |
|
1599 | style.top = `${Math.max(0, y)}px`;
|
1600 | style.left = `${Math.max(0, x)}px`;
|
1601 |
|
1602 |
|
1603 | style.visibility = '';
|
1604 | }
|
1605 |
|
1606 | |
1607 |
|
1608 |
|
1609 | export function openSubmenu(submenu: Menu, itemNode: HTMLElement): void {
|
1610 |
|
1611 | MessageLoop.sendMessage(submenu, Widget.Msg.UpdateRequest);
|
1612 |
|
1613 |
|
1614 | let px = window.pageXOffset;
|
1615 | let py = window.pageYOffset;
|
1616 | let cw = document.documentElement.clientWidth;
|
1617 | let ch = document.documentElement.clientHeight;
|
1618 |
|
1619 |
|
1620 | let maxHeight = ch;
|
1621 |
|
1622 |
|
1623 | let node = submenu.node;
|
1624 | let style = node.style;
|
1625 |
|
1626 |
|
1627 | style.top = '';
|
1628 | style.left = '';
|
1629 | style.width = '';
|
1630 | style.height = '';
|
1631 | style.visibility = 'hidden';
|
1632 | style.maxHeight = `${maxHeight}px`;
|
1633 |
|
1634 |
|
1635 | Widget.attach(submenu, document.body);
|
1636 |
|
1637 |
|
1638 | let { width, height } = node.getBoundingClientRect();
|
1639 |
|
1640 |
|
1641 | let box = ElementExt.boxSizing(submenu.node);
|
1642 |
|
1643 |
|
1644 | let itemRect = itemNode.getBoundingClientRect();
|
1645 |
|
1646 |
|
1647 | let x = itemRect.right - SUBMENU_OVERLAP;
|
1648 |
|
1649 |
|
1650 | if (x + width > px + cw) {
|
1651 | x = itemRect.left + SUBMENU_OVERLAP - width;
|
1652 | }
|
1653 |
|
1654 |
|
1655 | let y = itemRect.top - box.borderTop - box.paddingTop;
|
1656 |
|
1657 |
|
1658 | if (y + height > py + ch) {
|
1659 | y = itemRect.bottom + box.borderBottom + box.paddingBottom - height;
|
1660 | }
|
1661 |
|
1662 |
|
1663 | style.top = `${Math.max(0, y)}px`;
|
1664 | style.left = `${Math.max(0, x)}px`;
|
1665 |
|
1666 |
|
1667 | style.visibility = '';
|
1668 | }
|
1669 |
|
1670 | |
1671 |
|
1672 |
|
1673 | export interface IMnemonicResult {
|
1674 | |
1675 |
|
1676 |
|
1677 | index: number;
|
1678 |
|
1679 | |
1680 |
|
1681 |
|
1682 | multiple: boolean;
|
1683 |
|
1684 | |
1685 |
|
1686 |
|
1687 | auto: number;
|
1688 | }
|
1689 |
|
1690 | |
1691 |
|
1692 |
|
1693 |
|
1694 |
|
1695 | export function findMnemonic(
|
1696 | items: ReadonlyArray<Menu.IItem>,
|
1697 | key: string,
|
1698 | start: number
|
1699 | ): IMnemonicResult {
|
1700 |
|
1701 | let index = -1;
|
1702 | let auto = -1;
|
1703 | let multiple = false;
|
1704 |
|
1705 |
|
1706 | let upperKey = key.toUpperCase();
|
1707 |
|
1708 |
|
1709 | for (let i = 0, n = items.length; i < n; ++i) {
|
1710 |
|
1711 | let k = (i + start) % n;
|
1712 |
|
1713 |
|
1714 | let item = items[k];
|
1715 |
|
1716 |
|
1717 | if (!canActivate(item)) {
|
1718 | continue;
|
1719 | }
|
1720 |
|
1721 |
|
1722 | let label = item.label;
|
1723 | if (label.length === 0) {
|
1724 | continue;
|
1725 | }
|
1726 |
|
1727 |
|
1728 | let mn = item.mnemonic;
|
1729 |
|
1730 |
|
1731 | if (mn >= 0 && mn < label.length) {
|
1732 | if (label[mn].toUpperCase() === upperKey) {
|
1733 | if (index === -1) {
|
1734 | index = k;
|
1735 | } else {
|
1736 | multiple = true;
|
1737 | }
|
1738 | }
|
1739 | continue;
|
1740 | }
|
1741 |
|
1742 |
|
1743 | if (auto === -1 && label[0].toUpperCase() === upperKey) {
|
1744 | auto = k;
|
1745 | }
|
1746 | }
|
1747 |
|
1748 |
|
1749 | return { index, multiple, auto };
|
1750 | }
|
1751 |
|
1752 | |
1753 |
|
1754 |
|
1755 | class MenuItem implements Menu.IItem {
|
1756 | |
1757 |
|
1758 |
|
1759 | constructor(commands: CommandRegistry, options: Menu.IItemOptions) {
|
1760 | this._commands = commands;
|
1761 | this.type = options.type || 'command';
|
1762 | this.command = options.command || '';
|
1763 | this.args = options.args || JSONExt.emptyObject;
|
1764 | this.submenu = options.submenu || null;
|
1765 | }
|
1766 |
|
1767 | |
1768 |
|
1769 |
|
1770 | readonly type: Menu.ItemType;
|
1771 |
|
1772 | |
1773 |
|
1774 |
|
1775 | readonly command: string;
|
1776 |
|
1777 | |
1778 |
|
1779 |
|
1780 | readonly args: ReadonlyJSONObject;
|
1781 |
|
1782 | |
1783 |
|
1784 |
|
1785 | readonly submenu: Menu | null;
|
1786 |
|
1787 | |
1788 |
|
1789 |
|
1790 | get label(): string {
|
1791 | if (this.type === 'command') {
|
1792 | return this._commands.label(this.command, this.args);
|
1793 | }
|
1794 | if (this.type === 'submenu' && this.submenu) {
|
1795 | return this.submenu.title.label;
|
1796 | }
|
1797 | return '';
|
1798 | }
|
1799 |
|
1800 | |
1801 |
|
1802 |
|
1803 | get mnemonic(): number {
|
1804 | if (this.type === 'command') {
|
1805 | return this._commands.mnemonic(this.command, this.args);
|
1806 | }
|
1807 | if (this.type === 'submenu' && this.submenu) {
|
1808 | return this.submenu.title.mnemonic;
|
1809 | }
|
1810 | return -1;
|
1811 | }
|
1812 |
|
1813 | |
1814 |
|
1815 |
|
1816 | get icon():
|
1817 | | VirtualElement.IRenderer
|
1818 | | undefined
|
1819 |
|
1820 | | string {
|
1821 | if (this.type === 'command') {
|
1822 | return this._commands.icon(this.command, this.args);
|
1823 | }
|
1824 | if (this.type === 'submenu' && this.submenu) {
|
1825 | return this.submenu.title.icon;
|
1826 | }
|
1827 |
|
1828 |
|
1829 |
|
1830 | return this.iconClass;
|
1831 |
|
1832 |
|
1833 | |
1834 |
|
1835 |
|
1836 | }
|
1837 |
|
1838 | |
1839 |
|
1840 |
|
1841 | get iconClass(): string {
|
1842 | if (this.type === 'command') {
|
1843 | return this._commands.iconClass(this.command, this.args);
|
1844 | }
|
1845 | if (this.type === 'submenu' && this.submenu) {
|
1846 | return this.submenu.title.iconClass;
|
1847 | }
|
1848 | return '';
|
1849 | }
|
1850 |
|
1851 | |
1852 |
|
1853 |
|
1854 | get iconLabel(): string {
|
1855 | if (this.type === 'command') {
|
1856 | return this._commands.iconLabel(this.command, this.args);
|
1857 | }
|
1858 | if (this.type === 'submenu' && this.submenu) {
|
1859 | return this.submenu.title.iconLabel;
|
1860 | }
|
1861 | return '';
|
1862 | }
|
1863 |
|
1864 | |
1865 |
|
1866 |
|
1867 | get caption(): string {
|
1868 | if (this.type === 'command') {
|
1869 | return this._commands.caption(this.command, this.args);
|
1870 | }
|
1871 | if (this.type === 'submenu' && this.submenu) {
|
1872 | return this.submenu.title.caption;
|
1873 | }
|
1874 | return '';
|
1875 | }
|
1876 |
|
1877 | |
1878 |
|
1879 |
|
1880 | get className(): string {
|
1881 | if (this.type === 'command') {
|
1882 | return this._commands.className(this.command, this.args);
|
1883 | }
|
1884 | if (this.type === 'submenu' && this.submenu) {
|
1885 | return this.submenu.title.className;
|
1886 | }
|
1887 | return '';
|
1888 | }
|
1889 |
|
1890 | |
1891 |
|
1892 |
|
1893 | get dataset(): CommandRegistry.Dataset {
|
1894 | if (this.type === 'command') {
|
1895 | return this._commands.dataset(this.command, this.args);
|
1896 | }
|
1897 | if (this.type === 'submenu' && this.submenu) {
|
1898 | return this.submenu.title.dataset;
|
1899 | }
|
1900 | return {};
|
1901 | }
|
1902 |
|
1903 | |
1904 |
|
1905 |
|
1906 | get isEnabled(): boolean {
|
1907 | if (this.type === 'command') {
|
1908 | return this._commands.isEnabled(this.command, this.args);
|
1909 | }
|
1910 | if (this.type === 'submenu') {
|
1911 | return this.submenu !== null;
|
1912 | }
|
1913 | return true;
|
1914 | }
|
1915 |
|
1916 | |
1917 |
|
1918 |
|
1919 | get isToggled(): boolean {
|
1920 | if (this.type === 'command') {
|
1921 | return this._commands.isToggled(this.command, this.args);
|
1922 | }
|
1923 | return false;
|
1924 | }
|
1925 |
|
1926 | |
1927 |
|
1928 |
|
1929 | get isVisible(): boolean {
|
1930 | if (this.type === 'command') {
|
1931 | return this._commands.isVisible(this.command, this.args);
|
1932 | }
|
1933 | if (this.type === 'submenu') {
|
1934 | return this.submenu !== null;
|
1935 | }
|
1936 | return true;
|
1937 | }
|
1938 |
|
1939 | |
1940 |
|
1941 |
|
1942 | get keyBinding(): CommandRegistry.IKeyBinding | null {
|
1943 | if (this.type === 'command') {
|
1944 | let { command, args } = this;
|
1945 | return (
|
1946 | ArrayExt.findLastValue(this._commands.keyBindings, kb => {
|
1947 | return kb.command === command && JSONExt.deepEqual(kb.args, args);
|
1948 | }) || null
|
1949 | );
|
1950 | }
|
1951 | return null;
|
1952 | }
|
1953 |
|
1954 | private _commands: CommandRegistry;
|
1955 | }
|
1956 | }
|