|
| 1 | +import eventTypes from 'dom-event-types' |
| 2 | + |
| 3 | +interface TriggerOptions { |
| 4 | + code?: String |
| 5 | + key?: String |
| 6 | + keyCode?: Number |
| 7 | + [custom: string]: any |
| 8 | +} |
| 9 | + |
| 10 | +interface EventParams { |
| 11 | + eventType: string |
| 12 | + modifier: string |
| 13 | + meta: any |
| 14 | + options?: TriggerOptions |
| 15 | +} |
| 16 | + |
| 17 | +export const keyCodesByKeyName = { |
| 18 | + backspace: 8, |
| 19 | + tab: 9, |
| 20 | + enter: 13, |
| 21 | + esc: 27, |
| 22 | + space: 32, |
| 23 | + pageup: 33, |
| 24 | + pagedown: 34, |
| 25 | + end: 35, |
| 26 | + home: 36, |
| 27 | + left: 37, |
| 28 | + up: 38, |
| 29 | + right: 39, |
| 30 | + down: 40, |
| 31 | + insert: 45, |
| 32 | + delete: 46 |
| 33 | +} |
| 34 | + |
| 35 | +function getEventProperties(eventParams: EventParams) { |
| 36 | + const { modifier, meta, options } = eventParams |
| 37 | + const keyCode = |
| 38 | + keyCodesByKeyName[modifier] || |
| 39 | + (options && (options.keyCode || options.code)) |
| 40 | + |
| 41 | + return { |
| 42 | + ...options, // What the user passed in as the second argument to #trigger |
| 43 | + bubbles: meta.bubbles, |
| 44 | + meta: meta.cancelable, |
| 45 | + // Any derived options should go here |
| 46 | + keyCode, |
| 47 | + code: keyCode |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function createEvent(eventParams: EventParams) { |
| 52 | + const { eventType, meta } = eventParams |
| 53 | + const metaEventInterface = window[meta.eventInterface] |
| 54 | + |
| 55 | + const SupportedEventInterface = |
| 56 | + typeof metaEventInterface === 'function' ? metaEventInterface : window.Event |
| 57 | + |
| 58 | + const eventProperties = getEventProperties(eventParams) |
| 59 | + |
| 60 | + const event = new SupportedEventInterface( |
| 61 | + eventType, |
| 62 | + // event properties can only be added when the event is instantiated |
| 63 | + // custom properties must be added after the event has been instantiated |
| 64 | + eventProperties |
| 65 | + ) |
| 66 | + |
| 67 | + return event |
| 68 | +} |
| 69 | + |
| 70 | +function createDOMEvent(eventString: String, options?: TriggerOptions) { |
| 71 | + const [eventType, modifier] = eventString.split('.') |
| 72 | + const meta = eventTypes[eventType] || { |
| 73 | + eventInterface: 'Event', |
| 74 | + cancelable: true, |
| 75 | + bubbles: true |
| 76 | + } |
| 77 | + |
| 78 | + const eventParams: EventParams = { eventType, modifier, meta, options } |
| 79 | + const event: Event = createEvent(eventParams) |
| 80 | + const eventPrototype = Object.getPrototypeOf(event) |
| 81 | + |
| 82 | + options && |
| 83 | + Object.keys(options).forEach((key) => { |
| 84 | + const propertyDescriptor = Object.getOwnPropertyDescriptor( |
| 85 | + eventPrototype, |
| 86 | + key |
| 87 | + ) |
| 88 | + const canSetProperty = !( |
| 89 | + propertyDescriptor && propertyDescriptor.set === undefined |
| 90 | + ) |
| 91 | + if (canSetProperty) { |
| 92 | + event[key] = options[key] |
| 93 | + } |
| 94 | + }) |
| 95 | + return event |
| 96 | +} |
| 97 | + |
| 98 | +export { TriggerOptions, createDOMEvent } |
0 commit comments