forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMenus.js
More file actions
1 lines (1 loc) · 29.9 KB
/
Menus.js
File metadata and controls
1 lines (1 loc) · 29.9 KB
1
define(function(require,exports,module){let _=require("thirdparty/lodash"),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");require("utils/Global");const KEY=Keys.KEY,allMenuCommands=new Set;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"},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"},SubMenuIds={GIT_SUB_MENU:"git-submenu"};const EVENT_BEFORE_CONTEXT_MENU_OPEN="beforeContextMenuOpen",EVENT_BEFORE_CONTEXT_MENU_CLOSE="beforeContextMenuClose",EVENT_BEFORE_SUB_MENU_OPEN="beforeSubMenuOpen",EVENT_BEFORE_SUB_MENU_CLOSE="beforeSubMenuClose",EVENT_MENU_ADDED="menuAdded",EVENT_SUB_MENU_ADDED="subMenuAdded",EVENT_MENU_ITEM_ADDED="menuItemAdded",FILE_OPEN_CLOSE_COMMANDS={sectionMarker:Commands.FILE_NEW},FILE_SAVE_COMMANDS={sectionMarker:Commands.FILE_SAVE},FILE_LIVE={sectionMarker:Commands.FILE_LIVE_FILE_PREVIEW},FILE_SETTINGS={sectionMarker:Commands.FILE_EXTENSION_MANAGER},FILE_EXTENSION_MANAGER={sectionMarker:Commands.FILE_EXTENSION_MANAGER},EDIT_UNDO_REDO_COMMANDS={sectionMarker:Commands.EDIT_UNDO},EDIT_TEXT_COMMANDS={sectionMarker:Commands.EDIT_CUT},EDIT_SELECTION_COMMANDS={sectionMarker:Commands.EDIT_SELECT_ALL},EDIT_MODIFY_SELECTION={sectionMarker:Commands.EDIT_INDENT},EDIT_COMMENT_SELECTION={sectionMarker:Commands.EDIT_LINE_COMMENT},EDIT_CODE_HINTS_COMMANDS={sectionMarker:Commands.SHOW_CODE_HINTS},EDIT_TOGGLE_OPTIONS={sectionMarker:Commands.TOGGLE_CLOSE_BRACKETS},FIND_FIND_COMMANDS={sectionMarker:Commands.CMD_FIND},FIND_FIND_IN_COMMANDS={sectionMarker:Commands.CMD_FIND_IN_FILES},FIND_REPLACE_COMMANDS={sectionMarker:Commands.CMD_REPLACE},VIEW_HIDESHOW_COMMANDS={sectionMarker:Commands.VIEW_HIDE_SIDEBAR},VIEW_FONTSIZE_COMMANDS={sectionMarker:Commands.VIEW_ZOOM_SUBMENU},VIEW_TOGGLE_OPTIONS={sectionMarker:Commands.TOGGLE_ACTIVE_LINE},NAVIGATE_GOTO_COMMANDS={sectionMarker:Commands.NAVIGATE_QUICK_OPEN},NAVIGATE_DOCUMENTS_COMMANDS={sectionMarker:Commands.NAVIGATE_NEXT_DOC},NAVIGATE_OS_COMMANDS={sectionMarker:Commands.NAVIGATE_SHOW_IN_FILE_TREE},NAVIGATE_QUICK_EDIT_COMMANDS={sectionMarker:Commands.TOGGLE_QUICK_EDIT},NAVIGATE_QUICK_DOCS_COMMANDS={sectionMarker:Commands.TOGGLE_QUICK_DOCS},MenuSection={FILE_OPEN_CLOSE_COMMANDS:FILE_OPEN_CLOSE_COMMANDS,FILE_SAVE_COMMANDS:FILE_SAVE_COMMANDS,FILE_LIVE:FILE_LIVE,FILE_SETTINGS:FILE_SETTINGS,FILE_EXTENSION_MANAGER:FILE_EXTENSION_MANAGER,EDIT_UNDO_REDO_COMMANDS:EDIT_UNDO_REDO_COMMANDS,EDIT_TEXT_COMMANDS:EDIT_TEXT_COMMANDS,EDIT_SELECTION_COMMANDS:EDIT_SELECTION_COMMANDS,EDIT_MODIFY_SELECTION:EDIT_MODIFY_SELECTION,EDIT_COMMENT_SELECTION:EDIT_COMMENT_SELECTION,EDIT_CODE_HINTS_COMMANDS:EDIT_CODE_HINTS_COMMANDS,EDIT_TOGGLE_OPTIONS:EDIT_TOGGLE_OPTIONS,FIND_FIND_COMMANDS:FIND_FIND_COMMANDS,FIND_FIND_IN_COMMANDS:FIND_FIND_IN_COMMANDS,FIND_REPLACE_COMMANDS:FIND_REPLACE_COMMANDS,VIEW_HIDESHOW_COMMANDS:VIEW_HIDESHOW_COMMANDS,VIEW_FONTSIZE_COMMANDS:VIEW_FONTSIZE_COMMANDS,VIEW_TOGGLE_OPTIONS:VIEW_TOGGLE_OPTIONS,NAVIGATE_GOTO_COMMANDS:NAVIGATE_GOTO_COMMANDS,NAVIGATE_DOCUMENTS_COMMANDS:NAVIGATE_DOCUMENTS_COMMANDS,NAVIGATE_OS_COMMANDS:NAVIGATE_OS_COMMANDS,NAVIGATE_QUICK_EDIT_COMMANDS:NAVIGATE_QUICK_EDIT_COMMANDS,NAVIGATE_QUICK_DOCS_COMMANDS:NAVIGATE_QUICK_DOCS_COMMANDS},BEFORE="before",AFTER="after",FIRST="first",LAST="last",FIRST_IN_SECTION="firstInSection",LAST_IN_SECTION="lastInSection";let DIVIDER="---",SUBMENU="SUBMENU",menuMap={},contextMenuMap={},menuItemMap={},subMenuItemMap={};function getMenu(id){return menuMap[id]}function getSubMenu(id){return getContextMenu(id)}function getAllMenuItemCommands(){return new Set(allMenuCommands)}function getAllMenus(){return menuMap}function getContextMenu(id){return contextMenuMap[id]}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)}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(0===$shortcut.length){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;return bindings.length>0&&(binding=bindings[bindings.length-1],_addKeyBindingToMenuItem(commandID,$(_getHTMLMenuItem(menuItem.id)),binding.key,binding.displayKey)),binding}let _menuDividerIDCount=1;function _getNextMenuItemDividerID(){return"brackets-menuDivider-"+_menuDividerIDCount++}function _insertInList($list,$element,position,$relativeElement){let inserted=!1;position&&(position===FIRST_IN_SECTION?position=BEFORE:position===LAST_IN_SECTION&&(position=AFTER),position===FIRST?($list.prepend($element),inserted=!0):$relativeElement&&$relativeElement.length>0&&(position===AFTER?($relativeElement.after($element),inserted=!0):position===BEFORE&&($relativeElement.before($element),inserted=!0))),inserted||$list.append($element)}function MenuItem(id,command,options={}){this.id=id,this.isDivider=command===DIVIDER,this.isNative=!1,this.isDivider||command===SUBMENU||(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))}function Menu(id){this.id=id}Menu.prototype._getMenuItemId=function(commandId){return this.id+"-"+commandId},Menu.prototype._getMenuItemForCommand=function(command){if(!command)return null;let foundMenuItem=menuItemMap[this._getMenuItemId(command.getID())];return foundMenuItem?$(_getHTMLMenuItem(foundMenuItem.id)).closest("li"):null},Menu.prototype._getRelativeMenuItem=function(relativeID,position){let $relativeElement;if(relativeID){if(position===FIRST_IN_SECTION||position===LAST_IN_SECTION){if(!relativeID.hasOwnProperty("sectionMarker"))return console.error("Bad Parameter in _getRelativeMenuItem(): relativeID must be a MenuSection when position refers to a menu section"),null;let $sectionMarker=this._getMenuItemForCommand(CommandManager.get(relativeID.sectionMarker));if(!$sectionMarker)return console.error("_getRelativeMenuItem(): MenuSection "+relativeID.sectionMarker+" not found in Menu "+this.id),null;let $listElem=$sectionMarker;for($relativeElement=$listElem;0!==($listElem=position===FIRST_IN_SECTION?$listElem.prev():$listElem.next()).length&&!($listElem.find(".divider").length>0);)$relativeElement=$listElem}else{if(relativeID.hasOwnProperty("sectionMarker"))return console.error("Bad Parameter in _getRelativeMenuItem(): if relativeID is a MenuSection, position must be FIRST_IN_SECTION or LAST_IN_SECTION"),null;let command=CommandManager.get(relativeID);if(command&&($relativeElement=this._getMenuItemForCommand(command)),!$relativeElement)return console.error("_getRelativeMenuItem(): MenuItem with Command id "+relativeID+" not found in Menu "+this.id),null}return $relativeElement}return position&&position!==FIRST&&position!==LAST?(console.error("Bad Parameter in _getRelativeMenuItem(): relative position specified with no relativeID"),null):$relativeElement},Menu.prototype.removeMenuItem=function(command){let menuItemID,commandID,menuItem;if(command){if("string"==typeof command){let commandObj;if(!CommandManager.get(command))return void console.error("removeMenuItem(): command not found: "+command);commandID=command}else commandID=command.getID();removeMenuItemEventListeners(getMenuItem(menuItemID=this._getMenuItemId(commandID))),$(_getHTMLMenuItem(menuItemID)).parent().remove(),delete menuItemMap[menuItemID]}else console.error("removeMenuItem(): missing required parameters: command")},Menu.prototype.removeMenuDivider=function(menuItemID){let menuItem,$HTMLMenuItem;menuItemID?(menuItem=getMenuItem(menuItemID))?menuItem.isDivider?($HTMLMenuItem=$(_getHTMLMenuItem(menuItemID)).parent())?($HTMLMenuItem.remove(),menuItemMap[menuItemID]?delete menuItemMap[menuItemID]:console.error("removeMenuDivider(): menu divider not found in menuItemMap: %s",menuItemID)):console.error("removeMenuDivider(): HTML menu divider not found: %s",menuItemID):console.error("removeMenuDivider(): parameter menuItemID: %s is not a menu divider",menuItemID):console.error("removeMenuDivider(): parameter menuItemID: %s is not a valid menu item id",menuItemID):console.error("removeMenuDivider(): missing required parameters: menuItemID")},Menu.prototype.addMenuItem=function(command,keyBindings,position,relativeID,options={}){const self=this;let id,$menuItem,menuItem,name,commandID;if(!command)return console.error("addMenuItem(): missing required parameters: command"),null;if("string"==typeof command)if(command===DIVIDER)name=DIVIDER,commandID=_getNextMenuItemDividerID();else{if(commandID=command,!(command=CommandManager.get(commandID)))return console.error("addMenuItem(): commandID not found: "+commandID),null;name=command.getName(),allMenuCommands.has(commandID)||allMenuCommands.add(commandID)}else commandID=command.getID(),name=command.getName(),allMenuCommands.has(commandID)||allMenuCommands.add(commandID);if(id=this._getMenuItemId(commandID),menuItemMap[id])return console.log("MenuItem added with same id of existing MenuItem: "+id),null;if(menuItem=new MenuItem(id,command,{hideWhenCommandDisabled:options.hideWhenCommandDisabled}),menuItemMap[id]=menuItem,name===DIVIDER)$menuItem=$("<li><hr class='divider' id='"+id+"' /></li>");else{let keyboardIcon="";KeyBindingManager.canAssignBinding(commandID)&&(keyboardIcon=`<span class='keyboard-icon' title='${Strings.KEYBOARD_SHORTCUT_CHANGE_TITLE}'><i class="fa-regular fa-keyboard"></i></span>`);const $menuAnchor=($menuItem=$("<li><a href='#' class='menuAnchor' id='"+id+"'> <span class='menu-name'></span>"+`<span class='right-pusher'></span>${keyboardIcon}`+"</a></li>")).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"))return event.preventDefault(),event.stopPropagation(),!0;Metrics.countEvent(Metrics.EVENT_TYPE.UI_MENU,"click",menuItem._command.getID()),logger.leaveTrail("UI Menu Click: "+menuItem._command.getID()),MainViewManager.focusActivePane();const commandId=menuItem._command.getID();commandId===Commands.FILE_SAVE||commandId===Commands.FILE_SAVE_AS||commandId===Commands.FILE_SAVE_ALL?CommandManager.execute(commandId):menuItem._command._options.eventSource?menuItem._command.execute({eventSource:CommandManager.SOURCE_UI_MENU_CLICK,sourceType:self.id}):menuItem._command.execute()});let self=this;$menuItem.on("mouseenter",function(){$menuAnchor.addClass("use-invisible-for-width-compute");const currentWidth=$(this).width();$menuAnchor.removeClass("use-invisible-for-width-compute"),$(this).css("min-width",currentWidth+"px"),self.closeSubMenu(),$menuItem.parent().find(".menuAnchor").removeClass("selected"),$menuAnchor.hasClass("disabled")||$menuAnchor.addClass("selected")}),$menuItem.on("mouseleave",function(){$(this).css("min-width",""),self.closeSubMenu(),$menuItem.find(".menuAnchor").removeClass("selected")})}let $relativeElement=this._getRelativeMenuItem(relativeID,position);_insertInList($("li#"+StringUtils.jQueryIdEscape(this.id)+" > ul.dropdown-menu"),$menuItem,position,$relativeElement),menuItem.isDivider?menuItem.dividerId=commandID:(keyBindings&&(Array.isArray(keyBindings)||(keyBindings=[keyBindings])),KeyBindingManager.addBinding(commandID,keyBindings),_addExistingKeyBinding(menuItem),menuItem._checkedChanged(),menuItem._enabledChanged(),menuItem._nameChanged());const menuId=this.id;return exports.trigger("menuItemAdded",menuId,commandID,menuItem),menuItem},Menu.prototype.addMenuDivider=function(position,relativeID){return this.addMenuItem(DIVIDER,"",position,relativeID)},Menu.prototype.addSubMenu=function(name,id,position,relativeID){if(!name||!id)return console.error("addSubMenu(): missing required parameters: name and id"),null;if(contextMenuMap[id])return console.log("Context menu added with id of existing Context Menu: "+id),null;let menu=new ContextMenu(id);contextMenuMap[id]=menu;let menuItemID=this.id+"-"+id;if(menuItemMap[menuItemID])return console.log("MenuItem added with same id of existing MenuItem: "+id),null;let menuItem=new MenuItem(menuItemID,SUBMENU);menuItemMap[menuItemID]=menuItem,subMenuItemMap[menuItemID]=menu,menu.parentMenuItem=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();$menuAnchor.removeClass("use-invisible-for-width-compute"),$(this).css("min-width",currentWidth+"px"),self.openSubMenu&&self.openSubMenu.id===menu.id||(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")});let $relativeElement=this._getRelativeMenuItem(relativeID,position);return _insertInList($("li#"+StringUtils.jQueryIdEscape(this.id)+" > ul.dropdown-menu"),$menuItem,position,$relativeElement),exports.trigger("subMenuAdded",id,menu),menu},Menu.prototype.removeSubMenu=function(subMenuID){let subMenu,parentMenuItem,commandID="";subMenuID?(subMenu=getContextMenu(subMenuID))&&subMenu.parentMenuItem?(parentMenuItem=subMenu.parentMenuItem,menuItemMap[parentMenuItem.id]?(_.forEach(menuItemMap,function(value,key){_.startsWith(key,subMenuID)&&(value.isDivider?subMenu.removeMenuDivider(key):(commandID=value.getCommand(),subMenu.removeMenuItem(commandID)))}),$(_getHTMLMenuItem(parentMenuItem.id)).parent().remove(),$(_getHTMLMenu(subMenuID)).remove(),delete menuItemMap[parentMenuItem.id],delete subMenuItemMap[parentMenuItem.id],delete contextMenuMap[subMenuID]):console.error("removeSubMenu(): parent menuItem not found in menuItemMap: %s",parentMenuItem.id)):console.error("removeSubMenu(): parameter subMenuID: %s is not a valid submenu id",subMenuID):console.error("removeSubMenu(): missing required parameters: subMenuID")},Menu.prototype.closeSubMenu=function(){this.openSubMenu&&(this.openSubMenu.close(),this.openSubMenu=null)},MenuItem.prototype.getCommand=function(){return this._command},MenuItem.prototype.getParentMenu=function(){let parent=$(_getHTMLMenuItem(this.id)).parents(".dropdown").get(0);return parent?getMenu(parent.id):null},MenuItem.prototype._checkedChanged=function(){let checked=!!this._command.getChecked();if(this.isNative){let enabled=!!this._command.getEnabled(),command=this._command;brackets.app.setMenuItemState(this._command.getID(),enabled,checked,function(err){err&&console.log("Error setting menu item checked state for "+command+": "+err)})}else ViewUtils.toggleClass($(_getHTMLMenuItem(this.id)),"checked",checked)},MenuItem.prototype._enabledChanged=function(){if(this.isNative){let enabled=!!this._command.getEnabled(),checked=!!this._command.getChecked(),command=this._command;brackets.app.setMenuItemState(this._command.getID(),enabled,checked,function(err){err&&console.log("Error setting menu item enabled state for "+command+": "+err)})}else ViewUtils.toggleClass($(_getHTMLMenuItem(this.id)),"disabled",!this._command.getEnabled()),this._hideWhenCommandDisabled&&ViewUtils.toggleClass($(_getHTMLMenuItem(this.id)),"forced-hidden",!this._command.getEnabled())},MenuItem.prototype._nameChanged=function(){if(this.isNative){let command=this._command;brackets.app.setMenuTitle(this._command.getID(),this._command.getName(),function(err){err&&console.log("Error setting menu title for "+command+": "+err)})}else{const htmlName=this._command.getOptions().htmlName;if(htmlName)return void $(_getHTMLMenuItem(this.id)).find(".menu-name").html(htmlName);$(_getHTMLMenuItem(this.id)).find(".menu-name").text(this._command.getName())}},MenuItem.prototype._keyBindingAdded=function(event,keyBinding){if(this.isNative){let shortcutKey=keyBinding.displayKey||keyBinding.key,command=this._command;brackets.app.setMenuItemShortcut(this._command.getID(),shortcutKey,KeyBindingManager.formatKeyDescriptor(shortcutKey),function(err){err&&console.error("Error setting menu item shortcut key "+shortcutKey+", "+command+" : "+err)})}else _addKeyBindingToMenuItem(this._command.getID(),$(_getHTMLMenuItem(this.id)),keyBinding.key,keyBinding.displayKey)},MenuItem.prototype._keyBindingRemoved=function(event,keyBinding){if(this.isNative){let shortcutKey=keyBinding.displayKey||keyBinding.key,command=this._command;brackets.app.setMenuItemShortcut(this._command.getID(),"","",function(err){err&&console.error("Error setting menu item shortcut: "+err,shortcutKey,command)})}else{let $shortcut=$(_getHTMLMenuItem(this.id)).find(".menu-shortcut");$shortcut.length>0&&$shortcut.data("key")===keyBinding.key&&null===_addExistingKeyBinding(this)&&$shortcut.empty()}};let lastOpenedMenuID="file-menu";function closeAll(){const $openDropdownMenuList=$("#titlebar .dropdown.open");$openDropdownMenuList.length&&MainViewManager.focusActivePane(),getOpenMenu()&&(lastOpenedMenuID=getOpenMenu()),$(".dropdown").removeClass("open")}function _closeAllSubMenus(){for(let menu of Object.values(menuMap))menu.closeSubMenu()}function openMenu(id){if(id||(id=lastOpenedMenuID),!menuMap[id])return console.error("openMenu- no such menu: "+id),null;$(`#${getDropdownToggleMenuID(id)}`).click()}function getOpenMenu(){const $openDropdownMenuList=$("#titlebar .dropdown.open");return 1!==$openDropdownMenuList.length?null:$openDropdownMenuList[0].id}function getDropdownToggleMenuID(id){return`${id}-dropdown-toggle`}let assignedShortcutsMenus={},altKeyReleased=!0;function _addAltMenuShortcut(menuName,id){if("mac"===brackets.platform)return;let shortCutKey=menuName[0].toUpperCase();if(assignedShortcutsMenus[shortCutKey]){assignedShortcutsMenus[shortCutKey].push({menuName:menuName,id:id});const newShortcutList=[];for(let menu of assignedShortcutsMenus[shortCutKey])menu.menuName.toUpperCase().startsWith(shortCutKey)&&newShortcutList.push(menu);assignedShortcutsMenus[shortCutKey]=newShortcutList;const secondLetterShortCutKey=menuName[1];if(secondLetterShortCutKey&&assignedShortcutsMenus[secondLetterShortCutKey])return;shortCutKey=secondLetterShortCutKey}assignedShortcutsMenus[shortCutKey]=[{menuName:menuName,id:id}];const menuShortcutCommandID=`AltMenu-${shortCutKey}`;console.log(`Registering 'Alt-${shortCutKey}' menu shortcut handler..`),CommandManager.register(`Menu Shortcut For ${shortCutKey}`,menuShortcutCommandID,function(){const menusIdsForShortcut=assignedShortcutsMenus[shortCutKey].map(item=>item.id);if(altKeyReleased)return altKeyReleased=!1,openMenu(menusIdsForShortcut[0]),!0;let currentIndex=menusIdsForShortcut.indexOf(lastOpenedMenuID),menuToOpen;return openMenu(menuToOpen=-1===currentIndex||currentIndex===menusIdsForShortcut.length-1?menusIdsForShortcut[0]:menusIdsForShortcut[currentIndex+1]),!0}),KeyBindingManager.addBinding(menuShortcutCommandID,`Alt-${shortCutKey}`,null,{isMenuShortcut:!0})}function addMenu(name,id,position,relativeID){name=_.escape(name);let $menubar=$("#titlebar .nav"),menu;if(!name||!id)return console.error("call to addMenu() is missing required parameters"),null;if(menuMap[id])return console.log("Menu added with same name and id of existing Menu: "+id),null;menu=new Menu(id),menuMap[id]=menu;let $toggle=$(`<a id="${getDropdownToggleMenuID(id)}" href='#' class='dropdown-toggle' data-toggle='dropdown'>${name}</a>`),$popUp=$("<ul class='dropdown-menu'></ul>"),$dropdown,$newMenu=$("<li class='dropdown' id='"+id+"'></li>").append($toggle).append($popUp),$relativeElement;return $toggle.on("mouseenter",function(){_closeAllSubMenus();const $this=$(this);$("#titlebar, #titlebar *").is(":focus")?$this.addClass("selected").focus():$this.addClass("selected")}),$toggle.on("mouseleave",function(){$(this).removeClass("selected")}),_insertInList($menubar,$newMenu,position,relativeID&&$(_getHTMLMenu(relativeID))),PopUpManager.addPopUp($popUp,closeAll,!1),_addAltMenuShortcut(name,id),exports.trigger(EVENT_MENU_ADDED,id,menu),menu}function _switchMenus($menuDropdownToggle,event){$menuDropdownToggle.parent().removeClass("open");const menuID=$menuDropdownToggle.parent().get(0).id,mainMenu=menuMap[menuID],$dropdownToggles=$("#titlebar .dropdown-toggle");let currentIndex=$dropdownToggles.index($menuDropdownToggle),nextIndex=currentIndex=event.key===KEY.ARROW_LEFT?currentIndex-1:currentIndex+1;nextIndex<0?nextIndex=0:nextIndex>=$dropdownToggles.length&&(nextIndex=$dropdownToggles.length-1);const $nextDropdownToggle=$dropdownToggles.eq(nextIndex);$nextDropdownToggle.parent().addClass("open"),$nextDropdownToggle.focus(),lastOpenedMenuID=$nextDropdownToggle.parent()[0].id,mainMenu&&mainMenu.closeSubMenu()}function _switchMenuItems($menuDropdownToggle,event){const menuID=$menuDropdownToggle.parent().get(0).id,$dropdownMenu=$menuDropdownToggle.parent().find(".dropdown-menu"),$selected=$dropdownMenu.find("li a.selected"),currentWidth=$dropdownMenu.width();if($dropdownMenu.css("min-width",currentWidth+"px"),0===$selected.length)$dropdownMenu.find("li a").first().addClass("selected");else{let $next;if($selected.removeClass("selected"),event.key===KEY.ARROW_DOWN){let $nextLi=$selected.closest("li").next("li");for($next=$nextLi.find("a");(0===$next.length||$next.hasClass("disabled")||!$next.is(":visible"))&&$nextLi.length;)$next=($nextLi=$nextLi.next("li")).find("a");0===$next.length&&($next=$dropdownMenu.find("li a").first())}else if(event.key===KEY.ARROW_UP){let $prevLi=$selected.closest("li").prev("li");for($next=$prevLi.find("a");(0===$next.length||$next.hasClass("disabled")||!$next.is(":visible"))&&$prevLi.length;)$next=($prevLi=$prevLi.prev("li")).find("a");0===$next.length&&($next=$dropdownMenu.find("li a").last())}$next.addClass("selected");const mainMenu=menuMap[menuID];if($next.hasClass("sub-menu-item")){const submenuID=$next.get(0).id,submenu=subMenuItemMap[submenuID];submenu&&(mainMenu.closeSubMenu(),mainMenu.openSubMenu=submenu,submenu.open())}else mainMenu.closeSubMenu()}}function _execMenuItem($menuDropdownToggle,event){const $dropdownMenu=$menuDropdownToggle.parent().find(".dropdown-menu"),$selected=$dropdownMenu.find("li a.selected");if(1===$selected.length&&$dropdownMenu.is(":visible"))return MainViewManager.focusActivePane(),$selected.click(),event.preventDefault(),event.stopPropagation(),!0}function menuKeyboardNavigationHandler(event){const allowedKeys=[KEY.ARROW_LEFT,KEY.ARROW_RIGHT,KEY.ARROW_UP,KEY.ARROW_DOWN,KEY.ESCAPE,KEY.ENTER,KEY.RETURN];if(allowedKeys.includes(event.key)&&$("#titlebar, #titlebar *").is(":focus")){if(event.key===KEY.ESCAPE)return MainViewManager.focusActivePane(),event.preventDefault(),void event.stopPropagation();const $focusedElement=$(":focus"),isDescendantOfTitleBar=$focusedElement.closest("#titlebar").length>0;if(!isDescendantOfTitleBar)return;if($focusedElement.hasClass("dropdown-toggle")){if(event.key===KEY.ARROW_LEFT||event.key===KEY.ARROW_RIGHT)return _switchMenus($focusedElement,event);if(event.key===KEY.ARROW_UP||event.key===KEY.ARROW_DOWN)return _switchMenuItems($focusedElement,event);if(event.key===KEY.ENTER||event.key===KEY.RETURN||event.key===KEY.SPACE)return _execMenuItem($focusedElement,event)}}}function removeMenu(id){let menu,commandID="";id?menuMap[id]?(menu=getMenu(id),_.forEach(menuItemMap,function(value,key){_.startsWith(key,id)&&(value.isDivider?menu.removeMenuDivider(key):(commandID=value.getCommand(),menu.removeMenuItem(commandID)))}),$(_getHTMLMenu(id)).remove(),delete menuMap[id]):console.error("removeMenu(): menu id not found: %s",id):console.error("removeMenu(): missing required parameter: id")}function ContextMenu(id){Menu.apply(this,arguments);let $newMenu=$("<li class='dropdown context-menu' id='"+StringUtils.jQueryIdEscape(id)+"'></li>"),$popUp=$("<ul class='dropdown-menu'></ul>"),$toggle=$("<a href='#' class='dropdown-toggle' data-toggle='dropdown'></a>").hide();$newMenu.append($toggle).append($popUp),$("#context-menu-bar > ul").append($newMenu);let self=this;PopUpManager.addPopUp($popUp,function(){self.close()},!1),PopUpManager.listenToContextMenu(this)}function registerContextMenu(id){if(!id)return console.error("call to registerContextMenu() is missing required parameters"),null;if(contextMenuMap[id])return console.log("Context Menu added with same name and id of existing Context Menu: "+id),null;let cmenu=new ContextMenu(id);return contextMenuMap[id]=cmenu,cmenu}window.document.body.addEventListener("keyup",event=>{event.key===KEY.ALT&&(altKeyReleased=!0)},!0),ContextMenu.prototype=Object.create(Menu.prototype),ContextMenu.prototype.constructor=ContextMenu,ContextMenu.prototype.parentClass=Menu.prototype,EventDispatcher.makeEventDispatcher(ContextMenu.prototype),ContextMenu.prototype.open=function(mouseOrLocation){if(Metrics.countEvent(Metrics.EVENT_TYPE.UI_MENU,"contextMenuOpen",this.id),!(this.parentMenuItem||mouseOrLocation&&mouseOrLocation.hasOwnProperty("pageX")&&mouseOrLocation.hasOwnProperty("pageY")))return void console.error("ContextMenu open(): missing required parameter");let $window=$(window),escapedId=StringUtils.jQueryIdEscape(this.id),$menuAnchor=$("#"+escapedId),$menuWindow=$("#"+escapedId+" > ul"),posTop,posLeft;if(!($menuWindow.children().length<=0)){if(this.parentMenuItem){this.trigger("beforeSubMenuOpen");let $parentMenuItem=$(_getHTMLMenuItem(this.parentMenuItem.id)),elementRect={top:posTop=$parentMenuItem.offset().top,left:posLeft=$parentMenuItem.offset().left+$parentMenuItem.outerWidth(),height:$menuWindow.height()+25,width:$menuWindow.width()},clip=ViewUtils.getElementClipSize($window,elementRect);clip.bottom>0&&(posTop=Math.max(0,posTop+$parentMenuItem.height()-$menuWindow.height())),posTop-=30,posLeft+=3,clip.right>0&&(posLeft=Math.max(0,posLeft-$parentMenuItem.outerWidth()-$menuWindow.outerWidth()))}else{this.trigger("beforeContextMenuOpen"),closeAll();let elementRect={top:posTop=mouseOrLocation.pageY,left:posLeft=mouseOrLocation.pageX,height:$menuWindow.height()+25,width:$menuWindow.width()},clip=ViewUtils.getElementClipSize($window,elementRect);clip.bottom>0&&(posTop=Math.max(0,posTop-clip.bottom)),posTop-=30,posLeft+=5,clip.right>0&&(posLeft=Math.max(0,posLeft-clip.right))}$menuAnchor.addClass("open").css({left:posLeft,top:posTop})}},ContextMenu.prototype.close=function(){this.parentMenuItem?this.trigger("beforeSubMenuClose"):this.trigger("beforeContextMenuClose"),this.closeSubMenu(),$("#"+StringUtils.jQueryIdEscape(this.id)).removeClass("open")},ContextMenu.prototype.isOpen=function(){return $("#"+StringUtils.jQueryIdEscape(this.id)).hasClass("open")},ContextMenu.assignContextMenuToSelector=function(selector,cmenu){$(selector).on("click",function(e){let buttonOffset,buttonHeight;e.stopPropagation(),cmenu.isOpen()?cmenu.close():(buttonOffset=$(this).offset(),buttonHeight=$(this).outerHeight(),cmenu.open({pageX:buttonOffset.left,pageY:buttonOffset.top+buttonHeight}))})},AppInit.htmlReady(function(){$("#titlebar").on("focusin",function(){KeyBindingManager.addGlobalKeydownHook(menuKeyboardNavigationHandler)}),$("#titlebar").on("focusout",function(){KeyBindingManager.removeGlobalKeydownHook(menuKeyboardNavigationHandler)})}),EventDispatcher.makeEventDispatcher(exports),DeprecationWarning.deprecateConstant(ContextMenuIds,"WORKING_SET_MENU","WORKING_SET_CONTEXT_MENU"),DeprecationWarning.deprecateConstant(ContextMenuIds,"WORKING_SET_SETTINGS_MENU","WORKING_SET_CONFIG_MENU"),exports.AppMenuBar=AppMenuBar,exports.ContextMenuIds=ContextMenuIds,exports.MenuSection=MenuSection,exports.BEFORE=BEFORE,exports.AFTER=AFTER,exports.LAST=LAST,exports.FIRST=FIRST,exports.FIRST_IN_SECTION=FIRST_IN_SECTION,exports.LAST_IN_SECTION=LAST_IN_SECTION,exports.DIVIDER=DIVIDER,exports.getMenu=getMenu,exports.getSubMenu=getSubMenu,exports.getAllMenus=getAllMenus,exports.getMenuItem=getMenuItem,exports.getContextMenu=getContextMenu,exports.addMenu=addMenu,exports.removeMenu=removeMenu,exports.openMenu=openMenu,exports.getOpenMenu=getOpenMenu,exports.registerContextMenu=registerContextMenu,exports.closeAll=closeAll,exports.getAllMenuItemCommands=getAllMenuItemCommands,exports.Menu=Menu,exports.MenuItem=MenuItem,exports.ContextMenu=ContextMenu,exports.SubMenuIds=SubMenuIds,exports.EVENT_BEFORE_CONTEXT_MENU_OPEN="beforeContextMenuOpen",exports.EVENT_BEFORE_CONTEXT_MENU_CLOSE="beforeContextMenuClose",exports.EVENT_BEFORE_SUB_MENU_OPEN="beforeSubMenuOpen",exports.EVENT_BEFORE_SUB_MENU_CLOSE="beforeSubMenuClose",exports.EVENT_MENU_ADDED=EVENT_MENU_ADDED,exports.EVENT_SUB_MENU_ADDED="subMenuAdded",exports.EVENT_MENU_ITEM_ADDED="menuItemAdded"});