Repository navigation
Expand file tree
/
Copy pathPopUpManager.js
More file actions
1 lines (1 loc) · 6.34 KB
/
Copy pathPopUpManager.js
File metadata and controls
1 lines (1 loc) · 6.34 KB
1
define(function(require,exports,module){let AppInit=require("utils/AppInit"),EventDispatcher=require("utils/EventDispatcher"),WorkspaceManager=require("view/WorkspaceManager"),CommandManager=require("command/CommandManager"),MainViewManager=require("view/MainViewManager"),KeyEvent=require("utils/KeyEvent"),_popUps=[],addPopupInProgress=!1,currentEventPopups=[];function addPopUp($popUp,removeHandler,autoRemove,options){autoRemove=autoRemove||!1,addPopupInProgress=!0,(options=options||{}).closeCurrentPopups&&closeAllPopups();const popupManagesFocus=options.popupManagesFocus||!1;_popUps.push($popUp[0]),$popUp.data("PopUpManager-autoRemove",autoRemove),$popUp.data("PopUpManager-popupManagesFocus",popupManagesFocus),$popUp.data("PopUpManager-removeHandler",removeHandler),addPopupInProgress=!1}function handleSelectionEvents($popUp,options={}){const{keyboardEventHandler:keyboardEventHandler,enableSearchFilter:enableSearchFilter}=options;function _selectItem(){$popUp.find(".selected").removeClass("selected"),$(this).addClass("selected")}function _unselectItem(){$(this).removeClass("selected")}currentEventPopups.push({$popUp:$popUp,keyboardEventHandler:keyboardEventHandler,enableSearchFilter:enableSearchFilter}),currentEventPopups.length>1&&console.error(`${currentEventPopups.length} popups are visible while handling keyboard events!`,"Possible popup event handler leak. Only 1 popup event handler is expected at this time."),enableSearchFilter&&!$popUp.find(".sticky-li-top").length&&$popUp.prepend('<li class="sticky-li-top forced-hidden">\n <a class=\'stylesheet-link\'><i class="fa fa-search" aria-hidden="true"></i> \n <span class="searchTextSpan"></span></a>\n </li>'),$popUp.off("keydown",_processSelectionEvent),$popUp.on("keydown",_processSelectionEvent),$popUp.focus(),$popUp.off("mouseenter","a",_selectItem).off("mouseleave","a",_unselectItem),$popUp.on("mouseenter","a",_selectItem).on("mouseleave","a",_unselectItem)}function removePopUp($popUp){if($popUp.find(":visible").length>0){let removeHandler=$popUp.data("PopUpManager-removeHandler");removeHandler&&removeHandler()}let popupManagesFocus;$popUp.data("PopUpManager-popupManagesFocus")||addPopupInProgress||MainViewManager.focusActivePane();let handlerIndex=currentEventPopups.findIndex(item=>item.$popUp.is($popUp));handlerIndex>=0&&(currentEventPopups.splice(handlerIndex,1),searchStr="",$popUp.off("keydown",_processSelectionEvent));let index=_popUps.indexOf($popUp[0]);if(index>=0){let autoRemove;$popUp.data("PopUpManager-autoRemove")&&($popUp.remove(),_popUps.splice(index,1))}}function removeCurrentPopUp(keyEvent){let $popUp,i,event=new $.Event("popUpClose");for(i=_popUps.length-1;i>=0;i--)if(($popUp=$(_popUps[i])).find(":visible").length>0){if($popUp.trigger(event),!event.isDefaultPrevented())return keyEvent&&keyEvent.stopImmediatePropagation(),removePopUp($popUp),!0;break}}let searchStr="";function _filterDropdown($popup,searchString){searchStr=searchString;const $stickyLi=$popup.find("li.sticky-li-top");$stickyLi.length?(searchString?$stickyLi.removeClass("forced-hidden"):$stickyLi.addClass("forced-hidden"),$popup.find("li").each(function(index,li){if(0===index)return;const $li=$(li);$li.text().toLowerCase().includes(searchString.toLowerCase())?$li.removeClass("forced-hidden"):$li.addClass("forced-hidden")}),searchString?($stickyLi.removeClass("forced-hidden"),$stickyLi.find(".searchTextSpan").text(searchString)):$stickyLi.addClass("forced-hidden")):console.error("Search filter element not found! Please call PopUpManager.handleSelectionEvents with enableSearchFilter option.")}function selectNextItem(direction,$popUp){const $selectedItem=$popUp.find(".selected");let $links=$popUp.find("a:visible").not(function(){return $(this).closest(".sticky-li-top").length>0}),nextIndex=0;const selectedIndex=$links.index($selectedItem);if(nextIndex=selectedIndex>=0?(selectedIndex+direction)%$links.length:-1===direction?$links.length-1:0,searchStr&&0===$links.length)return;const $newItem=$links.eq(nextIndex);$selectedItem&&$selectedItem.removeClass("selected"),$newItem.addClass("selected")}function _processSelectionEvent(event){const{$popUp:$popUp,keyboardEventHandler:keyboardEventHandler}=currentEventPopups[currentEventPopups.length-1];if(!$popUp||!$popUp.is(":visible"))return!1;if(keyboardEventHandler){const processed=keyboardEventHandler(event,$popUp);if(processed)return!0}var keyHandled=!1;switch(event.keyCode){case KeyEvent.DOM_VK_UP:selectNextItem(-1,$popUp),keyHandled=!0;break;case KeyEvent.DOM_VK_DOWN:selectNextItem(1,$popUp),keyHandled=!0;break;case KeyEvent.DOM_VK_ENTER:case KeyEvent.DOM_VK_RETURN:const $dropdownItem=$popUp.find(".selected");$dropdownItem&&$dropdownItem.trigger("click"),keyHandled=!0}if(keyHandled)return event.stopImmediatePropagation(),event.preventDefault(),keyHandled;if((event.ctrlKey||event.metaKey)&&"v"===event.key)Phoenix.app.clipboardReadText().then(text=>{_filterDropdown($popUp,searchStr+=text)}),keyHandled=!0;else if(1===event.key.length)searchStr+=event.key,keyHandled=!0;else{if("Backspace"!==event.key)return!1;searchStr=searchStr.slice(0,-1),keyHandled=!0}return _filterDropdown($popUp,searchStr),keyHandled&&(event.stopImmediatePropagation(),event.preventDefault()),keyHandled}function _keydownCaptureListener(keyEvent){if((keyEvent.keyCode===KeyEvent.DOM_VK_ESCAPE||keyEvent.keyCode===KeyEvent.DOM_VK_ALT&&"win"===brackets.platform)&&(keyEvent.keyCode!==KeyEvent.DOM_VK_ALT||!keyEvent.ctrlKey))return removeCurrentPopUp(keyEvent)}function _beforeMenuPopup(){removeCurrentPopUp()}function _dontToggleWorkspacePanel(){for(let popUp of _popUps){let $popUp;if($(popUp).find(":visible").length>0)return!0}return!1}function listenToContextMenu(contextMenu){contextMenu.on("beforeContextMenuOpen",_beforeMenuPopup)}function closeAllPopups(){removeCurrentPopUp()}AppInit.htmlReady(function(){window.document.body.addEventListener("keydown",_keydownCaptureListener,!0),exports.on("beforeMenuPopup",_beforeMenuPopup),CommandManager.on("beforeExecuteCommand",function(event,commandId){removeCurrentPopUp()}),WorkspaceManager.addEscapeKeyEventHandler("PopUpManager",_dontToggleWorkspacePanel)}),EventDispatcher.makeEventDispatcher(exports),exports.addPopUp=addPopUp,exports.handleSelectionEvents=handleSelectionEvents,exports.selectNextItem=selectNextItem,exports.removePopUp=removePopUp,exports.closeAllPopups=closeAllPopups,exports.listenToContextMenu=listenToContextMenu});