Modules (188)

PopUpManager

Description

Utilities for managing pop-ups.

Dependencies

Functions

Private

_beforeMenuPopup

A menu is being popped up, so remove any menu that is currently popped up

    function _beforeMenuPopup() {
        removeCurrentPopUp();
    }
Public API

addPopUp

Add Esc key handling for a popup DOM element.

$popUp non-nullable jQuery
jQuery object for the DOM element pop-up
removeHandler function
Pop-up specific remove (e.g. display:none or DOM removal)
autoRemove nullable Boolean
Specify true to indicate the PopUpManager should remove the popup from the _popUps array when the popup is closed. Specify false when the popup is always persistant in the _popUps array.
    function addPopUp($popUp, removeHandler, autoRemove) {
        autoRemove = autoRemove || false;

        _popUps.push($popUp[0]);
        $popUp.data("PopUpManager-autoRemove", autoRemove);
        $popUp.data("PopUpManager-removeHandler", removeHandler);
    }
Public API

listenToContextMenu

Context menus are also created in AppInit.htmlReady(), so they may not yet have been created when we get our AppInit.htmlReady() callback, so we provide this method to tell us when to start listening for their events

contextMenu ContextMenu
    function listenToContextMenu(contextMenu) {
        contextMenu.on("beforeContextMenuOpen", _beforeMenuPopup);
    }

    AppInit.htmlReady(function () {
        // Register for events
        window.document.body.addEventListener("keydown", _keydownCaptureListener, true);
        exports.on("beforeMenuPopup", _beforeMenuPopup);

        // Close all popups when a command is executed
        CommandManager.on("beforeExecuteCommand", function (event, commandId) {
            removeCurrentPopUp();
        });
    });


    EventDispatcher.makeEventDispatcher(exports);

    exports.addPopUp            = addPopUp;
    exports.removePopUp         = removePopUp;
    exports.listenToContextMenu = listenToContextMenu;
});

removeCurrentPopUp

Remove Esc key handling for a pop-up. Removes the pop-up from the DOM if the pop-up is currently visible and was not originally attached.

keyEvent optional KeyboardEvent
(optional)
    function removeCurrentPopUp(keyEvent) {
        // allow the popUp to prevent closing
        var $popUp,
            i,
            event = new $.Event("popUpClose");

        for (i = _popUps.length - 1; i >= 0; i--) {
            $popUp = $(_popUps[i]);

            if ($popUp.find(":visible").length > 0) {
                $popUp.trigger(event);

                if (!event.isDefaultPrevented()) {
                    // Stop the DOM event from propagating
                    if (keyEvent) {
                        keyEvent.stopImmediatePropagation();
                    }

                    removePopUp($popUp);

                    // TODO: right now Menus and Context Menus do not take focus away from
                    // the editor. We need to have a focus manager to correctly manage focus
                    // between editors and other UI elements.
                    // For now we don't set focus here and assume individual popups
                    // adjust focus if necessary
                    // See story in Trello card #404
                    //EditorManager.focusEditor();
                }

                break;
            }
        }
    }

    function _keydownCaptureListener(keyEvent) {
         // Escape key or Alt key (Windows-only)
        if (keyEvent.keyCode !== KeyEvent.DOM_VK_ESCAPE &&
                !(keyEvent.keyCode === KeyEvent.DOM_VK_ALT && brackets.platform === "win")) {
            return;
        }

        // Don't dismiss the popup if both Ctrl and Alt keys are pressed.
        if (keyEvent.keyCode === KeyEvent.DOM_VK_ALT && keyEvent.ctrlKey) {
            return;
        }

        removeCurrentPopUp(keyEvent);
    }
Public API

removePopUp

Remove Esc key handling for a pop-up. Removes the pop-up from the DOM if the pop-up is currently visible and was not originally attached.

$popUp non-nullable jQuery
    function removePopUp($popUp) {
        // check visible first to help protect against recursive calls
        // via removeHandler
        if ($popUp.find(":visible").length > 0) {
            var removeHandler = $popUp.data("PopUpManager-removeHandler");
            if (removeHandler) {
                removeHandler();
            }
        }

        // check index after removeHandler is done processing to protect
        // against recursive calls
        var index = _popUps.indexOf($popUp[0]);
        if (index >= 0) {
            var autoRemove = $popUp.data("PopUpManager-autoRemove");
            if (autoRemove) {
                $popUp.remove();
                _popUps.splice(index, 1);
            }
        }
    }