This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1262 from adobe/jason-sanjose/esc-key
Re-reviewed. Looks good
- Loading branch information
Showing
5 changed files
with
195 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a | ||
* copy of this software and associated documentation files (the "Software"), | ||
* to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the | ||
* Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
* DEALINGS IN THE SOFTWARE. | ||
* | ||
*/ | ||
|
||
|
||
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */ | ||
/*global define, $, brackets, window */ | ||
|
||
/** | ||
* Utilities for managing pop-ups. | ||
*/ | ||
define(function (require, exports, module) { | ||
"use strict"; | ||
|
||
var EditorManager = require("editor/EditorManager"); | ||
|
||
var _popUps = []; | ||
|
||
function _removePopUp($popUp, index, visible) { | ||
var initiallyInDOM = $popUp.data("initiallyInDOM"), | ||
removeHandler = $popUp.data("removeHandler"); | ||
|
||
visible = visible || $popUp.find(":visible").length > 0; | ||
|
||
if (removeHandler && visible) { | ||
removeHandler(); | ||
} | ||
|
||
if (!initiallyInDOM) { | ||
$popUp.remove(); | ||
} | ||
|
||
_popUps = _popUps.slice(index); | ||
} | ||
|
||
/** | ||
* Add Esc key handling for pop-up DOM elements that persist in the DOM. | ||
* These pop-up elements must handle appearance (i.e. opening) themselves. | ||
* | ||
* @param {!jQuery} $popUp jQuery object for the DOM element pop-up | ||
* @param {function} removeHandler Pop-up specific remove (e.g. display:none or DOM removal) | ||
*/ | ||
function configurePopUp($popUp, removeHandler) { | ||
_popUps.push($popUp[0]); | ||
|
||
$popUp.data("initiallyInDOM", true); | ||
$popUp.data("removeHandler", removeHandler); | ||
} | ||
|
||
/** | ||
* Add Esc key handling for transient DOM elements. Immediately adds | ||
* the element to the DOM if it's not already attached. | ||
* | ||
* @param {!jQuery} $popUp jQuery object for the DOM element pop-up | ||
* @param {function} removeHandler Pop-up specific remove (e.g. display:none or DOM removal) | ||
*/ | ||
function addPopUp($popUp, removeHandler) { | ||
var initiallyInDOM = $popUp.parent().length === 1; | ||
|
||
configurePopUp($popUp, removeHandler, initiallyInDOM); | ||
|
||
if (!initiallyInDOM) { | ||
$(window.document.body).append($popUp); | ||
} | ||
} | ||
|
||
/** | ||
* 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. | ||
* | ||
* @param {!jQuery} $popUp | ||
*/ | ||
function removePopUp($popUp) { | ||
var index = _popUps.indexOf($popUp[0]), | ||
initiallyInDOM = $popUp.data("initiallyInDOM"), | ||
removeHandler = $popUp.data("removeHandler"); | ||
|
||
if (index >= 0) { | ||
_removePopUp($popUp, index); | ||
} | ||
} | ||
|
||
function _keydownCaptureListener(keyEvent) { | ||
if (keyEvent.keyCode !== 27) { | ||
return; | ||
} | ||
|
||
// 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 | ||
keyEvent.stopImmediatePropagation(); | ||
|
||
_removePopUp($popUp, i, true); | ||
EditorManager.focusEditor(); | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
window.document.body.addEventListener("keydown", _keydownCaptureListener, true); | ||
|
||
exports.addPopUp = addPopUp; | ||
exports.removePopUp = removePopUp; | ||
exports.configurePopUp = configurePopUp; | ||
}); |