All files / src/events event-info.ts

95.83% Statements 23/24
76.19% Branches 16/21
87.5% Functions 7/8
95.65% Lines 22/23

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  34x             133x 61x   61x     61x 61x                   34x   4x 4x 4x   4x               251x   251x           34x   510x   255x             34x           34x   421x   421x 217x   421x        
import { EventInfo } from "./types"
import { isTouchEvent } from "../gestures/utils/event-type"
 
/**
 * Filters out events not attached to the primary pointer (currently left mouse button)
 * @param eventHandler
 */
function filterPrimaryPointer(eventHandler: EventListener): EventListener {
    return (event: Event) => {
        const isMouseEvent = event instanceof MouseEvent
        const isPrimaryPointer =
            !isMouseEvent ||
            (isMouseEvent && (event as MouseEvent).button === 0)
 
        Eif (isPrimaryPointer) {
            eventHandler(event)
        }
    }
}
 
export type EventListenerWithPointInfo = (
    e: MouseEvent | TouchEvent | PointerEvent,
    info: EventInfo
) => void
 
const defaultPagePoint = { pageX: 0, pageY: 0 }
 
function pointFromTouch(e: TouchEvent, IpointType: "page" | "client" = "page") {
    const primaryTouch = e.touches[0] || e.changedTouches[0]
    const point = primaryTouch || defaultPagePoint
 
    return {
        x: point[pointType + "X"],
        y: point[pointType + "Y"],
    }
}
 
function pointFromMouse(
    point: MouseEvent | PointerEvent,
    IpointType: "page" | "client" = "page"
) {
    return {
        x: point[pointType + "X"],
        y: point[pointType + "Y"],
    }
}
 
export function extractEventInfo(
    event: MouseEvent | TouchEvent | PointerEvent,
    EpointType: "page" | "client" = "page"
): EventInfo {
    return {
        point: isTouchEvent(event)
            ? pointFromTouch(event, pointType)
            : pointFromMouse(event, pointType),
    }
}
 
export function getViewportPointFromEvent(
    event: MouseEvent | TouchEvent | PointerEvent
) {
    return extractEventInfo(event, "client")
}
 
export const wrapHandler = (
    handler: EventListenerWithPointInfo,
    IshouldFilterPrimaryPointer = false
): EventListener => {
    const listener: EventListener = (event: any) =>
        handler(event, extractEventInfo(event))
 
    return shouldFilterPrimaryPointer
        ? filterPrimaryPointer(listener)
        : listener
}