UNPKG

@jstarpl/react-contextmenu

Version:
55 lines (42 loc) 1.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _actions = require("./actions"); var _helpers = require("./helpers"); function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } class GlobalEventListener { constructor() { _defineProperty(this, "handleShowEvent", event => { for (const id in this.callbacks) { if ((0, _helpers.hasOwnProp)(this.callbacks, id)) this.callbacks[id].show(event); } }); _defineProperty(this, "handleHideEvent", event => { for (const id in this.callbacks) { if ((0, _helpers.hasOwnProp)(this.callbacks, id)) this.callbacks[id].hide(event); } }); _defineProperty(this, "register", (showCallback, hideCallback) => { const id = (0, _helpers.uniqueId)(); this.callbacks[id] = { show: showCallback, hide: hideCallback }; return id; }); _defineProperty(this, "unregister", id => { if (id && this.callbacks[id]) { delete this.callbacks[id]; } }); this.callbacks = {}; if (_helpers.canUseDOM) { window.addEventListener(_actions.MENU_SHOW, this.handleShowEvent); window.addEventListener(_actions.MENU_HIDE, this.handleHideEvent); } } } var _default = new GlobalEventListener(); exports.default = _default;