createEvent.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import '../utils/dataTransfer/Clipboard.js';
  2. import { getWindow } from '../utils/misc/getWindow.js';
  3. import { eventMap } from './eventMap.js';
  4. const eventInitializer = {
  5. ClipboardEvent: [
  6. initClipboardEvent
  7. ],
  8. Event: [],
  9. FocusEvent: [
  10. initUIEvent,
  11. initFocusEvent
  12. ],
  13. InputEvent: [
  14. initUIEvent,
  15. initInputEvent
  16. ],
  17. MouseEvent: [
  18. initUIEvent,
  19. initUIEventModifiers,
  20. initMouseEvent
  21. ],
  22. PointerEvent: [
  23. initUIEvent,
  24. initUIEventModifiers,
  25. initMouseEvent,
  26. initPointerEvent
  27. ],
  28. KeyboardEvent: [
  29. initUIEvent,
  30. initUIEventModifiers,
  31. initKeyboardEvent
  32. ]
  33. };
  34. function createEvent(type, target, init) {
  35. const window = getWindow(target);
  36. const { EventType, defaultInit } = eventMap[type];
  37. const event = new (getEventConstructors(window))[EventType](type, defaultInit);
  38. eventInitializer[EventType].forEach((f)=>f(event, init !== null && init !== undefined ? init : {}));
  39. return event;
  40. }
  41. /* istanbul ignore next */ function getEventConstructors(window) {
  42. var _window_Event;
  43. /* eslint-disable @typescript-eslint/no-unnecessary-condition, @typescript-eslint/no-extraneous-class */ const Event = (_window_Event = window.Event) !== null && _window_Event !== undefined ? _window_Event : class Event {
  44. };
  45. var _window_AnimationEvent;
  46. const AnimationEvent = (_window_AnimationEvent = window.AnimationEvent) !== null && _window_AnimationEvent !== undefined ? _window_AnimationEvent : class AnimationEvent extends Event {
  47. };
  48. var _window_ClipboardEvent;
  49. const ClipboardEvent = (_window_ClipboardEvent = window.ClipboardEvent) !== null && _window_ClipboardEvent !== undefined ? _window_ClipboardEvent : class ClipboardEvent extends Event {
  50. };
  51. var _window_PopStateEvent;
  52. const PopStateEvent = (_window_PopStateEvent = window.PopStateEvent) !== null && _window_PopStateEvent !== undefined ? _window_PopStateEvent : class PopStateEvent extends Event {
  53. };
  54. var _window_ProgressEvent;
  55. const ProgressEvent = (_window_ProgressEvent = window.ProgressEvent) !== null && _window_ProgressEvent !== undefined ? _window_ProgressEvent : class ProgressEvent extends Event {
  56. };
  57. var _window_TransitionEvent;
  58. const TransitionEvent = (_window_TransitionEvent = window.TransitionEvent) !== null && _window_TransitionEvent !== undefined ? _window_TransitionEvent : class TransitionEvent extends Event {
  59. };
  60. var _window_UIEvent;
  61. const UIEvent = (_window_UIEvent = window.UIEvent) !== null && _window_UIEvent !== undefined ? _window_UIEvent : class UIEvent extends Event {
  62. };
  63. var _window_CompositionEvent;
  64. const CompositionEvent = (_window_CompositionEvent = window.CompositionEvent) !== null && _window_CompositionEvent !== undefined ? _window_CompositionEvent : class CompositionEvent extends UIEvent {
  65. };
  66. var _window_FocusEvent;
  67. const FocusEvent = (_window_FocusEvent = window.FocusEvent) !== null && _window_FocusEvent !== undefined ? _window_FocusEvent : class FocusEvent extends UIEvent {
  68. };
  69. var _window_InputEvent;
  70. const InputEvent = (_window_InputEvent = window.InputEvent) !== null && _window_InputEvent !== undefined ? _window_InputEvent : class InputEvent extends UIEvent {
  71. };
  72. var _window_KeyboardEvent;
  73. const KeyboardEvent = (_window_KeyboardEvent = window.KeyboardEvent) !== null && _window_KeyboardEvent !== undefined ? _window_KeyboardEvent : class KeyboardEvent extends UIEvent {
  74. };
  75. var _window_MouseEvent;
  76. const MouseEvent = (_window_MouseEvent = window.MouseEvent) !== null && _window_MouseEvent !== undefined ? _window_MouseEvent : class MouseEvent extends UIEvent {
  77. };
  78. var _window_DragEvent;
  79. const DragEvent = (_window_DragEvent = window.DragEvent) !== null && _window_DragEvent !== undefined ? _window_DragEvent : class DragEvent extends MouseEvent {
  80. };
  81. var _window_PointerEvent;
  82. const PointerEvent = (_window_PointerEvent = window.PointerEvent) !== null && _window_PointerEvent !== undefined ? _window_PointerEvent : class PointerEvent extends MouseEvent {
  83. };
  84. var _window_TouchEvent;
  85. const TouchEvent = (_window_TouchEvent = window.TouchEvent) !== null && _window_TouchEvent !== undefined ? _window_TouchEvent : class TouchEvent extends UIEvent {
  86. };
  87. /* eslint-enable @typescript-eslint/no-unnecessary-condition, @typescript-eslint/no-extraneous-class */ return {
  88. Event,
  89. AnimationEvent,
  90. ClipboardEvent,
  91. PopStateEvent,
  92. ProgressEvent,
  93. TransitionEvent,
  94. UIEvent,
  95. CompositionEvent,
  96. FocusEvent,
  97. InputEvent,
  98. KeyboardEvent,
  99. MouseEvent,
  100. DragEvent,
  101. PointerEvent,
  102. TouchEvent
  103. };
  104. }
  105. function assignProps(obj, props) {
  106. for (const [key, value] of Object.entries(props)){
  107. Object.defineProperty(obj, key, {
  108. get: ()=>value !== null && value !== undefined ? value : null
  109. });
  110. }
  111. }
  112. function sanitizeNumber(n) {
  113. return Number(n !== null && n !== undefined ? n : 0);
  114. }
  115. function initClipboardEvent(event, { clipboardData }) {
  116. assignProps(event, {
  117. clipboardData
  118. });
  119. }
  120. function initFocusEvent(event, { relatedTarget }) {
  121. assignProps(event, {
  122. relatedTarget
  123. });
  124. }
  125. function initInputEvent(event, { data, inputType, isComposing }) {
  126. assignProps(event, {
  127. data,
  128. isComposing: Boolean(isComposing),
  129. inputType: String(inputType)
  130. });
  131. }
  132. function initUIEvent(event, { view, detail }) {
  133. assignProps(event, {
  134. view,
  135. detail: sanitizeNumber(detail !== null && detail !== undefined ? detail : 0)
  136. });
  137. }
  138. function initUIEventModifiers(event, { altKey, ctrlKey, metaKey, shiftKey, modifierAltGraph, modifierCapsLock, modifierFn, modifierFnLock, modifierNumLock, modifierScrollLock, modifierSymbol, modifierSymbolLock }) {
  139. assignProps(event, {
  140. altKey: Boolean(altKey),
  141. ctrlKey: Boolean(ctrlKey),
  142. metaKey: Boolean(metaKey),
  143. shiftKey: Boolean(shiftKey),
  144. getModifierState (k) {
  145. return Boolean({
  146. Alt: altKey,
  147. AltGraph: modifierAltGraph,
  148. CapsLock: modifierCapsLock,
  149. Control: ctrlKey,
  150. Fn: modifierFn,
  151. FnLock: modifierFnLock,
  152. Meta: metaKey,
  153. NumLock: modifierNumLock,
  154. ScrollLock: modifierScrollLock,
  155. Shift: shiftKey,
  156. Symbol: modifierSymbol,
  157. SymbolLock: modifierSymbolLock
  158. }[k]);
  159. }
  160. });
  161. }
  162. function initKeyboardEvent(event, { key, code, location, repeat, isComposing, charCode }) {
  163. assignProps(event, {
  164. key: String(key),
  165. code: String(code),
  166. location: sanitizeNumber(location),
  167. repeat: Boolean(repeat),
  168. isComposing: Boolean(isComposing),
  169. charCode
  170. });
  171. }
  172. function initMouseEvent(event, { x, y, screenX, screenY, clientX = x, clientY = y, button, buttons, relatedTarget, offsetX, offsetY, pageX, pageY }) {
  173. assignProps(event, {
  174. screenX: sanitizeNumber(screenX),
  175. screenY: sanitizeNumber(screenY),
  176. clientX: sanitizeNumber(clientX),
  177. x: sanitizeNumber(clientX),
  178. clientY: sanitizeNumber(clientY),
  179. y: sanitizeNumber(clientY),
  180. button: sanitizeNumber(button),
  181. buttons: sanitizeNumber(buttons),
  182. relatedTarget,
  183. offsetX: sanitizeNumber(offsetX),
  184. offsetY: sanitizeNumber(offsetY),
  185. pageX: sanitizeNumber(pageX),
  186. pageY: sanitizeNumber(pageY)
  187. });
  188. }
  189. function initPointerEvent(event, { pointerId, width, height, pressure, tangentialPressure, tiltX, tiltY, twist, pointerType, isPrimary }) {
  190. assignProps(event, {
  191. pointerId: sanitizeNumber(pointerId),
  192. width: sanitizeNumber(width !== null && width !== undefined ? width : 1),
  193. height: sanitizeNumber(height !== null && height !== undefined ? height : 1),
  194. pressure: sanitizeNumber(pressure),
  195. tangentialPressure: sanitizeNumber(tangentialPressure),
  196. tiltX: sanitizeNumber(tiltX),
  197. tiltY: sanitizeNumber(tiltY),
  198. twist: sanitizeNumber(twist),
  199. pointerType: String(pointerType),
  200. isPrimary: Boolean(isPrimary)
  201. });
  202. }
  203. export { createEvent };