From 63c0689a6160dc7f1a71fc21f82603652d5fd935 Mon Sep 17 00:00:00 2001 From: Tomek Wytrebowicz Date: Sun, 20 Aug 2017 15:26:44 +0200 Subject: [PATCH] Remove event listeners from (shadow) root instead of window, make context menus work in Shadow DOM. Fixes https://github.com/josdejong/jsoneditor/issues/447 --- src/js/ContextMenu.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/js/ContextMenu.js b/src/js/ContextMenu.js index ebeffc8..4de3844 100644 --- a/src/js/ContextMenu.js +++ b/src/js/ContextMenu.js @@ -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,7 @@ ContextMenu.prototype.show = function (anchor, contentWindow) { // create and attach event listeners var me = this; var list = this.dom.list; - var rootNode = list.getRootNode && list.getRootNode() || window; + 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; @@ -281,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]; }