import { SyntheticEvent } from '../../SyntheticEvent'; import { nativeEvents } from '../internal/NativeEvents'; import { NativeKeyboardEvent } from './NativeKeyboardEvent'; import { NativeMouseEvent } from './NativeMouseEvent'; import { NativeWheelEvent } from './NativeWheelEvent'; interface NativeEvent { map(e: any): SyntheticEvent; syntheticEvent: typeof SyntheticEvent; readonly type: string; readonly isKey: boolean; } export interface Mapping { target: any; mapper: (e: any) => void; type: string; isKey: boolean; } export class NativeEventMapper extends nativeEvents() { static map(target: any, type: string, callbackfn: (event: SyntheticEvent, type: string, nativeEvent: any) => void, thisArg?: any) { const event = NativeEventMapper.event(type); function mapper(nativeEvent: any) { callbackfn.call(thisArg, nativeEvent !== undefined ? event.map(nativeEvent) : event.syntheticEvent.create(), type, nativeEvent); } if (event.isKey) target.Keys[event.type].connect(mapper); else target[event.type].connect(mapper); return { target, mapper, isKey: event.isKey, type: event.type, } as Mapping; } static removeMapping({ target, mapper, type, isKey }: Mapping) { if (isKey) target.Keys[type].disconnect(mapper); else target[type].disconnect(mapper); } } ([ ['keydown', NativeKeyboardEvent, 'onPressed', true], ['keyup', NativeKeyboardEvent, 'onReleased', true], ['keypress', NativeKeyboardEvent, 'onPressed', true], ['mouseenter', NativeMouseEvent, 'onEntered', false], ['mouseleave', NativeMouseEvent, 'onExited', false], ['mousemove', NativeMouseEvent, 'onPositionChanged', false], ['mousedown', NativeMouseEvent, 'onPressed', false], ['mouseup', NativeMouseEvent, 'onReleased', false], ['click', NativeMouseEvent, 'onClicked', false], ['dblclick', NativeMouseEvent, 'onDoubleClicked', false], ['wheel', NativeWheelEvent, 'onWheel', false], ] as [ string, { map(e: any): SyntheticEvent; syntheticEvent: typeof SyntheticEvent; new(): any; }, string, boolean ][]).forEach(([type, NativeEvent, nativeType, isKey]) => { NativeEventMapper.register(type, class extends NativeEvent { static readonly type = nativeType; static readonly isKey = isKey; }); });