UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

239 lines (236 loc) • 8.24 kB
/** * DevExtreme (esm/__internal/events/short.js) * Version: 26.1.6 * Build date: Fri Oct 16 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import { addNamespace as pureAddNamespace } from "../../common/core/events/utils/index"; import eventsEngine from "../events/core/events_engine"; import KeyboardProcessor from "../events/core/keyboard_processor"; function addNamespace(eventName, namespace) { return namespace ? pureAddNamespace(eventName, namespace) : eventName } function executeAction(action, args) { return "function" === typeof action ? action(args) : action.execute(args) } export const active = { on: ($el, activeAction, inactiveAction, _ref) => { let { selector: selector, showTimeout: showTimeout, hideTimeout: hideTimeout, namespace: namespace } = _ref; eventsEngine.on($el, addNamespace("dxactive", namespace), selector, { timeout: showTimeout }, event => executeAction(activeAction, { event: event, element: event.currentTarget })); eventsEngine.on($el, addNamespace("dxinactive", namespace), selector, { timeout: hideTimeout }, event => executeAction(inactiveAction, { event: event, element: event.currentTarget })) }, off: ($el, _ref2) => { let { namespace: namespace, selector: selector } = _ref2; eventsEngine.off($el, addNamespace("dxactive", namespace), selector); eventsEngine.off($el, addNamespace("dxinactive", namespace), selector) } }; export const resize = { on: function($el, resizeHandler) { let { namespace: namespace } = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : {}; eventsEngine.on($el, addNamespace("dxresize", namespace), resizeHandler) }, off: function($el) { let { namespace: namespace } = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {}; eventsEngine.off($el, addNamespace("dxresize", namespace)) } }; export const hover = { on: ($el, start, end, _ref3) => { let { selector: selector, namespace: namespace } = _ref3; eventsEngine.on($el, addNamespace("dxhoverend", namespace), selector, event => end(event)); eventsEngine.on($el, addNamespace("dxhoverstart", namespace), selector, event => executeAction(start, { element: event.target, event: event })) }, off: ($el, _ref4) => { let { selector: selector, namespace: namespace } = _ref4; eventsEngine.off($el, addNamespace("dxhoverstart", namespace), selector); eventsEngine.off($el, addNamespace("dxhoverend", namespace), selector) } }; export const visibility = { on: ($el, shown, hiding, _ref5) => { let { namespace: namespace } = _ref5; eventsEngine.on($el, addNamespace("dxhiding", namespace), hiding); eventsEngine.on($el, addNamespace("dxshown", namespace), shown) }, off: ($el, _ref6) => { let { namespace: namespace } = _ref6; eventsEngine.off($el, addNamespace("dxhiding", namespace)); eventsEngine.off($el, addNamespace("dxshown", namespace)) } }; export const focus = { on: ($el, focusIn, focusOut, _ref7) => { let { namespace: namespace } = _ref7; eventsEngine.on($el, addNamespace("focusin", namespace), focusIn); eventsEngine.on($el, addNamespace("focusout", namespace), focusOut) }, off: ($el, _ref8) => { let { namespace: namespace } = _ref8; eventsEngine.off($el, addNamespace("focusin", namespace)); eventsEngine.off($el, addNamespace("focusout", namespace)) }, trigger: $el => eventsEngine.trigger($el, "focus") }; export const dxClick = { on: function($el, click) { let { namespace: namespace } = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : {}; eventsEngine.on($el, addNamespace("dxclick", namespace), click) }, off: function($el) { let { namespace: namespace } = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {}; eventsEngine.off($el, addNamespace("dxclick", namespace)) } }; export const click = { on: function($el, clickHandler) { let { namespace: namespace } = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : {}; eventsEngine.on($el, addNamespace("click", namespace), clickHandler) }, off: function($el) { let { namespace: namespace } = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {}; eventsEngine.off($el, addNamespace("click", namespace)) } }; let index = 0; const keyboardProcessors = {}; const generateListenerId = () => { const listenerId = `keyboardProcessorId${index}`; index += 1; return listenerId }; const toElements = value => { if (!value) { return [] } if ("undefined" === typeof Element) { return [] } if (value instanceof Element) { return [value] } if (Array.isArray(value)) { return value.filter(item => item instanceof Element) } const v = value; if ("function" === typeof v.toArray) { const arr = v.toArray(); if (Array.isArray(arr)) { return arr.filter(item => item instanceof Element) } } const first = v[0]; return first instanceof Element ? [first] : [] }; const processorIdsByNode = new WeakMap; const getProcessorNodes = processor => [...toElements(null === processor || void 0 === processor ? void 0 : processor._element), ...toElements(null === processor || void 0 === processor ? void 0 : processor._focusTarget)]; const addProcessorToIndex = (id, nodes) => { nodes.forEach(el => { let ids = processorIdsByNode.get(el); if (!ids) { ids = new Set; processorIdsByNode.set(el, ids) } ids.add(id) }) }; const removeProcessorFromIndex = (id, nodes) => { nodes.forEach(el => { const ids = processorIdsByNode.get(el); if (ids) { ids.delete(id); if (0 === ids.size) { processorIdsByNode.delete(el) } } }) }; export const keyboard = { on: (element, focusTarget, handler) => { const listenerId = generateListenerId(); const keyboardProcessor = new KeyboardProcessor({ element: element, focusTarget: focusTarget, handler: handler }); keyboardProcessors[listenerId] = keyboardProcessor; addProcessorToIndex(listenerId, getProcessorNodes(keyboardProcessor)); return listenerId }, off: listenerId => { const keyboardProcessor = listenerId ? keyboardProcessors[listenerId] : void 0; if (listenerId && keyboardProcessor) { const nodes = getProcessorNodes(keyboardProcessor); keyboardProcessor.dispose(); delete keyboardProcessors[listenerId]; removeProcessorFromIndex(listenerId, nodes) } }, disposeProcessorsForSubtree(root) { if (!(null !== root && void 0 !== root && root.nodeType)) { return } const nodes = [root]; if ("function" === typeof root.querySelectorAll) { nodes.push(...Array.from(root.querySelectorAll("*"))) } const idsToDispose = new Set; nodes.forEach(node => { var _processorIdsByNode$g; null === (_processorIdsByNode$g = processorIdsByNode.get(node)) || void 0 === _processorIdsByNode$g || _processorIdsByNode$g.forEach(id => idsToDispose.add(id)) }); idsToDispose.forEach(id => keyboard.off(id)) }, _getProcessor: listenerId => keyboardProcessors[listenerId] };