createEvent.js 8.3 KB

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