UNPKG

@mui/x-internal-gestures

Version:

The core engine of GestureEvents, a modern and robust multi-pointer gesture detection library for JavaScript.

116 lines (105 loc) 3.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ActiveGesturesRegistry = void 0; /** * ActiveGesturesRegistry - Centralized registry for tracking which gestures are active on elements * * This singleton class keeps track of all gesture instances that are currently in their active state, * allowing both the system and applications to query which gestures are active on specific elements. */ /** * Type for entries in the active gestures registry */ /** * Registry that maintains a record of all currently active gestures across elements */ class ActiveGesturesRegistry { /** Map of elements to their active gestures */ activeGestures = new Map(); /** * Register a gesture as active on an element * * @param element - The DOM element on which the gesture is active * @param gesture - The gesture instance that is active */ registerActiveGesture(element, gesture) { if (!this.activeGestures.has(element)) { this.activeGestures.set(element, new Set()); } const elementGestures = this.activeGestures.get(element); const entry = { gesture, element }; elementGestures.add(entry); } /** * Remove a gesture from the active registry * * @param element - The DOM element on which the gesture was active * @param gesture - The gesture instance to deactivate */ unregisterActiveGesture(element, gesture) { const elementGestures = this.activeGestures.get(element); if (!elementGestures) { return; } // Find and remove the specific gesture entry elementGestures.forEach(entry => { if (entry.gesture === gesture) { elementGestures.delete(entry); } }); // Remove the element from the map if it no longer has any active gestures if (elementGestures.size === 0) { this.activeGestures.delete(element); } } /** * Get all active gestures for a specific element * * @param element - The DOM element to query * @returns Array of active gesture names */ getActiveGestures(element) { const elementGestures = this.activeGestures.get(element); if (!elementGestures) { return {}; } return Array.from(elementGestures).reduce((acc, entry) => { acc[entry.gesture.name] = true; return acc; }, {}); } /** * Check if a specific gesture is active on an element * * @param element - The DOM element to check * @param gesture - The gesture instance to check * @returns True if the gesture is active on the element, false otherwise */ isGestureActive(element, gesture) { const elementGestures = this.activeGestures.get(element); if (!elementGestures) { return false; } return Array.from(elementGestures).some(entry => entry.gesture === gesture); } /** * Clear all active gestures from the registry */ destroy() { this.activeGestures.clear(); } /** * Clear all active gestures for a specific element * * @param element - The DOM element to clear */ unregisterElement(element) { this.activeGestures.delete(element); } } exports.ActiveGesturesRegistry = ActiveGesturesRegistry;