westures-core 1.0.0-alpha.5 → 1.2.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/CHANGELOG.md +10 -0
- package/README.md +137 -56
- package/dist/index.js +8 -8
- package/dist/index.js.map +1 -1
- package/package.json +30 -17
- package/src/Gesture.js +34 -20
- package/src/Input.js +9 -6
- package/src/Point2D.js +12 -4
- package/src/PointerData.js +0 -4
- package/src/Region.js +23 -34
- package/src/Smoothable.js +2 -6
- package/src/State.js +2 -6
- package/src/constants.js +27 -20
- package/src/utils.js +2 -6
- package/.travis.yml +0 -6
package/src/Region.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Contains the Region class
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
1
|
'use strict';
|
|
6
2
|
|
|
7
3
|
const State = require('./State.js');
|
|
@@ -31,17 +27,22 @@ const {
|
|
|
31
27
|
*
|
|
32
28
|
* @memberof westures-core
|
|
33
29
|
*
|
|
34
|
-
* @param {Element} element - The element which should listen to input
|
|
30
|
+
* @param {Element} element=window - The element which should listen to input
|
|
31
|
+
* events.
|
|
35
32
|
* @param {object} [options]
|
|
36
33
|
* @param {boolean} [options.capture=false] - Whether the region uses the
|
|
37
34
|
* capture phase of input events. If false, uses the bubbling phase.
|
|
35
|
+
* @param {boolean} [options.preferPointer=true] - If false, the region listens
|
|
36
|
+
* to mouse/touch events instead of pointer events.
|
|
38
37
|
* @param {boolean} [options.preventDefault=true] - Whether the default
|
|
39
38
|
* browser functionality should be disabled. This option should most likely be
|
|
40
39
|
* ignored. Here there by dragons if set to false.
|
|
40
|
+
* @param {string} [options.touchAction='none'] - Value to set the CSS
|
|
41
|
+
* 'touch-action' property to on elements added to the region.
|
|
41
42
|
*/
|
|
42
43
|
class Region {
|
|
43
|
-
constructor(element, options = {}) {
|
|
44
|
-
|
|
44
|
+
constructor(element = window, options = {}) {
|
|
45
|
+
options = { ...Region.DEFAULTS, ...options };
|
|
45
46
|
|
|
46
47
|
/**
|
|
47
48
|
* The list of relations between elements, their gestures, and the handlers.
|
|
@@ -73,19 +74,11 @@ class Region {
|
|
|
73
74
|
this.element = element;
|
|
74
75
|
|
|
75
76
|
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
* @type {boolean}
|
|
79
|
-
*/
|
|
80
|
-
this.capture = settings.capture;
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Whether the default browser functionality should be disabled. This option
|
|
84
|
-
* should most likely be ignored. Here there by dragons if set to false.
|
|
77
|
+
* The user-supplied options for the Region.
|
|
85
78
|
*
|
|
86
|
-
* @type {
|
|
79
|
+
* @type {object}
|
|
87
80
|
*/
|
|
88
|
-
this.
|
|
81
|
+
this.options = options;
|
|
89
82
|
|
|
90
83
|
/**
|
|
91
84
|
* The internal state object for a Region. Keeps track of inputs.
|
|
@@ -106,19 +99,13 @@ class Region {
|
|
|
106
99
|
*/
|
|
107
100
|
activate() {
|
|
108
101
|
/*
|
|
109
|
-
* Having to listen to both mouse and touch events is annoying, but
|
|
110
|
-
* necessary due to conflicting standards and browser implementations.
|
|
111
|
-
* Pointer is a fallback for now instead of the primary, until I figure out
|
|
112
|
-
* all the details to do with pointer-events and touch-action and their
|
|
113
|
-
* cross browser compatibility.
|
|
114
|
-
*
|
|
115
102
|
* Listening to both mouse and touch comes with the difficulty that
|
|
116
103
|
* preventDefault() must be called to prevent both events from iterating
|
|
117
104
|
* through the system. However I have left it as an option to the end user,
|
|
118
105
|
* which defaults to calling preventDefault(), in case there's a use-case I
|
|
119
106
|
* haven't considered or am not aware of.
|
|
120
107
|
*
|
|
121
|
-
* It
|
|
108
|
+
* It also may be a good idea to keep regions small in large pages.
|
|
122
109
|
*
|
|
123
110
|
* See:
|
|
124
111
|
* https://www.html5rocks.com/en/mobile/touchandmouse/
|
|
@@ -126,17 +113,17 @@ class Region {
|
|
|
126
113
|
* https://developer.mozilla.org/en-US/docs/Web/API/Pointer_events
|
|
127
114
|
*/
|
|
128
115
|
let eventNames = [];
|
|
129
|
-
if (
|
|
130
|
-
eventNames = MOUSE_EVENTS.concat(TOUCH_EVENTS);
|
|
131
|
-
} else {
|
|
116
|
+
if (this.options.preferPointer && window.PointerEvent) {
|
|
132
117
|
eventNames = POINTER_EVENTS;
|
|
118
|
+
} else {
|
|
119
|
+
eventNames = MOUSE_EVENTS.concat(TOUCH_EVENTS);
|
|
133
120
|
}
|
|
134
121
|
|
|
135
122
|
// Bind detected browser events to the region element.
|
|
136
123
|
const arbitrate = this.arbitrate.bind(this);
|
|
137
124
|
eventNames.forEach(eventName => {
|
|
138
125
|
this.element.addEventListener(eventName, arbitrate, {
|
|
139
|
-
capture: this.capture,
|
|
126
|
+
capture: this.options.capture,
|
|
140
127
|
once: false,
|
|
141
128
|
passive: false,
|
|
142
129
|
});
|
|
@@ -161,7 +148,7 @@ class Region {
|
|
|
161
148
|
* @param {Event} event - The event emitted from the window object.
|
|
162
149
|
*/
|
|
163
150
|
cancel(event) {
|
|
164
|
-
event.preventDefault();
|
|
151
|
+
if (this.options.preventDefault) event.preventDefault();
|
|
165
152
|
this.state.inputs.forEach(input => {
|
|
166
153
|
input.update(event);
|
|
167
154
|
});
|
|
@@ -274,7 +261,7 @@ class Region {
|
|
|
274
261
|
this.updateActiveGestures(event, isInitial);
|
|
275
262
|
|
|
276
263
|
if (this.activeGestures.size > 0) {
|
|
277
|
-
if (this.preventDefault) event.preventDefault();
|
|
264
|
+
if (this.options.preventDefault) event.preventDefault();
|
|
278
265
|
|
|
279
266
|
this.activeGestures.forEach(gesture => {
|
|
280
267
|
gesture.evaluateHook(PHASE[event.type], this.state);
|
|
@@ -291,6 +278,7 @@ class Region {
|
|
|
291
278
|
* @param {westures-core.Gesture} gesture - Instantiated gesture to add.
|
|
292
279
|
*/
|
|
293
280
|
addGesture(gesture) {
|
|
281
|
+
gesture.element.style.touchAction = this.options.touchAction;
|
|
294
282
|
this.gestures.add(gesture);
|
|
295
283
|
}
|
|
296
284
|
|
|
@@ -326,10 +314,11 @@ class Region {
|
|
|
326
314
|
}
|
|
327
315
|
}
|
|
328
316
|
|
|
329
|
-
Region.DEFAULTS =
|
|
317
|
+
Region.DEFAULTS = {
|
|
330
318
|
capture: false,
|
|
319
|
+
preferPointer: true,
|
|
331
320
|
preventDefault: true,
|
|
332
|
-
|
|
321
|
+
touchAction: 'none',
|
|
322
|
+
};
|
|
333
323
|
|
|
334
324
|
module.exports = Region;
|
|
335
|
-
|
package/src/Smoothable.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Contains the abstract Pinch class.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
1
|
'use strict';
|
|
6
2
|
|
|
7
3
|
const cascade = Symbol('cascade');
|
|
@@ -132,10 +128,10 @@ class Smoothable {
|
|
|
132
128
|
}
|
|
133
129
|
}
|
|
134
130
|
|
|
135
|
-
Smoothable.DEFAULTS =
|
|
131
|
+
Smoothable.DEFAULTS = {
|
|
136
132
|
applySmoothing: true,
|
|
137
133
|
identity: 0,
|
|
138
|
-
}
|
|
134
|
+
};
|
|
139
135
|
|
|
140
136
|
module.exports = Smoothable;
|
|
141
137
|
|
package/src/State.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Contains the {@link State} class
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
1
|
'use strict';
|
|
6
2
|
|
|
7
3
|
const {
|
|
@@ -14,9 +10,9 @@ const {
|
|
|
14
10
|
const Input = require('./Input.js');
|
|
15
11
|
const Point2D = require('./Point2D.js');
|
|
16
12
|
|
|
17
|
-
const symbols =
|
|
13
|
+
const symbols = {
|
|
18
14
|
inputs: Symbol.for('inputs'),
|
|
19
|
-
}
|
|
15
|
+
};
|
|
20
16
|
|
|
21
17
|
/**
|
|
22
18
|
* Set of helper functions for updating inputs based on type of input.
|
package/src/constants.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Contains event list definitions, PHASE mapping, and other constants.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
1
|
'use strict';
|
|
6
2
|
|
|
7
3
|
/**
|
|
@@ -10,11 +6,12 @@
|
|
|
10
6
|
* @memberof westures-core
|
|
11
7
|
* @type {string[]}
|
|
12
8
|
*/
|
|
13
|
-
const CANCEL_EVENTS =
|
|
9
|
+
const CANCEL_EVENTS = [
|
|
14
10
|
'blur',
|
|
15
11
|
'pointercancel',
|
|
16
12
|
'touchcancel',
|
|
17
|
-
|
|
13
|
+
'mouseleave',
|
|
14
|
+
];
|
|
18
15
|
|
|
19
16
|
/**
|
|
20
17
|
* List of keyboard events that trigger a restart.
|
|
@@ -22,10 +19,10 @@ const CANCEL_EVENTS = Object.freeze([
|
|
|
22
19
|
* @memberof westures-core
|
|
23
20
|
* @type {string[]}
|
|
24
21
|
*/
|
|
25
|
-
const KEYBOARD_EVENTS =
|
|
22
|
+
const KEYBOARD_EVENTS = [
|
|
26
23
|
'keydown',
|
|
27
24
|
'keyup',
|
|
28
|
-
]
|
|
25
|
+
];
|
|
29
26
|
|
|
30
27
|
/**
|
|
31
28
|
* List of mouse events to listen to.
|
|
@@ -33,11 +30,11 @@ const KEYBOARD_EVENTS = Object.freeze([
|
|
|
33
30
|
* @memberof westures-core
|
|
34
31
|
* @type {string[]}
|
|
35
32
|
*/
|
|
36
|
-
const MOUSE_EVENTS =
|
|
33
|
+
const MOUSE_EVENTS = [
|
|
37
34
|
'mousedown',
|
|
38
35
|
'mousemove',
|
|
39
36
|
'mouseup',
|
|
40
|
-
]
|
|
37
|
+
];
|
|
41
38
|
|
|
42
39
|
/**
|
|
43
40
|
* List of pointer events to listen to.
|
|
@@ -45,11 +42,11 @@ const MOUSE_EVENTS = Object.freeze([
|
|
|
45
42
|
* @memberof westures-core
|
|
46
43
|
* @type {string[]}
|
|
47
44
|
*/
|
|
48
|
-
const POINTER_EVENTS =
|
|
45
|
+
const POINTER_EVENTS = [
|
|
49
46
|
'pointerdown',
|
|
50
47
|
'pointermove',
|
|
51
48
|
'pointerup',
|
|
52
|
-
]
|
|
49
|
+
];
|
|
53
50
|
|
|
54
51
|
/**
|
|
55
52
|
* List of touch events to listen to.
|
|
@@ -57,24 +54,25 @@ const POINTER_EVENTS = Object.freeze([
|
|
|
57
54
|
* @memberof westures-core
|
|
58
55
|
* @type {string[]}
|
|
59
56
|
*/
|
|
60
|
-
const TOUCH_EVENTS =
|
|
57
|
+
const TOUCH_EVENTS = [
|
|
61
58
|
'touchend',
|
|
62
59
|
'touchmove',
|
|
63
60
|
'touchstart',
|
|
64
|
-
]
|
|
61
|
+
];
|
|
65
62
|
|
|
66
63
|
/**
|
|
67
64
|
* List of potentially state-modifying keys.
|
|
65
|
+
* Entries are: ['altKey', 'ctrlKey', 'metaKey', 'shiftKey'].
|
|
68
66
|
*
|
|
69
67
|
* @memberof westures-core
|
|
70
68
|
* @type {string[]}
|
|
71
69
|
*/
|
|
72
|
-
const STATE_KEYS =
|
|
70
|
+
const STATE_KEYS = [
|
|
73
71
|
'altKey',
|
|
74
72
|
'ctrlKey',
|
|
75
73
|
'metaKey',
|
|
76
74
|
'shiftKey',
|
|
77
|
-
]
|
|
75
|
+
];
|
|
78
76
|
|
|
79
77
|
/**
|
|
80
78
|
* List of the 'key' values on KeyboardEvent objects of the potentially
|
|
@@ -83,12 +81,12 @@ const STATE_KEYS = Object.freeze([
|
|
|
83
81
|
* @memberof westures-core
|
|
84
82
|
* @type {string[]}
|
|
85
83
|
*/
|
|
86
|
-
const STATE_KEY_STRINGS =
|
|
84
|
+
const STATE_KEY_STRINGS = [
|
|
87
85
|
'Alt',
|
|
88
86
|
'Control',
|
|
89
87
|
'Meta',
|
|
90
88
|
'Shift',
|
|
91
|
-
]
|
|
89
|
+
];
|
|
92
90
|
|
|
93
91
|
/**
|
|
94
92
|
* The cancel phase.
|
|
@@ -122,6 +120,14 @@ const MOVE = 'move';
|
|
|
122
120
|
*/
|
|
123
121
|
const START = 'start';
|
|
124
122
|
|
|
123
|
+
/**
|
|
124
|
+
* The recognized phases.
|
|
125
|
+
*
|
|
126
|
+
* @memberof westures-core
|
|
127
|
+
* @type {list.<string>}
|
|
128
|
+
*/
|
|
129
|
+
const PHASES = [START, MOVE, END, CANCEL];
|
|
130
|
+
|
|
125
131
|
/**
|
|
126
132
|
* Object that normalizes the names of window events to be either of type start,
|
|
127
133
|
* move, end, or cancel.
|
|
@@ -129,7 +135,7 @@ const START = 'start';
|
|
|
129
135
|
* @memberof westures-core
|
|
130
136
|
* @type {object}
|
|
131
137
|
*/
|
|
132
|
-
const PHASE =
|
|
138
|
+
const PHASE = {
|
|
133
139
|
blur: CANCEL,
|
|
134
140
|
pointercancel: CANCEL,
|
|
135
141
|
touchcancel: CANCEL,
|
|
@@ -145,7 +151,7 @@ const PHASE = Object.freeze({
|
|
|
145
151
|
mousedown: START,
|
|
146
152
|
pointerdown: START,
|
|
147
153
|
touchstart: START,
|
|
148
|
-
}
|
|
154
|
+
};
|
|
149
155
|
|
|
150
156
|
module.exports = {
|
|
151
157
|
CANCEL_EVENTS,
|
|
@@ -163,5 +169,6 @@ module.exports = {
|
|
|
163
169
|
START,
|
|
164
170
|
|
|
165
171
|
PHASE,
|
|
172
|
+
PHASES,
|
|
166
173
|
};
|
|
167
174
|
|
package/src/utils.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Contains various helpful utilities.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
1
|
'use strict';
|
|
6
2
|
|
|
7
3
|
const PI_2 = 2 * Math.PI;
|
|
@@ -89,10 +85,10 @@ function setDifference(left, right) {
|
|
|
89
85
|
return setFilter(left, element => !right.has(element));
|
|
90
86
|
}
|
|
91
87
|
|
|
92
|
-
module.exports =
|
|
88
|
+
module.exports = {
|
|
93
89
|
angularDifference,
|
|
94
90
|
getPropagationPath,
|
|
95
91
|
setDifference,
|
|
96
92
|
setFilter,
|
|
97
|
-
}
|
|
93
|
+
};
|
|
98
94
|
|