Merge pull request #448 from tomalec/issues/447-modes-in-shadow-dom

Add listeners to list (shadow) root instead of window,
This commit is contained in:
Jos de Jong 2017-08-20 20:37:22 +02:00 committed by GitHub
commit 9ee8700661
1 changed files with 13 additions and 3 deletions

View File

@ -2,6 +2,14 @@
var util = require('./util');
/**
* Node.getRootNode shim
* @param {Node} node node to check
* @return {Node} node's rootNode or `window` if there is ShadowDOM is not supported.
*/
function getRootNode(node){
return node.getRootNode && node.getRootNode() || window;
}
/**
* A context menu
* @param {Object[]} items Array containing the menu structure
@ -240,7 +248,8 @@ ContextMenu.prototype.show = function (anchor, contentWindow) {
// create and attach event listeners
var me = this;
var list = this.dom.list;
this.eventListeners.mousedown = util.addEventListener(window, 'mousedown', function (event) {
var rootNode = getRootNode(list);
this.eventListeners.mousedown = util.addEventListener(rootNode, 'mousedown', function (event) {
// hide menu on click outside of the menu
var target = event.target;
if ((target != list) && !me._isChildOf(target, list)) {
@ -249,7 +258,7 @@ ContextMenu.prototype.show = function (anchor, contentWindow) {
event.preventDefault();
}
});
this.eventListeners.keydown = util.addEventListener(window, 'keydown', function (event) {
this.eventListeners.keydown = util.addEventListener(rootNode, 'keydown', function (event) {
me._onKeyDown(event);
});
@ -280,11 +289,12 @@ ContextMenu.prototype.hide = function () {
// remove all event listeners
// all event listeners are supposed to be attached to document.
var rootNode = getRootNode(this.dom.list);
for (var name in this.eventListeners) {
if (this.eventListeners.hasOwnProperty(name)) {
var fn = this.eventListeners[name];
if (fn) {
util.removeEventListener(window, name, fn);
util.removeEventListener(rootNode, name, fn);
}
delete this.eventListeners[name];
}