UNPKG

@momentum-ui/react-collaboration

Version:

Cisco Momentum UI Framework for React Collaboration Applications

68 lines 2.81 kB
var _a; import { v4 as uuidV4 } from 'uuid'; /** * An event target to handle communication between instances of Dialog and Popover. * This will enable us to keep track of Popover instances (specifically those with hideOnEsc = true) * which are opened while an Dialog is rendered. While such Popover instances remain open, the Dialog * will not close on Esc. * * This operates on the assumption that any Popover instance that is opened after an Dialog is * rendered on the screen is part of the content of said Dialog and should be closed before the Dialog * itself. */ export var EventType; (function (EventType) { EventType["TIPPY_INSTANCE_ADDED"] = "tippyInstanceAdded"; EventType["TIPPY_INSTANCE_REMOVED"] = "tippyInstanceRemoved"; })(EventType || (EventType = {})); var eventHandlers = (_a = {}, _a[EventType.TIPPY_INSTANCE_ADDED] = {}, _a[EventType.TIPPY_INSTANCE_REMOVED] = {}, _a); /** * Event target used for Popover instances with hideOnEsc = true */ var eventTarget = new EventTarget(); /** * Dispatch a custom event to the event target. * @param eventType The category, allowing for listeners to listen to specific events * @param data The data associated with the event (provided in the custom event detail) */ var dispatchEvent = function (eventType, data) { eventTarget.dispatchEvent(new CustomEvent(eventType, { detail: { data: data } })); }; /** * Add a listener for a specific type (category) of event. * This wraps the provided event handler to obfuscate the use of the custom event. * Due to this the event listener is stored against an ID which can be used to remove it later. * @param eventType The category of event * @param handler The handler function to call, expecting a type and data arguments * @returns The ID of the listener added */ var addListener = function (eventType, handler) { var id = uuidV4(); var eventHandler = function (_a) { var data = _a.detail.data; handler(data); }; eventTarget.addEventListener(eventType, eventHandler); eventHandlers[eventType][id] = eventHandler; return id; }; /** * Remove a listener for a specific type (category) of event. * Removes the stored listener based on the event type and handler ID provided. * @param eventType The category of event * @param id The ID of the listener to be removed */ var removeListener = function (eventType, id) { var eventHandler = eventHandlers[eventType][id]; if (!eventHandler) { console.warn("PopoverEvents:removeListener - No event handler found for ID: ".concat(id)); return; } eventTarget.removeEventListener(eventType, eventHandler); delete eventHandlers[eventType][id]; }; export { dispatchEvent, addListener, removeListener }; //# sourceMappingURL=Popover.events.js.map