westures-core 1.2.0 → 1.2.1
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/dist/index.js +1249 -22
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,22 +1,1249 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
/**
|
|
2
|
+
* The global API interface for westures-core. Exposes all classes, constants,
|
|
3
|
+
* and routines used by the package. Use responsibly.
|
|
4
|
+
*
|
|
5
|
+
* @namespace westures-core
|
|
6
|
+
*/ 'use strict';
|
|
7
|
+
var $de0d6a332419bf3c$exports = {};
|
|
8
|
+
'use strict';
|
|
9
|
+
let $de0d6a332419bf3c$var$g_id = 0;
|
|
10
|
+
/**
|
|
11
|
+
* The Gesture class that all gestures inherit from. A custom gesture class will
|
|
12
|
+
* need to override some or all of the four phase "hooks": start, move, end, and
|
|
13
|
+
* cancel.
|
|
14
|
+
*
|
|
15
|
+
* @memberof westures-core
|
|
16
|
+
*
|
|
17
|
+
* @param {string} type - The name of the gesture.
|
|
18
|
+
* @param {Element} element - The element to which to associate the gesture.
|
|
19
|
+
* @param {Function} handler - The function handler to execute when a gesture
|
|
20
|
+
* is recognized on the associated element.
|
|
21
|
+
* @param {object} [options] - Generic gesture options
|
|
22
|
+
* @param {westures-core.STATE_KEYS[]} [options.enableKeys=[]] - List of keys
|
|
23
|
+
* which will enable the gesture. The gesture will not be recognized unless one
|
|
24
|
+
* of these keys is pressed while the interaction occurs. If not specified or an
|
|
25
|
+
* empty list, the gesture is treated as though the enable key is always down.
|
|
26
|
+
* @param {westures-core.STATE_KEYS[]} [options.disableKeys=[]] - List of keys
|
|
27
|
+
* which will disable the gesture. The gesture will not be recognized if one of
|
|
28
|
+
* these keys is pressed while the interaction occurs. If not specified or an
|
|
29
|
+
* empty list, the gesture is treated as though the disable key is never down.
|
|
30
|
+
* @param {number} [options.minInputs=1] - The minimum number of pointers that
|
|
31
|
+
* must be active for the gesture to be recognized. Uses >=.
|
|
32
|
+
* @param {number} [options.maxInputs=Number.MAX_VALUE] - The maximum number of
|
|
33
|
+
* pointers that may be active for the gesture to be recognized. Uses <=.
|
|
34
|
+
*/ class $de0d6a332419bf3c$var$Gesture {
|
|
35
|
+
constructor(type, element, handler, options = {
|
|
36
|
+
}){
|
|
37
|
+
if (typeof type !== 'string') throw new TypeError('Gestures require a string type / name');
|
|
38
|
+
/**
|
|
39
|
+
* The name of the gesture. (e.g. 'pan' or 'tap' or 'pinch').
|
|
40
|
+
*
|
|
41
|
+
* @type {string}
|
|
42
|
+
*/ this.type = type;
|
|
43
|
+
/**
|
|
44
|
+
* The unique identifier for each gesture. This allows for distinctions
|
|
45
|
+
* across instances of Gestures that are created on the fly (e.g.
|
|
46
|
+
* gesture-tap-1, gesture-tap-2).
|
|
47
|
+
*
|
|
48
|
+
* @type {string}
|
|
49
|
+
*/ this.id = `gesture-${this.type}-${$de0d6a332419bf3c$var$g_id++}`;
|
|
50
|
+
/**
|
|
51
|
+
* The element to which to associate the gesture.
|
|
52
|
+
*
|
|
53
|
+
* @type {Element}
|
|
54
|
+
*/ this.element = element;
|
|
55
|
+
/**
|
|
56
|
+
* The function handler to execute when the gesture is recognized on the
|
|
57
|
+
* associated element.
|
|
58
|
+
*
|
|
59
|
+
* @type {Function}
|
|
60
|
+
*/ this.handler = handler;
|
|
61
|
+
/**
|
|
62
|
+
* The options. Can usually be adjusted live, though be careful doing this.
|
|
63
|
+
*
|
|
64
|
+
* @type {object}
|
|
65
|
+
*/ this.options = {
|
|
66
|
+
...$de0d6a332419bf3c$var$Gesture.DEFAULTS,
|
|
67
|
+
...options
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Determines whether this gesture is enabled.
|
|
72
|
+
*
|
|
73
|
+
* @param {westures-core.State} state - The input state object of the current
|
|
74
|
+
* region.
|
|
75
|
+
*
|
|
76
|
+
* @return {boolean} true if enabled, false otherwise.
|
|
77
|
+
*/ isEnabled(state) {
|
|
78
|
+
const count = state.active.length;
|
|
79
|
+
const event = state.event;
|
|
80
|
+
const { enableKeys: enableKeys , disableKeys: disableKeys , minInputs: minInputs , maxInputs: maxInputs } = this.options;
|
|
81
|
+
return minInputs <= count && maxInputs >= count && (enableKeys.length === 0 || enableKeys.some((k)=>event[k]
|
|
82
|
+
)) && !disableKeys.some((k)=>event[k]
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Event hook for the start phase of a gesture.
|
|
87
|
+
*
|
|
88
|
+
* @param {westures-core.State} state - The input state object of the current
|
|
89
|
+
* region.
|
|
90
|
+
*
|
|
91
|
+
* @return {?Object} Gesture is considered recognized if an Object is
|
|
92
|
+
* returned.
|
|
93
|
+
*/ start() {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Event hook for the move phase of a gesture.
|
|
98
|
+
*
|
|
99
|
+
* @param {westures-core.State} state - The input state object of the current
|
|
100
|
+
* region.
|
|
101
|
+
*
|
|
102
|
+
* @return {?Object} Gesture is considered recognized if an Object is
|
|
103
|
+
* returned.
|
|
104
|
+
*/ move() {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Event hook for the end phase of a gesture.
|
|
109
|
+
*
|
|
110
|
+
* @param {westures-core.State} state - The input state object of the current
|
|
111
|
+
* region.
|
|
112
|
+
*
|
|
113
|
+
* @return {?Object} Gesture is considered recognized if an Object is
|
|
114
|
+
* returned.
|
|
115
|
+
*/ end() {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Event hook for when an input is cancelled.
|
|
120
|
+
*
|
|
121
|
+
* @param {westures-core.State} state - The input state object of the current
|
|
122
|
+
* region.
|
|
123
|
+
*
|
|
124
|
+
* @return {?Object} Gesture is considered recognized if an Object is
|
|
125
|
+
* returned.
|
|
126
|
+
*/ cancel() {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Evalutes the given gesture hook, and dispatches any data that is produced
|
|
131
|
+
* by calling [recognize]{@link westures-core.Gesture#recognize}.
|
|
132
|
+
*
|
|
133
|
+
* @param {string} hook - Must be one of 'start', 'move', 'end', or 'cancel'.
|
|
134
|
+
* @param {westures-core.State} state - The current State instance.
|
|
135
|
+
*/ evaluateHook(hook, state) {
|
|
136
|
+
const data = this[hook](state);
|
|
137
|
+
if (data) this.recognize(hook, state, data);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Recognize a Gesture by calling the handler. Standardizes the way the
|
|
141
|
+
* handler is called so that classes extending Gesture can circumvent the
|
|
142
|
+
* evaluateHook approach but still provide results that have a common format.
|
|
143
|
+
*
|
|
144
|
+
* Note that the properties in the "data" object will receive priority when
|
|
145
|
+
* constructing the results. This can be used to override standard results
|
|
146
|
+
* such as the phase or the centroid.
|
|
147
|
+
*
|
|
148
|
+
* @param {string} hook - Must be one of 'start', 'move', 'end', or 'cancel'.
|
|
149
|
+
* @param {westures-core.State} state - current input state.
|
|
150
|
+
* @param {Object} data - Results data specific to the recognized gesture.
|
|
151
|
+
*/ recognize(hook, state, data) {
|
|
152
|
+
this.handler({
|
|
153
|
+
centroid: state.centroid,
|
|
154
|
+
event: state.event,
|
|
155
|
+
phase: hook,
|
|
156
|
+
type: this.type,
|
|
157
|
+
target: this.element,
|
|
158
|
+
...data
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
$de0d6a332419bf3c$var$Gesture.DEFAULTS = {
|
|
163
|
+
enableKeys: [],
|
|
164
|
+
disableKeys: [],
|
|
165
|
+
minInputs: 1,
|
|
166
|
+
maxInputs: Number.MAX_VALUE
|
|
167
|
+
};
|
|
168
|
+
$de0d6a332419bf3c$exports = $de0d6a332419bf3c$var$Gesture;
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
var $e2125e2e71e37a0c$exports = {};
|
|
172
|
+
'use strict';
|
|
173
|
+
var $0ca7bfe1c074e8ca$exports = {};
|
|
174
|
+
'use strict';
|
|
175
|
+
var $6c3676f10a43b740$exports = {};
|
|
176
|
+
'use strict';
|
|
177
|
+
/**
|
|
178
|
+
* The Point2D class stores and operates on 2-dimensional points, represented as
|
|
179
|
+
* x and y coordinates.
|
|
180
|
+
*
|
|
181
|
+
* @memberof westures-core
|
|
182
|
+
*
|
|
183
|
+
* @param {number} [ x=0 ] - The x coordinate of the point.
|
|
184
|
+
* @param {number} [ y=0 ] - The y coordinate of the point.
|
|
185
|
+
*/ class $6c3676f10a43b740$var$Point2D {
|
|
186
|
+
constructor(x = 0, y = 0){
|
|
187
|
+
/**
|
|
188
|
+
* The x coordinate of the point.
|
|
189
|
+
*
|
|
190
|
+
* @type {number}
|
|
191
|
+
*/ this.x = x;
|
|
192
|
+
/**
|
|
193
|
+
* The y coordinate of the point.
|
|
194
|
+
*
|
|
195
|
+
* @type {number}
|
|
196
|
+
*/ this.y = y;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Calculates the angle between this point and the given point.
|
|
200
|
+
*
|
|
201
|
+
* @param {!westures-core.Point2D} point - Projected point for calculating the
|
|
202
|
+
* angle.
|
|
203
|
+
*
|
|
204
|
+
* @return {number} Radians along the unit circle where the projected
|
|
205
|
+
* point lies.
|
|
206
|
+
*/ angleTo(point) {
|
|
207
|
+
return Math.atan2(point.y - this.y, point.x - this.x);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Determine the angle from the centroid to each of the points.
|
|
211
|
+
*
|
|
212
|
+
* @param {!westures-core.Point2D[]} points - the Point2D objects to calculate
|
|
213
|
+
* the angles to.
|
|
214
|
+
*
|
|
215
|
+
* @returns {number[]}
|
|
216
|
+
*/ anglesTo(points) {
|
|
217
|
+
return points.map((point)=>this.angleTo(point)
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Determine the average distance from this point to the provided array of
|
|
222
|
+
* points.
|
|
223
|
+
*
|
|
224
|
+
* @param {!westures-core.Point2D[]} points - the Point2D objects to calculate
|
|
225
|
+
* the average distance to.
|
|
226
|
+
*
|
|
227
|
+
* @return {number} The average distance from this point to the provided
|
|
228
|
+
* points.
|
|
229
|
+
*/ averageDistanceTo(points) {
|
|
230
|
+
return this.totalDistanceTo(points) / points.length;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Clone this point.
|
|
234
|
+
*
|
|
235
|
+
* @return {westures-core.Point2D} A new Point2D, identical to this point.
|
|
236
|
+
*/ clone() {
|
|
237
|
+
return new $6c3676f10a43b740$var$Point2D(this.x, this.y);
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Calculates the distance between two points.
|
|
241
|
+
*
|
|
242
|
+
* @param {!westures-core.Point2D} point - Point to which the distance is
|
|
243
|
+
* calculated.
|
|
244
|
+
*
|
|
245
|
+
* @return {number} The distance between the two points, a.k.a. the
|
|
246
|
+
* hypoteneuse.
|
|
247
|
+
*/ distanceTo(point) {
|
|
248
|
+
return Math.hypot(point.x - this.x, point.y - this.y);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Subtract the given point from this point.
|
|
252
|
+
*
|
|
253
|
+
* @param {!westures-core.Point2D} point - Point to subtract from this point.
|
|
254
|
+
*
|
|
255
|
+
* @return {westures-core.Point2D} A new Point2D, which is the result of (this
|
|
256
|
+
* - point).
|
|
257
|
+
*/ minus(point) {
|
|
258
|
+
return new $6c3676f10a43b740$var$Point2D(this.x - point.x, this.y - point.y);
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Return the summation of this point to the given point.
|
|
262
|
+
*
|
|
263
|
+
* @param {!westures-core.Point2D} point - Point to add to this point.
|
|
264
|
+
*
|
|
265
|
+
* @return {westures-core.Point2D} A new Point2D, which is the addition of the
|
|
266
|
+
* two points.
|
|
267
|
+
*/ plus(point) {
|
|
268
|
+
return new $6c3676f10a43b740$var$Point2D(this.x + point.x, this.y + point.y);
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Calculates the total distance from this point to an array of points.
|
|
272
|
+
*
|
|
273
|
+
* @param {!westures-core.Point2D[]} points - The array of Point2D objects to
|
|
274
|
+
* calculate the total distance to.
|
|
275
|
+
*
|
|
276
|
+
* @return {number} The total distance from this point to the provided points.
|
|
277
|
+
*/ totalDistanceTo(points) {
|
|
278
|
+
return points.reduce((d, p)=>d + this.distanceTo(p)
|
|
279
|
+
, 0);
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Calculates the centroid of a list of points.
|
|
283
|
+
*
|
|
284
|
+
* @param {westures-core.Point2D[]} points - The array of Point2D objects for
|
|
285
|
+
* which to calculate the centroid.
|
|
286
|
+
*
|
|
287
|
+
* @return {westures-core.Point2D} The centroid of the provided points.
|
|
288
|
+
*/ static centroid(points = []) {
|
|
289
|
+
if (points.length === 0) return null;
|
|
290
|
+
const total = $6c3676f10a43b740$var$Point2D.sum(points);
|
|
291
|
+
total.x /= points.length;
|
|
292
|
+
total.y /= points.length;
|
|
293
|
+
return total;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Calculates the sum of the given points.
|
|
297
|
+
*
|
|
298
|
+
* @param {westures-core.Point2D[]} points - The Point2D objects to sum up.
|
|
299
|
+
*
|
|
300
|
+
* @return {westures-core.Point2D} A new Point2D representing the sum of the
|
|
301
|
+
* given points.
|
|
302
|
+
*/ static sum(points = []) {
|
|
303
|
+
return points.reduce((total, pt)=>{
|
|
304
|
+
total.x += pt.x;
|
|
305
|
+
total.y += pt.y;
|
|
306
|
+
return total;
|
|
307
|
+
}, new $6c3676f10a43b740$var$Point2D(0, 0));
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
$6c3676f10a43b740$exports = $6c3676f10a43b740$var$Point2D;
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
var $be6f0e84320366a7$exports = {};
|
|
314
|
+
'use strict';
|
|
315
|
+
/**
|
|
316
|
+
* List of events that trigger the cancel phase.
|
|
317
|
+
*
|
|
318
|
+
* @memberof westures-core
|
|
319
|
+
* @type {string[]}
|
|
320
|
+
*/ const $be6f0e84320366a7$var$CANCEL_EVENTS = [
|
|
321
|
+
'blur',
|
|
322
|
+
'pointercancel',
|
|
323
|
+
'touchcancel',
|
|
324
|
+
'mouseleave',
|
|
325
|
+
];
|
|
326
|
+
/**
|
|
327
|
+
* List of keyboard events that trigger a restart.
|
|
328
|
+
*
|
|
329
|
+
* @memberof westures-core
|
|
330
|
+
* @type {string[]}
|
|
331
|
+
*/ const $be6f0e84320366a7$var$KEYBOARD_EVENTS = [
|
|
332
|
+
'keydown',
|
|
333
|
+
'keyup',
|
|
334
|
+
];
|
|
335
|
+
/**
|
|
336
|
+
* List of mouse events to listen to.
|
|
337
|
+
*
|
|
338
|
+
* @memberof westures-core
|
|
339
|
+
* @type {string[]}
|
|
340
|
+
*/ const $be6f0e84320366a7$var$MOUSE_EVENTS = [
|
|
341
|
+
'mousedown',
|
|
342
|
+
'mousemove',
|
|
343
|
+
'mouseup',
|
|
344
|
+
];
|
|
345
|
+
/**
|
|
346
|
+
* List of pointer events to listen to.
|
|
347
|
+
*
|
|
348
|
+
* @memberof westures-core
|
|
349
|
+
* @type {string[]}
|
|
350
|
+
*/ const $be6f0e84320366a7$var$POINTER_EVENTS = [
|
|
351
|
+
'pointerdown',
|
|
352
|
+
'pointermove',
|
|
353
|
+
'pointerup',
|
|
354
|
+
];
|
|
355
|
+
/**
|
|
356
|
+
* List of touch events to listen to.
|
|
357
|
+
*
|
|
358
|
+
* @memberof westures-core
|
|
359
|
+
* @type {string[]}
|
|
360
|
+
*/ const $be6f0e84320366a7$var$TOUCH_EVENTS = [
|
|
361
|
+
'touchend',
|
|
362
|
+
'touchmove',
|
|
363
|
+
'touchstart',
|
|
364
|
+
];
|
|
365
|
+
/**
|
|
366
|
+
* List of potentially state-modifying keys.
|
|
367
|
+
* Entries are: ['altKey', 'ctrlKey', 'metaKey', 'shiftKey'].
|
|
368
|
+
*
|
|
369
|
+
* @memberof westures-core
|
|
370
|
+
* @type {string[]}
|
|
371
|
+
*/ const $be6f0e84320366a7$var$STATE_KEYS = [
|
|
372
|
+
'altKey',
|
|
373
|
+
'ctrlKey',
|
|
374
|
+
'metaKey',
|
|
375
|
+
'shiftKey',
|
|
376
|
+
];
|
|
377
|
+
/**
|
|
378
|
+
* List of the 'key' values on KeyboardEvent objects of the potentially
|
|
379
|
+
* state-modifying keys.
|
|
380
|
+
*
|
|
381
|
+
* @memberof westures-core
|
|
382
|
+
* @type {string[]}
|
|
383
|
+
*/ const $be6f0e84320366a7$var$STATE_KEY_STRINGS = [
|
|
384
|
+
'Alt',
|
|
385
|
+
'Control',
|
|
386
|
+
'Meta',
|
|
387
|
+
'Shift',
|
|
388
|
+
];
|
|
389
|
+
/**
|
|
390
|
+
* The cancel phase.
|
|
391
|
+
*
|
|
392
|
+
* @memberof westures-core
|
|
393
|
+
* @type {string}
|
|
394
|
+
*/ const $be6f0e84320366a7$var$CANCEL = 'cancel';
|
|
395
|
+
/**
|
|
396
|
+
* The end phase.
|
|
397
|
+
*
|
|
398
|
+
* @memberof westures-core
|
|
399
|
+
* @type {string}
|
|
400
|
+
*/ const $be6f0e84320366a7$var$END = 'end';
|
|
401
|
+
/**
|
|
402
|
+
* The move phase.
|
|
403
|
+
*
|
|
404
|
+
* @memberof westures-core
|
|
405
|
+
* @type {string}
|
|
406
|
+
*/ const $be6f0e84320366a7$var$MOVE = 'move';
|
|
407
|
+
/**
|
|
408
|
+
* The start phase.
|
|
409
|
+
*
|
|
410
|
+
* @memberof westures-core
|
|
411
|
+
* @type {string}
|
|
412
|
+
*/ const $be6f0e84320366a7$var$START = 'start';
|
|
413
|
+
/**
|
|
414
|
+
* The recognized phases.
|
|
415
|
+
*
|
|
416
|
+
* @memberof westures-core
|
|
417
|
+
* @type {list.<string>}
|
|
418
|
+
*/ const $be6f0e84320366a7$var$PHASES = [
|
|
419
|
+
$be6f0e84320366a7$var$START,
|
|
420
|
+
$be6f0e84320366a7$var$MOVE,
|
|
421
|
+
$be6f0e84320366a7$var$END,
|
|
422
|
+
$be6f0e84320366a7$var$CANCEL
|
|
423
|
+
];
|
|
424
|
+
/**
|
|
425
|
+
* Object that normalizes the names of window events to be either of type start,
|
|
426
|
+
* move, end, or cancel.
|
|
427
|
+
*
|
|
428
|
+
* @memberof westures-core
|
|
429
|
+
* @type {object}
|
|
430
|
+
*/ const $be6f0e84320366a7$var$PHASE = {
|
|
431
|
+
blur: $be6f0e84320366a7$var$CANCEL,
|
|
432
|
+
pointercancel: $be6f0e84320366a7$var$CANCEL,
|
|
433
|
+
touchcancel: $be6f0e84320366a7$var$CANCEL,
|
|
434
|
+
mouseup: $be6f0e84320366a7$var$END,
|
|
435
|
+
pointerup: $be6f0e84320366a7$var$END,
|
|
436
|
+
touchend: $be6f0e84320366a7$var$END,
|
|
437
|
+
mousemove: $be6f0e84320366a7$var$MOVE,
|
|
438
|
+
pointermove: $be6f0e84320366a7$var$MOVE,
|
|
439
|
+
touchmove: $be6f0e84320366a7$var$MOVE,
|
|
440
|
+
mousedown: $be6f0e84320366a7$var$START,
|
|
441
|
+
pointerdown: $be6f0e84320366a7$var$START,
|
|
442
|
+
touchstart: $be6f0e84320366a7$var$START
|
|
443
|
+
};
|
|
444
|
+
$be6f0e84320366a7$exports = {
|
|
445
|
+
CANCEL_EVENTS: $be6f0e84320366a7$var$CANCEL_EVENTS,
|
|
446
|
+
KEYBOARD_EVENTS: $be6f0e84320366a7$var$KEYBOARD_EVENTS,
|
|
447
|
+
MOUSE_EVENTS: $be6f0e84320366a7$var$MOUSE_EVENTS,
|
|
448
|
+
POINTER_EVENTS: $be6f0e84320366a7$var$POINTER_EVENTS,
|
|
449
|
+
TOUCH_EVENTS: $be6f0e84320366a7$var$TOUCH_EVENTS,
|
|
450
|
+
STATE_KEYS: $be6f0e84320366a7$var$STATE_KEYS,
|
|
451
|
+
STATE_KEY_STRINGS: $be6f0e84320366a7$var$STATE_KEY_STRINGS,
|
|
452
|
+
CANCEL: $be6f0e84320366a7$var$CANCEL,
|
|
453
|
+
END: $be6f0e84320366a7$var$END,
|
|
454
|
+
MOVE: $be6f0e84320366a7$var$MOVE,
|
|
455
|
+
START: $be6f0e84320366a7$var$START,
|
|
456
|
+
PHASE: $be6f0e84320366a7$var$PHASE,
|
|
457
|
+
PHASES: $be6f0e84320366a7$var$PHASES
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
var $0ca7bfe1c074e8ca$require$PHASE = $be6f0e84320366a7$exports.PHASE;
|
|
462
|
+
/**
|
|
463
|
+
* @private
|
|
464
|
+
* @inner
|
|
465
|
+
* @memberof westures-core.PointerData
|
|
466
|
+
*
|
|
467
|
+
* @return {Event} The Event object which corresponds to the given identifier.
|
|
468
|
+
* Contains clientX, clientY values.
|
|
469
|
+
*/ function $0ca7bfe1c074e8ca$var$getEventObject(event, identifier) {
|
|
470
|
+
if (event.changedTouches) return Array.from(event.changedTouches).find((touch)=>{
|
|
471
|
+
return touch.identifier === identifier;
|
|
472
|
+
});
|
|
473
|
+
return event;
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Low-level storage of pointer data based on incoming data from an interaction
|
|
477
|
+
* event.
|
|
478
|
+
*
|
|
479
|
+
* @memberof westures-core
|
|
480
|
+
*
|
|
481
|
+
* @param {Event} event - The event object being wrapped.
|
|
482
|
+
* @param {number} identifier - The index of touch if applicable
|
|
483
|
+
*/ class $0ca7bfe1c074e8ca$var$PointerData {
|
|
484
|
+
constructor(event, identifier){
|
|
485
|
+
const { clientX: clientX , clientY: clientY } = $0ca7bfe1c074e8ca$var$getEventObject(event, identifier);
|
|
486
|
+
/**
|
|
487
|
+
* The original event object.
|
|
488
|
+
*
|
|
489
|
+
* @type {Event}
|
|
490
|
+
*/ this.event = event;
|
|
491
|
+
/**
|
|
492
|
+
* The type or 'phase' of this batch of pointer data. 'start' or 'move' or
|
|
493
|
+
* 'end' or 'cancel'
|
|
494
|
+
*
|
|
495
|
+
* @type {string}
|
|
496
|
+
*/ this.type = $0ca7bfe1c074e8ca$require$PHASE[event.type];
|
|
497
|
+
/**
|
|
498
|
+
* The timestamp of the event in milliseconds elapsed since January 1, 1970,
|
|
499
|
+
* 00:00:00 UTC.
|
|
500
|
+
*
|
|
501
|
+
* @type {number}
|
|
502
|
+
*/ this.time = Date.now();
|
|
503
|
+
/**
|
|
504
|
+
* The (x,y) coordinate of the event, wrapped in a Point2D.
|
|
505
|
+
*
|
|
506
|
+
* @type {westures-core.Point2D}
|
|
507
|
+
*/ this.point = new $6c3676f10a43b740$exports(clientX, clientY);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
$0ca7bfe1c074e8ca$exports = $0ca7bfe1c074e8ca$var$PointerData;
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
var $4559ecf940edc78d$exports = {};
|
|
514
|
+
'use strict';
|
|
515
|
+
const $4559ecf940edc78d$var$PI_2 = 2 * Math.PI;
|
|
516
|
+
const $4559ecf940edc78d$var$PI_NVE = -Math.PI;
|
|
517
|
+
/**
|
|
518
|
+
* Helper function to regulate angular differences, so they don't jump from 0 to
|
|
519
|
+
* 2 * PI or vice versa.
|
|
520
|
+
*
|
|
521
|
+
* @memberof westures-core
|
|
522
|
+
*
|
|
523
|
+
* @param {number} a - Angle in radians.
|
|
524
|
+
* @param {number} b - Angle in radians.
|
|
525
|
+
|
|
526
|
+
* @return {number} c, given by: c = a - b such that |c| < PI
|
|
527
|
+
*/ function $4559ecf940edc78d$var$angularDifference(a, b) {
|
|
528
|
+
let diff = a - b;
|
|
529
|
+
if (diff < $4559ecf940edc78d$var$PI_NVE) diff += $4559ecf940edc78d$var$PI_2;
|
|
530
|
+
else if (diff > Math.PI) diff -= $4559ecf940edc78d$var$PI_2;
|
|
531
|
+
return diff;
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* In case event.composedPath() is not available.
|
|
535
|
+
*
|
|
536
|
+
* @memberof westures-core
|
|
537
|
+
*
|
|
538
|
+
* @param {Event} event
|
|
539
|
+
*
|
|
540
|
+
* @return {Element[]} The elements along the composed path of the event.
|
|
541
|
+
*/ function $4559ecf940edc78d$var$getPropagationPath(event) {
|
|
542
|
+
if (typeof event.composedPath === 'function') return event.composedPath();
|
|
543
|
+
const path = [];
|
|
544
|
+
for(let node = event.target; node !== document; node = node.parentNode)path.push(node);
|
|
545
|
+
path.push(document);
|
|
546
|
+
path.push(window);
|
|
547
|
+
return path;
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Performs a set filter operation.
|
|
551
|
+
*
|
|
552
|
+
* @memberof westures-core
|
|
553
|
+
*
|
|
554
|
+
* @param {Set} set - The set to filter.
|
|
555
|
+
* @param {Function} predicate - Function to test elements of 'set'. Receives
|
|
556
|
+
* one argument: the current set element.
|
|
557
|
+
*
|
|
558
|
+
* @return {Set} Set consisting of elements in 'set' for which 'predicate' is
|
|
559
|
+
* true.
|
|
560
|
+
*/ function $4559ecf940edc78d$var$setFilter(set, predicate) {
|
|
561
|
+
const result = new Set();
|
|
562
|
+
set.forEach((element)=>{
|
|
563
|
+
if (predicate(element)) result.add(element);
|
|
564
|
+
});
|
|
565
|
+
return result;
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* Performs a set difference operation.
|
|
569
|
+
*
|
|
570
|
+
* @memberof westures-core
|
|
571
|
+
*
|
|
572
|
+
* @param {Set} left - Base set.
|
|
573
|
+
* @param {Set} right - Set of elements to remove from 'left'.
|
|
574
|
+
*
|
|
575
|
+
* @return {Set} Set consisting of elements in 'left' that are not in
|
|
576
|
+
* 'right'.
|
|
577
|
+
*/ function $4559ecf940edc78d$var$setDifference(left, right) {
|
|
578
|
+
return $4559ecf940edc78d$var$setFilter(left, (element)=>!right.has(element)
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
$4559ecf940edc78d$exports = {
|
|
582
|
+
angularDifference: $4559ecf940edc78d$var$angularDifference,
|
|
583
|
+
getPropagationPath: $4559ecf940edc78d$var$getPropagationPath,
|
|
584
|
+
setDifference: $4559ecf940edc78d$var$setDifference,
|
|
585
|
+
setFilter: $4559ecf940edc78d$var$setFilter
|
|
586
|
+
};
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
var $e2125e2e71e37a0c$require$getPropagationPath = $4559ecf940edc78d$exports.getPropagationPath;
|
|
590
|
+
/**
|
|
591
|
+
* Tracks a single input and contains information about the current, previous,
|
|
592
|
+
* and initial events.
|
|
593
|
+
*
|
|
594
|
+
* @memberof westures-core
|
|
595
|
+
*
|
|
596
|
+
* @param {(PointerEvent | MouseEvent | TouchEvent)} event - The input event
|
|
597
|
+
* which will initialize this Input object.
|
|
598
|
+
* @param {number} identifier - The identifier for this input, so that it can
|
|
599
|
+
* be located in subsequent Event objects.
|
|
600
|
+
*/ class $e2125e2e71e37a0c$var$Input {
|
|
601
|
+
constructor(event, identifier){
|
|
602
|
+
const currentData = new $0ca7bfe1c074e8ca$exports(event, identifier);
|
|
603
|
+
/**
|
|
604
|
+
* The set of elements along the original event's propagation path at the
|
|
605
|
+
* time it was dispatched.
|
|
606
|
+
*
|
|
607
|
+
* @type {WeakSet.<Element>}
|
|
608
|
+
*/ this.initialElements = new WeakSet($e2125e2e71e37a0c$require$getPropagationPath(event));
|
|
609
|
+
/**
|
|
610
|
+
* Holds the initial data from the mousedown / touchstart / pointerdown that
|
|
611
|
+
* began this input.
|
|
612
|
+
*
|
|
613
|
+
* @type {westures-core.PointerData}
|
|
614
|
+
*/ this.initial = currentData;
|
|
615
|
+
/**
|
|
616
|
+
* Holds the most current pointer data for this Input.
|
|
617
|
+
*
|
|
618
|
+
* @type {westures-core.PointerData}
|
|
619
|
+
*/ this.current = currentData;
|
|
620
|
+
/**
|
|
621
|
+
* Holds the previous pointer data for this Input.
|
|
622
|
+
*
|
|
623
|
+
* @type {westures-core.PointerData}
|
|
624
|
+
*/ this.previous = currentData;
|
|
625
|
+
/**
|
|
626
|
+
* The identifier for the pointer / touch / mouse button associated with
|
|
627
|
+
* this input.
|
|
628
|
+
*
|
|
629
|
+
* @type {number}
|
|
630
|
+
*/ this.identifier = identifier;
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* The phase of the input: 'start' or 'move' or 'end' or 'cancel'
|
|
634
|
+
*
|
|
635
|
+
* @type {string}
|
|
636
|
+
*/ get phase() {
|
|
637
|
+
return this.current.type;
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* The timestamp of the initiating event for this input.
|
|
641
|
+
*
|
|
642
|
+
* @type {number}
|
|
643
|
+
*/ get startTime() {
|
|
644
|
+
return this.initial.time;
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* The amount of time elapsed between the start of this input and its latest
|
|
648
|
+
* event.
|
|
649
|
+
*
|
|
650
|
+
* @type {number}
|
|
651
|
+
*/ get elapsedTime() {
|
|
652
|
+
return this.current.time - this.initial.time;
|
|
653
|
+
}
|
|
654
|
+
/**
|
|
655
|
+
* @return {number} The distance between the initiating event for this input
|
|
656
|
+
* and its current event.
|
|
657
|
+
*/ totalDistance() {
|
|
658
|
+
return this.initial.point.distanceTo(this.current.point);
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* Saves the given raw event in PointerData form as the current data for this
|
|
662
|
+
* input, pushing the old current data into the previous slot, and tossing
|
|
663
|
+
* out the old previous data.
|
|
664
|
+
*
|
|
665
|
+
* @param {Event} event - The event object to wrap with a PointerData.
|
|
666
|
+
*/ update(event) {
|
|
667
|
+
this.previous = this.current;
|
|
668
|
+
this.current = new $0ca7bfe1c074e8ca$exports(event, this.identifier);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
$e2125e2e71e37a0c$exports = $e2125e2e71e37a0c$var$Input;
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
var $b66a0f22c18e3e3d$exports = {};
|
|
677
|
+
'use strict';
|
|
678
|
+
var $639be6fb478a6d5a$exports = {};
|
|
679
|
+
'use strict';
|
|
680
|
+
|
|
681
|
+
var $639be6fb478a6d5a$require$CANCEL = $be6f0e84320366a7$exports.CANCEL;
|
|
682
|
+
var $639be6fb478a6d5a$require$END = $be6f0e84320366a7$exports.END;
|
|
683
|
+
var $639be6fb478a6d5a$require$MOVE = $be6f0e84320366a7$exports.MOVE;
|
|
684
|
+
var $639be6fb478a6d5a$require$PHASE = $be6f0e84320366a7$exports.PHASE;
|
|
685
|
+
var $639be6fb478a6d5a$require$START = $be6f0e84320366a7$exports.START;
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
const $639be6fb478a6d5a$var$symbols = {
|
|
689
|
+
inputs: Symbol.for('inputs')
|
|
690
|
+
};
|
|
691
|
+
/**
|
|
692
|
+
* Set of helper functions for updating inputs based on type of input.
|
|
693
|
+
* Must be called with a bound 'this', via bind(), or call(), or apply().
|
|
694
|
+
*
|
|
695
|
+
* @private
|
|
696
|
+
* @inner
|
|
697
|
+
* @memberof westure-core.State
|
|
698
|
+
*/ const $639be6fb478a6d5a$var$update_fns = {
|
|
699
|
+
TouchEvent: function TouchEvent(event) {
|
|
700
|
+
Array.from(event.changedTouches).forEach((touch)=>{
|
|
701
|
+
this.updateInput(event, touch.identifier);
|
|
702
|
+
});
|
|
703
|
+
},
|
|
704
|
+
PointerEvent: function PointerEvent(event) {
|
|
705
|
+
this.updateInput(event, event.pointerId);
|
|
706
|
+
},
|
|
707
|
+
MouseEvent: function MouseEvent(event) {
|
|
708
|
+
if (event.button === 0) this.updateInput(event, event.button);
|
|
709
|
+
}
|
|
710
|
+
};
|
|
711
|
+
/**
|
|
712
|
+
* Keeps track of currently active and ending input points on the interactive
|
|
713
|
+
* surface.
|
|
714
|
+
*
|
|
715
|
+
* @memberof westures-core
|
|
716
|
+
*
|
|
717
|
+
* @param {Element} element - The element underpinning the associated Region.
|
|
718
|
+
*/ class $639be6fb478a6d5a$var$State {
|
|
719
|
+
constructor(element){
|
|
720
|
+
/**
|
|
721
|
+
* Keep a reference to the element for the associated region.
|
|
722
|
+
*
|
|
723
|
+
* @type {Element}
|
|
724
|
+
*/ this.element = element;
|
|
725
|
+
/**
|
|
726
|
+
* Keeps track of the current Input objects.
|
|
727
|
+
*
|
|
728
|
+
* @alias [@@inputs]
|
|
729
|
+
* @type {Map.<westures-core.Input>}
|
|
730
|
+
* @memberof westure-core.State
|
|
731
|
+
*/ this[$639be6fb478a6d5a$var$symbols.inputs] = new Map();
|
|
732
|
+
/**
|
|
733
|
+
* All currently valid inputs, including those that have ended.
|
|
734
|
+
*
|
|
735
|
+
* @type {westures-core.Input[]}
|
|
736
|
+
*/ this.inputs = [];
|
|
737
|
+
/**
|
|
738
|
+
* The array of currently active inputs, sourced from the current Input
|
|
739
|
+
* objects. "Active" is defined as not being in the 'end' phase.
|
|
740
|
+
*
|
|
741
|
+
* @type {westures-core.Input[]}
|
|
742
|
+
*/ this.active = [];
|
|
743
|
+
/**
|
|
744
|
+
* The array of latest point data for the currently active inputs, sourced
|
|
745
|
+
* from this.active.
|
|
746
|
+
*
|
|
747
|
+
* @type {westures-core.Point2D[]}
|
|
748
|
+
*/ this.activePoints = [];
|
|
749
|
+
/**
|
|
750
|
+
* The centroid of the currently active points.
|
|
751
|
+
*
|
|
752
|
+
* @type {westures-core.Point2D}
|
|
753
|
+
*/ this.centroid = {
|
|
754
|
+
};
|
|
755
|
+
/**
|
|
756
|
+
* The latest event that the state processed.
|
|
757
|
+
*
|
|
758
|
+
* @type {Event}
|
|
759
|
+
*/ this.event = null;
|
|
760
|
+
}
|
|
761
|
+
/**
|
|
762
|
+
* Deletes all inputs that are in the 'end' phase.
|
|
763
|
+
*/ clearEndedInputs() {
|
|
764
|
+
this[$639be6fb478a6d5a$var$symbols.inputs].forEach((v, k)=>{
|
|
765
|
+
if (v.phase === 'end') this[$639be6fb478a6d5a$var$symbols.inputs].delete(k);
|
|
766
|
+
});
|
|
767
|
+
}
|
|
768
|
+
/**
|
|
769
|
+
* @param {string} phase - One of 'start', 'move', 'end', or 'cancel'.
|
|
770
|
+
*
|
|
771
|
+
* @return {westures-core.Input[]} Inputs in the given phase.
|
|
772
|
+
*/ getInputsInPhase(phase) {
|
|
773
|
+
return this.inputs.filter((i)=>i.phase === phase
|
|
774
|
+
);
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* @param {string} phase - One of 'start', 'move', 'end', or 'cancel'.
|
|
778
|
+
*
|
|
779
|
+
* @return {westures-core.Input[]} Inputs <b>not</b> in the given phase.
|
|
780
|
+
*/ getInputsNotInPhase(phase) {
|
|
781
|
+
return this.inputs.filter((i)=>i.phase !== phase
|
|
782
|
+
);
|
|
783
|
+
}
|
|
784
|
+
/**
|
|
785
|
+
* @return {boolean} True if there are no active inputs. False otherwise.
|
|
786
|
+
*/ hasNoInputs() {
|
|
787
|
+
return this[$639be6fb478a6d5a$var$symbols.inputs].size === 0;
|
|
788
|
+
}
|
|
789
|
+
/**
|
|
790
|
+
* Update the input with the given identifier using the given event.
|
|
791
|
+
*
|
|
792
|
+
* @private
|
|
793
|
+
*
|
|
794
|
+
* @param {Event} event - The event being captured.
|
|
795
|
+
* @param {number} identifier - The identifier of the input to update.
|
|
796
|
+
*/ updateInput(event, identifier) {
|
|
797
|
+
switch($639be6fb478a6d5a$require$PHASE[event.type]){
|
|
798
|
+
case $639be6fb478a6d5a$require$START:
|
|
799
|
+
this[$639be6fb478a6d5a$var$symbols.inputs].set(identifier, new $e2125e2e71e37a0c$exports(event, identifier));
|
|
800
|
+
try {
|
|
801
|
+
this.element.setPointerCapture(identifier);
|
|
802
|
+
} catch (e) {
|
|
803
|
+
// NOP: Optional operation failed.
|
|
804
|
+
}
|
|
805
|
+
break;
|
|
806
|
+
// All of 'end', 'move', and 'cancel' perform updates, hence the
|
|
807
|
+
// following fall-throughs
|
|
808
|
+
case $639be6fb478a6d5a$require$END:
|
|
809
|
+
try {
|
|
810
|
+
this.element.releasePointerCapture(identifier);
|
|
811
|
+
} catch (e1) {
|
|
812
|
+
// NOP: Optional operation failed.
|
|
813
|
+
}
|
|
814
|
+
case $639be6fb478a6d5a$require$CANCEL:
|
|
815
|
+
case $639be6fb478a6d5a$require$MOVE:
|
|
816
|
+
if (this[$639be6fb478a6d5a$var$symbols.inputs].has(identifier)) this[$639be6fb478a6d5a$var$symbols.inputs].get(identifier).update(event);
|
|
817
|
+
break;
|
|
818
|
+
default:
|
|
819
|
+
console.warn(`Unrecognized event type: ${event.type}`);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
/**
|
|
823
|
+
* Updates the inputs with new information based upon a new event being fired.
|
|
824
|
+
*
|
|
825
|
+
* @private
|
|
826
|
+
* @param {Event} event - The event being captured.
|
|
827
|
+
*/ updateAllInputs(event) {
|
|
828
|
+
$639be6fb478a6d5a$var$update_fns[event.constructor.name].call(this, event);
|
|
829
|
+
this.updateFields(event);
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Updates the convenience fields.
|
|
833
|
+
*
|
|
834
|
+
* @private
|
|
835
|
+
* @param {Event} event - Event with which to update the convenience fields.
|
|
836
|
+
*/ updateFields(event) {
|
|
837
|
+
this.inputs = Array.from(this[$639be6fb478a6d5a$var$symbols.inputs].values());
|
|
838
|
+
this.active = this.getInputsNotInPhase('end');
|
|
839
|
+
this.activePoints = this.active.map((i)=>i.current.point
|
|
840
|
+
);
|
|
841
|
+
this.centroid = $6c3676f10a43b740$exports.centroid(this.activePoints);
|
|
842
|
+
this.event = event;
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
$639be6fb478a6d5a$exports = $639be6fb478a6d5a$var$State;
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
var $b66a0f22c18e3e3d$require$CANCEL_EVENTS = $be6f0e84320366a7$exports.CANCEL_EVENTS;
|
|
850
|
+
var $b66a0f22c18e3e3d$require$KEYBOARD_EVENTS = $be6f0e84320366a7$exports.KEYBOARD_EVENTS;
|
|
851
|
+
var $b66a0f22c18e3e3d$require$MOUSE_EVENTS = $be6f0e84320366a7$exports.MOUSE_EVENTS;
|
|
852
|
+
var $b66a0f22c18e3e3d$require$POINTER_EVENTS = $be6f0e84320366a7$exports.POINTER_EVENTS;
|
|
853
|
+
var $b66a0f22c18e3e3d$require$TOUCH_EVENTS = $be6f0e84320366a7$exports.TOUCH_EVENTS;
|
|
854
|
+
var $b66a0f22c18e3e3d$require$STATE_KEY_STRINGS = $be6f0e84320366a7$exports.STATE_KEY_STRINGS;
|
|
855
|
+
var $b66a0f22c18e3e3d$require$PHASE = $be6f0e84320366a7$exports.PHASE;
|
|
856
|
+
var $b66a0f22c18e3e3d$require$CANCEL = $be6f0e84320366a7$exports.CANCEL;
|
|
857
|
+
var $b66a0f22c18e3e3d$require$END = $be6f0e84320366a7$exports.END;
|
|
858
|
+
var $b66a0f22c18e3e3d$require$START = $be6f0e84320366a7$exports.START;
|
|
859
|
+
|
|
860
|
+
var $b66a0f22c18e3e3d$require$setDifference = $4559ecf940edc78d$exports.setDifference;
|
|
861
|
+
var $b66a0f22c18e3e3d$require$setFilter = $4559ecf940edc78d$exports.setFilter;
|
|
862
|
+
/**
|
|
863
|
+
* Allows the user to specify the control region which will listen for user
|
|
864
|
+
* input events.
|
|
865
|
+
*
|
|
866
|
+
* @memberof westures-core
|
|
867
|
+
*
|
|
868
|
+
* @param {Element} element=window - The element which should listen to input
|
|
869
|
+
* events.
|
|
870
|
+
* @param {object} [options]
|
|
871
|
+
* @param {boolean} [options.capture=false] - Whether the region uses the
|
|
872
|
+
* capture phase of input events. If false, uses the bubbling phase.
|
|
873
|
+
* @param {boolean} [options.preferPointer=true] - If false, the region listens
|
|
874
|
+
* to mouse/touch events instead of pointer events.
|
|
875
|
+
* @param {boolean} [options.preventDefault=true] - Whether the default
|
|
876
|
+
* browser functionality should be disabled. This option should most likely be
|
|
877
|
+
* ignored. Here there by dragons if set to false.
|
|
878
|
+
* @param {string} [options.touchAction='none'] - Value to set the CSS
|
|
879
|
+
* 'touch-action' property to on elements added to the region.
|
|
880
|
+
*/ class $b66a0f22c18e3e3d$var$Region {
|
|
881
|
+
constructor(element = window, options = {
|
|
882
|
+
}){
|
|
883
|
+
options = {
|
|
884
|
+
...$b66a0f22c18e3e3d$var$Region.DEFAULTS,
|
|
885
|
+
...options
|
|
886
|
+
};
|
|
887
|
+
/**
|
|
888
|
+
* The list of relations between elements, their gestures, and the handlers.
|
|
889
|
+
*
|
|
890
|
+
* @type {Set.<westures-core.Gesture>}
|
|
891
|
+
*/ this.gestures = new Set();
|
|
892
|
+
/**
|
|
893
|
+
* The list of active gestures for the current input session.
|
|
894
|
+
*
|
|
895
|
+
* @type {Set.<westures-core.Gesture>}
|
|
896
|
+
*/ this.activeGestures = new Set();
|
|
897
|
+
/**
|
|
898
|
+
* The base list of potentially active gestures for the current input
|
|
899
|
+
* session.
|
|
900
|
+
*
|
|
901
|
+
* @type {Set.<westures-core.Gesture>}
|
|
902
|
+
*/ this.potentialGestures = new Set();
|
|
903
|
+
/**
|
|
904
|
+
* The element being bound to.
|
|
905
|
+
*
|
|
906
|
+
* @type {Element}
|
|
907
|
+
*/ this.element = element;
|
|
908
|
+
/**
|
|
909
|
+
* The user-supplied options for the Region.
|
|
910
|
+
*
|
|
911
|
+
* @type {object}
|
|
912
|
+
*/ this.options = options;
|
|
913
|
+
/**
|
|
914
|
+
* The internal state object for a Region. Keeps track of inputs.
|
|
915
|
+
*
|
|
916
|
+
* @type {westures-core.State}
|
|
917
|
+
*/ this.state = new $639be6fb478a6d5a$exports(this.element);
|
|
918
|
+
// Begin operating immediately.
|
|
919
|
+
this.activate();
|
|
920
|
+
}
|
|
921
|
+
/**
|
|
922
|
+
* Activates the region by adding event listeners for all appropriate input
|
|
923
|
+
* events to the region's element.
|
|
924
|
+
*
|
|
925
|
+
* @private
|
|
926
|
+
*/ activate() {
|
|
927
|
+
/*
|
|
928
|
+
* Listening to both mouse and touch comes with the difficulty that
|
|
929
|
+
* preventDefault() must be called to prevent both events from iterating
|
|
930
|
+
* through the system. However I have left it as an option to the end user,
|
|
931
|
+
* which defaults to calling preventDefault(), in case there's a use-case I
|
|
932
|
+
* haven't considered or am not aware of.
|
|
933
|
+
*
|
|
934
|
+
* It also may be a good idea to keep regions small in large pages.
|
|
935
|
+
*
|
|
936
|
+
* See:
|
|
937
|
+
* https://www.html5rocks.com/en/mobile/touchandmouse/
|
|
938
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Touch_events
|
|
939
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Pointer_events
|
|
940
|
+
*/ let eventNames = [];
|
|
941
|
+
if (this.options.preferPointer && window.PointerEvent) eventNames = $b66a0f22c18e3e3d$require$POINTER_EVENTS;
|
|
942
|
+
else eventNames = $b66a0f22c18e3e3d$require$MOUSE_EVENTS.concat($b66a0f22c18e3e3d$require$TOUCH_EVENTS);
|
|
943
|
+
// Bind detected browser events to the region element.
|
|
944
|
+
const arbitrate = this.arbitrate.bind(this);
|
|
945
|
+
eventNames.forEach((eventName)=>{
|
|
946
|
+
this.element.addEventListener(eventName, arbitrate, {
|
|
947
|
+
capture: this.options.capture,
|
|
948
|
+
once: false,
|
|
949
|
+
passive: false
|
|
950
|
+
});
|
|
951
|
+
});
|
|
952
|
+
const cancel = this.cancel.bind(this);
|
|
953
|
+
$b66a0f22c18e3e3d$require$CANCEL_EVENTS.forEach((eventName)=>{
|
|
954
|
+
window.addEventListener(eventName, cancel);
|
|
955
|
+
});
|
|
956
|
+
const handleKeyboardEvent = this.handleKeyboardEvent.bind(this);
|
|
957
|
+
$b66a0f22c18e3e3d$require$KEYBOARD_EVENTS.forEach((eventName)=>{
|
|
958
|
+
window.addEventListener(eventName, handleKeyboardEvent);
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
/**
|
|
962
|
+
* Handles a cancel event. Resets the state and the active / potential gesture
|
|
963
|
+
* lists.
|
|
964
|
+
*
|
|
965
|
+
* @private
|
|
966
|
+
* @param {Event} event - The event emitted from the window object.
|
|
967
|
+
*/ cancel(event) {
|
|
968
|
+
if (this.options.preventDefault) event.preventDefault();
|
|
969
|
+
this.state.inputs.forEach((input)=>{
|
|
970
|
+
input.update(event);
|
|
971
|
+
});
|
|
972
|
+
this.activeGestures.forEach((gesture)=>{
|
|
973
|
+
gesture.evaluateHook($b66a0f22c18e3e3d$require$CANCEL, this.state);
|
|
974
|
+
});
|
|
975
|
+
this.state = new $639be6fb478a6d5a$exports(this.element);
|
|
976
|
+
this.resetActiveGestures();
|
|
977
|
+
}
|
|
978
|
+
/**
|
|
979
|
+
* Handles a keyboard event, triggering a restart of any gestures that need
|
|
980
|
+
* it.
|
|
981
|
+
*
|
|
982
|
+
* @private
|
|
983
|
+
* @param {KeyboardEvent} event - The keyboard event.
|
|
984
|
+
*/ handleKeyboardEvent(event) {
|
|
985
|
+
if ($b66a0f22c18e3e3d$require$STATE_KEY_STRINGS.indexOf(event.key) >= 0) {
|
|
986
|
+
this.state.event = event;
|
|
987
|
+
const oldActiveGestures = this.activeGestures;
|
|
988
|
+
this.setActiveGestures();
|
|
989
|
+
$b66a0f22c18e3e3d$require$setDifference(oldActiveGestures, this.activeGestures).forEach((gesture)=>{
|
|
990
|
+
gesture.evaluateHook($b66a0f22c18e3e3d$require$END, this.state);
|
|
991
|
+
});
|
|
992
|
+
$b66a0f22c18e3e3d$require$setDifference(this.activeGestures, oldActiveGestures).forEach((gesture)=>{
|
|
993
|
+
gesture.evaluateHook($b66a0f22c18e3e3d$require$START, this.state);
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
/**
|
|
998
|
+
* Resets the active gestures.
|
|
999
|
+
*
|
|
1000
|
+
* @private
|
|
1001
|
+
*/ resetActiveGestures() {
|
|
1002
|
+
this.potentialGestures = new Set();
|
|
1003
|
+
this.activeGestures = new Set();
|
|
1004
|
+
}
|
|
1005
|
+
/**
|
|
1006
|
+
* Selects active gestures from the list of potentially active gestures.
|
|
1007
|
+
*
|
|
1008
|
+
* @private
|
|
1009
|
+
*/ setActiveGestures() {
|
|
1010
|
+
this.activeGestures = $b66a0f22c18e3e3d$require$setFilter(this.potentialGestures, (gesture)=>{
|
|
1011
|
+
return gesture.isEnabled(this.state);
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
1014
|
+
/**
|
|
1015
|
+
* Selects the potentially active gestures.
|
|
1016
|
+
*
|
|
1017
|
+
* @private
|
|
1018
|
+
*/ setPotentialGestures() {
|
|
1019
|
+
const input = this.state.inputs[0];
|
|
1020
|
+
this.potentialGestures = $b66a0f22c18e3e3d$require$setFilter(this.gestures, (gesture)=>{
|
|
1021
|
+
return input.initialElements.has(gesture.element);
|
|
1022
|
+
});
|
|
1023
|
+
}
|
|
1024
|
+
/**
|
|
1025
|
+
* Selects the gestures that are active for the current input sequence.
|
|
1026
|
+
*
|
|
1027
|
+
* @private
|
|
1028
|
+
* @param {Event} event - The event emitted from the window object.
|
|
1029
|
+
* @param {boolean} isInitial - Whether this is an initial contact.
|
|
1030
|
+
*/ updateActiveGestures(event, isInitial) {
|
|
1031
|
+
if ($b66a0f22c18e3e3d$require$PHASE[event.type] === $b66a0f22c18e3e3d$require$START) {
|
|
1032
|
+
if (isInitial) this.setPotentialGestures();
|
|
1033
|
+
this.setActiveGestures();
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
/**
|
|
1037
|
+
* Evaluates whether the current input session has completed.
|
|
1038
|
+
*
|
|
1039
|
+
* @private
|
|
1040
|
+
* @param {Event} event - The event emitted from the window object.
|
|
1041
|
+
*/ pruneActiveGestures(event) {
|
|
1042
|
+
if ($b66a0f22c18e3e3d$require$PHASE[event.type] === $b66a0f22c18e3e3d$require$END) {
|
|
1043
|
+
if (this.state.hasNoInputs()) this.resetActiveGestures();
|
|
1044
|
+
else this.setActiveGestures();
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
/**
|
|
1048
|
+
* All input events flow through this function. It makes sure that the input
|
|
1049
|
+
* state is maintained, determines which gestures to analyze based on the
|
|
1050
|
+
* initial position of the inputs, calls the relevant gesture hooks, and
|
|
1051
|
+
* dispatches gesture data.
|
|
1052
|
+
*
|
|
1053
|
+
* @private
|
|
1054
|
+
* @param {Event} event - The event emitted from the window object.
|
|
1055
|
+
*/ arbitrate(event) {
|
|
1056
|
+
const isInitial = this.state.hasNoInputs();
|
|
1057
|
+
this.state.updateAllInputs(event);
|
|
1058
|
+
this.updateActiveGestures(event, isInitial);
|
|
1059
|
+
if (this.activeGestures.size > 0) {
|
|
1060
|
+
if (this.options.preventDefault) event.preventDefault();
|
|
1061
|
+
this.activeGestures.forEach((gesture)=>{
|
|
1062
|
+
gesture.evaluateHook($b66a0f22c18e3e3d$require$PHASE[event.type], this.state);
|
|
1063
|
+
});
|
|
1064
|
+
}
|
|
1065
|
+
this.state.clearEndedInputs();
|
|
1066
|
+
this.pruneActiveGestures(event);
|
|
1067
|
+
}
|
|
1068
|
+
/**
|
|
1069
|
+
* Adds the given gesture to the region.
|
|
1070
|
+
*
|
|
1071
|
+
* @param {westures-core.Gesture} gesture - Instantiated gesture to add.
|
|
1072
|
+
*/ addGesture(gesture) {
|
|
1073
|
+
gesture.element.style.touchAction = this.options.touchAction;
|
|
1074
|
+
this.gestures.add(gesture);
|
|
1075
|
+
}
|
|
1076
|
+
/**
|
|
1077
|
+
* Removes the given gesture from the region.
|
|
1078
|
+
*
|
|
1079
|
+
* @param {westures-core.Gesture} gesture - Instantiated gesture to add.
|
|
1080
|
+
*/ removeGesture(gesture) {
|
|
1081
|
+
this.gestures.delete(gesture);
|
|
1082
|
+
this.potentialGestures.delete(gesture);
|
|
1083
|
+
this.activeGestures.delete(gesture);
|
|
1084
|
+
}
|
|
1085
|
+
/**
|
|
1086
|
+
* Retrieves Gestures by their associated element.
|
|
1087
|
+
*
|
|
1088
|
+
* @param {Element} element - The element for which to find gestures.
|
|
1089
|
+
*
|
|
1090
|
+
* @return {westures-core.Gesture[]} Gestures to which the element is bound.
|
|
1091
|
+
*/ getGesturesByElement(element) {
|
|
1092
|
+
return $b66a0f22c18e3e3d$require$setFilter(this.gestures, (gesture)=>gesture.element === element
|
|
1093
|
+
);
|
|
1094
|
+
}
|
|
1095
|
+
/**
|
|
1096
|
+
* Remove all gestures bound to the given element.
|
|
1097
|
+
*
|
|
1098
|
+
* @param {Element} element - The element to unbind.
|
|
1099
|
+
*/ removeGesturesByElement(element) {
|
|
1100
|
+
this.getGesturesByElement(element).forEach((g)=>this.removeGesture(g)
|
|
1101
|
+
);
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
$b66a0f22c18e3e3d$var$Region.DEFAULTS = {
|
|
1105
|
+
capture: false,
|
|
1106
|
+
preferPointer: true,
|
|
1107
|
+
preventDefault: true,
|
|
1108
|
+
touchAction: 'none'
|
|
1109
|
+
};
|
|
1110
|
+
$b66a0f22c18e3e3d$exports = $b66a0f22c18e3e3d$var$Region;
|
|
1111
|
+
|
|
1112
|
+
|
|
1113
|
+
var $01c3d7b128023e4f$exports = {};
|
|
1114
|
+
'use strict';
|
|
1115
|
+
const $01c3d7b128023e4f$var$cascade = Symbol('cascade');
|
|
1116
|
+
const $01c3d7b128023e4f$var$smooth = Symbol('smooth');
|
|
1117
|
+
/**
|
|
1118
|
+
* Determines whether to apply smoothing. Smoothing is on by default but turned
|
|
1119
|
+
* off if either:<br>
|
|
1120
|
+
* 1. The user explicitly requests that it be turned off.<br>
|
|
1121
|
+
* 2. The active pointer is not "coarse".<br>
|
|
1122
|
+
*
|
|
1123
|
+
* @see {@link
|
|
1124
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia}
|
|
1125
|
+
*
|
|
1126
|
+
* @inner
|
|
1127
|
+
* @memberof westures-core.Smoothable
|
|
1128
|
+
*
|
|
1129
|
+
* @param {boolean} isRequested - Whether smoothing was requested by the user.
|
|
1130
|
+
*
|
|
1131
|
+
* @returns {boolean} Whether to apply smoothing.
|
|
1132
|
+
*/ function $01c3d7b128023e4f$var$smoothingIsApplicable(isRequested) {
|
|
1133
|
+
if (isRequested) try {
|
|
1134
|
+
return window.matchMedia('(pointer: coarse)').matches;
|
|
1135
|
+
} catch (e) {
|
|
1136
|
+
return true;
|
|
1137
|
+
}
|
|
1138
|
+
return false;
|
|
1139
|
+
}
|
|
1140
|
+
/**
|
|
1141
|
+
* A Smoothable datatype is one that is capable of smoothing out a series of
|
|
1142
|
+
* values as they come in, one at a time, providing a more consistent series. It
|
|
1143
|
+
* does this by creating some inertia in the values using a cascading average.
|
|
1144
|
+
* (For those who are interested in such things, this effectively means that it
|
|
1145
|
+
* provides a practical application of Zeno's Dichotomy).
|
|
1146
|
+
*
|
|
1147
|
+
* @example
|
|
1148
|
+
* const x = new Smoothable({ identity: 1 });
|
|
1149
|
+
* const a = x.next(1); // 1.0
|
|
1150
|
+
* const b = x.next(1.2); // 1.1
|
|
1151
|
+
* const c = x.next(0.9); // 1.0
|
|
1152
|
+
* const d = x.next(0.6); // 0.8
|
|
1153
|
+
* const e = x.next(1.2); // 1.0
|
|
1154
|
+
* const f = x.next(1.6); // 1.3
|
|
1155
|
+
* x.restart();
|
|
1156
|
+
* const g = x.next(0); // 0.5
|
|
1157
|
+
*
|
|
1158
|
+
* @memberof westures-core
|
|
1159
|
+
*
|
|
1160
|
+
* @param {Object} [options]
|
|
1161
|
+
* @param {boolean} [options.applySmoothing=true] Whether to apply smoothing to
|
|
1162
|
+
* the data.
|
|
1163
|
+
* @param {*} [options.identity=0] The identity value of this smoothable data.
|
|
1164
|
+
*/ class $01c3d7b128023e4f$var$Smoothable {
|
|
1165
|
+
constructor(options = {
|
|
1166
|
+
}){
|
|
1167
|
+
const final_options = {
|
|
1168
|
+
...$01c3d7b128023e4f$var$Smoothable.DEFAULTS,
|
|
1169
|
+
...options
|
|
1170
|
+
};
|
|
1171
|
+
/**
|
|
1172
|
+
* The function through which smoothed emits are passed.
|
|
1173
|
+
*
|
|
1174
|
+
* @method
|
|
1175
|
+
* @param {*} data - The data to emit.
|
|
1176
|
+
*
|
|
1177
|
+
* @return {*} The smoothed out data.
|
|
1178
|
+
*/ this.next = null;
|
|
1179
|
+
if ($01c3d7b128023e4f$var$smoothingIsApplicable(final_options.applySmoothing)) this.next = this[$01c3d7b128023e4f$var$smooth].bind(this);
|
|
1180
|
+
else this.next = (data)=>data
|
|
1181
|
+
;
|
|
1182
|
+
/**
|
|
1183
|
+
* The "identity" value of the data that will be smoothed.
|
|
1184
|
+
*
|
|
1185
|
+
* @type {*}
|
|
1186
|
+
* @default 0
|
|
1187
|
+
*/ this.identity = final_options.identity;
|
|
1188
|
+
/**
|
|
1189
|
+
* The cascading average of outgoing values.
|
|
1190
|
+
*
|
|
1191
|
+
* @memberof westures-core.Smoothable
|
|
1192
|
+
* @alias [@@cascade]
|
|
1193
|
+
* @type {object}
|
|
1194
|
+
*/ this[$01c3d7b128023e4f$var$cascade] = this.identity;
|
|
1195
|
+
}
|
|
1196
|
+
/**
|
|
1197
|
+
* Restart the Smoothable gesture.
|
|
1198
|
+
*/ restart() {
|
|
1199
|
+
this[$01c3d7b128023e4f$var$cascade] = this.identity;
|
|
1200
|
+
}
|
|
1201
|
+
/**
|
|
1202
|
+
* Smooth out the outgoing data.
|
|
1203
|
+
*
|
|
1204
|
+
* @memberof westures-core.Smoothable
|
|
1205
|
+
* @alias [@@smooth]
|
|
1206
|
+
* @param {object} data - The next batch of data to emit.
|
|
1207
|
+
*
|
|
1208
|
+
* @return {?object}
|
|
1209
|
+
*/ [$01c3d7b128023e4f$var$smooth](data) {
|
|
1210
|
+
const average = this.average(this[$01c3d7b128023e4f$var$cascade], data);
|
|
1211
|
+
this[$01c3d7b128023e4f$var$cascade] = average;
|
|
1212
|
+
return average;
|
|
1213
|
+
}
|
|
1214
|
+
/**
|
|
1215
|
+
* Average out two values, as part of the smoothing algorithm. Override this
|
|
1216
|
+
* method if the data being smoothed is not a Number.
|
|
1217
|
+
*
|
|
1218
|
+
* @param {number} a
|
|
1219
|
+
* @param {number} b
|
|
1220
|
+
*
|
|
1221
|
+
* @return {number} The average of 'a' and 'b'
|
|
1222
|
+
*/ average(a, b) {
|
|
1223
|
+
return (a + b) / 2;
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
$01c3d7b128023e4f$var$Smoothable.DEFAULTS = {
|
|
1227
|
+
applySmoothing: true,
|
|
1228
|
+
identity: 0
|
|
1229
|
+
};
|
|
1230
|
+
$01c3d7b128023e4f$exports = $01c3d7b128023e4f$var$Smoothable;
|
|
1231
|
+
|
|
1232
|
+
|
|
1233
|
+
|
|
1234
|
+
|
|
1235
|
+
|
|
1236
|
+
module.exports = {
|
|
1237
|
+
Gesture: $de0d6a332419bf3c$exports,
|
|
1238
|
+
Input: $e2125e2e71e37a0c$exports,
|
|
1239
|
+
Point2D: $6c3676f10a43b740$exports,
|
|
1240
|
+
PointerData: $0ca7bfe1c074e8ca$exports,
|
|
1241
|
+
Region: $b66a0f22c18e3e3d$exports,
|
|
1242
|
+
Smoothable: $01c3d7b128023e4f$exports,
|
|
1243
|
+
State: $639be6fb478a6d5a$exports,
|
|
1244
|
+
...$be6f0e84320366a7$exports,
|
|
1245
|
+
...$4559ecf940edc78d$exports
|
|
1246
|
+
};
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
//# sourceMappingURL=index.js.map
|