grainjs 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -26
- package/dist/cjs/index.js +28 -17
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/lib/PriorityQueue.d.ts +2 -2
- package/dist/cjs/lib/PriorityQueue.js +1 -0
- package/dist/cjs/lib/PriorityQueue.js.map +1 -1
- package/dist/cjs/lib/_computed_queue.js +4 -3
- package/dist/cjs/lib/_computed_queue.js.map +1 -1
- package/dist/cjs/lib/binding.d.ts +11 -4
- package/dist/cjs/lib/binding.js +6 -5
- package/dist/cjs/lib/binding.js.map +1 -1
- package/dist/cjs/lib/browserGlobals.d.ts +4 -1
- package/dist/cjs/lib/browserGlobals.js +2 -0
- package/dist/cjs/lib/browserGlobals.js.map +1 -1
- package/dist/cjs/lib/computed.d.ts +49 -28
- package/dist/cjs/lib/computed.js +38 -51
- package/dist/cjs/lib/computed.js.map +1 -1
- package/dist/cjs/lib/dispose.d.ts +109 -96
- package/dist/cjs/lib/dispose.js +106 -79
- package/dist/cjs/lib/dispose.js.map +1 -1
- package/dist/cjs/lib/dom.d.ts +40 -18
- package/dist/cjs/lib/dom.js +63 -29
- package/dist/cjs/lib/dom.js.map +1 -1
- package/dist/cjs/lib/domComponent.d.ts +56 -51
- package/dist/cjs/lib/domComponent.js +46 -44
- package/dist/cjs/lib/domComponent.js.map +1 -1
- package/dist/cjs/lib/domComputed.d.ts +50 -20
- package/dist/cjs/lib/domComputed.js +37 -7
- package/dist/cjs/lib/domComputed.js.map +1 -1
- package/dist/cjs/lib/domDispose.d.ts +27 -12
- package/dist/cjs/lib/domDispose.js +27 -11
- package/dist/cjs/lib/domDispose.js.map +1 -1
- package/dist/cjs/lib/domForEach.d.ts +5 -4
- package/dist/cjs/lib/domForEach.js +41 -41
- package/dist/cjs/lib/domForEach.js.map +1 -1
- package/dist/cjs/lib/domImpl.d.ts +33 -10
- package/dist/cjs/lib/domImpl.js +29 -9
- package/dist/cjs/lib/domImpl.js.map +1 -1
- package/dist/cjs/lib/domMethods.d.ts +93 -47
- package/dist/cjs/lib/domMethods.js +91 -47
- package/dist/cjs/lib/domMethods.js.map +1 -1
- package/dist/cjs/lib/domevent.d.ts +87 -62
- package/dist/cjs/lib/domevent.js +85 -59
- package/dist/cjs/lib/domevent.js.map +1 -1
- package/dist/cjs/lib/emit.d.ts +62 -32
- package/dist/cjs/lib/emit.js +68 -53
- package/dist/cjs/lib/emit.js.map +1 -1
- package/dist/cjs/lib/kowrap.d.ts +6 -3
- package/dist/cjs/lib/kowrap.js +7 -3
- package/dist/cjs/lib/kowrap.js.map +1 -1
- package/dist/cjs/lib/obsArray.d.ts +91 -53
- package/dist/cjs/lib/obsArray.js +87 -54
- package/dist/cjs/lib/obsArray.js.map +1 -1
- package/dist/cjs/lib/observable.d.ts +25 -15
- package/dist/cjs/lib/observable.js +31 -19
- package/dist/cjs/lib/observable.js.map +1 -1
- package/dist/cjs/lib/pureComputed.d.ts +12 -15
- package/dist/cjs/lib/pureComputed.js +16 -18
- package/dist/cjs/lib/pureComputed.js.map +1 -1
- package/dist/cjs/lib/styled.d.ts +78 -61
- package/dist/cjs/lib/styled.js +27 -79
- package/dist/cjs/lib/styled.js.map +1 -1
- package/dist/cjs/lib/subscribe.d.ts +41 -37
- package/dist/cjs/lib/subscribe.js +31 -39
- package/dist/cjs/lib/subscribe.js.map +1 -1
- package/dist/cjs/lib/util.js +2 -0
- package/dist/cjs/lib/util.js.map +1 -1
- package/dist/cjs/lib/widgets/input.d.ts +3 -1
- package/dist/cjs/lib/widgets/input.js +7 -4
- package/dist/cjs/lib/widgets/input.js.map +1 -1
- package/dist/cjs/lib/widgets/select.d.ts +4 -2
- package/dist/cjs/lib/widgets/select.js +8 -5
- package/dist/cjs/lib/widgets/select.js.map +1 -1
- package/dist/esm/lib/_computed_queue.js +3 -3
- package/dist/esm/lib/_computed_queue.js.map +1 -1
- package/dist/esm/lib/binding.js +2 -2
- package/dist/esm/lib/binding.js.map +1 -1
- package/dist/esm/lib/browserGlobals.js +1 -0
- package/dist/esm/lib/browserGlobals.js.map +1 -1
- package/dist/esm/lib/computed.js +36 -50
- package/dist/esm/lib/computed.js.map +1 -1
- package/dist/esm/lib/dispose.js +104 -78
- package/dist/esm/lib/dispose.js.map +1 -1
- package/dist/esm/lib/dom.js +40 -18
- package/dist/esm/lib/dom.js.map +1 -1
- package/dist/esm/lib/domComponent.js +45 -44
- package/dist/esm/lib/domComponent.js.map +1 -1
- package/dist/esm/lib/domComputed.js +32 -5
- package/dist/esm/lib/domComputed.js.map +1 -1
- package/dist/esm/lib/domDispose.js +26 -11
- package/dist/esm/lib/domDispose.js.map +1 -1
- package/dist/esm/lib/domForEach.js +40 -41
- package/dist/esm/lib/domForEach.js.map +1 -1
- package/dist/esm/lib/domImpl.js +26 -7
- package/dist/esm/lib/domImpl.js.map +1 -1
- package/dist/esm/lib/domMethods.js +77 -35
- package/dist/esm/lib/domMethods.js.map +1 -1
- package/dist/esm/lib/domevent.js +84 -59
- package/dist/esm/lib/domevent.js.map +1 -1
- package/dist/esm/lib/emit.js +67 -53
- package/dist/esm/lib/emit.js.map +1 -1
- package/dist/esm/lib/kowrap.js +5 -2
- package/dist/esm/lib/kowrap.js.map +1 -1
- package/dist/esm/lib/obsArray.js +82 -50
- package/dist/esm/lib/obsArray.js.map +1 -1
- package/dist/esm/lib/observable.js +26 -15
- package/dist/esm/lib/observable.js.map +1 -1
- package/dist/esm/lib/pureComputed.js +15 -18
- package/dist/esm/lib/pureComputed.js.map +1 -1
- package/dist/esm/lib/styled.js +24 -77
- package/dist/esm/lib/styled.js.map +1 -1
- package/dist/esm/lib/subscribe.js +27 -36
- package/dist/esm/lib/subscribe.js.map +1 -1
- package/dist/esm/lib/util.js +1 -0
- package/dist/esm/lib/util.js.map +1 -1
- package/dist/esm/lib/widgets/input.js +3 -1
- package/dist/esm/lib/widgets/input.js.map +1 -1
- package/dist/esm/lib/widgets/select.js +3 -1
- package/dist/esm/lib/widgets/select.js.map +1 -1
- package/dist/grain-full.debug.js +2138 -3052
- package/dist/grain-full.debug.js.map +7 -0
- package/dist/grain-full.min.js +6 -2
- package/dist/grain-full.min.js.map +7 -1
- package/lib/binding.ts +9 -2
- package/lib/browserGlobals.ts +3 -1
- package/lib/computed.ts +56 -56
- package/lib/dispose.ts +110 -85
- package/lib/dom.ts +41 -20
- package/lib/domComponent.ts +68 -70
- package/lib/domComputed.ts +66 -21
- package/lib/domDispose.ts +28 -11
- package/lib/domForEach.ts +13 -12
- package/lib/domImpl.ts +30 -7
- package/lib/domMethods.ts +101 -46
- package/lib/domevent.ts +86 -61
- package/lib/emit.ts +64 -50
- package/lib/kowrap.ts +5 -2
- package/lib/obsArray.ts +89 -54
- package/lib/observable.ts +26 -15
- package/lib/pureComputed.ts +16 -22
- package/lib/styled.ts +85 -71
- package/lib/subscribe.ts +41 -45
- package/lib/util.ts +1 -0
- package/lib/widgets/input.ts +3 -1
- package/lib/widgets/select.ts +3 -1
- package/package.json +48 -38
|
@@ -1,86 +1,106 @@
|
|
|
1
|
+
import { IDisposable } from './dispose';
|
|
2
|
+
import { DomElementMethod, DomMethod } from './domImpl';
|
|
3
|
+
export type EventName = keyof HTMLElementEventMap;
|
|
4
|
+
export type EventType<E extends EventName | string> = E extends EventName ? HTMLElementEventMap[E] : Event;
|
|
5
|
+
export type EventCB<E extends Event = Event, T extends EventTarget = EventTarget> = (this: void, event: E, elem: T) => void;
|
|
1
6
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* dom('div', dom.on('click', (event, elem) => { ... }));
|
|
8
|
-
*
|
|
9
|
-
* When the div is disposed, the listener is automatically removed.
|
|
10
|
-
*
|
|
11
|
-
* The underlying interface to listen to an event is this:
|
|
12
|
-
*
|
|
13
|
-
* let listener = dom.onElem(elem, 'click', (event, elem) => { ... });
|
|
14
|
-
*
|
|
15
|
-
* The callback is called with the event and the element to which it was attached. Unlike in
|
|
16
|
-
* JQuery, the callback's return value is ignored. Use event.stopPropagation() and
|
|
17
|
-
* event.preventDefault() explicitly if needed.
|
|
7
|
+
* Listen to a DOM event, returning the listener object.
|
|
8
|
+
* ```ts
|
|
9
|
+
* const listener = dom.onElem(elem, 'click', (event, elem) => { ... });
|
|
10
|
+
* ```
|
|
18
11
|
*
|
|
19
12
|
* To stop listening:
|
|
13
|
+
* ```ts
|
|
14
|
+
* listener.dispose();
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Disposing the listener returned by `onElem()` is the only way to stop listening to an event. You
|
|
18
|
+
* can use `autoDispose` to stop listening automatically when subscribing in a `Disposable` object:
|
|
19
|
+
* ```ts
|
|
20
|
+
* this.autoDispose(domevent.onElem(document, 'mouseup', callback));
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* If you need "once" semantics, i.e. to remove the callback on first call, here's a useful pattern:
|
|
24
|
+
* ```ts
|
|
25
|
+
* const lis = domevent.onElem(elem, 'mouseup', e => { lis.dispose(); other_work(); });
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @param elem - DOM Element to listen to.
|
|
29
|
+
* @param eventType - Event type to listen for (e.g. `'click'`).
|
|
30
|
+
* @param callback - Callback to call as `callback(event, elem)`, where elem is `elem`.
|
|
31
|
+
* @param options - `useCapture: boolean`: Add the listener in the capture phase. This should very
|
|
32
|
+
* rarely be useful (e.g. JQuery doesn't even offer it as an option).
|
|
33
|
+
* @returns Listener object whose `.dispose()` method will remove the event listener.
|
|
34
|
+
*/
|
|
35
|
+
export declare function onElem<E extends EventName | string, T extends EventTarget>(elem: T, eventType: E, callback: EventCB<EventType<E>, T>, { useCapture }?: {
|
|
36
|
+
useCapture?: boolean | undefined;
|
|
37
|
+
}): IDisposable;
|
|
38
|
+
/**
|
|
39
|
+
* Listen to a DOM event. It is typically used as an argument to the `dom()` function:
|
|
40
|
+
* ```ts
|
|
41
|
+
* dom('div', dom.on('click', (event, elem) => { ... }));
|
|
42
|
+
* ```
|
|
20
43
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* Disposing the listener returned by .onElem() is the only way to stop listening to an event. You
|
|
24
|
-
* can use autoDispose to stop listening automatically when subscribing in a Disposable object:
|
|
44
|
+
* When the div is disposed, the listener is automatically removed.
|
|
25
45
|
*
|
|
26
|
-
*
|
|
46
|
+
* The callback is called with the event and the element to which it was attached. Unlike in, say,
|
|
47
|
+
* JQuery, the callback's return value is ignored. Use `event.stopPropagation()` and
|
|
48
|
+
* `event.preventDefault()` explicitly if needed.
|
|
27
49
|
*
|
|
28
50
|
* To listen to descendants of an element matching the given selector (what JQuery calls
|
|
29
|
-
* "delegated events", see http://api.jquery.com/on/)
|
|
30
|
-
*
|
|
31
|
-
* dom('div', dom.onMatch('.selector', 'click', (event, elem) => { ... }));
|
|
32
|
-
* or
|
|
33
|
-
* let lis = domevent.onMatchElem(elem, '.selector', 'click', (event, el) => { ... });
|
|
51
|
+
* "delegated events", see http://api.jquery.com/on/), see [`onMatch`](#onMatch).
|
|
34
52
|
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* let lis = domevent.onElem(elem, 'mouseup', e => { lis.dispose(); other_work(); });
|
|
40
|
-
*/
|
|
41
|
-
import { IDisposable } from './dispose';
|
|
42
|
-
import { DomElementMethod, DomMethod } from './domImpl';
|
|
43
|
-
export declare type EventName = keyof HTMLElementEventMap;
|
|
44
|
-
export declare type EventType<E extends EventName | string> = E extends EventName ? HTMLElementEventMap[E] : Event;
|
|
45
|
-
export declare type EventCB<E extends Event = Event, T extends EventTarget = EventTarget> = (this: void, event: E, elem: T) => void;
|
|
46
|
-
/**
|
|
47
|
-
* Listen to a DOM event. The `on()` variant takes no `elem` argument, and may be used as an
|
|
48
|
-
* argument to dom() function.
|
|
49
|
-
* @param {DOMElement} elem: DOM Element to listen to.
|
|
50
|
-
* @param {String} eventType: Event type to listen for (e.g. 'click').
|
|
51
|
-
* @param {Function} callback: Callback to call as `callback(event, elem)`, where elem is `elem`.
|
|
52
|
-
* @param [Boolean] options.useCapture: Add the listener in the capture phase. This should very
|
|
53
|
-
* rarely be useful (e.g. JQuery doesn't even offer it as an option).
|
|
54
|
-
* @returns {Object} Listener object whose .dispose() method will remove the event listener.
|
|
53
|
+
* @param eventType - Event type to listen for (e.g. `'click'`).
|
|
54
|
+
* @param callback - Callback to call as `callback(event, elem)`, where `elem` is the element this
|
|
55
|
+
* listener is attached to.
|
|
56
|
+
* @param options - `useCapture?: boolean`: Add the listener in the capture phase.
|
|
55
57
|
*/
|
|
56
|
-
export declare function onElem<E extends EventName | string, T extends EventTarget>(elem: T, eventType: E, callback: EventCB<EventType<E>, T>, { useCapture }?: {
|
|
57
|
-
useCapture?: boolean | undefined;
|
|
58
|
-
}): IDisposable;
|
|
59
58
|
export declare function on<E extends EventName | string, T extends EventTarget>(eventType: E, callback: EventCB<EventType<E>, T>, { useCapture }?: {
|
|
60
59
|
useCapture?: boolean | undefined;
|
|
61
60
|
}): DomMethod<T>;
|
|
62
61
|
/**
|
|
63
|
-
* Listen to a DOM event on descendants of the given elem matching the given selector.
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
62
|
+
* Listen to a DOM event on descendants of the given elem matching the given selector.
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* const let lis = domevent.onMatchElem(elem, '.selector', 'click', (event, el) => { ... });
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* @param elem - DOM Element to whose descendants to listen.
|
|
69
|
+
* @param selector - CSS selector string to filter elements that trigger this event.
|
|
67
70
|
* JQuery calls it "delegated events" (http://api.jquery.com/on/). The callback will only be
|
|
68
71
|
* called when the event occurs for an element matching the given selector. If there are
|
|
69
72
|
* multiple elements matching the selector, the callback is only called for the innermost one.
|
|
70
|
-
* @param
|
|
71
|
-
* @param
|
|
73
|
+
* @param eventType - Event type to listen for (e.g. 'click').
|
|
74
|
+
* @param callback - Callback to call as `callback(event, elem)`, where elem is a
|
|
72
75
|
* descendent of `elem` which matches `selector`.
|
|
73
|
-
* @param
|
|
74
|
-
*
|
|
75
|
-
* @returns {Object} Listener object whose .dispose() method will remove the event listener.
|
|
76
|
+
* @param options - `useCapture?: boolean`: Add the listener in the capture phase.
|
|
77
|
+
* @returns Listener object whose `.dispose()` method will remove the event listener.
|
|
76
78
|
*/
|
|
77
79
|
export declare function onMatchElem(elem: EventTarget, selector: string, eventType: string, callback: EventCB, { useCapture }?: {
|
|
78
80
|
useCapture?: boolean | undefined;
|
|
79
81
|
}): IDisposable;
|
|
82
|
+
/**
|
|
83
|
+
* Listen to a DOM event on descendants of the given element matching the given selector.
|
|
84
|
+
*
|
|
85
|
+
* This is similar to JQuery's [delegated events](https://api.jquery.com/on/#direct-and-delegated-events)
|
|
86
|
+
*
|
|
87
|
+
* ```ts
|
|
88
|
+
* dom('div', dom.onMatch('.selector', 'click', (event, elem) => { ... }));
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* In this usage, the element passed to the callback will be a DOM element matching the given
|
|
92
|
+
* selector. If there are multiple matches, the callback is only called for the innermost one.
|
|
93
|
+
*
|
|
94
|
+
* @param selector - CSS selector string to filter elements that trigger this event.
|
|
95
|
+
* @param eventType - Event type to listen for (e.g. `'click'`).
|
|
96
|
+
* @param callback - Callback to call as `callback(event, elem)`, where `elem` is an element
|
|
97
|
+
* matching `selector`.
|
|
98
|
+
* @param options - `useCapture?: boolean`: Add the listener in the capture phase.
|
|
99
|
+
*/
|
|
80
100
|
export declare function onMatch(selector: string, eventType: string, callback: EventCB, { useCapture }?: {
|
|
81
101
|
useCapture?: boolean | undefined;
|
|
82
102
|
}): DomElementMethod;
|
|
83
|
-
export
|
|
103
|
+
export type KeyEventType = 'keypress' | 'keyup' | 'keydown';
|
|
84
104
|
export interface IKeyHandlers<T extends HTMLElement = HTMLElement> {
|
|
85
105
|
[key: string]: (this: void, ev: KeyboardEvent, elem: T) => void;
|
|
86
106
|
}
|
|
@@ -88,8 +108,6 @@ export interface IKeyHandlers<T extends HTMLElement = HTMLElement> {
|
|
|
88
108
|
* Listen to key events (typically 'keydown' or 'keypress'), with specified per-key callbacks.
|
|
89
109
|
* Key names are listed at https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values
|
|
90
110
|
*
|
|
91
|
-
* Methods onKeyPress() and onKeyDown() are intended to be used as arguments to dom().
|
|
92
|
-
*
|
|
93
111
|
* By default, handled events are stopped from bubbling with stopPropagation() and
|
|
94
112
|
* preventDefault(). If, however, you register a key with a "$" suffix (i.e. "Enter$" instead of
|
|
95
113
|
* "Enter"), then the event is allowed to bubble normally.
|
|
@@ -98,7 +116,7 @@ export interface IKeyHandlers<T extends HTMLElement = HTMLElement> {
|
|
|
98
116
|
* to allow this element to receive keyboard events.
|
|
99
117
|
*
|
|
100
118
|
* For example:
|
|
101
|
-
*
|
|
119
|
+
* ```
|
|
102
120
|
* dom('input', ...
|
|
103
121
|
* dom.onKeyDown({
|
|
104
122
|
* Enter: (e, elem) => console.log("Enter pressed"),
|
|
@@ -106,7 +124,14 @@ export interface IKeyHandlers<T extends HTMLElement = HTMLElement> {
|
|
|
106
124
|
* Delete$: (e, elem) => console.log("Delete pressed, will bubble"),
|
|
107
125
|
* })
|
|
108
126
|
* )
|
|
127
|
+
* ```
|
|
109
128
|
*/
|
|
110
129
|
export declare function onKeyElem<T extends HTMLElement>(elem: T, evType: KeyEventType, keyHandlers: IKeyHandlers<T>): IDisposable;
|
|
130
|
+
/**
|
|
131
|
+
* Add listeners to `"keypress"` events. See [`onKeyElem`](#onKeyElem) for details.
|
|
132
|
+
*/
|
|
111
133
|
export declare function onKeyPress<T extends HTMLElement>(keyHandlers: IKeyHandlers<T>): DomMethod<T>;
|
|
134
|
+
/**
|
|
135
|
+
* Add listeners to `"keydown"` events. See [`onKeyElem`](#onKeyElem) for details.
|
|
136
|
+
*/
|
|
112
137
|
export declare function onKeyDown<T extends HTMLElement>(keyHandlers: IKeyHandlers<T>): DomMethod<T>;
|
package/dist/cjs/lib/domevent.js
CHANGED
|
@@ -1,45 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* domevent provides a way to listen to DOM events, similar to JQuery's `on()` function. Its
|
|
4
|
-
* methods are also exposed via the dom.js module, as `dom.on()`, etc.
|
|
5
|
-
*
|
|
6
|
-
* It is typically used as an argument to the dom() function:
|
|
7
|
-
*
|
|
8
|
-
* dom('div', dom.on('click', (event, elem) => { ... }));
|
|
9
|
-
*
|
|
10
|
-
* When the div is disposed, the listener is automatically removed.
|
|
11
|
-
*
|
|
12
|
-
* The underlying interface to listen to an event is this:
|
|
13
|
-
*
|
|
14
|
-
* let listener = dom.onElem(elem, 'click', (event, elem) => { ... });
|
|
15
|
-
*
|
|
16
|
-
* The callback is called with the event and the element to which it was attached. Unlike in
|
|
17
|
-
* JQuery, the callback's return value is ignored. Use event.stopPropagation() and
|
|
18
|
-
* event.preventDefault() explicitly if needed.
|
|
19
|
-
*
|
|
20
|
-
* To stop listening:
|
|
21
|
-
*
|
|
22
|
-
* listener.dispose();
|
|
23
|
-
*
|
|
24
|
-
* Disposing the listener returned by .onElem() is the only way to stop listening to an event. You
|
|
25
|
-
* can use autoDispose to stop listening automatically when subscribing in a Disposable object:
|
|
26
|
-
*
|
|
27
|
-
* this.autoDispose(domevent.onElem(document, 'mouseup', callback));
|
|
28
|
-
*
|
|
29
|
-
* To listen to descendants of an element matching the given selector (what JQuery calls
|
|
30
|
-
* "delegated events", see http://api.jquery.com/on/):
|
|
31
|
-
*
|
|
32
|
-
* dom('div', dom.onMatch('.selector', 'click', (event, elem) => { ... }));
|
|
33
|
-
* or
|
|
34
|
-
* let lis = domevent.onMatchElem(elem, '.selector', 'click', (event, el) => { ... });
|
|
35
|
-
*
|
|
36
|
-
* In this usage, the element passed to the callback will be a DOM element matching the given
|
|
37
|
-
* selector. If there are multiple matches, the callback is only called for the innermost one.
|
|
38
|
-
*
|
|
39
|
-
* If you need to remove the callback on first call, here's a useful pattern:
|
|
40
|
-
* let lis = domevent.onElem(elem, 'mouseup', e => { lis.dispose(); other_work(); });
|
|
41
|
-
*/
|
|
42
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.onKeyDown = exports.onKeyPress = exports.onKeyElem = exports.onMatch = exports.onMatchElem = exports.on = exports.onElem = void 0;
|
|
43
4
|
function _findMatch(inner, outer, selector) {
|
|
44
5
|
for (let el = inner; el && el !== outer; el = el.parentElement) {
|
|
45
6
|
if (el.matches(selector)) {
|
|
@@ -75,43 +36,103 @@ class DomEventMatchListener extends DomEventListener {
|
|
|
75
36
|
}
|
|
76
37
|
}
|
|
77
38
|
/**
|
|
78
|
-
* Listen to a DOM event
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
39
|
+
* Listen to a DOM event, returning the listener object.
|
|
40
|
+
* ```ts
|
|
41
|
+
* const listener = dom.onElem(elem, 'click', (event, elem) => { ... });
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* To stop listening:
|
|
45
|
+
* ```ts
|
|
46
|
+
* listener.dispose();
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* Disposing the listener returned by `onElem()` is the only way to stop listening to an event. You
|
|
50
|
+
* can use `autoDispose` to stop listening automatically when subscribing in a `Disposable` object:
|
|
51
|
+
* ```ts
|
|
52
|
+
* this.autoDispose(domevent.onElem(document, 'mouseup', callback));
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* If you need "once" semantics, i.e. to remove the callback on first call, here's a useful pattern:
|
|
56
|
+
* ```ts
|
|
57
|
+
* const lis = domevent.onElem(elem, 'mouseup', e => { lis.dispose(); other_work(); });
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* @param elem - DOM Element to listen to.
|
|
61
|
+
* @param eventType - Event type to listen for (e.g. `'click'`).
|
|
62
|
+
* @param callback - Callback to call as `callback(event, elem)`, where elem is `elem`.
|
|
63
|
+
* @param options - `useCapture: boolean`: Add the listener in the capture phase. This should very
|
|
84
64
|
* rarely be useful (e.g. JQuery doesn't even offer it as an option).
|
|
85
|
-
* @returns
|
|
65
|
+
* @returns Listener object whose `.dispose()` method will remove the event listener.
|
|
86
66
|
*/
|
|
87
67
|
function onElem(elem, eventType, callback, { useCapture = false } = {}) {
|
|
88
68
|
return new DomEventListener(elem, eventType, callback, useCapture);
|
|
89
69
|
}
|
|
90
70
|
exports.onElem = onElem;
|
|
71
|
+
/**
|
|
72
|
+
* Listen to a DOM event. It is typically used as an argument to the `dom()` function:
|
|
73
|
+
* ```ts
|
|
74
|
+
* dom('div', dom.on('click', (event, elem) => { ... }));
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* When the div is disposed, the listener is automatically removed.
|
|
78
|
+
*
|
|
79
|
+
* The callback is called with the event and the element to which it was attached. Unlike in, say,
|
|
80
|
+
* JQuery, the callback's return value is ignored. Use `event.stopPropagation()` and
|
|
81
|
+
* `event.preventDefault()` explicitly if needed.
|
|
82
|
+
*
|
|
83
|
+
* To listen to descendants of an element matching the given selector (what JQuery calls
|
|
84
|
+
* "delegated events", see http://api.jquery.com/on/), see [`onMatch`](#onMatch).
|
|
85
|
+
*
|
|
86
|
+
* @param eventType - Event type to listen for (e.g. `'click'`).
|
|
87
|
+
* @param callback - Callback to call as `callback(event, elem)`, where `elem` is the element this
|
|
88
|
+
* listener is attached to.
|
|
89
|
+
* @param options - `useCapture?: boolean`: Add the listener in the capture phase.
|
|
90
|
+
*/
|
|
91
91
|
function on(eventType, callback, { useCapture = false } = {}) {
|
|
92
92
|
// tslint:disable-next-line:no-unused-expression
|
|
93
93
|
return (elem) => { new DomEventListener(elem, eventType, callback, useCapture); };
|
|
94
94
|
}
|
|
95
95
|
exports.on = on;
|
|
96
96
|
/**
|
|
97
|
-
* Listen to a DOM event on descendants of the given elem matching the given selector.
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
97
|
+
* Listen to a DOM event on descendants of the given elem matching the given selector.
|
|
98
|
+
*
|
|
99
|
+
* ```ts
|
|
100
|
+
* const let lis = domevent.onMatchElem(elem, '.selector', 'click', (event, el) => { ... });
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* @param elem - DOM Element to whose descendants to listen.
|
|
104
|
+
* @param selector - CSS selector string to filter elements that trigger this event.
|
|
101
105
|
* JQuery calls it "delegated events" (http://api.jquery.com/on/). The callback will only be
|
|
102
106
|
* called when the event occurs for an element matching the given selector. If there are
|
|
103
107
|
* multiple elements matching the selector, the callback is only called for the innermost one.
|
|
104
|
-
* @param
|
|
105
|
-
* @param
|
|
108
|
+
* @param eventType - Event type to listen for (e.g. 'click').
|
|
109
|
+
* @param callback - Callback to call as `callback(event, elem)`, where elem is a
|
|
106
110
|
* descendent of `elem` which matches `selector`.
|
|
107
|
-
* @param
|
|
108
|
-
*
|
|
109
|
-
* @returns {Object} Listener object whose .dispose() method will remove the event listener.
|
|
111
|
+
* @param options - `useCapture?: boolean`: Add the listener in the capture phase.
|
|
112
|
+
* @returns Listener object whose `.dispose()` method will remove the event listener.
|
|
110
113
|
*/
|
|
111
114
|
function onMatchElem(elem, selector, eventType, callback, { useCapture = false } = {}) {
|
|
112
115
|
return new DomEventMatchListener(elem, eventType, callback, useCapture, selector);
|
|
113
116
|
}
|
|
114
117
|
exports.onMatchElem = onMatchElem;
|
|
118
|
+
/**
|
|
119
|
+
* Listen to a DOM event on descendants of the given element matching the given selector.
|
|
120
|
+
*
|
|
121
|
+
* This is similar to JQuery's [delegated events](https://api.jquery.com/on/#direct-and-delegated-events)
|
|
122
|
+
*
|
|
123
|
+
* ```ts
|
|
124
|
+
* dom('div', dom.onMatch('.selector', 'click', (event, elem) => { ... }));
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* In this usage, the element passed to the callback will be a DOM element matching the given
|
|
128
|
+
* selector. If there are multiple matches, the callback is only called for the innermost one.
|
|
129
|
+
*
|
|
130
|
+
* @param selector - CSS selector string to filter elements that trigger this event.
|
|
131
|
+
* @param eventType - Event type to listen for (e.g. `'click'`).
|
|
132
|
+
* @param callback - Callback to call as `callback(event, elem)`, where `elem` is an element
|
|
133
|
+
* matching `selector`.
|
|
134
|
+
* @param options - `useCapture?: boolean`: Add the listener in the capture phase.
|
|
135
|
+
*/
|
|
115
136
|
function onMatch(selector, eventType, callback, { useCapture = false } = {}) {
|
|
116
137
|
// tslint:disable-next-line:no-unused-expression
|
|
117
138
|
return (elem) => { new DomEventMatchListener(elem, eventType, callback, useCapture, selector); };
|
|
@@ -121,8 +142,6 @@ exports.onMatch = onMatch;
|
|
|
121
142
|
* Listen to key events (typically 'keydown' or 'keypress'), with specified per-key callbacks.
|
|
122
143
|
* Key names are listed at https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values
|
|
123
144
|
*
|
|
124
|
-
* Methods onKeyPress() and onKeyDown() are intended to be used as arguments to dom().
|
|
125
|
-
*
|
|
126
145
|
* By default, handled events are stopped from bubbling with stopPropagation() and
|
|
127
146
|
* preventDefault(). If, however, you register a key with a "$" suffix (i.e. "Enter$" instead of
|
|
128
147
|
* "Enter"), then the event is allowed to bubble normally.
|
|
@@ -131,7 +150,7 @@ exports.onMatch = onMatch;
|
|
|
131
150
|
* to allow this element to receive keyboard events.
|
|
132
151
|
*
|
|
133
152
|
* For example:
|
|
134
|
-
*
|
|
153
|
+
* ```
|
|
135
154
|
* dom('input', ...
|
|
136
155
|
* dom.onKeyDown({
|
|
137
156
|
* Enter: (e, elem) => console.log("Enter pressed"),
|
|
@@ -139,6 +158,7 @@ exports.onMatch = onMatch;
|
|
|
139
158
|
* Delete$: (e, elem) => console.log("Delete pressed, will bubble"),
|
|
140
159
|
* })
|
|
141
160
|
* )
|
|
161
|
+
* ```
|
|
142
162
|
*/
|
|
143
163
|
function onKeyElem(elem, evType, keyHandlers) {
|
|
144
164
|
if (!(elem.tabIndex >= 0)) { // If tabIndex property is undefined or -1,
|
|
@@ -157,10 +177,16 @@ function onKeyElem(elem, evType, keyHandlers) {
|
|
|
157
177
|
});
|
|
158
178
|
}
|
|
159
179
|
exports.onKeyElem = onKeyElem;
|
|
180
|
+
/**
|
|
181
|
+
* Add listeners to `"keypress"` events. See [`onKeyElem`](#onKeyElem) for details.
|
|
182
|
+
*/
|
|
160
183
|
function onKeyPress(keyHandlers) {
|
|
161
184
|
return (elem) => { onKeyElem(elem, 'keypress', keyHandlers); };
|
|
162
185
|
}
|
|
163
186
|
exports.onKeyPress = onKeyPress;
|
|
187
|
+
/**
|
|
188
|
+
* Add listeners to `"keydown"` events. See [`onKeyElem`](#onKeyElem) for details.
|
|
189
|
+
*/
|
|
164
190
|
function onKeyDown(keyHandlers) {
|
|
165
191
|
return (elem) => { onKeyElem(elem, 'keydown', keyHandlers); };
|
|
166
192
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"domevent.js","sourceRoot":"","sources":["../../../lib/domevent.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"domevent.js","sourceRoot":"","sources":["../../../lib/domevent.ts"],"names":[],"mappings":"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"}
|
package/dist/cjs/lib/emit.d.ts
CHANGED
|
@@ -1,29 +1,12 @@
|
|
|
1
|
+
export type ListenerCB<T> = (this: T, ...args: any[]) => void;
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* This is similar to Backbone events, with more focus on efficiency. Both inserting and removing
|
|
6
|
-
* listeners is constant time.
|
|
7
|
-
*
|
|
8
|
-
* To create an emitter:
|
|
9
|
-
* let emitter = new Emitter();
|
|
10
|
-
*
|
|
11
|
-
* To add a listener:
|
|
12
|
-
* let listener = fooEmitter.addListener(callback);
|
|
13
|
-
* To remove a listener:
|
|
14
|
-
* listener.dispose();
|
|
15
|
-
*
|
|
16
|
-
* The only way to remove a listener is to dispose the Listener object returned by addListener().
|
|
17
|
-
* You can often use autoDispose to do this automatically when subscribing in a constructor:
|
|
18
|
-
* this.autoDispose(fooEmitter.addListener(this.onFoo, this));
|
|
19
|
-
*
|
|
20
|
-
* To emit an event, call emit() with any number of arguments:
|
|
21
|
-
* emitter.emit("hello", "world");
|
|
3
|
+
* A callback that listens to _changes_ in the Emitter listeners. This is mainly used for
|
|
4
|
+
* internal purposes.
|
|
22
5
|
*/
|
|
23
|
-
export
|
|
24
|
-
export declare type ChangeCB = (hasListeners: boolean) => void;
|
|
6
|
+
export type ChangeCB = (hasListeners: boolean) => void;
|
|
25
7
|
/**
|
|
26
8
|
* This is an implementation of a doubly-linked list, with just the minimal functionality we need.
|
|
9
|
+
* @internal
|
|
27
10
|
*/
|
|
28
11
|
export declare class LLink {
|
|
29
12
|
protected _next: LLink | null;
|
|
@@ -34,18 +17,62 @@ export declare class LLink {
|
|
|
34
17
|
protected _removeNode(node: LLink): void;
|
|
35
18
|
protected _disposeList(): void;
|
|
36
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* An `Emitter` emits events to a list of listeners. Listeners are
|
|
22
|
+
* simply functions to call, and "emitting an event" just calls those functions.
|
|
23
|
+
*
|
|
24
|
+
* This is similar to Backbone events, with more focus on efficiency. Both inserting and removing
|
|
25
|
+
* listeners is constant time.
|
|
26
|
+
*
|
|
27
|
+
* To create an emitter:
|
|
28
|
+
* ```ts
|
|
29
|
+
* const emitter = new Emitter();
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* To add a listener:
|
|
33
|
+
* ```ts
|
|
34
|
+
* const listener = fooEmitter.addListener(callback);
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* To remove a listener:
|
|
38
|
+
* ```ts
|
|
39
|
+
* listener.dispose();
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* The only way to remove a listener is to dispose the `Listener` object returned by `addListener()`.
|
|
43
|
+
* You can often use autoDispose to do this automatically when subscribing in a constructor:
|
|
44
|
+
* ```ts
|
|
45
|
+
* this.autoDispose(fooEmitter.addListener(this.onFoo, this));
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* To emit an event, call `emit()` with any number of arguments:
|
|
49
|
+
* ```ts
|
|
50
|
+
* emitter.emit("hello", "world");
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @privateRemarks
|
|
54
|
+
*
|
|
55
|
+
* Note about a possible alternative implementation.
|
|
56
|
+
*
|
|
57
|
+
* We could implement the same interface using an array of listeners. Certain issues apply, in
|
|
58
|
+
* particular with removing listeners from inside emit(), and in ensuring that removals are
|
|
59
|
+
* constant time on average. Such an implementation was attempted and timed. The result is that
|
|
60
|
+
* compared to the linked-list implementation here, add/remove combination could be made nearly
|
|
61
|
+
* twice faster (on average), while emit and add/remove/emit are consistently slightly slower.
|
|
62
|
+
*
|
|
63
|
+
* The implementation here was chosen based on those timings, and as the simpler one. For example,
|
|
64
|
+
* on one setup (macbook, node4, 5-listener queue), add+remove take 0.1us, while add+remove+emit
|
|
65
|
+
* take 3.82us. (In array-based implementation with same set up, add+remove is 0.06us, while
|
|
66
|
+
* add+remove+emit is 4.80us.)
|
|
67
|
+
*/
|
|
37
68
|
export declare class Emitter extends LLink {
|
|
38
69
|
private _changeCB;
|
|
39
70
|
private _changeCBContext;
|
|
40
|
-
/**
|
|
41
|
-
* Constructs an Emitter object.
|
|
42
|
-
*/
|
|
43
|
-
constructor();
|
|
44
71
|
/**
|
|
45
72
|
* Adds a listening callback to the list of functions to call on emit().
|
|
46
|
-
* @param
|
|
47
|
-
* @param
|
|
48
|
-
* @returns
|
|
73
|
+
* @param callback - Function to call.
|
|
74
|
+
* @param optContext - Context for the function.
|
|
75
|
+
* @returns Listener object. Its dispose() method removes the callback from the list.
|
|
49
76
|
*/
|
|
50
77
|
addListener<T>(callback: ListenerCB<T>, optContext?: T): Listener;
|
|
51
78
|
/**
|
|
@@ -54,7 +81,7 @@ export declare class Emitter extends LLink {
|
|
|
54
81
|
emit(...args: any[]): void;
|
|
55
82
|
/**
|
|
56
83
|
* Sets the single callback that would get called when a listener is added or removed.
|
|
57
|
-
* @param
|
|
84
|
+
* @param changeCB - Function to call after a listener is added or
|
|
58
85
|
* removed. It's called with a boolean indicating whether this Emitter has any listeners.
|
|
59
86
|
* Pass in `null` to unset the callback. Note that it can be called multiple times in a row
|
|
60
87
|
* with hasListeners `true`.
|
|
@@ -62,6 +89,7 @@ export declare class Emitter extends LLink {
|
|
|
62
89
|
setChangeCB(changeCB: ChangeCB, optContext?: any): void;
|
|
63
90
|
/**
|
|
64
91
|
* Helper used by Listener class, but not intended for public usage.
|
|
92
|
+
* @internal
|
|
65
93
|
*/
|
|
66
94
|
_triggerChangeCB(): void;
|
|
67
95
|
/**
|
|
@@ -75,14 +103,16 @@ export declare class Emitter extends LLink {
|
|
|
75
103
|
dispose(): void;
|
|
76
104
|
}
|
|
77
105
|
/**
|
|
78
|
-
* Listener object wraps a callback added to an Emitter, allowing for O(1) removal when the
|
|
79
|
-
* listener is disposed.
|
|
106
|
+
* The `Listener` object wraps a callback added to an Emitter, allowing for O(1) removal when the
|
|
107
|
+
* listener is disposed. It implements `IDisposable`.
|
|
80
108
|
*/
|
|
81
109
|
export declare class Listener extends LLink {
|
|
82
110
|
private emitter;
|
|
83
111
|
private callback;
|
|
84
112
|
private context?;
|
|
113
|
+
/** @internal */
|
|
85
114
|
static callAll(begin: LLink, end: LLink, args: any[]): void;
|
|
86
115
|
constructor(emitter: Emitter, callback: ListenerCB<any>, context?: any);
|
|
116
|
+
/** @internal */
|
|
87
117
|
dispose(): void;
|
|
88
118
|
}
|