-
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathMenus.js
More file actions
1798 lines (1577 loc) · 66.8 KB
/
Menus.js
File metadata and controls
1798 lines (1577 loc) · 66.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2012 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
// @INCLUDE_IN_API_DOCS
/*global logger*/
define(function (require, exports, module) {
let _ = require("thirdparty/lodash");
// Load dependent modules
let Commands = require("command/Commands"),
EventDispatcher = require("utils/EventDispatcher"),
KeyBindingManager = require("command/KeyBindingManager"),
Keys = require("command/Keys"),
Strings = require("strings"),
StringUtils = require("utils/StringUtils"),
CommandManager = require("command/CommandManager"),
PopUpManager = require("widgets/PopUpManager"),
ViewUtils = require("utils/ViewUtils"),
Metrics = require("utils/Metrics"),
MainViewManager = require("view/MainViewManager"),
AppInit = require("utils/AppInit"),
DeprecationWarning = require("utils/DeprecationWarning");
// make sure the global brackets letiable is loaded
require("utils/Global");
const KEY = Keys.KEY;
const allMenuCommands = new Set();
/**
* Brackets Application Menu Constants
* @enum {string}
*/
let AppMenuBar = {
FILE_MENU: "file-menu",
EDIT_MENU: "edit-menu",
FIND_MENU: "find-menu",
VIEW_MENU: "view-menu",
NAVIGATE_MENU: "navigate-menu",
DEBUG_MENU: "debug-menu",
HELP_MENU: "help-menu"
};
/**
* Brackets Context Menu Constants
* @enum {string}
*/
let ContextMenuIds = {
EDITOR_MENU: "editor-context-menu",
INLINE_EDITOR_MENU: "inline-editor-context-menu",
PROJECT_MENU: "project-context-menu",
WORKING_SET_CONTEXT_MENU: "workingset-context-menu",
WORKING_SET_CONFIG_MENU: "workingset-configuration-menu",
SPLITVIEW_MENU: "splitview-menu"
};
/**
* Brackets well known submenus
* @enum {string}
*/
let SubMenuIds = {
GIT_SUB_MENU: "git-submenu"
};
/**
* Event triggered before the context menu opens.
* @event EVENT_BEFORE_CONTEXT_MENU_OPEN
*/
const EVENT_BEFORE_CONTEXT_MENU_OPEN = "beforeContextMenuOpen";
/**
* Event triggered before the context menu closes.
* @event EVENT_BEFORE_CONTEXT_MENU_CLOSE
*/
const EVENT_BEFORE_CONTEXT_MENU_CLOSE = "beforeContextMenuClose";
/**
* Event triggered before a sub-menu opens.
* @event EVENT_BEFORE_SUB_MENU_OPEN
*/
const EVENT_BEFORE_SUB_MENU_OPEN = "beforeSubMenuOpen";
/**
* Event triggered before a sub-menu closes.
* @event EVENT_BEFORE_SUB_MENU_CLOSE
*/
const EVENT_BEFORE_SUB_MENU_CLOSE = "beforeSubMenuClose";
/**
* Event triggered when a menu or menu is added
* @event EVENT_MENU_ADDED
*/
const EVENT_MENU_ADDED = "menuAdded";
/**
* Event triggered when a menu or submenu is added
* @event EVENT_SUB_MENU_ADDED
*/
const EVENT_SUB_MENU_ADDED = "subMenuAdded";
/**
* Event triggered when a menu item is added
* @event EVENT_MENU_ITEM_ADDED
*/
const EVENT_MENU_ITEM_ADDED = "menuItemAdded";
// Define each section as a separate constant
const FILE_OPEN_CLOSE_COMMANDS = { sectionMarker: Commands.FILE_NEW };
const FILE_SAVE_COMMANDS = { sectionMarker: Commands.FILE_SAVE };
const FILE_LIVE = { sectionMarker: Commands.FILE_LIVE_FILE_PREVIEW };
const FILE_SETTINGS = { sectionMarker: Commands.FILE_EXTENSION_MANAGER };
const FILE_EXTENSION_MANAGER = { sectionMarker: Commands.FILE_EXTENSION_MANAGER }; // Deprecated
const EDIT_UNDO_REDO_COMMANDS = { sectionMarker: Commands.EDIT_UNDO };
const EDIT_TEXT_COMMANDS = { sectionMarker: Commands.EDIT_CUT };
const EDIT_SELECTION_COMMANDS = { sectionMarker: Commands.EDIT_SELECT_ALL };
const EDIT_MODIFY_SELECTION = { sectionMarker: Commands.EDIT_INDENT };
const EDIT_COMMENT_SELECTION = { sectionMarker: Commands.EDIT_LINE_COMMENT };
const EDIT_CODE_HINTS_COMMANDS = { sectionMarker: Commands.SHOW_CODE_HINTS };
const EDIT_TOGGLE_OPTIONS = { sectionMarker: Commands.TOGGLE_CLOSE_BRACKETS };
const FIND_FIND_COMMANDS = { sectionMarker: Commands.CMD_FIND };
const FIND_FIND_IN_COMMANDS = { sectionMarker: Commands.CMD_FIND_IN_FILES };
const FIND_REPLACE_COMMANDS = { sectionMarker: Commands.CMD_REPLACE };
const VIEW_HIDESHOW_COMMANDS = { sectionMarker: Commands.VIEW_HIDE_SIDEBAR };
const VIEW_FONTSIZE_COMMANDS = { sectionMarker: Commands.VIEW_ZOOM_SUBMENU };
const VIEW_TOGGLE_OPTIONS = { sectionMarker: Commands.TOGGLE_ACTIVE_LINE };
const NAVIGATE_GOTO_COMMANDS = { sectionMarker: Commands.NAVIGATE_QUICK_OPEN };
const NAVIGATE_DOCUMENTS_COMMANDS = { sectionMarker: Commands.NAVIGATE_NEXT_DOC };
const NAVIGATE_OS_COMMANDS = { sectionMarker: Commands.NAVIGATE_SHOW_IN_FILE_TREE };
const NAVIGATE_QUICK_EDIT_COMMANDS = { sectionMarker: Commands.TOGGLE_QUICK_EDIT };
const NAVIGATE_QUICK_DOCS_COMMANDS = { sectionMarker: Commands.TOGGLE_QUICK_DOCS };
/**
* Brackets Application Menu Section Constants
* It is preferred that plug-ins specify the location of new MenuItems
* in terms of a menu section rather than a specific MenuItem. This provides
* looser coupling to Bracket's internal MenuItems and makes menu organization
* more semantic.
* Use these constants as the "relativeID" parameter when calling addMenuItem() and
* specify a position of FIRST_IN_SECTION or LAST_IN_SECTION.
*
* Menu sections are denoted by dividers or the beginning/end of a menu
*
* @enum {string}
*/
const MenuSection = {
FILE_OPEN_CLOSE_COMMANDS,
FILE_SAVE_COMMANDS,
FILE_LIVE,
FILE_SETTINGS,
FILE_EXTENSION_MANAGER, // Deprecated
EDIT_UNDO_REDO_COMMANDS,
EDIT_TEXT_COMMANDS,
EDIT_SELECTION_COMMANDS,
EDIT_MODIFY_SELECTION,
EDIT_COMMENT_SELECTION,
EDIT_CODE_HINTS_COMMANDS,
EDIT_TOGGLE_OPTIONS,
FIND_FIND_COMMANDS,
FIND_FIND_IN_COMMANDS,
FIND_REPLACE_COMMANDS,
VIEW_HIDESHOW_COMMANDS,
VIEW_FONTSIZE_COMMANDS,
VIEW_TOGGLE_OPTIONS,
NAVIGATE_GOTO_COMMANDS,
NAVIGATE_DOCUMENTS_COMMANDS,
NAVIGATE_OS_COMMANDS,
NAVIGATE_QUICK_EDIT_COMMANDS,
NAVIGATE_QUICK_DOCS_COMMANDS
};
/**
* Insertion position constants
* Used by addMenu(), addMenuItem(), and addSubMenu() to
* specify the relative position of a newly created menu object
* @enum {string}
*/
const BEFORE = "before",
AFTER = "after",
FIRST = "first",
LAST = "last",
FIRST_IN_SECTION = "firstInSection",
LAST_IN_SECTION = "lastInSection";
/**
* Other constants
*/
let DIVIDER = "---";
let SUBMENU = "SUBMENU";
/**
* Maps menuID's to Menu objects
* @type {Object} menuMap
* @property {Object.<string, Menu>} menuID - A map of Menu IDs to Menu objects.
* @private
*/
let menuMap = {};
/**
* Maps contextMenuID's to ContextMenu objects
* @type {Object} contextMenuMap
* @property {Object.<string, ContextMenu>} contextMenuID - A map of ContextMenu IDs to ContextMenu objects.
* @private
*/
let contextMenuMap = {};
/**
* Maps menuItemID's to MenuItem object
* @type {Object} menuItemMap
* @property {Object.<string, MenuItem>} menuItemID - A map of MenuItem IDs to MenuItem objects.
* @private
*/
let menuItemMap = {};
/**
* Maps menuItemID's to ContextMenu objects
* @type {Object} subMenuItemMap
* @property {Object.<string, ContextMenu>} menuItemId - A map of MenuItem IDs to ContextMenu objects.
* @private
*/
let subMenuItemMap = {};
/**
* Retrieves the Menu object for the corresponding id.
* @param {string} id
* @return {Menu}
*/
function getMenu(id) {
return menuMap[id];
}
/**
* Retrieves the subMenu object for the corresponding id if present.
* @param {string} id
* @return {Menu}
*/
function getSubMenu(id) {
return getContextMenu(id);
}
/**
* retruns a set containing all commands that has a menu item registered
* @returns {Set<string>}
*/
function getAllMenuItemCommands() {
return new Set(allMenuCommands);
}
/**
* Retrieves the map of all Menu objects.
* @return {Object.<string, Menu>}
*/
function getAllMenus() {
return menuMap;
}
/**
* Retrieves the ContextMenu object for the corresponding id.
* @param {string} id
* @return {ContextMenu}
*/
function getContextMenu(id) {
return contextMenuMap[id];
}
/**
* Removes the attached event listeners from the corresponding object.
* @param {MenuItem} menuItem
* @private
*/
function removeMenuItemEventListeners(menuItem) {
menuItem._command
.off("enabledStateChange", menuItem._enabledChanged)
.off("checkedStateChange", menuItem._checkedChanged)
.off("nameChange", menuItem._nameChanged)
.off("keyBindingAdded", menuItem._keyBindingAdded)
.off("keyBindingRemoved", menuItem._keyBindingRemoved);
}
/**
* Retrieves the MenuItem object for the corresponding id.
* @param {string} id
* @return {MenuItem}
*/
function getMenuItem(id) {
return menuItemMap[id];
}
function _getHTMLMenu(id) {
return $("#" + StringUtils.jQueryIdEscape(id)).get(0);
}
function _getHTMLMenuItem(id) {
return $("#" + StringUtils.jQueryIdEscape(id)).get(0);
}
function _addKeyBindingToMenuItem(commandID, $menuItem, key, displayKey) {
let $shortcut = $menuItem.find(".menu-shortcut");
if ($shortcut.length === 0) {
const html = KeyBindingManager.canAssignBinding(commandID) ?
"<span class='menu-shortcut' />" :
"<span class='menu-shortcut fixed-shortcut' />";
$shortcut = $(html);
$menuItem.append($shortcut);
}
$shortcut.data("key", key);
$shortcut.text(KeyBindingManager.formatKeyDescriptor(displayKey));
}
function _addExistingKeyBinding(menuItem) {
const commandID = menuItem.getCommand().getID();
let bindings = KeyBindingManager.getKeyBindings(commandID),
binding = null;
if (bindings.length > 0) {
// add the latest key binding
binding = bindings[bindings.length - 1];
_addKeyBindingToMenuItem(commandID, $(_getHTMLMenuItem(menuItem.id)), binding.key, binding.displayKey);
}
return binding;
}
let _menuDividerIDCount = 1;
function _getNextMenuItemDividerID() {
return "brackets-menuDivider-" + _menuDividerIDCount++;
}
// Help function for inserting elements into a list
function _insertInList($list, $element, position, $relativeElement) {
// Determine where to insert. Default is LAST.
let inserted = false;
if (position) {
// Adjust relative position for menu section positions since $relativeElement
// has already been resolved by _getRelativeMenuItem() to a menuItem
if (position === FIRST_IN_SECTION) {
position = BEFORE;
} else if (position === LAST_IN_SECTION) {
position = AFTER;
}
if (position === FIRST) {
$list.prepend($element);
inserted = true;
} else if ($relativeElement && $relativeElement.length > 0) {
if (position === AFTER) {
$relativeElement.after($element);
inserted = true;
} else if (position === BEFORE) {
$relativeElement.before($element);
inserted = true;
}
}
}
// Default to LAST
if (!inserted) {
$list.append($element);
}
}
/**
* MenuItem represents a single menu item that executes a Command or a menu divider. MenuItems
* may have a sub-menu. A MenuItem may correspond to an HTML-based
* menu item or a native menu item if Brackets is running in a native application shell
*
* Since MenuItems may have a native implementation clients should create MenuItems through
* addMenuItem() and should NOT construct a MenuItem object directly.
* Clients should also not access HTML content of a menu directly and instead use
* the MenuItem API to query and modify menus items.
*
* MenuItems are views on to Command objects so modify the underlying Command to modify the
* name, enabled, and checked state of a MenuItem. The MenuItem will update automatically
*
* @constructor
*
* @param {string} id
* @param {string|Command} command - the Command this MenuItem will reflect.
* Use DIVIDER to specify a menu divider
* @param [options]
* @param {boolean} options.hideWhenCommandDisabled will not show the menu item if command is disabled.
*/
function MenuItem(id, command, options = {}) {
this.id = id;
this.isDivider = (command === DIVIDER);
this.isNative = false;
if (!this.isDivider && command !== SUBMENU) {
// Bind event handlers
this._enabledChanged = this._enabledChanged.bind(this);
this._checkedChanged = this._checkedChanged.bind(this);
this._nameChanged = this._nameChanged.bind(this);
this._keyBindingAdded = this._keyBindingAdded.bind(this);
this._keyBindingRemoved = this._keyBindingRemoved.bind(this);
this._command = command;
this._hideWhenCommandDisabled = options.hideWhenCommandDisabled;
this._command
.on("enabledStateChange", this._enabledChanged)
.on("checkedStateChange", this._checkedChanged)
.on("nameChange", this._nameChanged)
.on("keyBindingAdded", this._keyBindingAdded)
.on("keyBindingRemoved", this._keyBindingRemoved);
}
}
/**
* Menu represents a top-level menu in the menu bar. A Menu may correspond to an HTML-based
* menu or a native menu if Brackets is running in a native application shell.
*
* Since menus may have a native implementation clients should create Menus through
* addMenu() and should NOT construct a Menu object directly.
* Clients should also not access HTML content of a menu directly and instead use
* the Menu API to query and modify menus.
*
* @constructor
*
* @param {string} id
*/
function Menu(id) {
this.id = id;
}
Menu.prototype._getMenuItemId = function (commandId) {
return (this.id + "-" + commandId);
};
/**
* Determine MenuItem in this Menu, that has the specified command
*
* @param {Command} command - the command to search for.
* @private
* @return {?HTMLLIElement} menu item list element
*/
Menu.prototype._getMenuItemForCommand = function (command) {
if (!command) {
return null;
}
let foundMenuItem = menuItemMap[this._getMenuItemId(command.getID())];
if (!foundMenuItem) {
return null;
}
return $(_getHTMLMenuItem(foundMenuItem.id)).closest("li");
};
/**
* Determine relative MenuItem
*
* @param {?string} relativeID - id of command (future: sub-menu).
* @param {?string} position - only needed when relativeID is a MenuSection
* @return {?HTMLLIElement} menu item list element
* @private
*/
Menu.prototype._getRelativeMenuItem = function (relativeID, position) {
let $relativeElement;
if (relativeID) {
if (position === FIRST_IN_SECTION || position === LAST_IN_SECTION) {
if (!relativeID.hasOwnProperty("sectionMarker")) {
console.error("Bad Parameter in _getRelativeMenuItem(): relativeID must be a MenuSection when position refers to a menu section");
return null;
}
// Determine the $relativeElement by traversing the sibling list and
// stop at the first divider found
// TODO: simplify using nextUntil()/prevUntil()
let $sectionMarker = this._getMenuItemForCommand(CommandManager.get(relativeID.sectionMarker));
if (!$sectionMarker) {
console.error("_getRelativeMenuItem(): MenuSection " + relativeID.sectionMarker +
" not found in Menu " + this.id);
return null;
}
let $listElem = $sectionMarker;
$relativeElement = $listElem;
while (true) {
$listElem = (position === FIRST_IN_SECTION ? $listElem.prev() : $listElem.next());
if ($listElem.length === 0) {
break;
} else if ($listElem.find(".divider").length > 0) {
break;
} else {
$relativeElement = $listElem;
}
}
} else {
if (relativeID.hasOwnProperty("sectionMarker")) {
console.error("Bad Parameter in _getRelativeMenuItem(): if relativeID is a MenuSection, position must be FIRST_IN_SECTION or LAST_IN_SECTION");
return null;
}
// handle FIRST, LAST, BEFORE, & AFTER
let command = CommandManager.get(relativeID);
if (command) {
// Lookup Command for this Command id
// Find MenuItem that has this command
$relativeElement = this._getMenuItemForCommand(command);
}
if (!$relativeElement) {
console.error("_getRelativeMenuItem(): MenuItem with Command id " + relativeID +
" not found in Menu " + this.id);
return null;
}
}
return $relativeElement;
} else if (position && position !== FIRST && position !== LAST) {
console.error("Bad Parameter in _getRelativeMenuItem(): relative position specified with no relativeID");
return null;
}
return $relativeElement;
};
/**
* Removes the specified menu item from this Menu. Key bindings are unaffected; use KeyBindingManager
* directly to remove key bindings if desired.
*
* @param {!string | Command} command - command the menu would execute if we weren't deleting it.
*/
Menu.prototype.removeMenuItem = function (command) {
let menuItemID,
commandID;
if (!command) {
console.error("removeMenuItem(): missing required parameters: command");
return;
}
if (typeof (command) === "string") {
let commandObj = CommandManager.get(command);
if (!commandObj) {
console.error("removeMenuItem(): command not found: " + command);
return;
}
commandID = command;
} else {
commandID = command.getID();
}
menuItemID = this._getMenuItemId(commandID);
let menuItem = getMenuItem(menuItemID);
removeMenuItemEventListeners(menuItem);
$(_getHTMLMenuItem(menuItemID)).parent().remove();
delete menuItemMap[menuItemID];
};
/**
* Removes the specified menu divider from this Menu.
*
* @param {!string} menuItemID - the menu item id of the divider to remove.
*/
Menu.prototype.removeMenuDivider = function (menuItemID) {
let menuItem,
$HTMLMenuItem;
if (!menuItemID) {
console.error("removeMenuDivider(): missing required parameters: menuItemID");
return;
}
menuItem = getMenuItem(menuItemID);
if (!menuItem) {
console.error("removeMenuDivider(): parameter menuItemID: %s is not a valid menu item id", menuItemID);
return;
}
if (!menuItem.isDivider) {
console.error("removeMenuDivider(): parameter menuItemID: %s is not a menu divider", menuItemID);
return;
}
// Targeting parent to get the menu divider <hr> and the <li> that contains it
$HTMLMenuItem = $(_getHTMLMenuItem(menuItemID)).parent();
if ($HTMLMenuItem) {
$HTMLMenuItem.remove();
} else {
console.error("removeMenuDivider(): HTML menu divider not found: %s", menuItemID);
return;
}
if (!menuItemMap[menuItemID]) {
console.error("removeMenuDivider(): menu divider not found in menuItemMap: %s", menuItemID);
return;
}
delete menuItemMap[menuItemID];
};
/**
* Adds a new menu item with the specified id and display text. The insertion position is
* specified via the relativeID and position arguments which describe a position
* relative to another MenuItem or MenuGroup. It is preferred that plug-ins
* insert new MenuItems relative to a menu section rather than a specific
* MenuItem (see Menu Section Constants).
*
* TODO: Sub-menus are not yet supported, but when they are implemented this API will
* allow adding new MenuItems to sub-menus as well.
*
* Note, keyBindings are bound to Command objects not MenuItems. The provided keyBindings
* will be bound to the supplied Command object rather than the MenuItem.
*
* @param {!string | Command} command - the command the menu will execute.
* Pass Menus.DIVIDER for a menu divider, or just call addMenuDivider() instead.
* @param {?(string | {key: string, platform: string[]})} [keyBindings] - Register one or more key bindings to associate with the supplied command
* @param {?string} [position] - constant defining the position of new MenuItem relative to
* other MenuItems. Values:
* - With no relativeID, use Menus.FIRST or LAST (default is LAST)
* - Relative to a command id, use BEFORE or AFTER (required)
* - Relative to a MenuSection, use FIRST_IN_SECTION or LAST_IN_SECTION (required)
* @param {?string} [relativeID] - command id OR one of the MenuSection.* constants. Required
* for all position constants except FIRST and LAST.
* @param [options]
* @param {boolean} options.hideWhenCommandDisabled will not show the menu item if command is disabled. Helps to
* clear the clutter on greyed out menu items if not applicable to context.
*
* @return {MenuItem} the newly created MenuItem
*/
Menu.prototype.addMenuItem = function (command, keyBindings, position, relativeID, options = {}) {
const self = this;
let id,
$menuItem,
menuItem,
name,
commandID;
if (!command) {
console.error("addMenuItem(): missing required parameters: command");
return null;
}
if (typeof (command) === "string") {
if (command === DIVIDER) {
name = DIVIDER;
commandID = _getNextMenuItemDividerID();
} else {
commandID = command;
command = CommandManager.get(commandID);
if (!command) {
console.error("addMenuItem(): commandID not found: " + commandID);
return null;
}
name = command.getName();
if (!allMenuCommands.has(commandID)) {
allMenuCommands.add(commandID);
}
}
} else {
commandID = command.getID();
name = command.getName();
if (!allMenuCommands.has(commandID)) {
allMenuCommands.add(commandID);
}
}
// Internal id is the a composite of the parent menu id and the command id.
id = self._getMenuItemId(commandID);
if (menuItemMap[id]) {
console.log("MenuItem added with same id of existing MenuItem: " + id);
return null;
}
// create MenuItem
menuItem = new MenuItem(id, command, {
hideWhenCommandDisabled: options.hideWhenCommandDisabled
});
menuItemMap[id] = menuItem;
if (name === DIVIDER) {
$menuItem = $("<li><hr class='divider' id='" + id + "' /></li>");
} else {
// Create the HTML Menu
let keyboardIcon = '';
if (KeyBindingManager.canAssignBinding(commandID)) {
keyboardIcon = `<span class='keyboard-icon' title='${Strings.KEYBOARD_SHORTCUT_CHANGE_TITLE}'><i class="fa-regular fa-keyboard"></i></span>`;
}
$menuItem = $("<li><a href='#' class='menuAnchor' id='" + id + "'> <span class='menu-name'></span>" +
`<span class='right-pusher'></span>${keyboardIcon}` +
"</a></li>");
const $menuAnchor = $menuItem.find(".menuAnchor");
$menuItem.find(".keyboard-icon").on("click", (event) => {
KeyBindingManager.showShortcutSelectionDialog(command);
event.preventDefault();
event.stopPropagation();
});
$menuItem.on("click", function (event) {
if ($menuAnchor.hasClass('disabled')) {
event.preventDefault();
event.stopPropagation();
return true;
}
Metrics.countEvent(Metrics.EVENT_TYPE.UI_MENU, "click", menuItem._command.getID());
logger.leaveTrail("UI Menu Click: " + menuItem._command.getID());
MainViewManager.focusActivePane();
if (menuItem._command._options.eventSource) {
menuItem._command.execute({
eventSource: CommandManager.SOURCE_UI_MENU_CLICK,
sourceType: self.id
});
} else {
menuItem._command.execute();
}
});
let self = this;
$menuItem.on("mouseenter", function () {
// This is to prevent size jumps when the keyboard
// icon hides and shows as selection changes
$menuAnchor.addClass("use-invisible-for-width-compute");
const currentWidth = $(this).width(); // Get the current width
$menuAnchor.removeClass("use-invisible-for-width-compute");
$(this).css('min-width', currentWidth + 'px');
self.closeSubMenu();
// now show selection
$menuItem.parent().find(".menuAnchor").removeClass("selected");
if (!$menuAnchor.hasClass('disabled')) {
$menuAnchor.addClass("selected");
}
});
$menuItem.on("mouseleave", function () {
$(this).css('min-width', '');
self.closeSubMenu();
$menuItem.find(".menuAnchor").removeClass("selected");
});
}
// Insert menu item
let $relativeElement = this._getRelativeMenuItem(relativeID, position);
_insertInList($("li#" + StringUtils.jQueryIdEscape(this.id) + " > ul.dropdown-menu"),
$menuItem, position, $relativeElement);
// Initialize MenuItem state
if (menuItem.isDivider) {
menuItem.dividerId = commandID;
} else {
if (keyBindings) {
// Add key bindings. The MenuItem listens to the Command object to update MenuItem DOM with shortcuts.
if (!Array.isArray(keyBindings)) {
keyBindings = [keyBindings];
}
}
// Note that keyBindings passed during MenuItem creation take precedent over any existing key bindings
KeyBindingManager.addBinding(commandID, keyBindings);
// Look for existing key bindings
_addExistingKeyBinding(menuItem);
menuItem._checkedChanged();
menuItem._enabledChanged();
menuItem._nameChanged();
}
const menuId = self.id;
exports.trigger(EVENT_MENU_ITEM_ADDED, menuId, commandID, menuItem);
return menuItem;
};
/**
* Inserts divider item in menu.
* @param {?string} position - constant defining the position of new the divider relative
* to other MenuItems. Default is LAST. (see Insertion position constants).
* @param {?string} relativeID - id of menuItem, sub-menu, or menu section that the new
* divider will be positioned relative to. Required for all position constants
* except FIRST and LAST
*
* @return {MenuItem} the newly created divider
*/
Menu.prototype.addMenuDivider = function (position, relativeID) {
return this.addMenuItem(DIVIDER, "", position, relativeID);
};
/**
* NOT IMPLEMENTED
*
* All properties are required unless noted as optional.
*
* @param {Array<{id: string, command: string | Command, bindings: string | Array<{key: string, platform: string}> | undefined}>} jsonStr
* @param {?string} position - constant defining the position of the new MenuItem relative
* to other MenuItems. Default is LAST. (see Insertion position constants).
* @param {?string} relativeID - id of menuItem, sub-menu, or menu section that the new
* menuItem will be positioned relative to. Required when position is
* AFTER or BEFORE, ignored when position is FIRST or LAST.
*
* @return {MenuItem} the newly created MenuItem
* @private
*/
// Menu.prototype.createMenuItemsFromJSON = function (jsonStr, position, relativeID) {
// NOT IMPLEMENTED
// };
/**
* NOT IMPLEMENTED
* @param {!string} text displayed in menu item
* @param {!string} id
* @param {?string} position - constant defining the position of new the MenuItem relative
* to other MenuItems. Default is LAST. (see Insertion position constants)
* @param {?string} relativeID - id of menuItem, sub-menu, or menu section that the new
* menuItem will be positioned relative to. Required when position is
* AFTER or BEFORE, ignored when position is FIRST or LAST.
*
* @return {MenuItem} newly created menuItem for sub-menu
* @private
*/
// MenuItem.prototype.createSubMenu = function (text, id, position, relativeID) {
// NOT IMPLEMENTED
// };
/**
*
* Creates a new submenu and a menuItem and adds the menuItem of the submenu
* to the menu and returns the submenu.
*
* A submenu will have the same structure of a menu with a additional field
* parentMenuItem which has the reference of the submenu's parent menuItem.
* A submenu will raise the following events:
* - beforeSubMenuOpen
* - beforeSubMenuClose
*
* Note, This function will create only a context submenu.
*
* TODO: Make this function work for Menus
*
*
* @param {!string} name displayed in menu item of the submenu
* @param {!string} id
* @param {?string} position - constant defining the position of new MenuItem of the submenu relative to
* other MenuItems. Values:
* - With no relativeID, use Menus.FIRST or LAST (default is LAST)
* - Relative to a command id, use BEFORE or AFTER (required)
* - Relative to a MenuSection, use FIRST_IN_SECTION or LAST_IN_SECTION (required)
* @param {?string} relativeID - command id OR one of the MenuSection.* constants. Required
* for all position constants except FIRST and LAST.
*
* @return {Menu} the newly created submenu
*/
Menu.prototype.addSubMenu = function (name, id, position, relativeID) {
if (!name || !id) {
console.error("addSubMenu(): missing required parameters: name and id");
return null;
}
// Guard against duplicate context menu ids
if (contextMenuMap[id]) {
console.log("Context menu added with id of existing Context Menu: " + id);
return null;
}
let menu = new ContextMenu(id);
contextMenuMap[id] = menu;
let menuItemID = this.id + "-" + id;
if (menuItemMap[menuItemID]) {
console.log("MenuItem added with same id of existing MenuItem: " + id);
return null;
}
// create MenuItem
let menuItem = new MenuItem(menuItemID, SUBMENU);
menuItemMap[menuItemID] = menuItem;
subMenuItemMap[menuItemID] = menu;
menu.parentMenuItem = menuItem;
// create MenuItem DOM
// Create the HTML MenuItem
let $menuItem = $("<li><a class='sub-menu-item menuAnchor' href='#' id='" + menuItemID + "'> " +
"<span class='menu-name'>" + name + "</span>" +
"<span class='right-pusher'></span>" +
"<span>▸</span>" +
"</a></li>");
const $menuAnchor = $menuItem.find(".menuAnchor");
let self = this;
$menuItem.on("mouseenter", function (e) {
$menuAnchor.addClass("use-invisible-for-width-compute");
const currentWidth = $(this).width(); // Get the current width
$menuAnchor.removeClass("use-invisible-for-width-compute");
$(this).css('min-width', currentWidth + 'px'); // Set min-width to the current width
if (self.openSubMenu && self.openSubMenu.id === menu.id) {
return;
}
self.closeSubMenu();
self.openSubMenu = menu;
menu.open();
$menuItem.parent().find(".menuAnchor").removeClass("selected");
$menuItem.find(".menuAnchor").addClass("selected");
});
$menuItem.on("mouseleave", function () {
$(this).css('min-width', '');
$menuItem.find(".menuAnchor").removeClass("selected");
});
// Insert menu item
let $relativeElement = this._getRelativeMenuItem(relativeID, position);
_insertInList($("li#" + StringUtils.jQueryIdEscape(this.id) + " > ul.dropdown-menu"),
$menuItem, position, $relativeElement);
exports.trigger(EVENT_SUB_MENU_ADDED, id, menu);
return menu;
};
/**
* Removes the specified submenu from this Menu.
*
* Note, this function will only remove context submenus
*
* TODO: Make this function work for Menus
*
* @param {!string} subMenuID - the menu id of the submenu to remove.
*/
Menu.prototype.removeSubMenu = function (subMenuID) {
let subMenu,
parentMenuItem,
commandID = "";
if (!subMenuID) {
console.error("removeSubMenu(): missing required parameters: subMenuID");
return;
}
subMenu = getContextMenu(subMenuID);
if (!subMenu || !subMenu.parentMenuItem) {
console.error("removeSubMenu(): parameter subMenuID: %s is not a valid submenu id", subMenuID);
return;
}
parentMenuItem = subMenu.parentMenuItem;
if (!menuItemMap[parentMenuItem.id]) {
console.error("removeSubMenu(): parent menuItem not found in menuItemMap: %s", parentMenuItem.id);
return;
}
// Remove all of the menu items in the submenu
_.forEach(menuItemMap, function (value, key) {
if (_.startsWith(key, subMenuID)) {
if (value.isDivider) {
subMenu.removeMenuDivider(key);
} else {
commandID = value.getCommand();
subMenu.removeMenuItem(commandID);
}
}