All files / coherent.js/packages/core/src/events dom-integration.js

35.56% Statements 165/464
50% Branches 1/2
4.16% Functions 1/24
35.56% Lines 165/464

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 5371x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x   6x 6x 6x   6x 6x 6x 6x 6x 6x 6x 6x 6x   1x 1x 1x 1x 1x                                       1x 1x 1x 1x 1x                                                                                 1x 1x 1x 1x 1x                                     1x 1x 1x 1x 1x                               1x 1x 1x 1x 1x                                 1x 1x 1x 1x 1x                                 1x 1x 1x 1x 1x                                   1x 1x 1x 1x 1x                                               1x 1x 1x 1x 1x                   1x 1x 1x 1x 1x                       1x 1x 1x 1x 1x                                                   1x 1x 1x 1x 1x                                                                         1x 1x 1x 1x 1x                                               1x 1x 1x 1x 1x           1x 1x 1x 1x 1x                       1x 1x 1x 1x 1x                     1x 1x 1x 1x 1x 1x 1x           1x 1x 1x 1x 1x               1x 1x 1x 1x 1x       1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x                   1x 1x 1x 1x                                   1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x                                 1x 1x 1x 1x                     1x
/**
 * DOM Integration for Coherent.js Event Bus
 *
 * Provides seamless integration between DOM events, data attributes,
 * and the event bus system. Enables declarative event handling in HTML.
 */
 
import { globalEventBus } from './event-bus.js';
 
/**
 * DOM Event Integration Class
 * Manages DOM event listeners and data-action attribute handling
 */
export class DOMEventIntegration {
    constructor(eventBus = globalEventBus, options = {}) {
        this.eventBus = eventBus;
        this.options = {
            debug: false,
            debounceDelay: 150,
            throttleDelay: 100,
            enableDelegation: true,
            enableDebounce: true,
            enableThrottle: false,
            ...options
        };
 
        this.boundHandlers = new Map();
        this.activeElement = null;
        this.isInitialized = false;
 
        // Bind context for event handlers
        this.handleClick = this.handleClick.bind(this);
        this.handleChange = this.handleChange.bind(this);
        this.handleInput = this.handleInput.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
        this.handleKeydown = this.handleKeydown.bind(this);
        this.handleFocus = this.handleFocus.bind(this);
        this.handleBlur = this.handleBlur.bind(this);
    }
 
    /**
     * Initialize DOM event listeners
     * @param {HTMLElement} rootElement - Root element to attach listeners to (default: document)
     */
    initialize(rootElement = document) {
        if (this.isInitialized) {
            console.warn('[DOMEventIntegration] Already initialized');
            return;
        }
 
        if (typeof window === 'undefined' || !rootElement) {
            console.warn('[DOMEventIntegration] Cannot initialize: no DOM environment');
            return;
        }
 
        this.rootElement = rootElement;
        this.setupDOMEventListeners();
        this.isInitialized = true;
 
        if (this.options.debug) {
            console.log('[DOMEventIntegration] Initialized with options:', this.options);
        }
    }
 
    /**
     * Set up delegated DOM event listeners
     * @private
     */
    setupDOMEventListeners() {
        // Click events
        const clickHandler = this.createDelegatedHandler('click', this.handleClick);
        this.rootElement.addEventListener('click', clickHandler, { passive: false });
        this.boundHandlers.set('click', clickHandler);
 
        // Change events (with debouncing)
        const changeHandler = this.options.enableDebounce
            ? this.debounce(this.createDelegatedHandler('change', this.handleChange), this.options.debounceDelay)
            : this.createDelegatedHandler('change', this.handleChange);
        this.rootElement.addEventListener('change', changeHandler, { passive: true });
        this.boundHandlers.set('change', changeHandler);
 
        // Input events (with debouncing)
        const inputHandler = this.options.enableDebounce
            ? this.debounce(this.createDelegatedHandler('input', this.handleInput), this.options.debounceDelay)
            : this.createDelegatedHandler('input', this.handleInput);
        this.rootElement.addEventListener('input', inputHandler, { passive: true });
        this.boundHandlers.set('input', inputHandler);
 
        // Submit events
        const submitHandler = this.createDelegatedHandler('submit', this.handleSubmit);
        this.rootElement.addEventListener('submit', submitHandler, { passive: false });
        this.boundHandlers.set('submit', submitHandler);
 
        // Keyboard events
        const keydownHandler = this.createDelegatedHandler('keydown', this.handleKeydown);
        this.rootElement.addEventListener('keydown', keydownHandler, { passive: false });
        this.boundHandlers.set('keydown', keydownHandler);
 
        // Focus events
        const focusHandler = this.createDelegatedHandler('focus', this.handleFocus);
        this.rootElement.addEventListener('focus', focusHandler, { passive: true, capture: true });
        this.boundHandlers.set('focus', focusHandler);
 
        // Blur events
        const blurHandler = this.createDelegatedHandler('blur', this.handleBlur);
        this.rootElement.addEventListener('blur', blurHandler, { passive: true, capture: true });
        this.boundHandlers.set('blur', blurHandler);
    }
 
    /**
     * Create a delegated event handler
     * @private
     */
    createDelegatedHandler(eventType, handler) {
        return (event) => {
            const target = event.target;
            if (!target) return;
 
            // Find the closest element with data-action
            const actionElement = this.options.enableDelegation
                ? target.closest('[data-action]')
                : (target.hasAttribute?.('data-action') ? target : null);
 
            if (actionElement) {
                handler(actionElement, event);
            } else {
                // Also handle direct handler calls for elements without data-action
                handler(target, event);
            }
        };
    }
 
    /**
     * Handle click events
     * @private
     */
    handleClick(element, event) {
        const action = element.getAttribute?.('data-action');
 
        if (action) {
            this.handleDataAction(element, event, action);
        }
 
        // Emit generic DOM event
        this.eventBus.emitSync('dom:click', {
            element,
            event,
            action,
            data: this.parseDataAttributes(element)
        });
    }
 
    /**
     * Handle change events
     * @private
     */
    handleChange(element, event) {
        const action = element.getAttribute?.('data-action');
 
        if (action) {
            this.handleDataAction(element, event, action);
        }
 
        // Emit form change event
        this.eventBus.emitSync('dom:change', {
            element,
            event,
            value: element.value,
            action,
            data: this.parseDataAttributes(element)
        });
    }
 
    /**
     * Handle input events
     * @private
     */
    handleInput(element, event) {
        const action = element.getAttribute?.('data-action');
 
        if (action) {
            this.handleDataAction(element, event, action);
        }
 
        // Emit input event
        this.eventBus.emitSync('dom:input', {
            element,
            event,
            value: element.value,
            action,
            data: this.parseDataAttributes(element)
        });
    }
 
    /**
     * Handle submit events
     * @private
     */
    handleSubmit(element, event) {
        const action = element.getAttribute?.('data-action');
 
        if (action) {
            event.preventDefault(); // Prevent default form submission
            this.handleDataAction(element, event, action);
        }
 
        // Emit form submit event
        this.eventBus.emitSync('dom:submit', {
            element,
            event,
            action,
            formData: this.extractFormData(element),
            data: this.parseDataAttributes(element)
        });
    }
 
    /**
     * Handle keydown events
     * @private
     */
    handleKeydown(element, event) {
        const action = element.getAttribute?.('data-action');
        const keyAction = element.getAttribute?.(`data-key-${event.key.toLowerCase()}`);
 
        if (action && this.shouldTriggerKeyAction(event)) {
            this.handleDataAction(element, event, action);
        }
 
        if (keyAction) {
            this.handleDataAction(element, event, keyAction);
        }
 
        // Emit keyboard event
        this.eventBus.emitSync('dom:keydown', {
            element,
            event,
            key: event.key,
            code: event.code,
            action,
            keyAction,
            data: this.parseDataAttributes(element)
        });
    }
 
    /**
     * Handle focus events
     * @private
     */
    handleFocus(element, event) {
        this.activeElement = element;
 
        this.eventBus.emitSync('dom:focus', {
            element,
            event,
            data: this.parseDataAttributes(element)
        });
    }
 
    /**
     * Handle blur events
     * @private
     */
    handleBlur(element, event) {
        if (this.activeElement === element) {
            this.activeElement = null;
        }
 
        this.eventBus.emitSync('dom:blur', {
            element,
            event,
            data: this.parseDataAttributes(element)
        });
    }
 
    /**
     * Handle data-action attributes
     * @private
     */
    handleDataAction(element, event, action) {
        if (!action) return;
 
        // Parse additional data from element
        const data = this.parseDataAttributes(element);
 
        // Emit generic action event
        this.eventBus.emitSync('dom:action', {
            action,
            element,
            event,
            data
        });
 
        // Let the event bus handle the specific action
        this.eventBus.handleAction(action, element, event, data);
 
        if (this.options.debug) {
            console.log(`[DOMEventIntegration] Action triggered: ${action}`, {
                element,
                event: event.type,
                data
            });
        }
    }
 
    /**
     * Parse data attributes from an element
     * @private
     */
    parseDataAttributes(element) {
        if (!element?.attributes) return {};
 
        const data = {};
 
        Array.from(element.attributes).forEach(attr => {
            if (attr.name.startsWith('data-') && attr.name !== 'data-action') {
                // Convert kebab-case to camelCase
                const key = attr.name
                    .slice(5) // Remove 'data-' prefix
                    .replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
 
                // Try to parse as JSON, fall back to string
                let value = attr.value;
                try {
                    // Attempt to parse numbers, booleans, and JSON
                    if (value === 'true') value = true;
                    else if (value === 'false') value = false;
                    else if (value === 'null') value = null;
                    else if (value === 'undefined') value = undefined;
                    else if (/^\d+$/.test(value)) value = parseInt(value, 10);
                    else if (/^\d*\.\d+$/.test(value)) value = parseFloat(value);
                    else if ((value.startsWith('{') && value.endsWith('}')) ||
                             (value.startsWith('[') && value.endsWith(']'))) {
                        value = JSON.parse(value);
                    }
                } catch {
                    // Keep as string if parsing fails
                }
 
                data[key] = value;
            }
        });
 
        return data;
    }
 
    /**
     * Extract form data from a form element
     * @private
     */
    extractFormData(formElement) {
        if (!formElement || formElement.tagName !== 'FORM') {
            return {};
        }
 
        const formData = new FormData(formElement);
        const data = {};
 
        for (const [key, value] of formData.entries()) {
            // Handle multiple values (checkboxes, multiple selects)
            if (data[key]) {
                if (Array.isArray(data[key])) {
                    data[key].push(value);
                } else {
                    data[key] = [data[key], value];
                }
            } else {
                data[key] = value;
            }
        }
 
        return data;
    }
 
    /**
     * Check if a key event should trigger an action
     * @private
     */
    shouldTriggerKeyAction(event) {
        // Common keys that should trigger actions
        const triggerKeys = ['Enter', 'Space', 'Escape'];
        return triggerKeys.includes(event.key);
    }
 
    /**
     * Debounce utility function
     * @private
     */
    debounce(func, wait) {
        let timeout;
        return function executedFunction(...args) {
            const later = () => {
                clearTimeout(timeout);
                func.apply(this, args);
            };
            clearTimeout(timeout);
            timeout = setTimeout(later, wait);
        };
    }
 
    /**
     * Throttle utility function
     * @private
     */
    throttle(func, limit) {
        let inThrottle;
        return function executedFunction(...args) {
            if (!inThrottle) {
                func.apply(this, args);
                inThrottle = true;
                setTimeout(() => inThrottle = false, limit);
            }
        };
    }
 
    /**
     * Add custom event listener
     * @param {string} eventType - Event type
     * @param {Function} handler - Event handler
     * @param {Object} options - Event listener options
     */
    addCustomListener(eventType, handler, options = {}) {
        const wrappedHandler = this.createDelegatedHandler(eventType, handler);
        this.rootElement.addEventListener(eventType, wrappedHandler, options);
        this.boundHandlers.set(`custom:${eventType}`, wrappedHandler);
    }
 
    /**
     * Remove custom event listener
     * @param {string} eventType - Event type
     */
    removeCustomListener(eventType) {
        const handler = this.boundHandlers.get(`custom:${eventType}`);
        if (handler) {
            this.rootElement.removeEventListener(eventType, handler);
            this.boundHandlers.delete(`custom:${eventType}`);
        }
    }
 
    /**
     * Register action handlers in bulk
     * @param {Object} actions - Object mapping action names to handlers
     */
    registerActions(actions) {
        this.eventBus.registerActions(actions);
    }
 
    /**
     * Get the currently active (focused) element
     * @returns {HTMLElement|null}
     */
    getActiveElement() {
        return this.activeElement;
    }
 
    /**
     * Trigger an action programmatically
     * @param {string} action - Action name
     * @param {HTMLElement} element - Target element
     * @param {Object} data - Additional data
     */
    triggerAction(action, element, data = {}) {
        const syntheticEvent = new CustomEvent('synthetic', {
            bubbles: true,
            cancelable: true,
            detail: data
        });
 
        this.eventBus.handleAction(action, element, syntheticEvent, data);
    }
 
    /**
     * Clean up event listeners
     */
    destroy() {
        if (!this.isInitialized) return;
 
        this.boundHandlers.forEach((handler, eventType) => {
            this.rootElement.removeEventListener(
                eventType.replace('custom:', ''),
                handler
            );
        });
 
        this.boundHandlers.clear();
        this.activeElement = null;
        this.isInitialized = false;
 
        if (this.options.debug) {
            console.log('[DOMEventIntegration] Destroyed');
        }
    }
}
 
/**
 * Global DOM integration instance
 */
export const globalDOMIntegration = new DOMEventIntegration(globalEventBus, {
    debug: typeof process !== 'undefined' && process.env?.NODE_ENV === 'development'
});
 
/**
 * Initialize DOM integration with auto-start
 * @param {Object} options - Configuration options
 * @returns {DOMEventIntegration} DOM integration instance
 */
export function initializeDOMIntegration(options = {}) {
    const integration = new DOMEventIntegration(globalEventBus, options);
 
    // Auto-initialize when DOM is ready
    if (typeof window !== 'undefined') {
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', () => {
                integration.initialize();
            });
        } else {
            integration.initialize();
        }
    }
 
    return integration;
}
 
/**
 * Auto-initialize global DOM integration
 */
if (typeof window !== 'undefined') {
    // Auto-start the global integration
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', () => {
            globalDOMIntegration.initialize();
        });
    } else {
        globalDOMIntegration.initialize();
    }
}
 
export default globalDOMIntegration;