diff --git a/docs/api-reference/event-manager.md b/docs/api-reference/event-manager.md index 64ecdee..c1ef995 100644 --- a/docs/api-reference/event-manager.md +++ b/docs/api-reference/event-manager.md @@ -12,8 +12,8 @@ import {EventManager} from 'mjolnir.js'; const eventManager = new EventManager(target, options); ``` -* `target` (HTMLElement) - DOM element on which event handlers will be registered. -* `options` (object) - Options +- `target` (HTMLElement) - DOM element on which event handlers will be registered. +- `options` (object) - Options - `events` (object) - A map from event names to their handler functions, to register on init. - `recognizers` (RecognizerTuple[]) - Gesture recognizers. See [Recognize Gestures](#recognize-gestures) section below for usage. - `touchAction` (string) - Allow browser default touch actions. See [touch-action CSS property](https://developer.mozilla.org/en-US/docs/Web/CSS/touch-action). Use 'compute' to automatically set as the least restrictive value to support the recognizers. Default `'compute'`. @@ -30,7 +30,7 @@ Tears down internal event management implementations. eventManager.destroy(); ``` -*Note: It is recommended to call `destroy` when done since `EventManager` adds event listeners to `window`.* +_Note: It is recommended to call `destroy` when done since `EventManager` adds event listeners to `window`._ ### on @@ -45,10 +45,10 @@ eventManager.on(eventMap, options); - `handler` ((event: MjolnirEvent) => void) - The function to be called on `event`. - `eventMap` (object) - A map from event names to their handler functions - `options` (object, optional) - + `srcElement` (HTMLElement) - The source element of this event. If provided, only events that are targeting this element or its decendants will invoke the handler. If ignored, default to the root element of the event manager. Events are propagated up the DOM tree. - + `priority` (number) - Handlers targeting the same `srcElement` will be executed by their priorities (higher numbers first). Handlers with the same priority will be executed in the order of registration. Default `0`. + - `srcElement` (HTMLElement) - The source element of this event. If provided, only events that are targeting this element or its decendants will invoke the handler. If ignored, default to the root element of the event manager. Events are propagated up the DOM tree. + - `priority` (number) - Handlers targeting the same `srcElement` will be executed by their priorities (higher numbers first). Handlers with the same priority will be executed in the order of registration. Default `0`. -*Note: Unlike the DOM event system, developers are responsible of deregistering event handlers when `srcElement` is removed.* +_Note: Unlike the DOM event system, developers are responsible of deregistering event handlers when `srcElement` is removed._ ### once @@ -111,23 +111,25 @@ eventManager.watch('dblClick', evt => evt.stopPropagation(), {srcElement: void) - Do not invoke handlers registered for any ancestors in the DOM tree. - `stopImmediatePropagation` (() => void) - Do not invoke any other handlers registered for the same element or its ancestors. - ### MjolnirPointerEvent Emitted by `pointer*` events. Extends `MjolnirEvent` with the following fields: @@ -40,7 +39,6 @@ Emitted by `pointer*` events. Extends `MjolnirEvent` with the following fields: - `rightButton` (boolean) - Flag indicating whether the right mouse button is involved during the event - `pointerType` (string) - A string indicating the type of input (e.g. `'mouse'`, `'touch'`, `'pen'`) - ### MjolnirGestureEvent Emitted by recognizers (`Pan`, `Rotate` etc.). Extends `MjolnirEvent` with the following fields: @@ -63,7 +61,6 @@ Emitted by recognizers (`Pan`, `Rotate` etc.). Extends `MjolnirEvent` with the f - `middleButton` (boolean) - Flag indicating whether the middle mouse button is involved during the event - `rightButton` (boolean) - Flag indicating whether the right mouse button is involved during the event - ### MjolnirWheelEvent Emitted by the `wheel` event. Extends `MjolnirEvent` with the following fields: diff --git a/docs/get-started.md b/docs/get-started.md index 9e6f29c..48447a7 100644 --- a/docs/get-started.md +++ b/docs/get-started.md @@ -48,10 +48,8 @@ const eventManager = new EventManager(document.getElementById('container'), { eventManager.destroy(); ``` - ## Using with React - ```tsx import React, {useRef, useEffect} from 'react'; import {EventManager, Pinch, Pan} from 'mjolnir.js'; @@ -78,4 +76,4 @@ function App() { } ``` -*Note that React's event chain is independent from that of mjolnir.js'. Therefore, a `click` event handler registered with mjolnir.js cannot be blocked by calling `stopPropagation` on a React `onClick` event.* +_Note that React's event chain is independent from that of mjolnir.js'. Therefore, a `click` event handler registered with mjolnir.js cannot be blocked by calling `stopPropagation` on a React `onClick` event._ diff --git a/docs/upgrade-guide.md b/docs/upgrade-guide.md index 3e598ee..a572d48 100644 --- a/docs/upgrade-guide.md +++ b/docs/upgrade-guide.md @@ -2,7 +2,7 @@ ## From 2.x to 3.0 -- `EventManager` no longer comes with a default set of recognizers. Specify `options.recognizers` to emit gesture events. +- `EventManager` no longer comes with a default set of recognizers. Specify `options.recognizers` to emit gesture events. - `EventManager`'s `recognizerOptions` prop is removed. - Element must be supplied when constructing `EventManager` and cannot be reassigned. To change the event target, destroy the existing event manager instance and construct a new one. - Hammer.js is no longer a dependency. Due to the lack of maintenance on the legacy hammerjs project, mjolnir.js has ported it to TypeScript and incorporated it into the code base. To configure recognizers (Pan, Pinch etc.), directly import them from `mjolnir.js`. For details, see the documentation of each recognizer. diff --git a/src/event-manager.ts b/src/event-manager.ts index a00549d..1201eb0 100644 --- a/src/event-manager.ts +++ b/src/event-manager.ts @@ -19,6 +19,16 @@ import {EventRegistrar, HandlerOptions} from './utils/event-registrar'; type RecognizerConstructor = {new (options: any): Recognizer}; +type RecognizerTupleNormalized = { + recognizer: Recognizer; + /** Allow another gesture to be recognized simultaneously with this one. + * For example an interaction can trigger pinch and rotate at the same time. */ + recognizeWith?: string[]; + /** Another recognizer is mutually exclusive with this one. + * For example an interaction could be singletap or doubletap; pan-horizontal or pan-vertical; but never both. */ + requireFailure?: string[]; +}; + export type RecognizerTuple = | Recognizer | RecognizerConstructor @@ -35,16 +45,6 @@ export type RecognizerTuple = requireFailure?: string | string[] ]; -type RecognizerTupleNormalized = { - recognizer: Recognizer; - /** Allow another gesture to be recognized simultaneously with this one. - * For example an interaction can trigger pinch and rotate at the same time. */ - recognizeWith?: string[]; - /** Another recognizer is mutually exclusive with this one. - * For example an interaction could be singletap or doubletap; pan-horizontal or pan-vertical; but never both. */ - requireFailure?: string[]; -}; - export type EventManagerOptions = { /** Event listeners */ events?: MjolnirEventHandlers; @@ -170,7 +170,7 @@ export class EventManager { on(events: MjolnirEventHandlers, opts?: HandlerOptions): void; on( event: EventT['type'], - handler: (event: EventT) => void, + handler: (ev: EventT) => void, opts?: HandlerOptions ): void; @@ -183,7 +183,7 @@ export class EventManager { once(events: MjolnirEventHandlers, opts?: HandlerOptions): void; once( event: EventT['type'], - handler: (event: EventT) => void, + handler: (ev: EventT) => void, opts?: HandlerOptions ): void; @@ -198,7 +198,7 @@ export class EventManager { watch(events: MjolnirEventHandlers, opts?: HandlerOptions): void; watch( event: EventT['type'], - handler: (event: EventT) => void, + handler: (ev: EventT) => void, opts?: HandlerOptions ): void; @@ -210,7 +210,7 @@ export class EventManager { * Deregister a previously-registered event handler. */ off(events: MjolnirEventHandlers): void; - off(event: EventT['type'], handler: (event: EventT) => void): void; + off(event: EventT['type'], handler: (ev: EventT) => void): void; off(event: any, handler?: any) { this._removeEventHandler(event, handler); @@ -249,8 +249,8 @@ export class EventManager { // @ts-ignore opts = handler; // If `event` is a map, call `on()` for each entry. - for (const [eventName, handler] of Object.entries(event)) { - this._addEventHandler(eventName, handler!, opts, once, passive); + for (const [eventName, eventHandler] of Object.entries(event)) { + this._addEventHandler(eventName, eventHandler!, opts, once, passive); } return; } @@ -282,8 +282,8 @@ export class EventManager { private _removeEventHandler(event: string | MjolnirEventHandlers, handler?: MjolnirEventHandler) { if (typeof event !== 'string') { // If `event` is a map, call `off()` for each entry. - for (const [eventName, handler] of Object.entries(event)) { - this._removeEventHandler(eventName, handler); + for (const [eventName, eventHandler] of Object.entries(event)) { + this._removeEventHandler(eventName, eventHandler); } return; } @@ -315,7 +315,7 @@ export class EventManager { } private _getRecognizerName(event: string): string | undefined { - return this.manager.recognizers.find(recognizer => { + return this.manager.recognizers.find((recognizer) => { return recognizer.getEventNames().includes(event); })?.options.event; } diff --git a/src/hammerjs/input/compute-input-data.ts b/src/hammerjs/input/compute-input-data.ts index 0800eab..928e126 100644 --- a/src/hammerjs/input/compute-input-data.ts +++ b/src/hammerjs/input/compute-input-data.ts @@ -62,8 +62,8 @@ export function computeInputData(manager: Manager, input: RawInput): HammerInput input.maxPointers = !session.prevInput ? input.pointers.length : input.pointers.length > session.prevInput.maxPointers - ? input.pointers.length - : session.prevInput.maxPointers; + ? input.pointers.length + : session.prevInput.maxPointers; // find the correct target let target = manager.element!; diff --git a/src/hammerjs/input/types.ts b/src/hammerjs/input/types.ts index acbd3e2..5f9be3c 100644 --- a/src/hammerjs/input/types.ts +++ b/src/hammerjs/input/types.ts @@ -64,6 +64,13 @@ export type RawInput = { isFinal?: boolean; }; +/** + * Emitted input event */ +export type HammerInput = Omit, 'isFirst' | 'isFinal'> & { + /** Number of consecutive taps recognized. Populated if emitted by TapRecognizer */ + tapCount?: number; +}; + /** * Simplified HammerInput object retained in memory to help event processing */ export type SimpleInput = { @@ -92,10 +99,3 @@ export type Session = { prevented?: boolean; }; - -/** - * Emitted input event */ -export type HammerInput = Omit, 'isFirst' | 'isFinal'> & { - /** Number of consecutive taps recognized. Populated if emitted by TapRecognizer */ - tapCount?: number; -}; diff --git a/src/hammerjs/inputs/pointerevent.ts b/src/hammerjs/inputs/pointerevent.ts index c0e0a33..166aa37 100644 --- a/src/hammerjs/inputs/pointerevent.ts +++ b/src/hammerjs/inputs/pointerevent.ts @@ -42,7 +42,7 @@ export class PointerEventInput extends Input { const isTouch = pointerType === 'touch'; // get index of the event in the store - let storeIndex = store.findIndex(e => e.pointerId === ev.pointerId); + let storeIndex = store.findIndex((e) => e.pointerId === ev.pointerId); // start and mouse must be down if (eventType & InputEvent.Start && (ev.buttons || isTouch)) { diff --git a/src/hammerjs/manager.ts b/src/hammerjs/manager.ts index 45d94bb..e653979 100644 --- a/src/hammerjs/manager.ts +++ b/src/hammerjs/manager.ts @@ -32,12 +32,6 @@ export type ManagerOptions = { */ inputTarget?: null | EventTarget; - /** - * force an input class - * @default null - */ - inputClass?: null | Function; - /** * Some CSS properties can be used to improve the working of Hammer. * Add them to this method and they will be set when creating a new Manager. @@ -55,7 +49,6 @@ const defaultOptions: Required = { touchAction: 'compute', enable: true, inputTarget: null, - inputClass: null, cssProps: { /** * Disables text selection to improve the dragging gesture. Mainly for desktop browsers. @@ -210,17 +203,14 @@ export class Manager { /** * get a recognizer by its event name. */ - get(recognizer: Recognizer | string): Recognizer | null { - if (typeof recognizer === 'string') { - const {recognizers} = this; - for (let i = 0; i < recognizers.length; i++) { - if (recognizers[i].options.event === recognizer) { - return recognizers[i]; - } + get(recognizerName: string): Recognizer | null { + const {recognizers} = this; + for (let i = 0; i < recognizers.length; i++) { + if (recognizers[i].options.event === recognizerName) { + return recognizers[i]; } - return null; } - return recognizer; + return null; } /** @@ -259,7 +249,8 @@ export class Manager { return this; } - const recognizer = this.get(recognizerOrName); + const recognizer = + typeof recognizerOrName === 'string' ? this.get(recognizerOrName) : recognizerOrName; // let's make sure this recognizer exists if (recognizer) { diff --git a/src/hammerjs/recognizer/recognizer.ts b/src/hammerjs/recognizer/recognizer.ts index 6be1967..f129502 100644 --- a/src/hammerjs/recognizer/recognizer.ts +++ b/src/hammerjs/recognizer/recognizer.ts @@ -86,11 +86,16 @@ export abstract class Recognizer { return this; } - const {simultaneous} = this; - const otherRecognizer = this.manager.get(recognizerOrName); - if (!otherRecognizer) { - throw new Error(`Cannot find recognizer ${recognizerOrName}`); + let otherRecognizer: Recognizer | null; + if (typeof recognizerOrName === 'string') { + otherRecognizer = this.manager.get(recognizerOrName); + if (!otherRecognizer) { + throw new Error(`Cannot find recognizer ${recognizerOrName}`); + } + } else { + otherRecognizer = recognizerOrName; } + const {simultaneous} = this; if (!simultaneous[otherRecognizer.id]) { simultaneous[otherRecognizer.id] = otherRecognizer; otherRecognizer.recognizeWith(this); @@ -109,7 +114,12 @@ export abstract class Recognizer { return this; } - const otherRecognizer = this.manager.get(recognizerOrName); + let otherRecognizer: Recognizer | null; + if (typeof recognizerOrName === 'string') { + otherRecognizer = this.manager.get(recognizerOrName); + } else { + otherRecognizer = recognizerOrName; + } if (otherRecognizer) { delete this.simultaneous[otherRecognizer.id]; } @@ -127,11 +137,16 @@ export abstract class Recognizer { return this; } - const {requireFail} = this; - const otherRecognizer = this.manager.get(recognizerOrName); - if (!otherRecognizer) { - throw new Error(`Cannot find recognizer ${recognizerOrName}`); + let otherRecognizer: Recognizer | null; + if (typeof recognizerOrName === 'string') { + otherRecognizer = this.manager.get(recognizerOrName); + if (!otherRecognizer) { + throw new Error(`Cannot find recognizer ${recognizerOrName}`); + } + } else { + otherRecognizer = recognizerOrName; } + const {requireFail} = this; if (requireFail.indexOf(otherRecognizer) === -1) { requireFail.push(otherRecognizer); otherRecognizer.requireFailure(this); @@ -150,7 +165,12 @@ export abstract class Recognizer { return this; } - const otherRecognizer = this.manager.get(recognizerOrName); + let otherRecognizer: Recognizer | null; + if (typeof recognizerOrName === 'string') { + otherRecognizer = this.manager.get(recognizerOrName); + } else { + otherRecognizer = recognizerOrName; + } if (otherRecognizer) { const index = this.requireFail.indexOf(otherRecognizer); if (index > -1) { @@ -164,7 +184,7 @@ export abstract class Recognizer { * has require failures boolean */ hasRequireFailures(): boolean { - return Boolean(this.requireFail.find(recognier => recognier.options.enable)); + return Boolean(this.requireFail.find((recognier) => recognier.options.enable)); } /** diff --git a/src/hammerjs/recognizers/pan.ts b/src/hammerjs/recognizers/pan.ts index b082d02..875a7cc 100644 --- a/src/hammerjs/recognizers/pan.ts +++ b/src/hammerjs/recognizers/pan.ts @@ -65,7 +65,7 @@ export class PanRecognizer extends AttrRecognizer } getEventNames(): string[] { - return EVENT_NAMES.map(suffix => this.options.event + suffix); + return EVENT_NAMES.map((suffix) => this.options.event + suffix); } directionTest(input: HammerInput): boolean { diff --git a/src/hammerjs/recognizers/pinch.ts b/src/hammerjs/recognizers/pinch.ts index 1b285f0..cf48bbc 100644 --- a/src/hammerjs/recognizers/pinch.ts +++ b/src/hammerjs/recognizers/pinch.ts @@ -44,7 +44,7 @@ export class PinchRecognizer extends AttrRecognizer this.options.event + suffix); + return EVENT_NAMES.map((suffix) => this.options.event + suffix); } attrTest(input: HammerInput): boolean { diff --git a/src/hammerjs/recognizers/press.ts b/src/hammerjs/recognizers/press.ts index d8f5598..bce536d 100644 --- a/src/hammerjs/recognizers/press.ts +++ b/src/hammerjs/recognizers/press.ts @@ -54,7 +54,7 @@ export class PressRecognizer extends Recognizer } getEventNames(): string[] { - return EVENT_NAMES.map(suffix => this.options.event + suffix); + return EVENT_NAMES.map((suffix) => this.options.event + suffix); } process(input: HammerInput) { diff --git a/src/hammerjs/recognizers/rotate.ts b/src/hammerjs/recognizers/rotate.ts index f126918..b5d1a48 100644 --- a/src/hammerjs/recognizers/rotate.ts +++ b/src/hammerjs/recognizers/rotate.ts @@ -44,7 +44,7 @@ export class RotateRecognizer extends AttrRecognizer this.options.event + suffix); + return EVENT_NAMES.map((suffix) => this.options.event + suffix); } attrTest(input: HammerInput): boolean { diff --git a/src/hammerjs/recognizers/swipe.ts b/src/hammerjs/recognizers/swipe.ts index 3e0e5f1..c151a73 100644 --- a/src/hammerjs/recognizers/swipe.ts +++ b/src/hammerjs/recognizers/swipe.ts @@ -1,7 +1,6 @@ import {AttrRecognizer} from './attribute'; -import {InputDirection} from '../input/input-consts'; +import {InputDirection, InputEvent} from '../input/input-consts'; import {PanRecognizer} from './pan'; -import {InputEvent} from '../input/input-consts'; import type {HammerInput} from '../input/types'; export type SwipeRecognizerOptions = { @@ -55,7 +54,7 @@ export class SwipeRecognizer extends AttrRecognizer this.options.event + suffix); + return EVENT_NAMES.map((suffix) => this.options.event + suffix); } attrTest(input: HammerInput): boolean { diff --git a/src/inputs/key-input.ts b/src/inputs/key-input.ts index b7a2f4b..5d16eaa 100644 --- a/src/inputs/key-input.ts +++ b/src/inputs/key-input.ts @@ -27,11 +27,11 @@ export class KeyInput extends Input element.addEventListener(event, this.handleEvent)); + KEY_EVENTS.forEach((event) => element.addEventListener(event, this.handleEvent)); } destroy() { - KEY_EVENTS.forEach(event => this.element.removeEventListener(event, this.handleEvent)); + KEY_EVENTS.forEach((event) => this.element.removeEventListener(event, this.handleEvent)); } /** diff --git a/src/inputs/move-input.ts b/src/inputs/move-input.ts index b33582a..19ad1ea 100644 --- a/src/inputs/move-input.ts +++ b/src/inputs/move-input.ts @@ -48,11 +48,11 @@ export class MoveInput extends Input element.addEventListener(event, this.handleEvent)); + MOUSE_EVENTS.forEach((event) => element.addEventListener(event, this.handleEvent)); } destroy() { - MOUSE_EVENTS.forEach(event => this.element.removeEventListener(event, this.handleEvent)); + MOUSE_EVENTS.forEach((event) => this.element.removeEventListener(event, this.handleEvent)); } /** diff --git a/src/utils/event-registrar.ts b/src/utils/event-registrar.ts index aa5ce82..a50c553 100644 --- a/src/utils/event-registrar.ts +++ b/src/utils/event-registrar.ts @@ -116,7 +116,7 @@ export class EventRegistrar { } } } - this._active = handlers.some(entry => !entry.passive); + this._active = handlers.some((entry) => !entry.passive); } /** diff --git a/test/browser.ts b/test/browser.ts index 8a5791e..0f471b2 100644 --- a/test/browser.ts +++ b/test/browser.ts @@ -15,7 +15,7 @@ test.onFailure(() => { // tap-browser-color alternative enableDOMLogging({ - getStyle: message => ({ + getStyle: () => ({ background: failed ? '#F28E82' : '#8ECA6C', position: 'absolute', top: '500px', diff --git a/test/event-manager.spec.ts b/test/event-manager.spec.ts index 91c7466..a6bf328 100644 --- a/test/event-manager.spec.ts +++ b/test/event-manager.spec.ts @@ -7,7 +7,7 @@ import {EventManager, Tap, Pan} from 'mjolnir.js'; import {spy} from './test-utils/spy'; import {createEventTarget} from './test-utils/dom'; -test('eventManager#constructor', t => { +test('eventManager#constructor', (t) => { const root = createEventTarget(); let eventManager = new EventManager(root); @@ -40,7 +40,7 @@ test('eventManager#constructor', t => { t.end(); }); -test('eventManager#destroy', t => { +test('eventManager#destroy', (t) => { const root = createEventTarget(); const eventManager = new EventManager(root); const {manager, moveInput, wheelInput, keyInput} = eventManager; @@ -72,7 +72,7 @@ test('eventManager#destroy', t => { t.end(); }); -test('eventManager#on', t => { +test('eventManager#on', (t) => { const root = createEventTarget(); const eventManager = new EventManager(root, { recognizers: [new Tap({event: 'click'}), new Tap({event: 'dblclick', taps: 2})] @@ -103,7 +103,7 @@ test('eventManager#on', t => { t.end(); }); -test('eventManager#watch', t => { +test('eventManager#watch', (t) => { const root = createEventTarget(); const eventManager = new EventManager(root, { recognizers: [new Tap({event: 'click'}), new Tap({event: 'dblclick', taps: 2})] @@ -118,7 +118,7 @@ test('eventManager#watch', t => { t.end(); }); -test('eventManager#once', t => { +test('eventManager#once', (t) => { const root = createEventTarget(); const eventManager = new EventManager(root, { recognizers: [new Tap({event: 'click'}), new Tap({event: 'dblclick', taps: 2})] @@ -149,7 +149,7 @@ test('eventManager#once', t => { t.end(); }); -test('eventManager#off', t => { +test('eventManager#off', (t) => { const root = createEventTarget(); const eventManager = new EventManager(root, { recognizers: [new Tap({event: 'click'}), new Tap({event: 'dblclick', taps: 2}), new Pan()] @@ -211,7 +211,7 @@ test('eventManager#off', t => { t.end(); }); -test('eventManager#eventHandling', t => { +test('eventManager#eventHandling', (t) => { const root = createEventTarget(); const eventMock = {type: 'foo'}; const eventManager = new EventManager(root); @@ -225,7 +225,7 @@ test('eventManager#eventHandling', t => { t.end(); }); -test('eventManager#normalizeEvent', t => { +test('eventManager#normalizeEvent', (t) => { const root = createEventTarget(); const eventMock = { type: 'foo', @@ -240,7 +240,7 @@ test('eventManager#normalizeEvent', t => { let normalizedEvent; - eventManager.on('foo', evt => { + eventManager.on('foo', (evt) => { normalizedEvent = evt; }); @@ -256,7 +256,7 @@ test('eventManager#normalizeEvent', t => { t.end(); }); -test('eventManager#propagation', t => { +test('eventManager#propagation', (t) => { const rootNode = createEventTarget({ id: 'root', children: [ @@ -275,7 +275,7 @@ test('eventManager#propagation', t => { const fooHandler = (message: string, stopPropagation = false) => - evt => { + (evt) => { handlerCalls.push(message); if (stopPropagation) { evt.stopPropagation(); diff --git a/test/inputs/key-input.spec.ts b/test/inputs/key-input.spec.ts index dab871c..43d5116 100644 --- a/test/inputs/key-input.spec.ts +++ b/test/inputs/key-input.spec.ts @@ -7,7 +7,7 @@ import {KeyInput} from 'mjolnir.js/inputs/key-input'; import {spy} from '../test-utils/spy'; import {createEventTarget} from '../test-utils/dom'; -test('keyInput#constructor', t => { +test('keyInput#constructor', (t) => { const element = createEventTarget(); const numKeyEvents = 2; // KEY_EVENTS.length @@ -24,7 +24,7 @@ test('keyInput#constructor', t => { t.end(); }); -test('keyInput#destroy', t => { +test('keyInput#destroy', (t) => { const element = createEventTarget(); const numKeyEvents = 2; // KEY_EVENTS.length const removeELSpy = spy(element, 'removeEventListener'); @@ -41,7 +41,7 @@ test('keyInput#destroy', t => { }); /* eslint-disable max-statements */ -test('keyInput#enableEventType', t => { +test('keyInput#enableEventType', (t) => { const element = createEventTarget(); const keyDownMock = { type: 'keydown', diff --git a/test/inputs/move-input.spec.ts b/test/inputs/move-input.spec.ts index 203c6fa..82f3328 100644 --- a/test/inputs/move-input.spec.ts +++ b/test/inputs/move-input.spec.ts @@ -7,7 +7,7 @@ import {MoveInput} from 'mjolnir.js/inputs/move-input'; import {spy} from '../test-utils/spy'; import {createEventTarget} from '../test-utils/dom'; -test('moveInput#constructor', t => { +test('moveInput#constructor', (t) => { const element = createEventTarget(); const numMouseEvents = 6; // MOUSE_EVENTS.length const addELSpy = spy(element, 'addEventListener'); @@ -21,7 +21,7 @@ test('moveInput#constructor', t => { t.end(); }); -test('moveInput#destroy', t => { +test('moveInput#destroy', (t) => { const element = createEventTarget(); const numMouseEvents = 6; // MOUSE_EVENTS.length const removeELSpy = spy(element, 'removeEventListener'); @@ -37,7 +37,7 @@ test('moveInput#destroy', t => { t.end(); }); -test('moveInput#handleEvent', t => { +test('moveInput#handleEvent', (t) => { const element = createEventTarget(); const callbackSpy = spy(); const mouseDownMock = { @@ -78,13 +78,13 @@ test('moveInput#handleEvent', t => { t.end(); }); -test('moveInput#enableEventType', t => { +test('moveInput#enableEventType', (t) => { const element = createEventTarget(); let callbackSpy; let moveInput; - t.test('pointermove', assert => { + t.test('pointermove', (assert) => { const mouseHoverMock = { type: 'mousemove', button: 0, @@ -106,7 +106,7 @@ test('moveInput#enableEventType', t => { assert.end(); }); - t.test('pointerleave', assert => { + t.test('pointerleave', (assert) => { const mouseLeaveMock = { type: 'mouseleave', target: element @@ -126,7 +126,7 @@ test('moveInput#enableEventType', t => { assert.end(); }); - t.test('pointerover', assert => { + t.test('pointerover', (assert) => { const mouseOverMock = { type: 'mouseover', target: element @@ -146,7 +146,7 @@ test('moveInput#enableEventType', t => { assert.end(); }); - t.test('pointerout', assert => { + t.test('pointerout', (assert) => { const mouseOutMock = { type: 'mouseout', target: element diff --git a/test/inputs/wheel-input.spec.ts b/test/inputs/wheel-input.spec.ts index c7f4ff3..806e8dc 100644 --- a/test/inputs/wheel-input.spec.ts +++ b/test/inputs/wheel-input.spec.ts @@ -7,7 +7,7 @@ import {WheelInput} from 'mjolnir.js/inputs/wheel-input'; import {spy} from '../test-utils/spy'; import {createEventTarget} from '../test-utils/dom'; -test('wheelInput#constructor', t => { +test('wheelInput#constructor', (t) => { const element = createEventTarget(); const numWheelEvents = 1; // WHEEL_EVENTS.length const addELSpy = spy(element, 'addEventListener'); @@ -23,7 +23,7 @@ test('wheelInput#constructor', t => { t.end(); }); -test('wheelInput#destroy', t => { +test('wheelInput#destroy', (t) => { const element = createEventTarget(); const numWheelEvents = 1; // WHEEL_EVENTS.length const removeELSpy = spy(element, 'removeEventListener'); @@ -39,7 +39,7 @@ test('wheelInput#destroy', t => { t.end(); }); -test('moveInput#enableEventType', t => { +test('moveInput#enableEventType', (t) => { const element = createEventTarget(); const WHEEL_EVENT_TYPES = ['wheel']; // wheel-input.EVENT_TYPE const wheelInput = new WheelInput(element, null, { @@ -49,7 +49,7 @@ test('moveInput#enableEventType', t => { t.notOk(wheelInput.options.enable, 'should not enable for unsupported event'); t.ok( - WHEEL_EVENT_TYPES.every(event => { + WHEEL_EVENT_TYPES.every((event) => { wheelInput.options.enable = false; wheelInput.enableEventType(event, true); return wheelInput.options.enable; @@ -61,7 +61,7 @@ test('moveInput#enableEventType', t => { t.end(); }); -test('wheelInput#handleEvent', t => { +test('wheelInput#handleEvent', (t) => { const element = createEventTarget(); const wheelEventMock = { @@ -74,7 +74,7 @@ test('wheelInput#handleEvent', t => { }; let callbackParams = null; - const callback = evt => (callbackParams = evt); + const callback = (evt) => (callbackParams = evt); const wheelInput = new WheelInput(element, callback, { enable: false diff --git a/test/test-utils/spy.ts b/test/test-utils/spy.ts index 565a0b2..0824169 100644 --- a/test/test-utils/spy.ts +++ b/test/test-utils/spy.ts @@ -26,23 +26,23 @@ export function spy(obj?: object, methodName?: string): SpyFunction { } // @ts-ignore - const spy: SpyFunction = function (...args) { - spy.callCount++; - spy.called = true; + const spyInstance: SpyFunction = function (...args) { + spyInstance.callCount++; + spyInstance.called = true; /* eslint-disable no-invalid-this */ return func.apply(this, args); }; - spy.callCount = 0; - spy.called = false; - spy.reset = () => { - spy.callCount = 0; - spy.called = false; + spyInstance.callCount = 0; + spyInstance.called = false; + spyInstance.reset = () => { + spyInstance.callCount = 0; + spyInstance.called = false; }; - spy.restore = () => { + spyInstance.restore = () => { obj[methodName] = func; }; - obj[methodName] = spy; - return spy; + obj[methodName] = spyInstance; + return spyInstance; } diff --git a/test/utils/event-registrar.spec.ts b/test/utils/event-registrar.spec.ts index b9ec069..7ea5407 100644 --- a/test/utils/event-registrar.spec.ts +++ b/test/utils/event-registrar.spec.ts @@ -7,7 +7,7 @@ import {EventRegistrar} from 'mjolnir.js/utils/event-registrar'; import {createEventTarget} from '../test-utils/dom'; /* eslint-disable max-statements */ -test('EventRegistrar#add, remove', t => { +test('EventRegistrar#add, remove', (t) => { const eventRegistrar = new EventRegistrar(null, 'test'); const handler1 = () => {}; const handler2 = () => {}; @@ -82,7 +82,7 @@ test('EventRegistrar#add, remove', t => { t.end(); }); -test('EventRegistrar#normalizeEvent', t => { +test('EventRegistrar#normalizeEvent', (t) => { const root = createEventTarget(); const eventMock = { type: 'foo', @@ -96,7 +96,7 @@ test('EventRegistrar#normalizeEvent', t => { let normalizedEvent; const eventRegistrar = new EventRegistrar({getElement: () => root}); - eventRegistrar.add('foo', evt => { + eventRegistrar.add('foo', (evt) => { normalizedEvent = evt; }); @@ -116,7 +116,7 @@ test('EventRegistrar#normalizeEvent', t => { t.end(); }); -test('EventRegistrar#propagation', t => { +test('EventRegistrar#propagation', (t) => { const rootNode = createEventTarget({ id: 'root', children: [ @@ -146,7 +146,7 @@ test('EventRegistrar#propagation', t => { const fooHandler = (message: string, stopPropagation = false, stopImmediatePropagation = false) => - evt => { + (evt) => { handlerCalls.push(message); if (stopPropagation) { evt.stopPropagation(); diff --git a/test/utils/event-utils.spec.ts b/test/utils/event-utils.spec.ts index d280135..b31d9d5 100644 --- a/test/utils/event-utils.spec.ts +++ b/test/utils/event-utils.spec.ts @@ -5,7 +5,7 @@ import test from 'tape-promise/tape'; import {whichButtons} from 'mjolnir.js/utils/event-utils'; -test('EventUtils#whichButtons', t => { +test('EventUtils#whichButtons', (t) => { const TESTS = [ { srcEvent: { diff --git a/test/utils/global.spec.ts b/test/utils/global.spec.ts index d8f7911..bd2d2cb 100644 --- a/test/utils/global.spec.ts +++ b/test/utils/global.spec.ts @@ -5,7 +5,7 @@ import test from 'tape-promise/tape'; import {global, window, document, userAgent} from 'mjolnir.js/utils/globals'; -test('globals', t => { +test('globals', (t) => { t.ok(global, 'global is an object'); t.ok(window, 'window is an object'); t.ok(document, 'document is an object');