UNPKG

@jstarpl/react-contextmenu

Version:
45 lines (36 loc) 1.34 kB
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; } import { MENU_SHOW, MENU_HIDE } from './actions'; import { uniqueId, hasOwnProp, canUseDOM } from './helpers'; class GlobalEventListener { constructor() { _defineProperty(this, "handleShowEvent", event => { for (const id in this.callbacks) { if (hasOwnProp(this.callbacks, id)) this.callbacks[id].show(event); } }); _defineProperty(this, "handleHideEvent", event => { for (const id in this.callbacks) { if (hasOwnProp(this.callbacks, id)) this.callbacks[id].hide(event); } }); _defineProperty(this, "register", (showCallback, hideCallback) => { const id = 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 (canUseDOM) { window.addEventListener(MENU_SHOW, this.handleShowEvent); window.addEventListener(MENU_HIDE, this.handleHideEvent); } } } export default new GlobalEventListener();