1 |
|
2 | var win = window;
|
3 | var doc = document;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var VENDOR_PREFIXES = ['', 'webkit', 'Moz', 'MS', 'ms', 'o'];
|
10 | var TEST_ELEMENT = doc.createElement('div');
|
11 | var TYPE_FUNCTION = 'function';
|
12 | var round = Math.round;
|
13 | var abs = Math.abs;
|
14 | var now = Date.now;
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function setTimeoutContext(fn, timeout, context) {
|
23 | return setTimeout(bindFn(fn, context), timeout);
|
24 | }
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function invokeArrayArg(arg, fn, context) {
|
35 | if (Array.isArray(arg)) {
|
36 | each(arg, context[fn], context);
|
37 | return true;
|
38 | }
|
39 | return false;
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | function each(obj, iterator, context) {
|
48 | var i;
|
49 | if (!obj) {
|
50 | return;
|
51 | }
|
52 | if (obj.forEach) {
|
53 | obj.forEach(iterator, context);
|
54 | }
|
55 | else if (obj.length !== undefined) {
|
56 | i = 0;
|
57 | while (i < obj.length) {
|
58 | iterator.call(context, obj[i], i, obj);
|
59 | i++;
|
60 | }
|
61 | }
|
62 | else {
|
63 | for (i in obj) {
|
64 | obj.hasOwnProperty(i) && iterator.call(context, obj[i], i, obj);
|
65 | }
|
66 | }
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | function inherit(child, base, properties) {
|
75 | var baseP = base.prototype, childP;
|
76 | childP = child.prototype = Object.create(baseP);
|
77 | childP.constructor = child;
|
78 | childP._super = baseP;
|
79 | if (properties) {
|
80 | Object.assign(childP, properties);
|
81 | }
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | function bindFn(fn, context) {
|
90 | return function boundFn() {
|
91 | return fn.apply(context, arguments);
|
92 | };
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | function boolOrFn(val, args) {
|
102 | if (typeof val == TYPE_FUNCTION) {
|
103 | return val.apply(args ? args[0] || undefined : undefined, args);
|
104 | }
|
105 | return val;
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | function ifUndefined(val1, val2) {
|
114 | return (val1 === undefined) ? val2 : val1;
|
115 | }
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | function addEventListeners(target, types, handler) {
|
123 | each(splitStr(types), function (type) {
|
124 | target.addEventListener(type, handler, false);
|
125 | });
|
126 | }
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | function removeEventListeners(target, types, handler) {
|
134 | each(splitStr(types), function (type) {
|
135 | target.removeEventListener(type, handler, false);
|
136 | });
|
137 | }
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | function hasParent(node, parent) {
|
146 | while (node) {
|
147 | if (node == parent) {
|
148 | return true;
|
149 | }
|
150 | node = node.parentNode;
|
151 | }
|
152 | return false;
|
153 | }
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | function inStr(str, find) {
|
161 | return str.indexOf(find) > -1;
|
162 | }
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | function splitStr(str) {
|
169 | return str.trim().split(/\s+/g);
|
170 | }
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | function inArray(src, find, findByKey) {
|
179 | if (src.indexOf && !findByKey) {
|
180 | return src.indexOf(find);
|
181 | }
|
182 | else {
|
183 | var i = 0;
|
184 | while (i < src.length) {
|
185 | if ((findByKey && src[i][findByKey] == find) || (!findByKey && src[i] === find)) {
|
186 | return i;
|
187 | }
|
188 | i++;
|
189 | }
|
190 | return -1;
|
191 | }
|
192 | }
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | function toArray(obj) {
|
199 | return Array.prototype.slice.call(obj, 0);
|
200 | }
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | function uniqueArray(src, key, sort) {
|
209 | var results = [];
|
210 | var values = [];
|
211 | var i = 0;
|
212 | while (i < src.length) {
|
213 | var val = key ? src[i][key] : src[i];
|
214 | if (inArray(values, val) < 0) {
|
215 | results.push(src[i]);
|
216 | }
|
217 | values[i] = val;
|
218 | i++;
|
219 | }
|
220 | if (sort) {
|
221 | if (!key) {
|
222 | results = results.sort();
|
223 | }
|
224 | else {
|
225 | results = results.sort(function sortUniqueArray(a, b) {
|
226 | return a[key] > b[key] ? 1 : 0;
|
227 | });
|
228 | }
|
229 | }
|
230 | return results;
|
231 | }
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 | function prefixed(obj, property) {
|
239 | var prefix, prop;
|
240 | var camelProp = property[0].toUpperCase() + property.slice(1);
|
241 | var i = 0;
|
242 | while (i < VENDOR_PREFIXES.length) {
|
243 | prefix = VENDOR_PREFIXES[i];
|
244 | prop = (prefix) ? prefix + camelProp : property;
|
245 | if (prop in obj) {
|
246 | return prop;
|
247 | }
|
248 | i++;
|
249 | }
|
250 | return undefined;
|
251 | }
|
252 |
|
253 |
|
254 |
|
255 |
|
256 | var _uniqueId = 1;
|
257 | function uniqueId() {
|
258 | return _uniqueId++;
|
259 | }
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | function getWindowForElement(element) {
|
266 | var doc = element.ownerDocument || element;
|
267 | return (doc.defaultView || doc.parentWindow || window);
|
268 | }
|
269 | var MOBILE_REGEX = /mobile|tablet|ip(ad|hone|od)|android/i;
|
270 | var SUPPORT_TOUCH = ('ontouchstart' in window);
|
271 | var SUPPORT_POINTER_EVENTS = prefixed(window, 'PointerEvent') !== undefined;
|
272 | var SUPPORT_ONLY_TOUCH = SUPPORT_TOUCH && MOBILE_REGEX.test(navigator.userAgent);
|
273 | var INPUT_TYPE_TOUCH = 'touch';
|
274 | var INPUT_TYPE_PEN = 'pen';
|
275 | var INPUT_TYPE_MOUSE = 'mouse';
|
276 | var INPUT_TYPE_KINECT = 'kinect';
|
277 | var COMPUTE_INTERVAL = 25;
|
278 | var INPUT_START = 1;
|
279 | var INPUT_MOVE = 2;
|
280 | var INPUT_END = 4;
|
281 | var INPUT_CANCEL = 8;
|
282 | var DIRECTION_NONE = 1;
|
283 | export var DIRECTION_LEFT = 2;
|
284 | export var DIRECTION_RIGHT = 4;
|
285 | var DIRECTION_UP = 8;
|
286 | var DIRECTION_DOWN = 16;
|
287 | export var DIRECTION_HORIZONTAL = DIRECTION_LEFT | DIRECTION_RIGHT;
|
288 | export var DIRECTION_VERTICAL = DIRECTION_UP | DIRECTION_DOWN;
|
289 | var DIRECTION_ALL = DIRECTION_HORIZONTAL | DIRECTION_VERTICAL;
|
290 | var PROPS_XY = ['x', 'y'];
|
291 | var PROPS_CLIENT_XY = ['clientX', 'clientY'];
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 | function Input(manager, callback) {
|
300 | var self = this;
|
301 | this.manager = manager;
|
302 | this.callback = callback;
|
303 | this.element = manager.element;
|
304 | this.target = manager.options.inputTarget;
|
305 |
|
306 |
|
307 | this.domHandler = function (ev) {
|
308 | if (boolOrFn(manager.options.enable, [manager])) {
|
309 | self.handler(ev);
|
310 | }
|
311 | };
|
312 | this.init();
|
313 | }
|
314 | Input.prototype = {
|
315 | |
316 |
|
317 |
|
318 |
|
319 | handler: function () { },
|
320 | |
321 |
|
322 |
|
323 | init: function () {
|
324 | this.evEl && addEventListeners(this.element, this.evEl, this.domHandler);
|
325 | this.evTarget && addEventListeners(this.target, this.evTarget, this.domHandler);
|
326 | this.evWin && addEventListeners(getWindowForElement(this.element), this.evWin, this.domHandler);
|
327 | },
|
328 | |
329 |
|
330 |
|
331 | destroy: function () {
|
332 | this.evEl && removeEventListeners(this.element, this.evEl, this.domHandler);
|
333 | this.evTarget && removeEventListeners(this.target, this.evTarget, this.domHandler);
|
334 | this.evWin && removeEventListeners(getWindowForElement(this.element), this.evWin, this.domHandler);
|
335 | }
|
336 | };
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 | function createInputInstance(manager) {
|
344 | var Type;
|
345 | var inputClass = manager.options.inputClass;
|
346 | if (inputClass) {
|
347 | Type = inputClass;
|
348 | }
|
349 | else if (SUPPORT_POINTER_EVENTS) {
|
350 | Type = PointerEventInput;
|
351 | }
|
352 | else if (SUPPORT_ONLY_TOUCH) {
|
353 | Type = TouchInput;
|
354 | }
|
355 | else if (!SUPPORT_TOUCH) {
|
356 | Type = MouseInput;
|
357 | }
|
358 | else {
|
359 | Type = TouchMouseInput;
|
360 | }
|
361 | return new (Type)(manager, inputHandler);
|
362 | }
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 | function inputHandler(manager, eventType, input) {
|
370 | var pointersLen = input.pointers.length;
|
371 | var changedPointersLen = input.changedPointers.length;
|
372 | var isFirst = (eventType & INPUT_START && (pointersLen - changedPointersLen === 0));
|
373 | var isFinal = (eventType & (INPUT_END | INPUT_CANCEL) && (pointersLen - changedPointersLen === 0));
|
374 | input.isFirst = !!isFirst;
|
375 | input.isFinal = !!isFinal;
|
376 | if (isFirst) {
|
377 | manager.session = {};
|
378 | }
|
379 |
|
380 |
|
381 | input.eventType = eventType;
|
382 |
|
383 | computeInputData(manager, input);
|
384 |
|
385 | manager.emit('hammer.input', input);
|
386 | manager.recognize(input);
|
387 | manager.session.prevInput = input;
|
388 | }
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 | function computeInputData(manager, input) {
|
395 | var session = manager.session;
|
396 | var pointers = input.pointers;
|
397 | var pointersLength = pointers.length;
|
398 |
|
399 | if (!session.firstInput) {
|
400 | session.firstInput = simpleCloneInputData(input);
|
401 | }
|
402 |
|
403 | if (pointersLength > 1 && !session.firstMultiple) {
|
404 | session.firstMultiple = simpleCloneInputData(input);
|
405 | }
|
406 | else if (pointersLength === 1) {
|
407 | session.firstMultiple = false;
|
408 | }
|
409 | var firstInput = session.firstInput;
|
410 | var firstMultiple = session.firstMultiple;
|
411 | var offsetCenter = firstMultiple ? firstMultiple.center : firstInput.center;
|
412 | var center = input.center = getCenter(pointers);
|
413 | input.timeStamp = now();
|
414 | input.deltaTime = input.timeStamp - firstInput.timeStamp;
|
415 | input.angle = getAngle(offsetCenter, center);
|
416 | input.distance = getDistance(offsetCenter, center);
|
417 | computeDeltaXY(session, input);
|
418 | input.offsetDirection = getDirection(input.deltaX, input.deltaY);
|
419 | var overallVelocity = getVelocity(input.deltaTime, input.deltaX, input.deltaY);
|
420 | input.overallVelocityX = overallVelocity.x;
|
421 | input.overallVelocityY = overallVelocity.y;
|
422 | input.overallVelocity = (abs(overallVelocity.x) > abs(overallVelocity.y)) ? overallVelocity.x : overallVelocity.y;
|
423 | input.scale = firstMultiple ? getScale(firstMultiple.pointers, pointers) : 1;
|
424 | input.rotation = firstMultiple ? getRotation(firstMultiple.pointers, pointers) : 0;
|
425 | input.maxPointers = !session.prevInput ? input.pointers.length : ((input.pointers.length >
|
426 | session.prevInput.maxPointers) ? input.pointers.length : session.prevInput.maxPointers);
|
427 | computeIntervalInputData(session, input);
|
428 |
|
429 | var target = manager.element;
|
430 | if (hasParent(input.srcEvent.target, target)) {
|
431 | target = input.srcEvent.target;
|
432 | }
|
433 | input.target = target;
|
434 | }
|
435 | function computeDeltaXY(session, input) {
|
436 | var center = input.center;
|
437 | var offset = session.offsetDelta || {};
|
438 | var prevDelta = session.prevDelta || {};
|
439 | var prevInput = session.prevInput || {};
|
440 | if (input.eventType === INPUT_START || prevInput.eventType === INPUT_END) {
|
441 | prevDelta = session.prevDelta = {
|
442 | x: prevInput.deltaX || 0,
|
443 | y: prevInput.deltaY || 0
|
444 | };
|
445 | offset = session.offsetDelta = {
|
446 | x: center.x,
|
447 | y: center.y
|
448 | };
|
449 | }
|
450 | input.deltaX = prevDelta.x + (center.x - offset.x);
|
451 | input.deltaY = prevDelta.y + (center.y - offset.y);
|
452 | }
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 | function computeIntervalInputData(session, input) {
|
459 | var last = session.lastInterval || input, deltaTime = input.timeStamp - last.timeStamp, velocity, velocityX, velocityY, direction;
|
460 | if (input.eventType != INPUT_CANCEL && (deltaTime > COMPUTE_INTERVAL || last.velocity === undefined)) {
|
461 | var deltaX = input.deltaX - last.deltaX;
|
462 | var deltaY = input.deltaY - last.deltaY;
|
463 | var v = getVelocity(deltaTime, deltaX, deltaY);
|
464 | velocityX = v.x;
|
465 | velocityY = v.y;
|
466 | velocity = (abs(v.x) > abs(v.y)) ? v.x : v.y;
|
467 | direction = getDirection(deltaX, deltaY);
|
468 | session.lastInterval = input;
|
469 | }
|
470 | else {
|
471 |
|
472 | velocity = last.velocity;
|
473 | velocityX = last.velocityX;
|
474 | velocityY = last.velocityY;
|
475 | direction = last.direction;
|
476 | }
|
477 | input.velocity = velocity;
|
478 | input.velocityX = velocityX;
|
479 | input.velocityY = velocityY;
|
480 | input.direction = direction;
|
481 | }
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 | function simpleCloneInputData(input) {
|
488 |
|
489 |
|
490 | var pointers = [];
|
491 | var i = 0;
|
492 | while (i < input.pointers.length) {
|
493 | pointers[i] = {
|
494 | clientX: round(input.pointers[i].clientX),
|
495 | clientY: round(input.pointers[i].clientY)
|
496 | };
|
497 | i++;
|
498 | }
|
499 | return {
|
500 | timeStamp: now(),
|
501 | pointers: pointers,
|
502 | center: getCenter(pointers),
|
503 | deltaX: input.deltaX,
|
504 | deltaY: input.deltaY
|
505 | };
|
506 | }
|
507 |
|
508 |
|
509 |
|
510 |
|
511 |
|
512 | function getCenter(pointers) {
|
513 | var pointersLength = pointers.length;
|
514 |
|
515 | if (pointersLength === 1) {
|
516 | return {
|
517 | x: round(pointers[0].clientX),
|
518 | y: round(pointers[0].clientY)
|
519 | };
|
520 | }
|
521 | var x = 0, y = 0, i = 0;
|
522 | while (i < pointersLength) {
|
523 | x += pointers[i].clientX;
|
524 | y += pointers[i].clientY;
|
525 | i++;
|
526 | }
|
527 | return {
|
528 | x: round(x / pointersLength),
|
529 | y: round(y / pointersLength)
|
530 | };
|
531 | }
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 | function getVelocity(deltaTime, x, y) {
|
540 | return {
|
541 | x: x / deltaTime || 0,
|
542 | y: y / deltaTime || 0
|
543 | };
|
544 | }
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 | function getDirection(x, y) {
|
552 | if (x === y) {
|
553 | return DIRECTION_NONE;
|
554 | }
|
555 | if (abs(x) >= abs(y)) {
|
556 | return x < 0 ? DIRECTION_LEFT : DIRECTION_RIGHT;
|
557 | }
|
558 | return y < 0 ? DIRECTION_UP : DIRECTION_DOWN;
|
559 | }
|
560 |
|
561 |
|
562 |
|
563 |
|
564 |
|
565 |
|
566 |
|
567 | function getDistance(p1, p2, props) {
|
568 | if (!props) {
|
569 | props = PROPS_XY;
|
570 | }
|
571 | var x = p2[props[0]] - p1[props[0]], y = p2[props[1]] - p1[props[1]];
|
572 | return Math.sqrt((x * x) + (y * y));
|
573 | }
|
574 |
|
575 |
|
576 |
|
577 |
|
578 |
|
579 |
|
580 |
|
581 | function getAngle(p1, p2, props) {
|
582 | if (!props) {
|
583 | props = PROPS_XY;
|
584 | }
|
585 | var x = p2[props[0]] - p1[props[0]], y = p2[props[1]] - p1[props[1]];
|
586 | return Math.atan2(y, x) * 180 / Math.PI;
|
587 | }
|
588 |
|
589 |
|
590 |
|
591 |
|
592 |
|
593 |
|
594 | function getRotation(start, end) {
|
595 | return getAngle(end[1], end[0], PROPS_CLIENT_XY) + getAngle(start[1], start[0], PROPS_CLIENT_XY);
|
596 | }
|
597 |
|
598 |
|
599 |
|
600 |
|
601 |
|
602 |
|
603 |
|
604 | function getScale(start, end) {
|
605 | return getDistance(end[0], end[1], PROPS_CLIENT_XY) / getDistance(start[0], start[1], PROPS_CLIENT_XY);
|
606 | }
|
607 | var MOUSE_INPUT_MAP = {
|
608 | mousedown: INPUT_START,
|
609 | mousemove: INPUT_MOVE,
|
610 | mouseup: INPUT_END
|
611 | };
|
612 | var MOUSE_ELEMENT_EVENTS = 'mousedown';
|
613 | var MOUSE_WINDOW_EVENTS = 'mousemove mouseup';
|
614 |
|
615 |
|
616 |
|
617 |
|
618 |
|
619 | function MouseInput(_manager, _handler) {
|
620 | this.evEl = MOUSE_ELEMENT_EVENTS;
|
621 | this.evWin = MOUSE_WINDOW_EVENTS;
|
622 | this.allow = true;
|
623 | this.pressed = false;
|
624 | Input.apply(this, arguments);
|
625 | }
|
626 | inherit(MouseInput, Input, {
|
627 | |
628 |
|
629 |
|
630 |
|
631 | handler: function MEhandler(ev) {
|
632 | var eventType = MOUSE_INPUT_MAP[ev.type];
|
633 |
|
634 | if (eventType & INPUT_START && ev.button === 0) {
|
635 | this.pressed = true;
|
636 | }
|
637 | if (eventType & INPUT_MOVE && ev.which !== 1) {
|
638 | eventType = INPUT_END;
|
639 | }
|
640 |
|
641 | if (!this.pressed || !this.allow) {
|
642 | return;
|
643 | }
|
644 | if (eventType & INPUT_END) {
|
645 | this.pressed = false;
|
646 | }
|
647 | this.callback(this.manager, eventType, {
|
648 | pointers: [ev],
|
649 | changedPointers: [ev],
|
650 | pointerType: INPUT_TYPE_MOUSE,
|
651 | srcEvent: ev
|
652 | });
|
653 | }
|
654 | });
|
655 | var POINTER_INPUT_MAP = {
|
656 | pointerdown: INPUT_START,
|
657 | pointermove: INPUT_MOVE,
|
658 | pointerup: INPUT_END,
|
659 | pointercancel: INPUT_CANCEL,
|
660 | pointerout: INPUT_CANCEL
|
661 | };
|
662 |
|
663 | var IE10_POINTER_TYPE_ENUM = {
|
664 | 2: INPUT_TYPE_TOUCH,
|
665 | 3: INPUT_TYPE_PEN,
|
666 | 4: INPUT_TYPE_MOUSE,
|
667 | 5: INPUT_TYPE_KINECT
|
668 | };
|
669 | var POINTER_ELEMENT_EVENTS = 'pointerdown';
|
670 | var POINTER_WINDOW_EVENTS = 'pointermove pointerup pointercancel';
|
671 |
|
672 | if (win.MSPointerEvent && !win.PointerEvent) {
|
673 | POINTER_ELEMENT_EVENTS = 'MSPointerDown';
|
674 | POINTER_WINDOW_EVENTS = 'MSPointerMove MSPointerUp MSPointerCancel';
|
675 | }
|
676 |
|
677 |
|
678 |
|
679 |
|
680 |
|
681 | function PointerEventInput() {
|
682 | this.evEl = POINTER_ELEMENT_EVENTS;
|
683 | this.evWin = POINTER_WINDOW_EVENTS;
|
684 | Input.apply(this, arguments);
|
685 | this.store = (this.manager.session.pointerEvents = []);
|
686 | }
|
687 | inherit(PointerEventInput, Input, {
|
688 | |
689 |
|
690 |
|
691 |
|
692 | handler: function PEhandler(ev) {
|
693 | var store = this.store;
|
694 | var removePointer = false;
|
695 | var eventTypeNormalized = ev.type.toLowerCase().replace('ms', '');
|
696 | var eventType = POINTER_INPUT_MAP[eventTypeNormalized];
|
697 | var pointerType = IE10_POINTER_TYPE_ENUM[ev.pointerType] || ev.pointerType;
|
698 | var isTouch = (pointerType == INPUT_TYPE_TOUCH);
|
699 |
|
700 | var storeIndex = inArray(store, ev.pointerId, 'pointerId');
|
701 |
|
702 | if (eventType & INPUT_START && (ev.button === 0 || isTouch)) {
|
703 | if (storeIndex < 0) {
|
704 | store.push(ev);
|
705 | storeIndex = store.length - 1;
|
706 | }
|
707 | }
|
708 | else if (eventType & (INPUT_END | INPUT_CANCEL)) {
|
709 | removePointer = true;
|
710 | }
|
711 |
|
712 | if (storeIndex < 0) {
|
713 | return;
|
714 | }
|
715 |
|
716 | store[storeIndex] = ev;
|
717 | this.callback(this.manager, eventType, {
|
718 | pointers: store,
|
719 | changedPointers: [ev],
|
720 | pointerType: pointerType,
|
721 | srcEvent: ev
|
722 | });
|
723 | if (removePointer) {
|
724 |
|
725 | store.splice(storeIndex, 1);
|
726 | }
|
727 | }
|
728 | });
|
729 | var SINGLE_TOUCH_INPUT_MAP = {
|
730 | touchstart: INPUT_START,
|
731 | touchmove: INPUT_MOVE,
|
732 | touchend: INPUT_END,
|
733 | touchcancel: INPUT_CANCEL
|
734 | };
|
735 | var SINGLE_TOUCH_TARGET_EVENTS = 'touchstart';
|
736 | var SINGLE_TOUCH_WINDOW_EVENTS = 'touchstart touchmove touchend touchcancel';
|
737 |
|
738 |
|
739 |
|
740 |
|
741 |
|
742 | function SingleTouchInput() {
|
743 | this.evTarget = SINGLE_TOUCH_TARGET_EVENTS;
|
744 | this.evWin = SINGLE_TOUCH_WINDOW_EVENTS;
|
745 | this.started = false;
|
746 | Input.apply(this, arguments);
|
747 | }
|
748 | inherit(SingleTouchInput, Input, {
|
749 | handler: function TEhandler(ev) {
|
750 | var type = SINGLE_TOUCH_INPUT_MAP[ev.type];
|
751 |
|
752 | if (type === INPUT_START) {
|
753 | this.started = true;
|
754 | }
|
755 | if (!this.started) {
|
756 | return;
|
757 | }
|
758 | var touches = normalizeSingleTouches.call(this, ev, type);
|
759 |
|
760 | if (type & (INPUT_END | INPUT_CANCEL) && touches[0].length - touches[1].length === 0) {
|
761 | this.started = false;
|
762 | }
|
763 | this.callback(this.manager, type, {
|
764 | pointers: touches[0],
|
765 | changedPointers: touches[1],
|
766 | pointerType: INPUT_TYPE_TOUCH,
|
767 | srcEvent: ev
|
768 | });
|
769 | }
|
770 | });
|
771 |
|
772 |
|
773 |
|
774 |
|
775 |
|
776 |
|
777 | function normalizeSingleTouches(ev, type) {
|
778 | var all = toArray(ev.touches);
|
779 | var changed = toArray(ev.changedTouches);
|
780 | if (type & (INPUT_END | INPUT_CANCEL)) {
|
781 | all = uniqueArray(all.concat(changed), 'identifier', true);
|
782 | }
|
783 | return [all, changed];
|
784 | }
|
785 | var TOUCH_INPUT_MAP = {
|
786 | touchstart: INPUT_START,
|
787 | touchmove: INPUT_MOVE,
|
788 | touchend: INPUT_END,
|
789 | touchcancel: INPUT_CANCEL
|
790 | };
|
791 | var TOUCH_TARGET_EVENTS = 'touchstart touchmove touchend touchcancel';
|
792 |
|
793 |
|
794 |
|
795 |
|
796 |
|
797 | function TouchInput(_manager, _handler) {
|
798 | this.evTarget = TOUCH_TARGET_EVENTS;
|
799 | this.targetIds = {};
|
800 | Input.apply(this, arguments);
|
801 | }
|
802 | inherit(TouchInput, Input, {
|
803 | handler: function MTEhandler(ev) {
|
804 | var type = TOUCH_INPUT_MAP[ev.type];
|
805 | var touches = getTouches.call(this, ev, type);
|
806 | if (!touches) {
|
807 | return;
|
808 | }
|
809 | this.callback(this.manager, type, {
|
810 | pointers: touches[0],
|
811 | changedPointers: touches[1],
|
812 | pointerType: INPUT_TYPE_TOUCH,
|
813 | srcEvent: ev
|
814 | });
|
815 | }
|
816 | });
|
817 |
|
818 |
|
819 |
|
820 |
|
821 |
|
822 |
|
823 | function getTouches(ev, type) {
|
824 | var allTouches = toArray(ev.touches);
|
825 | var targetIds = this.targetIds;
|
826 |
|
827 | if (type & (INPUT_START | INPUT_MOVE) && allTouches.length === 1) {
|
828 | targetIds[allTouches[0].identifier] = true;
|
829 | return [allTouches, allTouches];
|
830 | }
|
831 | var i, targetTouches, changedTouches = toArray(ev.changedTouches), changedTargetTouches = [], target = this.target;
|
832 |
|
833 | targetTouches = allTouches.filter(function (touch) {
|
834 | return hasParent(touch.target, target);
|
835 | });
|
836 |
|
837 | if (type === INPUT_START) {
|
838 | i = 0;
|
839 | while (i < targetTouches.length) {
|
840 | targetIds[targetTouches[i].identifier] = true;
|
841 | i++;
|
842 | }
|
843 | }
|
844 |
|
845 | i = 0;
|
846 | while (i < changedTouches.length) {
|
847 | if (targetIds[changedTouches[i].identifier]) {
|
848 | changedTargetTouches.push(changedTouches[i]);
|
849 | }
|
850 |
|
851 | if (type & (INPUT_END | INPUT_CANCEL)) {
|
852 | delete targetIds[changedTouches[i].identifier];
|
853 | }
|
854 | i++;
|
855 | }
|
856 | if (!changedTargetTouches.length) {
|
857 | return;
|
858 | }
|
859 | return [
|
860 |
|
861 | uniqueArray(targetTouches.concat(changedTargetTouches), 'identifier', true),
|
862 | changedTargetTouches
|
863 | ];
|
864 | }
|
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 |
|
872 |
|
873 |
|
874 | function TouchMouseInput() {
|
875 | Input.apply(this, arguments);
|
876 | var handler = bindFn(this.handler, this);
|
877 | this.touch = new TouchInput(this.manager, handler);
|
878 | this.mouse = new MouseInput(this.manager, handler);
|
879 | }
|
880 | inherit(TouchMouseInput, Input, {
|
881 | |
882 |
|
883 |
|
884 |
|
885 |
|
886 |
|
887 | handler: function TMEhandler(manager, inputEvent, inputData) {
|
888 | var isTouch = (inputData.pointerType == INPUT_TYPE_TOUCH), isMouse = (inputData.pointerType == INPUT_TYPE_MOUSE);
|
889 |
|
890 |
|
891 | if (isTouch) {
|
892 | this.mouse.allow = false;
|
893 | }
|
894 | else if (isMouse && !this.mouse.allow) {
|
895 | return;
|
896 | }
|
897 |
|
898 | if (inputEvent & (INPUT_END | INPUT_CANCEL)) {
|
899 | this.mouse.allow = true;
|
900 | }
|
901 | this.callback(manager, inputEvent, inputData);
|
902 | },
|
903 | |
904 |
|
905 |
|
906 | destroy: function destroy() {
|
907 | this.touch.destroy();
|
908 | this.mouse.destroy();
|
909 | }
|
910 | });
|
911 | var PREFIXED_TOUCH_ACTION = prefixed(TEST_ELEMENT.style, 'touchAction');
|
912 | var NATIVE_TOUCH_ACTION = PREFIXED_TOUCH_ACTION !== undefined;
|
913 |
|
914 | var TOUCH_ACTION_COMPUTE = 'compute';
|
915 | var TOUCH_ACTION_AUTO = 'auto';
|
916 | var TOUCH_ACTION_MANIPULATION = 'manipulation';
|
917 | var TOUCH_ACTION_NONE = 'none';
|
918 | var TOUCH_ACTION_PAN_X = 'pan-x';
|
919 | var TOUCH_ACTION_PAN_Y = 'pan-y';
|
920 |
|
921 |
|
922 |
|
923 |
|
924 |
|
925 |
|
926 |
|
927 | function TouchAction(manager, value) {
|
928 | this.manager = manager;
|
929 | this.set(value);
|
930 | }
|
931 | TouchAction.prototype = {
|
932 | |
933 |
|
934 |
|
935 |
|
936 | set: function (value) {
|
937 |
|
938 | if (value == TOUCH_ACTION_COMPUTE) {
|
939 | value = this.compute();
|
940 | }
|
941 | if (NATIVE_TOUCH_ACTION && this.manager.element.style) {
|
942 | this.manager.element.style[PREFIXED_TOUCH_ACTION] = value;
|
943 | }
|
944 | this.actions = value.toLowerCase().trim();
|
945 | },
|
946 | |
947 |
|
948 |
|
949 | update: function () {
|
950 | this.set(this.manager.options.touchAction);
|
951 | },
|
952 | |
953 |
|
954 |
|
955 |
|
956 | compute: function () {
|
957 | var actions = [];
|
958 | each(this.manager.recognizers, function (recognizer) {
|
959 | if (boolOrFn(recognizer.options.enable, [recognizer])) {
|
960 | actions = actions.concat(recognizer.getTouchAction());
|
961 | }
|
962 | });
|
963 | return cleanTouchActions(actions.join(' '));
|
964 | },
|
965 | |
966 |
|
967 |
|
968 |
|
969 | preventDefaults: function (input) {
|
970 |
|
971 | if (NATIVE_TOUCH_ACTION) {
|
972 | return;
|
973 | }
|
974 | var srcEvent = input.srcEvent;
|
975 | var direction = input.offsetDirection;
|
976 |
|
977 | if (this.manager.session.prevented) {
|
978 | srcEvent.preventDefault();
|
979 | return;
|
980 | }
|
981 | var actions = this.actions;
|
982 | var hasNone = inStr(actions, TOUCH_ACTION_NONE);
|
983 | var hasPanY = inStr(actions, TOUCH_ACTION_PAN_Y);
|
984 | var hasPanX = inStr(actions, TOUCH_ACTION_PAN_X);
|
985 | if (hasNone) {
|
986 |
|
987 | var isTapPointer = input.pointers.length === 1;
|
988 | var isTapMovement = input.distance < 2;
|
989 | var isTapTouchTime = input.deltaTime < 250;
|
990 | if (isTapPointer && isTapMovement && isTapTouchTime) {
|
991 | return;
|
992 | }
|
993 | }
|
994 | if (hasPanX && hasPanY) {
|
995 |
|
996 | return;
|
997 | }
|
998 | if (hasNone ||
|
999 | (hasPanY && direction & DIRECTION_HORIZONTAL) ||
|
1000 | (hasPanX && direction & DIRECTION_VERTICAL)) {
|
1001 | return this.preventSrc(srcEvent);
|
1002 | }
|
1003 | },
|
1004 | |
1005 |
|
1006 |
|
1007 |
|
1008 | preventSrc: function (srcEvent) {
|
1009 | this.manager.session.prevented = true;
|
1010 | srcEvent.preventDefault();
|
1011 | }
|
1012 | };
|
1013 |
|
1014 |
|
1015 |
|
1016 |
|
1017 |
|
1018 | function cleanTouchActions(actions) {
|
1019 |
|
1020 | if (inStr(actions, TOUCH_ACTION_NONE)) {
|
1021 | return TOUCH_ACTION_NONE;
|
1022 | }
|
1023 | var hasPanX = inStr(actions, TOUCH_ACTION_PAN_X);
|
1024 | var hasPanY = inStr(actions, TOUCH_ACTION_PAN_Y);
|
1025 |
|
1026 |
|
1027 |
|
1028 |
|
1029 | if (hasPanX && hasPanY) {
|
1030 | return TOUCH_ACTION_NONE;
|
1031 | }
|
1032 |
|
1033 | if (hasPanX || hasPanY) {
|
1034 | return hasPanX ? TOUCH_ACTION_PAN_X : TOUCH_ACTION_PAN_Y;
|
1035 | }
|
1036 |
|
1037 | if (inStr(actions, TOUCH_ACTION_MANIPULATION)) {
|
1038 | return TOUCH_ACTION_MANIPULATION;
|
1039 | }
|
1040 | return TOUCH_ACTION_AUTO;
|
1041 | }
|
1042 |
|
1043 |
|
1044 |
|
1045 |
|
1046 |
|
1047 |
|
1048 |
|
1049 |
|
1050 |
|
1051 |
|
1052 |
|
1053 |
|
1054 |
|
1055 |
|
1056 |
|
1057 |
|
1058 |
|
1059 |
|
1060 |
|
1061 |
|
1062 |
|
1063 |
|
1064 |
|
1065 |
|
1066 |
|
1067 |
|
1068 |
|
1069 | var STATE_POSSIBLE = 1;
|
1070 | var STATE_BEGAN = 2;
|
1071 | var STATE_CHANGED = 4;
|
1072 | var STATE_ENDED = 8;
|
1073 | var STATE_RECOGNIZED = STATE_ENDED;
|
1074 | var STATE_CANCELLED = 16;
|
1075 | var STATE_FAILED = 32;
|
1076 |
|
1077 |
|
1078 |
|
1079 |
|
1080 |
|
1081 |
|
1082 | function Recognizer(options) {
|
1083 | this.options = Object.assign({}, this.defaults, options || {});
|
1084 | this.id = uniqueId();
|
1085 | this.manager = null;
|
1086 |
|
1087 | this.options.enable = ifUndefined(this.options.enable, true);
|
1088 | this.state = STATE_POSSIBLE;
|
1089 | this.simultaneous = {};
|
1090 | this.requireFail = [];
|
1091 | }
|
1092 | Recognizer.prototype = {
|
1093 | |
1094 |
|
1095 |
|
1096 |
|
1097 | defaults: {},
|
1098 | |
1099 |
|
1100 |
|
1101 |
|
1102 |
|
1103 | set: function (options) {
|
1104 | Object.assign(this.options, options);
|
1105 |
|
1106 | this.manager && this.manager.touchAction.update();
|
1107 | return this;
|
1108 | },
|
1109 | |
1110 |
|
1111 |
|
1112 |
|
1113 |
|
1114 | recognizeWith: function (otherRecognizer) {
|
1115 | if (invokeArrayArg(otherRecognizer, 'recognizeWith', this)) {
|
1116 | return this;
|
1117 | }
|
1118 | var simultaneous = this.simultaneous;
|
1119 | otherRecognizer = getRecognizerByNameIfManager(otherRecognizer, this);
|
1120 | if (!simultaneous[otherRecognizer.id]) {
|
1121 | simultaneous[otherRecognizer.id] = otherRecognizer;
|
1122 | otherRecognizer.recognizeWith(this);
|
1123 | }
|
1124 | return this;
|
1125 | },
|
1126 | |
1127 |
|
1128 |
|
1129 |
|
1130 |
|
1131 | dropRecognizeWith: function (otherRecognizer) {
|
1132 | if (invokeArrayArg(otherRecognizer, 'dropRecognizeWith', this)) {
|
1133 | return this;
|
1134 | }
|
1135 | otherRecognizer = getRecognizerByNameIfManager(otherRecognizer, this);
|
1136 | delete this.simultaneous[otherRecognizer.id];
|
1137 | return this;
|
1138 | },
|
1139 | |
1140 |
|
1141 |
|
1142 |
|
1143 |
|
1144 | requireFailure: function (otherRecognizer) {
|
1145 | if (invokeArrayArg(otherRecognizer, 'requireFailure', this)) {
|
1146 | return this;
|
1147 | }
|
1148 | var requireFail = this.requireFail;
|
1149 | otherRecognizer = getRecognizerByNameIfManager(otherRecognizer, this);
|
1150 | if (inArray(requireFail, otherRecognizer) === -1) {
|
1151 | requireFail.push(otherRecognizer);
|
1152 | otherRecognizer.requireFailure(this);
|
1153 | }
|
1154 | return this;
|
1155 | },
|
1156 | |
1157 |
|
1158 |
|
1159 |
|
1160 |
|
1161 | dropRequireFailure: function (otherRecognizer) {
|
1162 | if (invokeArrayArg(otherRecognizer, 'dropRequireFailure', this)) {
|
1163 | return this;
|
1164 | }
|
1165 | otherRecognizer = getRecognizerByNameIfManager(otherRecognizer, this);
|
1166 | var index = inArray(this.requireFail, otherRecognizer);
|
1167 | if (index > -1) {
|
1168 | this.requireFail.splice(index, 1);
|
1169 | }
|
1170 | return this;
|
1171 | },
|
1172 | |
1173 |
|
1174 |
|
1175 |
|
1176 | hasRequireFailures: function () {
|
1177 | return this.requireFail.length > 0;
|
1178 | },
|
1179 | |
1180 |
|
1181 |
|
1182 |
|
1183 |
|
1184 | canRecognizeWith: function (otherRecognizer) {
|
1185 | return !!this.simultaneous[otherRecognizer.id];
|
1186 | },
|
1187 | |
1188 |
|
1189 |
|
1190 |
|
1191 |
|
1192 | emit: function (input) {
|
1193 | var self = this;
|
1194 | var state = this.state;
|
1195 | function emit(event) {
|
1196 | self.manager.emit(event, input);
|
1197 | }
|
1198 |
|
1199 | if (state < STATE_ENDED) {
|
1200 | emit(self.options.event + stateStr(state));
|
1201 | }
|
1202 | emit(self.options.event);
|
1203 | if (input.additionalEvent) {
|
1204 | emit(input.additionalEvent);
|
1205 | }
|
1206 |
|
1207 | if (state >= STATE_ENDED) {
|
1208 | emit(self.options.event + stateStr(state));
|
1209 | }
|
1210 | },
|
1211 | |
1212 |
|
1213 |
|
1214 |
|
1215 |
|
1216 |
|
1217 | tryEmit: function (input) {
|
1218 | if (this.canEmit()) {
|
1219 | return this.emit(input);
|
1220 | }
|
1221 |
|
1222 | this.state = STATE_FAILED;
|
1223 | },
|
1224 | |
1225 |
|
1226 |
|
1227 |
|
1228 | canEmit: function () {
|
1229 | var i = 0;
|
1230 | while (i < this.requireFail.length) {
|
1231 | if (!(this.requireFail[i].state & (STATE_FAILED | STATE_POSSIBLE))) {
|
1232 | return false;
|
1233 | }
|
1234 | i++;
|
1235 | }
|
1236 | return true;
|
1237 | },
|
1238 | |
1239 |
|
1240 |
|
1241 |
|
1242 | recognize: function (inputData) {
|
1243 |
|
1244 |
|
1245 | var inputDataClone = Object.assign({}, inputData);
|
1246 |
|
1247 | if (!boolOrFn(this.options.enable, [this, inputDataClone])) {
|
1248 | this.reset();
|
1249 | this.state = STATE_FAILED;
|
1250 | return;
|
1251 | }
|
1252 |
|
1253 | if (this.state & (STATE_RECOGNIZED | STATE_CANCELLED | STATE_FAILED)) {
|
1254 | this.state = STATE_POSSIBLE;
|
1255 | }
|
1256 | this.state = this.process(inputDataClone);
|
1257 |
|
1258 |
|
1259 | if (this.state & (STATE_BEGAN | STATE_CHANGED | STATE_ENDED | STATE_CANCELLED)) {
|
1260 | this.tryEmit(inputDataClone);
|
1261 | }
|
1262 | },
|
1263 | |
1264 |
|
1265 |
|
1266 |
|
1267 |
|
1268 |
|
1269 |
|
1270 | process: function (_inputData) { },
|
1271 | |
1272 |
|
1273 |
|
1274 |
|
1275 |
|
1276 | getTouchAction: function () { },
|
1277 | |
1278 |
|
1279 |
|
1280 |
|
1281 |
|
1282 | reset: function () { }
|
1283 | };
|
1284 |
|
1285 |
|
1286 |
|
1287 |
|
1288 |
|
1289 | function stateStr(state) {
|
1290 | if (state & STATE_CANCELLED) {
|
1291 | return 'cancel';
|
1292 | }
|
1293 | else if (state & STATE_ENDED) {
|
1294 | return 'end';
|
1295 | }
|
1296 | else if (state & STATE_CHANGED) {
|
1297 | return 'move';
|
1298 | }
|
1299 | else if (state & STATE_BEGAN) {
|
1300 | return 'start';
|
1301 | }
|
1302 | return '';
|
1303 | }
|
1304 |
|
1305 |
|
1306 |
|
1307 |
|
1308 |
|
1309 | function directionStr(direction) {
|
1310 | if (direction == DIRECTION_DOWN) {
|
1311 | return 'down';
|
1312 | }
|
1313 | else if (direction == DIRECTION_UP) {
|
1314 | return 'up';
|
1315 | }
|
1316 | else if (direction == DIRECTION_LEFT) {
|
1317 | return 'left';
|
1318 | }
|
1319 | else if (direction == DIRECTION_RIGHT) {
|
1320 | return 'right';
|
1321 | }
|
1322 | return '';
|
1323 | }
|
1324 |
|
1325 |
|
1326 |
|
1327 |
|
1328 |
|
1329 |
|
1330 | function getRecognizerByNameIfManager(otherRecognizer, recognizer) {
|
1331 | var manager = recognizer.manager;
|
1332 | if (manager) {
|
1333 | return manager.get(otherRecognizer);
|
1334 | }
|
1335 | return otherRecognizer;
|
1336 | }
|
1337 |
|
1338 |
|
1339 |
|
1340 |
|
1341 |
|
1342 | function AttrRecognizer() {
|
1343 | Recognizer.apply(this, arguments);
|
1344 | }
|
1345 | inherit(AttrRecognizer, Recognizer, {
|
1346 | |
1347 |
|
1348 |
|
1349 |
|
1350 | defaults: {
|
1351 | |
1352 |
|
1353 |
|
1354 |
|
1355 | pointers: 1
|
1356 | },
|
1357 | |
1358 |
|
1359 |
|
1360 |
|
1361 |
|
1362 |
|
1363 | attrTest: function (input) {
|
1364 | var optionPointers = this.options.pointers;
|
1365 | return optionPointers === 0 || input.pointers.length === optionPointers;
|
1366 | },
|
1367 | |
1368 |
|
1369 |
|
1370 |
|
1371 |
|
1372 |
|
1373 | process: function (input) {
|
1374 | var state = this.state;
|
1375 | var eventType = input.eventType;
|
1376 | var isRecognized = state & (STATE_BEGAN | STATE_CHANGED);
|
1377 | var isValid = this.attrTest(input);
|
1378 |
|
1379 | if (isRecognized && (eventType & INPUT_CANCEL || !isValid)) {
|
1380 | return state | STATE_CANCELLED;
|
1381 | }
|
1382 | else if (isRecognized || isValid) {
|
1383 | if (eventType & INPUT_END) {
|
1384 | return state | STATE_ENDED;
|
1385 | }
|
1386 | else if (!(state & STATE_BEGAN)) {
|
1387 | return STATE_BEGAN;
|
1388 | }
|
1389 | return state | STATE_CHANGED;
|
1390 | }
|
1391 | return STATE_FAILED;
|
1392 | }
|
1393 | });
|
1394 |
|
1395 |
|
1396 |
|
1397 |
|
1398 |
|
1399 |
|
1400 | function PanRecognizer() {
|
1401 | AttrRecognizer.apply(this, arguments);
|
1402 | this.pX = null;
|
1403 | this.pY = null;
|
1404 | }
|
1405 | inherit(PanRecognizer, AttrRecognizer, {
|
1406 | |
1407 |
|
1408 |
|
1409 |
|
1410 | defaults: {
|
1411 | event: 'pan',
|
1412 | threshold: 10,
|
1413 | pointers: 1,
|
1414 | direction: DIRECTION_ALL
|
1415 | },
|
1416 | getTouchAction: function () {
|
1417 | var direction = this.options.direction;
|
1418 | var actions = [];
|
1419 | if (direction & DIRECTION_HORIZONTAL) {
|
1420 | actions.push(TOUCH_ACTION_PAN_Y);
|
1421 | }
|
1422 | if (direction & DIRECTION_VERTICAL) {
|
1423 | actions.push(TOUCH_ACTION_PAN_X);
|
1424 | }
|
1425 | return actions;
|
1426 | },
|
1427 | directionTest: function (input) {
|
1428 | var options = this.options;
|
1429 | var hasMoved = true;
|
1430 | var distance = input.distance;
|
1431 | var direction = input.direction;
|
1432 | var x = input.deltaX;
|
1433 | var y = input.deltaY;
|
1434 |
|
1435 | if (!(direction & options.direction)) {
|
1436 | if (options.direction & DIRECTION_HORIZONTAL) {
|
1437 | direction = (x === 0) ? DIRECTION_NONE : (x < 0) ? DIRECTION_LEFT : DIRECTION_RIGHT;
|
1438 | hasMoved = x != this.pX;
|
1439 | distance = Math.abs(input.deltaX);
|
1440 | }
|
1441 | else {
|
1442 | direction = (y === 0) ? DIRECTION_NONE : (y < 0) ? DIRECTION_UP : DIRECTION_DOWN;
|
1443 | hasMoved = y != this.pY;
|
1444 | distance = Math.abs(input.deltaY);
|
1445 | }
|
1446 | }
|
1447 | input.direction = direction;
|
1448 | return hasMoved && distance > options.threshold && direction & options.direction;
|
1449 | },
|
1450 | attrTest: function (input) {
|
1451 | return AttrRecognizer.prototype.attrTest.call(this, input) &&
|
1452 | (this.state & STATE_BEGAN || (!(this.state & STATE_BEGAN) && this.directionTest(input)));
|
1453 | },
|
1454 | emit: function (input) {
|
1455 | this.pX = input.deltaX;
|
1456 | this.pY = input.deltaY;
|
1457 | var direction = directionStr(input.direction);
|
1458 | if (direction) {
|
1459 | input.additionalEvent = this.options.event + direction;
|
1460 | }
|
1461 | this._super.emit.call(this, input);
|
1462 | }
|
1463 | });
|
1464 |
|
1465 |
|
1466 |
|
1467 |
|
1468 |
|
1469 |
|
1470 | function PinchRecognizer() {
|
1471 | AttrRecognizer.apply(this, arguments);
|
1472 | }
|
1473 | inherit(PinchRecognizer, AttrRecognizer, {
|
1474 | |
1475 |
|
1476 |
|
1477 |
|
1478 | defaults: {
|
1479 | event: 'pinch',
|
1480 | threshold: 0,
|
1481 | pointers: 2
|
1482 | },
|
1483 | getTouchAction: function () {
|
1484 | return [TOUCH_ACTION_NONE];
|
1485 | },
|
1486 | attrTest: function (input) {
|
1487 | return this._super.attrTest.call(this, input) &&
|
1488 | (Math.abs(input.scale - 1) > this.options.threshold || this.state & STATE_BEGAN);
|
1489 | },
|
1490 | emit: function (input) {
|
1491 | if (input.scale !== 1) {
|
1492 | var inOut = input.scale < 1 ? 'in' : 'out';
|
1493 | input.additionalEvent = this.options.event + inOut;
|
1494 | }
|
1495 | this._super.emit.call(this, input);
|
1496 | }
|
1497 | });
|
1498 |
|
1499 |
|
1500 |
|
1501 |
|
1502 |
|
1503 |
|
1504 | function PressRecognizer() {
|
1505 | Recognizer.apply(this, arguments);
|
1506 | this._timer = null;
|
1507 | this._input = null;
|
1508 | }
|
1509 | inherit(PressRecognizer, Recognizer, {
|
1510 | |
1511 |
|
1512 |
|
1513 |
|
1514 | defaults: {
|
1515 | event: 'press',
|
1516 | pointers: 1,
|
1517 | time: 251,
|
1518 | threshold: 9
|
1519 | },
|
1520 | getTouchAction: function () {
|
1521 | return [TOUCH_ACTION_AUTO];
|
1522 | },
|
1523 | process: function (input) {
|
1524 | var options = this.options;
|
1525 | var validPointers = input.pointers.length === options.pointers;
|
1526 | var validMovement = input.distance < options.threshold;
|
1527 | var validTime = input.deltaTime > options.time;
|
1528 | this._input = input;
|
1529 |
|
1530 |
|
1531 | if (!validMovement || !validPointers || (input.eventType & (INPUT_END | INPUT_CANCEL) && !validTime)) {
|
1532 | this.reset();
|
1533 | }
|
1534 | else if (input.eventType & INPUT_START) {
|
1535 | this.reset();
|
1536 | this._timer = setTimeoutContext(function () {
|
1537 | this.state = STATE_RECOGNIZED;
|
1538 | this.tryEmit();
|
1539 | }, options.time, this);
|
1540 | }
|
1541 | else if (input.eventType & INPUT_END) {
|
1542 | return STATE_RECOGNIZED;
|
1543 | }
|
1544 | return STATE_FAILED;
|
1545 | },
|
1546 | reset: function () {
|
1547 | clearTimeout(this._timer);
|
1548 | },
|
1549 | emit: function (input) {
|
1550 | if (this.state !== STATE_RECOGNIZED) {
|
1551 | return;
|
1552 | }
|
1553 | if (input && (input.eventType & INPUT_END)) {
|
1554 | this.manager.emit(this.options.event + 'up', input);
|
1555 | }
|
1556 | else {
|
1557 | this._input.timeStamp = now();
|
1558 | this.manager.emit(this.options.event, this._input);
|
1559 | }
|
1560 | }
|
1561 | });
|
1562 |
|
1563 |
|
1564 |
|
1565 |
|
1566 |
|
1567 |
|
1568 | function RotateRecognizer() {
|
1569 | AttrRecognizer.apply(this, arguments);
|
1570 | }
|
1571 | inherit(RotateRecognizer, AttrRecognizer, {
|
1572 | |
1573 |
|
1574 |
|
1575 |
|
1576 | defaults: {
|
1577 | event: 'rotate',
|
1578 | threshold: 0,
|
1579 | pointers: 2
|
1580 | },
|
1581 | getTouchAction: function () {
|
1582 | return [TOUCH_ACTION_NONE];
|
1583 | },
|
1584 | attrTest: function (input) {
|
1585 | return this._super.attrTest.call(this, input) &&
|
1586 | (Math.abs(input.rotation) > this.options.threshold || this.state & STATE_BEGAN);
|
1587 | }
|
1588 | });
|
1589 |
|
1590 |
|
1591 |
|
1592 |
|
1593 |
|
1594 |
|
1595 | function SwipeRecognizer() {
|
1596 | AttrRecognizer.apply(this, arguments);
|
1597 | }
|
1598 | inherit(SwipeRecognizer, AttrRecognizer, {
|
1599 | |
1600 |
|
1601 |
|
1602 |
|
1603 | defaults: {
|
1604 | event: 'swipe',
|
1605 | threshold: 10,
|
1606 | velocity: 0.3,
|
1607 | direction: DIRECTION_HORIZONTAL | DIRECTION_VERTICAL,
|
1608 | pointers: 1
|
1609 | },
|
1610 | getTouchAction: function () {
|
1611 | return PanRecognizer.prototype.getTouchAction.call(this);
|
1612 | },
|
1613 | attrTest: function (input) {
|
1614 | var direction = this.options.direction;
|
1615 | var velocity;
|
1616 | if (direction & (DIRECTION_HORIZONTAL | DIRECTION_VERTICAL)) {
|
1617 | velocity = input.overallVelocity;
|
1618 | }
|
1619 | else if (direction & DIRECTION_HORIZONTAL) {
|
1620 | velocity = input.overallVelocityX;
|
1621 | }
|
1622 | else if (direction & DIRECTION_VERTICAL) {
|
1623 | velocity = input.overallVelocityY;
|
1624 | }
|
1625 | return this._super.attrTest.call(this, input) &&
|
1626 | direction & input.offsetDirection &&
|
1627 | input.distance > this.options.threshold &&
|
1628 | input.maxPointers == this.options.pointers &&
|
1629 | abs(velocity) > this.options.velocity && input.eventType & INPUT_END;
|
1630 | },
|
1631 | emit: function (input) {
|
1632 | var direction = directionStr(input.offsetDirection);
|
1633 | if (direction) {
|
1634 | this.manager.emit(this.options.event + direction, input);
|
1635 | }
|
1636 | this.manager.emit(this.options.event, input);
|
1637 | }
|
1638 | });
|
1639 |
|
1640 |
|
1641 |
|
1642 |
|
1643 |
|
1644 |
|
1645 |
|
1646 |
|
1647 |
|
1648 |
|
1649 | function TapRecognizer() {
|
1650 | Recognizer.apply(this, arguments);
|
1651 |
|
1652 |
|
1653 | this.pTime = false;
|
1654 | this.pCenter = false;
|
1655 | this._timer = null;
|
1656 | this._input = null;
|
1657 | this.count = 0;
|
1658 | }
|
1659 | inherit(TapRecognizer, Recognizer, {
|
1660 | |
1661 |
|
1662 |
|
1663 |
|
1664 | defaults: {
|
1665 | event: 'tap',
|
1666 | pointers: 1,
|
1667 | taps: 1,
|
1668 | interval: 300,
|
1669 | time: 250,
|
1670 | threshold: 9,
|
1671 | posThreshold: 10
|
1672 | },
|
1673 | getTouchAction: function () {
|
1674 | return [TOUCH_ACTION_MANIPULATION];
|
1675 | },
|
1676 | process: function (input) {
|
1677 | var options = this.options;
|
1678 | var validPointers = input.pointers.length === options.pointers;
|
1679 | var validMovement = input.distance < options.threshold;
|
1680 | var validTouchTime = input.deltaTime < options.time;
|
1681 | this.reset();
|
1682 | if ((input.eventType & INPUT_START) && (this.count === 0)) {
|
1683 | return this.failTimeout();
|
1684 | }
|
1685 |
|
1686 |
|
1687 | if (validMovement && validTouchTime && validPointers) {
|
1688 | if (input.eventType != INPUT_END) {
|
1689 | return this.failTimeout();
|
1690 | }
|
1691 | var validInterval = this.pTime ? (input.timeStamp - this.pTime < options.interval) : true;
|
1692 | var validMultiTap = !this.pCenter || getDistance(this.pCenter, input.center) < options.posThreshold;
|
1693 | this.pTime = input.timeStamp;
|
1694 | this.pCenter = input.center;
|
1695 | if (!validMultiTap || !validInterval) {
|
1696 | this.count = 1;
|
1697 | }
|
1698 | else {
|
1699 | this.count += 1;
|
1700 | }
|
1701 | this._input = input;
|
1702 |
|
1703 |
|
1704 | var tapCount = this.count % options.taps;
|
1705 | if (tapCount === 0) {
|
1706 |
|
1707 |
|
1708 | if (!this.hasRequireFailures()) {
|
1709 | return STATE_RECOGNIZED;
|
1710 | }
|
1711 | else {
|
1712 | this._timer = setTimeoutContext(function () {
|
1713 | this.state = STATE_RECOGNIZED;
|
1714 | this.tryEmit();
|
1715 | }, options.interval, this);
|
1716 | return STATE_BEGAN;
|
1717 | }
|
1718 | }
|
1719 | }
|
1720 | return STATE_FAILED;
|
1721 | },
|
1722 | failTimeout: function () {
|
1723 | this._timer = setTimeoutContext(function () {
|
1724 | this.state = STATE_FAILED;
|
1725 | }, this.options.interval, this);
|
1726 | return STATE_FAILED;
|
1727 | },
|
1728 | reset: function () {
|
1729 | clearTimeout(this._timer);
|
1730 | },
|
1731 | emit: function () {
|
1732 | if (this.state == STATE_RECOGNIZED) {
|
1733 | this._input.tapCount = this.count;
|
1734 | this.manager.emit(this.options.event, this._input);
|
1735 | }
|
1736 | }
|
1737 | });
|
1738 |
|
1739 |
|
1740 |
|
1741 |
|
1742 |
|
1743 |
|
1744 | function Hammer(element, options) {
|
1745 | options = options || {};
|
1746 | options.recognizers = ifUndefined(options.recognizers, _defaults.preset);
|
1747 | return new Manager(element, options);
|
1748 | }
|
1749 |
|
1750 |
|
1751 |
|
1752 |
|
1753 | var _defaults = {
|
1754 | |
1755 |
|
1756 |
|
1757 |
|
1758 |
|
1759 |
|
1760 | domEvents: false,
|
1761 | |
1762 |
|
1763 |
|
1764 |
|
1765 |
|
1766 |
|
1767 | touchAction: TOUCH_ACTION_COMPUTE,
|
1768 | |
1769 |
|
1770 |
|
1771 |
|
1772 | enable: true,
|
1773 | |
1774 |
|
1775 |
|
1776 |
|
1777 |
|
1778 |
|
1779 |
|
1780 | inputTarget: null,
|
1781 | |
1782 |
|
1783 |
|
1784 |
|
1785 |
|
1786 | inputClass: null,
|
1787 | |
1788 |
|
1789 |
|
1790 |
|
1791 |
|
1792 | preset: [
|
1793 |
|
1794 | [RotateRecognizer, { enable: false }],
|
1795 | [PinchRecognizer, { enable: false }, ['rotate']],
|
1796 | [SwipeRecognizer, { direction: DIRECTION_HORIZONTAL }],
|
1797 | [PanRecognizer, { direction: DIRECTION_HORIZONTAL }, ['swipe']],
|
1798 | [TapRecognizer],
|
1799 | [TapRecognizer, { event: 'doubletap', taps: 2 }, ['tap']],
|
1800 | [PressRecognizer]
|
1801 | ],
|
1802 | |
1803 |
|
1804 |
|
1805 |
|
1806 |
|
1807 | cssProps: {
|
1808 | |
1809 |
|
1810 |
|
1811 |
|
1812 |
|
1813 | userSelect: 'none',
|
1814 | |
1815 |
|
1816 |
|
1817 |
|
1818 |
|
1819 | touchSelect: 'none',
|
1820 | |
1821 |
|
1822 |
|
1823 |
|
1824 |
|
1825 |
|
1826 |
|
1827 | touchCallout: 'none',
|
1828 | |
1829 |
|
1830 |
|
1831 |
|
1832 |
|
1833 | contentZooming: 'none',
|
1834 | |
1835 |
|
1836 |
|
1837 |
|
1838 |
|
1839 | userDrag: 'none',
|
1840 | |
1841 |
|
1842 |
|
1843 |
|
1844 |
|
1845 |
|
1846 | tapHighlightColor: 'rgba(0,0,0,0)'
|
1847 | }
|
1848 | };
|
1849 | var STOP = 1;
|
1850 | var FORCED_STOP = 2;
|
1851 |
|
1852 |
|
1853 |
|
1854 |
|
1855 |
|
1856 |
|
1857 | function Manager(element, options) {
|
1858 | this.options = Object.assign({}, _defaults, options || {});
|
1859 | this.options.inputTarget = this.options.inputTarget || element;
|
1860 | this.handlers = {};
|
1861 | this.session = {};
|
1862 | this.recognizers = [];
|
1863 | this.element = element;
|
1864 | this.input = createInputInstance(this);
|
1865 | this.touchAction = new TouchAction(this, this.options.touchAction);
|
1866 | toggleCssProps(this, true);
|
1867 | each(this.options.recognizers, function (item) {
|
1868 | var recognizer = this.add(new (item[0])(item[1]));
|
1869 | item[2] && recognizer.recognizeWith(item[2]);
|
1870 | item[3] && recognizer.requireFailure(item[3]);
|
1871 | }, this);
|
1872 | }
|
1873 | Manager.prototype = {
|
1874 | |
1875 |
|
1876 |
|
1877 |
|
1878 |
|
1879 | set: function (options) {
|
1880 | Object.assign(this.options, options);
|
1881 |
|
1882 | if (options.touchAction) {
|
1883 | this.touchAction.update();
|
1884 | }
|
1885 | if (options.inputTarget) {
|
1886 |
|
1887 | this.input.destroy();
|
1888 | this.input.target = options.inputTarget;
|
1889 | this.input.init();
|
1890 | }
|
1891 | return this;
|
1892 | },
|
1893 | |
1894 |
|
1895 |
|
1896 |
|
1897 |
|
1898 |
|
1899 | stop: function (force) {
|
1900 | this.session.stopped = force ? FORCED_STOP : STOP;
|
1901 | },
|
1902 | |
1903 |
|
1904 |
|
1905 |
|
1906 |
|
1907 |
|
1908 | recognize: function (inputData) {
|
1909 | var session = this.session;
|
1910 | if (session.stopped) {
|
1911 | return;
|
1912 | }
|
1913 |
|
1914 | this.touchAction.preventDefaults(inputData);
|
1915 | var recognizer;
|
1916 | var recognizers = this.recognizers;
|
1917 |
|
1918 |
|
1919 |
|
1920 | var curRecognizer = session.curRecognizer;
|
1921 |
|
1922 |
|
1923 | if (!curRecognizer || (curRecognizer && curRecognizer.state & STATE_RECOGNIZED)) {
|
1924 | curRecognizer = session.curRecognizer = null;
|
1925 | }
|
1926 | var i = 0;
|
1927 | while (i < recognizers.length) {
|
1928 | recognizer = recognizers[i];
|
1929 |
|
1930 |
|
1931 |
|
1932 |
|
1933 |
|
1934 |
|
1935 | if (session.stopped !== FORCED_STOP && (!curRecognizer || recognizer == curRecognizer ||
|
1936 | recognizer.canRecognizeWith(curRecognizer))) {
|
1937 | recognizer.recognize(inputData);
|
1938 | }
|
1939 | else {
|
1940 | recognizer.reset();
|
1941 | }
|
1942 |
|
1943 |
|
1944 | if (!curRecognizer && recognizer.state & (STATE_BEGAN | STATE_CHANGED | STATE_ENDED)) {
|
1945 | curRecognizer = session.curRecognizer = recognizer;
|
1946 | }
|
1947 | i++;
|
1948 | }
|
1949 | },
|
1950 | |
1951 |
|
1952 |
|
1953 |
|
1954 |
|
1955 | get: function (recognizer) {
|
1956 | if (recognizer instanceof Recognizer) {
|
1957 | return recognizer;
|
1958 | }
|
1959 | var recognizers = this.recognizers;
|
1960 | for (var i = 0; i < recognizers.length; i++) {
|
1961 | if (recognizers[i].options.event == recognizer) {
|
1962 | return recognizers[i];
|
1963 | }
|
1964 | }
|
1965 | return null;
|
1966 | },
|
1967 | |
1968 |
|
1969 |
|
1970 |
|
1971 |
|
1972 |
|
1973 | add: function (recognizer) {
|
1974 | if (invokeArrayArg(recognizer, 'add', this)) {
|
1975 | return this;
|
1976 | }
|
1977 |
|
1978 | var existing = this.get(recognizer.options.event);
|
1979 | if (existing) {
|
1980 | this.remove(existing);
|
1981 | }
|
1982 | this.recognizers.push(recognizer);
|
1983 | recognizer.manager = this;
|
1984 | this.touchAction.update();
|
1985 | return recognizer;
|
1986 | },
|
1987 | |
1988 |
|
1989 |
|
1990 |
|
1991 |
|
1992 | remove: function (recognizer) {
|
1993 | if (invokeArrayArg(recognizer, 'remove', this)) {
|
1994 | return this;
|
1995 | }
|
1996 | recognizer = this.get(recognizer);
|
1997 |
|
1998 | if (recognizer) {
|
1999 | var recognizers = this.recognizers;
|
2000 | var index = inArray(recognizers, recognizer);
|
2001 | if (index !== -1) {
|
2002 | recognizers.splice(index, 1);
|
2003 | this.touchAction.update();
|
2004 | }
|
2005 | }
|
2006 | return this;
|
2007 | },
|
2008 | |
2009 |
|
2010 |
|
2011 |
|
2012 |
|
2013 |
|
2014 | on: function (events, handler) {
|
2015 | var handlers = this.handlers;
|
2016 | each(splitStr(events), function (event) {
|
2017 | handlers[event] = handlers[event] || [];
|
2018 | handlers[event].push(handler);
|
2019 | });
|
2020 | return this;
|
2021 | },
|
2022 | |
2023 |
|
2024 |
|
2025 |
|
2026 |
|
2027 |
|
2028 | off: function (events, handler) {
|
2029 | var handlers = this.handlers;
|
2030 | each(splitStr(events), function (event) {
|
2031 | if (!handler) {
|
2032 | delete handlers[event];
|
2033 | }
|
2034 | else {
|
2035 | handlers[event] && handlers[event].splice(inArray(handlers[event], handler), 1);
|
2036 | }
|
2037 | });
|
2038 | return this;
|
2039 | },
|
2040 | |
2041 |
|
2042 |
|
2043 |
|
2044 |
|
2045 | emit: function (event, data) {
|
2046 |
|
2047 | if (this.options.domEvents) {
|
2048 | triggerDomEvent(event, data);
|
2049 | }
|
2050 |
|
2051 | var handlers = this.handlers[event] && this.handlers[event].slice();
|
2052 | if (!handlers || !handlers.length) {
|
2053 | return;
|
2054 | }
|
2055 | data.type = event;
|
2056 | data.preventDefault = function () {
|
2057 | data.srcEvent.preventDefault();
|
2058 | };
|
2059 | var i = 0;
|
2060 | while (i < handlers.length) {
|
2061 | handlers[i](data);
|
2062 | i++;
|
2063 | }
|
2064 | },
|
2065 | |
2066 |
|
2067 |
|
2068 |
|
2069 | destroy: function () {
|
2070 | this.element && toggleCssProps(this, false);
|
2071 | this.handlers = {};
|
2072 | this.session = {};
|
2073 | this.input.destroy();
|
2074 | this.element = null;
|
2075 | }
|
2076 | };
|
2077 |
|
2078 |
|
2079 |
|
2080 |
|
2081 |
|
2082 | function toggleCssProps(manager, add) {
|
2083 | var element = manager.element;
|
2084 | if (!element.style) {
|
2085 | return;
|
2086 | }
|
2087 | each(manager.options.cssProps, function (value, name) {
|
2088 | element.style[prefixed(element.style, name)] = add ? value : '';
|
2089 | });
|
2090 | }
|
2091 |
|
2092 |
|
2093 |
|
2094 |
|
2095 |
|
2096 | function triggerDomEvent(event, data) {
|
2097 | var gestureEvent = doc.createEvent('Event');
|
2098 | gestureEvent.initEvent(event, true, true);
|
2099 | gestureEvent.gesture = data;
|
2100 | data.target.dispatchEvent(gestureEvent);
|
2101 | }
|
2102 | Object.assign(Hammer, {
|
2103 | INPUT_START: INPUT_START,
|
2104 | INPUT_MOVE: INPUT_MOVE,
|
2105 | INPUT_END: INPUT_END,
|
2106 | INPUT_CANCEL: INPUT_CANCEL,
|
2107 | STATE_POSSIBLE: STATE_POSSIBLE,
|
2108 | STATE_BEGAN: STATE_BEGAN,
|
2109 | STATE_CHANGED: STATE_CHANGED,
|
2110 | STATE_ENDED: STATE_ENDED,
|
2111 | STATE_RECOGNIZED: STATE_RECOGNIZED,
|
2112 | STATE_CANCELLED: STATE_CANCELLED,
|
2113 | STATE_FAILED: STATE_FAILED,
|
2114 | DIRECTION_NONE: DIRECTION_NONE,
|
2115 | DIRECTION_LEFT: DIRECTION_LEFT,
|
2116 | DIRECTION_RIGHT: DIRECTION_RIGHT,
|
2117 | DIRECTION_UP: DIRECTION_UP,
|
2118 | DIRECTION_DOWN: DIRECTION_DOWN,
|
2119 | DIRECTION_HORIZONTAL: DIRECTION_HORIZONTAL,
|
2120 | DIRECTION_VERTICAL: DIRECTION_VERTICAL,
|
2121 | DIRECTION_ALL: DIRECTION_ALL,
|
2122 | Manager: Manager,
|
2123 | Input: Input,
|
2124 | TouchAction: TouchAction,
|
2125 | TouchInput: TouchInput,
|
2126 | MouseInput: MouseInput,
|
2127 | PointerEventInput: PointerEventInput,
|
2128 | TouchMouseInput: TouchMouseInput,
|
2129 | SingleTouchInput: SingleTouchInput,
|
2130 | Recognizer: Recognizer,
|
2131 | AttrRecognizer: AttrRecognizer,
|
2132 | Tap: TapRecognizer,
|
2133 | Pan: PanRecognizer,
|
2134 | Swipe: SwipeRecognizer,
|
2135 | Pinch: PinchRecognizer,
|
2136 | Rotate: RotateRecognizer,
|
2137 | Press: PressRecognizer,
|
2138 | on: addEventListeners,
|
2139 | off: removeEventListeners,
|
2140 | each: each,
|
2141 | inherit: inherit,
|
2142 | bindFn: bindFn,
|
2143 | prefixed: prefixed
|
2144 | });
|
2145 | win.Hammer = Hammer;
|
2146 | export { Hammer };
|
2147 |
|
\ | No newline at end of file |