All files / client/src/events wrapper.js

50% Statements 5/10
81.81% Branches 9/11
60% Functions 3/5
50% Lines 5/10

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                                                                          50x                           4x       2x 2x                                   50x    
/**
 * Event Wrapper for Coherent.js
 *
 * Wraps native DOM events with component context, providing handlers
 * access to component state, setState, and props.
 */
 
/**
 * @typedef {object} CoherentEvent
 * @property {Event} originalEvent - The native DOM event
 * @property {string} type - The event type
 * @property {Element} target - The element with the data-coherent-* attribute
 * @property {Element} currentTarget - Same as `target`: the element whose handler runs
 * @property {boolean} defaultPrevented - Whether the default action was prevented
 * @property {boolean} propagationStopped - Whether a handler stopped delegated propagation
 * @property {function(): void} preventDefault - Delegates to originalEvent.preventDefault()
 * @property {function(): void} stopPropagation - Stops delegation to ancestor handlers and the native event
 * @property {function(): void} stopImmediatePropagation - Like stopPropagation, also for other native listeners
 * @property {function|null} component - The component function (if available)
 * @property {object|null} state - Current component state (if available)
 * @property {function|null} setState - State setter function (if available)
 * @property {object|null} props - Component props (if available)
 */
 
/**
 * Wrap a native DOM event with component context
 *
 * @param {Event} originalEvent - The native DOM event
 * @param {Element} target - The element that matched the data attribute selector
 * @param {object|null} componentRef - Optional component reference object
 * @param {function} [componentRef.component] - The component function
 * @param {object} [componentRef.state] - Current component state
 * @param {function} [componentRef.setState] - State setter function
 * @param {object} [componentRef.props] - Component props
 * @returns {CoherentEvent} Wrapped event with component context
 */
export function wrapEvent(originalEvent, target, componentRef = null) {
  const wrapped = {
    // Native event access
    originalEvent,
    type: originalEvent?.type,
    target,
    currentTarget: target,
    propagationStopped: false,
 
    get defaultPrevented() {
      return Boolean(originalEvent?.defaultPrevented);
    },
 
    // Delegate common methods
    preventDefault() {
      originalEvent.preventDefault();
    },
 
    stopPropagation() {
      wrapped.propagationStopped = true;
      originalEvent.stopPropagation();
    },
 
    stopImmediatePropagation() {
      wrapped.propagationStopped = true;
      if (typeof originalEvent.stopImmediatePropagation === 'function') {
        originalEvent.stopImmediatePropagation();
      } else {
        originalEvent.stopPropagation();
      }
    },
 
    // Component context (null if no componentRef provided)
    component: componentRef?.component ?? null,
    state: componentRef?.state ?? null,
    setState: componentRef?.setState ?? null,
    props: componentRef?.props ?? null,
  };
  return wrapped;
}