1 | /*
|
2 | * Gesture detector library that forked from github.com/EightMedia/hammer.js.
|
3 | */
|
4 |
|
5 | ;
|
6 |
|
7 | import util from './util.js';
|
8 |
|
9 | var Event, Utils, Detection, PointerEvent;
|
10 |
|
11 | /**
|
12 | * @object ons.GestureDetector
|
13 | * @category gesture
|
14 | * @description
|
15 | * [en]Utility class for gesture detection.[/en]
|
16 | * [ja]ジェスチャを検知するためのユーティリティクラスです。[/ja]
|
17 | */
|
18 |
|
19 | /**
|
20 | * @method constructor
|
21 | * @signature constructor(element[, options])
|
22 | * @description
|
23 | * [en]Create a new GestureDetector instance.[/en]
|
24 | * [ja]GestureDetectorのインスタンスを生成します。[/ja]
|
25 | * @param {Element} element
|
26 | * [en]Name of the event.[/en]
|
27 | * [ja]ジェスチャを検知するDOM要素を指定します。[/ja]
|
28 | * @param {Object} [options]
|
29 | * [en]Options object.[/en]
|
30 | * [ja]オプションを指定します。[/ja]
|
31 | * @return {ons.GestureDetector.Instance}
|
32 | */
|
33 | var GestureDetector = function GestureDetector(element, options) {
|
34 | return new GestureDetector.Instance(element, options || {});
|
35 | };
|
36 |
|
37 |
|
38 | /**
|
39 | * default settings.
|
40 | * more settings are defined per gesture at `/gestures`. Each gesture can be disabled/enabled
|
41 | * by setting it's name (like `swipe`) to false.
|
42 | * You can set the defaults for all instances by changing this object before creating an instance.
|
43 | * @example
|
44 | * ````
|
45 | * GestureDetector.defaults.drag = false;
|
46 | * GestureDetector.defaults.behavior.touchAction = 'pan-y';
|
47 | * delete GestureDetector.defaults.behavior.userSelect;
|
48 | * ````
|
49 | * @property defaults
|
50 | * @type {Object}
|
51 | */
|
52 | GestureDetector.defaults = {
|
53 | behavior: {
|
54 | // userSelect: 'none', // Also disables selection in `input` children
|
55 | touchAction: 'pan-y',
|
56 | touchCallout: 'none',
|
57 | contentZooming: 'none',
|
58 | userDrag: 'none',
|
59 | tapHighlightColor: 'rgba(0,0,0,0)'
|
60 | }
|
61 | };
|
62 |
|
63 | /**
|
64 | * GestureDetector document where the base events are added at
|
65 | * @property DOCUMENT
|
66 | * @type {HTMLElement}
|
67 | * @default window.document
|
68 | */
|
69 | GestureDetector.DOCUMENT = document;
|
70 |
|
71 | /**
|
72 | * detect support for pointer events
|
73 | * @property HAS_POINTEREVENTS
|
74 | * @type {Boolean}
|
75 | */
|
76 | GestureDetector.HAS_POINTEREVENTS = navigator.pointerEnabled || navigator.msPointerEnabled;
|
77 |
|
78 | /**
|
79 | * detect support for touch events
|
80 | * @property HAS_TOUCHEVENTS
|
81 | * @type {Boolean}
|
82 | */
|
83 | GestureDetector.HAS_TOUCHEVENTS = ('ontouchstart' in window);
|
84 |
|
85 | /**
|
86 | * detect mobile browsers
|
87 | * @property IS_MOBILE
|
88 | * @type {Boolean}
|
89 | */
|
90 | GestureDetector.IS_MOBILE = /mobile|tablet|ip(ad|hone|od)|android|silk/i.test(navigator.userAgent);
|
91 |
|
92 | /**
|
93 | * detect if we want to support mouseevents at all
|
94 | * @property NO_MOUSEEVENTS
|
95 | * @type {Boolean}
|
96 | */
|
97 | GestureDetector.NO_MOUSEEVENTS = (GestureDetector.HAS_TOUCHEVENTS && GestureDetector.IS_MOBILE) || GestureDetector.HAS_POINTEREVENTS;
|
98 |
|
99 | /**
|
100 | * interval in which GestureDetector recalculates current velocity/direction/angle in ms
|
101 | * @property CALCULATE_INTERVAL
|
102 | * @type {Number}
|
103 | * @default 25
|
104 | */
|
105 | GestureDetector.CALCULATE_INTERVAL = 25;
|
106 |
|
107 | /**
|
108 | * eventtypes per touchevent (start, move, end) are filled by `Event.determineEventTypes` on `setup`
|
109 | * the object contains the DOM event names per type (`EVENT_START`, `EVENT_MOVE`, `EVENT_END`)
|
110 | * @property EVENT_TYPES
|
111 | * @private
|
112 | * @writeOnce
|
113 | * @type {Object}
|
114 | */
|
115 | var EVENT_TYPES = {};
|
116 |
|
117 | /**
|
118 | * direction strings, for safe comparisons
|
119 | * @property DIRECTION_DOWN|LEFT|UP|RIGHT
|
120 | * @final
|
121 | * @type {String}
|
122 | * @default 'down' 'left' 'up' 'right'
|
123 | */
|
124 | var DIRECTION_DOWN = GestureDetector.DIRECTION_DOWN = 'down';
|
125 | var DIRECTION_LEFT = GestureDetector.DIRECTION_LEFT = 'left';
|
126 | var DIRECTION_UP = GestureDetector.DIRECTION_UP = 'up';
|
127 | var DIRECTION_RIGHT = GestureDetector.DIRECTION_RIGHT = 'right';
|
128 |
|
129 | /**
|
130 | * pointertype strings, for safe comparisons
|
131 | * @property POINTER_MOUSE|TOUCH|PEN
|
132 | * @final
|
133 | * @type {String}
|
134 | * @default 'mouse' 'touch' 'pen'
|
135 | */
|
136 | var POINTER_MOUSE = GestureDetector.POINTER_MOUSE = 'mouse';
|
137 | var POINTER_TOUCH = GestureDetector.POINTER_TOUCH = 'touch';
|
138 | var POINTER_PEN = GestureDetector.POINTER_PEN = 'pen';
|
139 |
|
140 | /**
|
141 | * eventtypes
|
142 | * @property EVENT_START|MOVE|END|RELEASE|TOUCH
|
143 | * @final
|
144 | * @type {String}
|
145 | * @default 'start' 'change' 'move' 'end' 'release' 'touch'
|
146 | */
|
147 | var EVENT_START = GestureDetector.EVENT_START = 'start';
|
148 | var EVENT_MOVE = GestureDetector.EVENT_MOVE = 'move';
|
149 | var EVENT_END = GestureDetector.EVENT_END = 'end';
|
150 | var EVENT_RELEASE = GestureDetector.EVENT_RELEASE = 'release';
|
151 | var EVENT_TOUCH = GestureDetector.EVENT_TOUCH = 'touch';
|
152 |
|
153 | /**
|
154 | * if the window events are set...
|
155 | * @property READY
|
156 | * @writeOnce
|
157 | * @type {Boolean}
|
158 | * @default false
|
159 | */
|
160 | GestureDetector.READY = false;
|
161 |
|
162 | /**
|
163 | * plugins namespace
|
164 | * @property plugins
|
165 | * @type {Object}
|
166 | */
|
167 | GestureDetector.plugins = GestureDetector.plugins || {};
|
168 |
|
169 | /**
|
170 | * gestures namespace
|
171 | * see `/gestures` for the definitions
|
172 | * @property gestures
|
173 | * @type {Object}
|
174 | */
|
175 | GestureDetector.gestures = GestureDetector.gestures || {};
|
176 |
|
177 | /**
|
178 | * setup events to detect gestures on the document
|
179 | * this function is called when creating an new instance
|
180 | * @private
|
181 | */
|
182 | function setup(opts) {
|
183 | if (GestureDetector.READY) {
|
184 | return;
|
185 | }
|
186 |
|
187 | // find what eventtypes we add listeners to
|
188 | Event.determineEventTypes();
|
189 |
|
190 | // Register all gestures inside GestureDetector.gestures
|
191 | Utils.each(GestureDetector.gestures, function(gesture) {
|
192 | Detection.register(gesture);
|
193 | });
|
194 |
|
195 | // Add touch events on the document
|
196 | Event.onTouch(GestureDetector.DOCUMENT, EVENT_MOVE, Detection.detect, opts);
|
197 | Event.onTouch(GestureDetector.DOCUMENT, EVENT_END, Detection.detect, opts);
|
198 |
|
199 | // GestureDetector is ready...!
|
200 | GestureDetector.READY = true;
|
201 | }
|
202 |
|
203 | /**
|
204 | * @module GestureDetector
|
205 | *
|
206 | * @class Utils
|
207 | * @static
|
208 | */
|
209 | Utils = GestureDetector.utils = {
|
210 | /**
|
211 | * extend method, could also be used for cloning when `dest` is an empty object.
|
212 | * changes the dest object
|
213 | * @param {Object} dest
|
214 | * @param {Object} src
|
215 | * @param {Boolean} [merge=false] do a merge
|
216 | * @return {Object} dest
|
217 | */
|
218 | extend: function extend(dest, src, merge) {
|
219 | for (var key in src) {
|
220 | if (Object.prototype.hasOwnProperty.call(src, key) && (dest[key] === undefined || !merge)) {
|
221 | dest[key] = src[key];
|
222 | }
|
223 | }
|
224 | return dest;
|
225 | },
|
226 |
|
227 | /**
|
228 | * simple addEventListener wrapper
|
229 | * @param {HTMLElement} element
|
230 | * @param {String} type
|
231 | * @param {Function} handler
|
232 | */
|
233 | on: function on(element, type, handler, opt) {
|
234 | util.addEventListener(element, type, handler, opt, true);
|
235 | },
|
236 |
|
237 | /**
|
238 | * simple removeEventListener wrapper
|
239 | * @param {HTMLElement} element
|
240 | * @param {String} type
|
241 | * @param {Function} handler
|
242 | */
|
243 | off: function off(element, type, handler, opt) {
|
244 | util.removeEventListener(element, type, handler, opt, true);
|
245 | },
|
246 |
|
247 | /**
|
248 | * forEach over arrays and objects
|
249 | * @param {Object|Array} obj
|
250 | * @param {Function} iterator
|
251 | * @param {any} iterator.item
|
252 | * @param {Number} iterator.index
|
253 | * @param {Object|Array} iterator.obj the source object
|
254 | * @param {Object} context value to use as `this` in the iterator
|
255 | */
|
256 | each: function each(obj, iterator, context) {
|
257 | var i, len;
|
258 |
|
259 | // native forEach on arrays
|
260 | if ('forEach' in obj) {
|
261 | obj.forEach(iterator, context);
|
262 | // arrays
|
263 | } else if (obj.length !== undefined) {
|
264 | for (i = 0, len = obj.length; i < len; i++) {
|
265 | if (iterator.call(context, obj[i], i, obj) === false) {
|
266 | return;
|
267 | }
|
268 | }
|
269 | // objects
|
270 | } else {
|
271 | for (i in obj) {
|
272 | if (Object.prototype.hasOwnProperty.call(obj, i) &&
|
273 | iterator.call(context, obj[i], i, obj) === false) {
|
274 | return;
|
275 | }
|
276 | }
|
277 | }
|
278 | },
|
279 |
|
280 | /**
|
281 | * find if a string contains the string using indexOf
|
282 | * @param {String} src
|
283 | * @param {String} find
|
284 | * @return {Boolean} found
|
285 | */
|
286 | inStr: function inStr(src, find) {
|
287 | return src.indexOf(find) > -1;
|
288 | },
|
289 |
|
290 | /**
|
291 | * find if a array contains the object using indexOf or a simple polyfill
|
292 | * @param {String} src
|
293 | * @param {String} find
|
294 | * @return {Boolean|Number} false when not found, or the index
|
295 | */
|
296 | inArray: function inArray(src, find, deep) {
|
297 | if (deep) {
|
298 | for (var i = 0, len = src.length; i < len; i++) { // Array.findIndex
|
299 | if (Object.keys(find).every(function(key) { return src[i][key] === find[key]; })) {
|
300 | return i;
|
301 | }
|
302 | }
|
303 | return -1;
|
304 | }
|
305 |
|
306 | if (src.indexOf) {
|
307 | return src.indexOf(find);
|
308 | } else {
|
309 | for (var i = 0, len = src.length; i < len; i++) {
|
310 | if (src[i] === find) {
|
311 | return i;
|
312 | }
|
313 | }
|
314 | return -1;
|
315 | }
|
316 | },
|
317 |
|
318 | /**
|
319 | * convert an array-like object (`arguments`, `touchlist`) to an array
|
320 | * @param {Object} obj
|
321 | * @return {Array}
|
322 | */
|
323 | toArray: function toArray(obj) {
|
324 | return Array.prototype.slice.call(obj, 0);
|
325 | },
|
326 |
|
327 | /**
|
328 | * find if a node is in the given parent
|
329 | * @param {HTMLElement} node
|
330 | * @param {HTMLElement} parent
|
331 | * @return {Boolean} found
|
332 | */
|
333 | hasParent: function hasParent(node, parent) {
|
334 | while (node) {
|
335 | if (node == parent) {
|
336 | return true;
|
337 | }
|
338 | node = node.parentNode;
|
339 | }
|
340 | return false;
|
341 | },
|
342 |
|
343 | /**
|
344 | * get the center of all the touches
|
345 | * @param {Array} touches
|
346 | * @return {Object} center contains `pageX`, `pageY`, `clientX` and `clientY` properties
|
347 | */
|
348 | getCenter: function getCenter(touches) {
|
349 | var pageX = [],
|
350 | pageY = [],
|
351 | clientX = [],
|
352 | clientY = [],
|
353 | min = Math.min,
|
354 | max = Math.max;
|
355 |
|
356 | // no need to loop when only one touch
|
357 | if (touches.length === 1) {
|
358 | return {
|
359 | pageX: touches[0].pageX,
|
360 | pageY: touches[0].pageY,
|
361 | clientX: touches[0].clientX,
|
362 | clientY: touches[0].clientY
|
363 | };
|
364 | }
|
365 |
|
366 | Utils.each(touches, function(touch) {
|
367 | pageX.push(touch.pageX);
|
368 | pageY.push(touch.pageY);
|
369 | clientX.push(touch.clientX);
|
370 | clientY.push(touch.clientY);
|
371 | });
|
372 |
|
373 | return {
|
374 | pageX: (min.apply(Math, pageX) + max.apply(Math, pageX)) / 2,
|
375 | pageY: (min.apply(Math, pageY) + max.apply(Math, pageY)) / 2,
|
376 | clientX: (min.apply(Math, clientX) + max.apply(Math, clientX)) / 2,
|
377 | clientY: (min.apply(Math, clientY) + max.apply(Math, clientY)) / 2
|
378 | };
|
379 | },
|
380 |
|
381 | /**
|
382 | * calculate the velocity between two points. unit is in px per ms.
|
383 | * @param {Number} deltaTime
|
384 | * @param {Number} deltaX
|
385 | * @param {Number} deltaY
|
386 | * @return {Object} velocity `x` and `y`
|
387 | */
|
388 | getVelocity: function getVelocity(deltaTime, deltaX, deltaY) {
|
389 | return {
|
390 | x: Math.abs(deltaX / deltaTime) || 0,
|
391 | y: Math.abs(deltaY / deltaTime) || 0
|
392 | };
|
393 | },
|
394 |
|
395 | /**
|
396 | * calculate the angle between two coordinates
|
397 | * @param {Touch} touch1
|
398 | * @param {Touch} touch2
|
399 | * @return {Number} angle
|
400 | */
|
401 | getAngle: function getAngle(touch1, touch2) {
|
402 | var x = touch2.clientX - touch1.clientX,
|
403 | y = touch2.clientY - touch1.clientY;
|
404 |
|
405 | return Math.atan2(y, x) * 180 / Math.PI;
|
406 | },
|
407 |
|
408 | /**
|
409 | * do a small comparison to get the direction between two touches.
|
410 | * @param {Touch} touch1
|
411 | * @param {Touch} touch2
|
412 | * @return {String} direction matches `DIRECTION_LEFT|RIGHT|UP|DOWN`
|
413 | */
|
414 | getDirection: function getDirection(touch1, touch2) {
|
415 | var x = Math.abs(touch1.clientX - touch2.clientX),
|
416 | y = Math.abs(touch1.clientY - touch2.clientY);
|
417 |
|
418 | if (x >= y) {
|
419 | return touch1.clientX - touch2.clientX > 0 ? DIRECTION_LEFT : DIRECTION_RIGHT;
|
420 | }
|
421 | return touch1.clientY - touch2.clientY > 0 ? DIRECTION_UP : DIRECTION_DOWN;
|
422 | },
|
423 |
|
424 | /**
|
425 | * calculate the distance between two touches
|
426 | * @param {Touch}touch1
|
427 | * @param {Touch} touch2
|
428 | * @return {Number} distance
|
429 | */
|
430 | getDistance: function getDistance(touch1, touch2) {
|
431 | var x = touch2.clientX - touch1.clientX,
|
432 | y = touch2.clientY - touch1.clientY;
|
433 |
|
434 | return Math.sqrt((x * x) + (y * y));
|
435 | },
|
436 |
|
437 | /**
|
438 | * calculate the scale factor between two touchLists
|
439 | * no scale is 1, and goes down to 0 when pinched together, and bigger when pinched out
|
440 | * @param {Array} start array of touches
|
441 | * @param {Array} end array of touches
|
442 | * @return {Number} scale
|
443 | */
|
444 | getScale: function getScale(start, end) {
|
445 | // need two fingers...
|
446 | if (start.length >= 2 && end.length >= 2) {
|
447 | return this.getDistance(end[0], end[1]) / this.getDistance(start[0], start[1]);
|
448 | }
|
449 | return 1;
|
450 | },
|
451 |
|
452 | /**
|
453 | * calculate the rotation degrees between two touchLists
|
454 | * @param {Array} start array of touches
|
455 | * @param {Array} end array of touches
|
456 | * @return {Number} rotation
|
457 | */
|
458 | getRotation: function getRotation(start, end) {
|
459 | // need two fingers
|
460 | if (start.length >= 2 && end.length >= 2) {
|
461 | return this.getAngle(end[1], end[0]) - this.getAngle(start[1], start[0]);
|
462 | }
|
463 | return 0;
|
464 | },
|
465 |
|
466 | /**
|
467 | * find out if the direction is vertical *
|
468 | * @param {String} direction matches `DIRECTION_UP|DOWN`
|
469 | * @return {Boolean} is_vertical
|
470 | */
|
471 | isVertical: function isVertical(direction) {
|
472 | return direction == DIRECTION_UP || direction == DIRECTION_DOWN;
|
473 | },
|
474 |
|
475 | /**
|
476 | * set css properties with their prefixes
|
477 | * @param {HTMLElement} element
|
478 | * @param {String} prop
|
479 | * @param {String} value
|
480 | * @param {Boolean} [toggle=true]
|
481 | * @return {Boolean}
|
482 | */
|
483 | setPrefixedCss: function setPrefixedCss(element, prop, value, toggle) {
|
484 | var prefixes = ['', 'Webkit', 'Moz', 'O', 'ms'];
|
485 | prop = Utils.toCamelCase(prop);
|
486 |
|
487 | for (var i = 0; i < prefixes.length; i++) {
|
488 | var p = prop;
|
489 | // prefixes
|
490 | if (prefixes[i]) {
|
491 | p = prefixes[i] + p.slice(0, 1).toUpperCase() + p.slice(1);
|
492 | }
|
493 |
|
494 | // test the style
|
495 | if (p in element.style) {
|
496 | element.style[p] = (toggle === null || toggle) && value || '';
|
497 | break;
|
498 | }
|
499 | }
|
500 | },
|
501 |
|
502 | /**
|
503 | * toggle browser default behavior by setting css properties.
|
504 | * `userSelect='none'` also sets `element.onselectstart` to false
|
505 | * `userDrag='none'` also sets `element.ondragstart` to false
|
506 | *
|
507 | * @param {HtmlElement} element
|
508 | * @param {Object} props
|
509 | * @param {Boolean} [toggle=true]
|
510 | */
|
511 | toggleBehavior: function toggleBehavior(element, props, toggle) {
|
512 | if (!props || !element || !element.style) {
|
513 | return;
|
514 | }
|
515 |
|
516 | // set the css properties
|
517 | Utils.each(props, function(value, prop) {
|
518 | Utils.setPrefixedCss(element, prop, value, toggle);
|
519 | });
|
520 |
|
521 | var falseFn = toggle && function() {
|
522 | return false;
|
523 | };
|
524 |
|
525 | // also the disable onselectstart
|
526 | if (props.userSelect == 'none') {
|
527 | element.onselectstart = falseFn;
|
528 | }
|
529 | // and disable ondragstart
|
530 | if (props.userDrag == 'none') {
|
531 | element.ondragstart = falseFn;
|
532 | }
|
533 | },
|
534 |
|
535 | /**
|
536 | * convert a string with underscores to camelCase
|
537 | * so prevent_default becomes preventDefault
|
538 | * @param {String} str
|
539 | * @return {String} camelCaseStr
|
540 | */
|
541 | toCamelCase: function toCamelCase(str) {
|
542 | return str.replace(/[_-]([a-z])/g, function(s) {
|
543 | return s[1].toUpperCase();
|
544 | });
|
545 | }
|
546 | };
|
547 |
|
548 |
|
549 | /**
|
550 | * @module GestureDetector
|
551 | */
|
552 | /**
|
553 | * @class Event
|
554 | * @static
|
555 | */
|
556 | Event = GestureDetector.event = {
|
557 | /**
|
558 | * when touch events have been fired, this is true
|
559 | * this is used to stop mouse events
|
560 | * @property prevent_mouseevents
|
561 | * @private
|
562 | * @type {Boolean}
|
563 | */
|
564 | preventMouseEvents: false,
|
565 |
|
566 | /**
|
567 | * if EVENT_START has been fired
|
568 | * @property started
|
569 | * @private
|
570 | * @type {Boolean}
|
571 | */
|
572 | started: false,
|
573 |
|
574 | /**
|
575 | * when the mouse is hold down, this is true
|
576 | * @property should_detect
|
577 | * @private
|
578 | * @type {Boolean}
|
579 | */
|
580 | shouldDetect: false,
|
581 |
|
582 | /**
|
583 | * simple event binder with a hook and support for multiple types
|
584 | * @param {HTMLElement} element
|
585 | * @param {String} type
|
586 | * @param {Function} handler
|
587 | * @param {Object} [opt]
|
588 | * @param {Function} [hook]
|
589 | * @param {Object} hook.type
|
590 | */
|
591 | on: function on(element, type, handler, opt, hook) {
|
592 | var types = type.split(' ');
|
593 | Utils.each(types, function(type) {
|
594 | Utils.on(element, type, handler, opt);
|
595 | hook && hook(type);
|
596 | });
|
597 | },
|
598 |
|
599 | /**
|
600 | * simple event unbinder with a hook and support for multiple types
|
601 | * @param {HTMLElement} element
|
602 | * @param {String} type
|
603 | * @param {Function} handler
|
604 | * @param {Object} [opt]
|
605 | * @param {Function} [hook]
|
606 | * @param {Object} hook.type
|
607 | */
|
608 | off: function off(element, type, handler, opt, hook) {
|
609 | var types = type.split(' ');
|
610 | Utils.each(types, function(type) {
|
611 | Utils.off(element, type, handler, opt);
|
612 | hook && hook(type);
|
613 | });
|
614 | },
|
615 |
|
616 | /**
|
617 | * the core touch event handler.
|
618 | * this finds out if we should to detect gestures
|
619 | * @param {HTMLElement} element
|
620 | * @param {String} eventType matches `EVENT_START|MOVE|END`
|
621 | * @param {Function} handler
|
622 | * @return onTouchHandler {Function} the core event handler
|
623 | */
|
624 | onTouch: function onTouch(element, eventType, handler, opt) {
|
625 | var self = this;
|
626 |
|
627 | var onTouchHandler = function onTouchHandler(ev) {
|
628 | var srcType = ev.type.toLowerCase(),
|
629 | isPointer = GestureDetector.HAS_POINTEREVENTS,
|
630 | isMouse = Utils.inStr(srcType, 'mouse'),
|
631 | triggerType;
|
632 |
|
633 | // if we are in a mouseevent, but there has been a touchevent triggered in this session
|
634 | // we want to do nothing. simply break out of the event.
|
635 | if (isMouse && self.preventMouseEvents) {
|
636 | return;
|
637 |
|
638 | // mousebutton must be down
|
639 | } else if (isMouse && eventType == EVENT_START && ev.button === 0) {
|
640 | self.preventMouseEvents = false;
|
641 | self.shouldDetect = true;
|
642 | } else if (isPointer && eventType == EVENT_START) {
|
643 | self.shouldDetect = (ev.buttons === 1 || PointerEvent.matchType(POINTER_TOUCH, ev));
|
644 | // just a valid start event, but no mouse
|
645 | } else if (!isMouse && eventType == EVENT_START) {
|
646 | self.preventMouseEvents = true;
|
647 | self.shouldDetect = true;
|
648 | }
|
649 |
|
650 | // update the pointer event before entering the detection
|
651 | if (isPointer && eventType != EVENT_END) {
|
652 | PointerEvent.updatePointer(eventType, ev);
|
653 | }
|
654 |
|
655 | // we are in a touch/down state, so allowed detection of gestures
|
656 | if (self.shouldDetect) {
|
657 | triggerType = self.doDetect.call(self, ev, eventType, element, handler);
|
658 | }
|
659 |
|
660 | // ...and we are done with the detection
|
661 | // so reset everything to start each detection totally fresh
|
662 | if (triggerType == EVENT_END) {
|
663 | self.preventMouseEvents = false;
|
664 | self.shouldDetect = false;
|
665 | PointerEvent.reset();
|
666 | // update the pointerevent object after the detection
|
667 | }
|
668 |
|
669 | if (isPointer && eventType == EVENT_END) {
|
670 | PointerEvent.updatePointer(eventType, ev);
|
671 | }
|
672 | };
|
673 |
|
674 | this.on(element, EVENT_TYPES[eventType], onTouchHandler, opt);
|
675 | return onTouchHandler;
|
676 | },
|
677 |
|
678 | /**
|
679 | * the core detection method
|
680 | * this finds out what GestureDetector-touch-events to trigger
|
681 | * @param {Object} ev
|
682 | * @param {String} eventType matches `EVENT_START|MOVE|END`
|
683 | * @param {HTMLElement} element
|
684 | * @param {Function} handler
|
685 | * @return {String} triggerType matches `EVENT_START|MOVE|END`
|
686 | */
|
687 | doDetect: function doDetect(ev, eventType, element, handler) {
|
688 | var touchList = this.getTouchList(ev, eventType);
|
689 | var touchListLength = touchList.length;
|
690 | var triggerType = eventType;
|
691 | var triggerChange = touchList.trigger; // used by fakeMultitouch plugin
|
692 | var changedLength = touchListLength;
|
693 |
|
694 | // at each touchstart-like event we want also want to trigger a TOUCH event...
|
695 | if (eventType == EVENT_START) {
|
696 | triggerChange = EVENT_TOUCH;
|
697 | // ...the same for a touchend-like event
|
698 | } else if (eventType == EVENT_END) {
|
699 | triggerChange = EVENT_RELEASE;
|
700 |
|
701 | // keep track of how many touches have been removed
|
702 | changedLength = touchList.length - ((ev.changedTouches) ? ev.changedTouches.length : 1);
|
703 | }
|
704 |
|
705 | // after there are still touches on the screen,
|
706 | // we just want to trigger a MOVE event. so change the START or END to a MOVE
|
707 | // but only after detection has been started, the first time we actually want a START
|
708 | if (changedLength > 0 && this.started) {
|
709 | triggerType = EVENT_MOVE;
|
710 | }
|
711 |
|
712 | // detection has been started, we keep track of this, see above
|
713 | this.started = true;
|
714 |
|
715 | // generate some event data, some basic information
|
716 | var evData = this.collectEventData(element, triggerType, touchList, ev);
|
717 |
|
718 | // trigger the triggerType event before the change (TOUCH, RELEASE) events
|
719 | // but the END event should be at last
|
720 | if (eventType != EVENT_END) {
|
721 | handler.call(Detection, evData);
|
722 | }
|
723 |
|
724 | // trigger a change (TOUCH, RELEASE) event, this means the length of the touches changed
|
725 | if (triggerChange) {
|
726 | evData.changedLength = changedLength;
|
727 | evData.eventType = triggerChange;
|
728 |
|
729 | handler.call(Detection, evData);
|
730 |
|
731 | evData.eventType = triggerType;
|
732 | delete evData.changedLength;
|
733 | }
|
734 |
|
735 | // trigger the END event
|
736 | if (triggerType == EVENT_END) {
|
737 | handler.call(Detection, evData);
|
738 |
|
739 | // ...and we are done with the detection
|
740 | // so reset everything to start each detection totally fresh
|
741 | this.started = false;
|
742 | }
|
743 |
|
744 | return triggerType;
|
745 | },
|
746 |
|
747 | /**
|
748 | * we have different events for each device/browser
|
749 | * determine what we need and set them in the EVENT_TYPES constant
|
750 | * the `onTouch` method is bind to these properties.
|
751 | * @return {Object} events
|
752 | */
|
753 | determineEventTypes: function determineEventTypes() {
|
754 | var types;
|
755 | if (GestureDetector.HAS_POINTEREVENTS) {
|
756 | if (window.PointerEvent) {
|
757 | types = [
|
758 | 'pointerdown',
|
759 | 'pointermove',
|
760 | 'pointerup pointercancel lostpointercapture'
|
761 | ];
|
762 | } else {
|
763 | types = [
|
764 | 'MSPointerDown',
|
765 | 'MSPointerMove',
|
766 | 'MSPointerUp MSPointerCancel MSLostPointerCapture'
|
767 | ];
|
768 | }
|
769 | } else if (GestureDetector.NO_MOUSEEVENTS) {
|
770 | types = [
|
771 | 'touchstart',
|
772 | 'touchmove',
|
773 | 'touchend touchcancel'
|
774 | ];
|
775 | } else {
|
776 | types = [
|
777 | 'touchstart mousedown',
|
778 | 'touchmove mousemove',
|
779 | 'touchend touchcancel mouseup'
|
780 | ];
|
781 | }
|
782 |
|
783 | EVENT_TYPES[EVENT_START] = types[0];
|
784 | EVENT_TYPES[EVENT_MOVE] = types[1];
|
785 | EVENT_TYPES[EVENT_END] = types[2];
|
786 | return EVENT_TYPES;
|
787 | },
|
788 |
|
789 | /**
|
790 | * create touchList depending on the event
|
791 | * @param {Object} ev
|
792 | * @param {String} eventType
|
793 | * @return {Array} touches
|
794 | */
|
795 | getTouchList: function getTouchList(ev, eventType) {
|
796 | // get the fake pointerEvent touchlist
|
797 | if (GestureDetector.HAS_POINTEREVENTS) {
|
798 | return PointerEvent.getTouchList();
|
799 | }
|
800 |
|
801 | // get the touchlist
|
802 | if (ev.touches) {
|
803 | if (eventType == EVENT_MOVE) {
|
804 | return ev.touches;
|
805 | }
|
806 |
|
807 | var identifiers = [];
|
808 | var concat = [].concat(Utils.toArray(ev.touches), Utils.toArray(ev.changedTouches));
|
809 | var touchList = [];
|
810 |
|
811 | Utils.each(concat, function(touch) {
|
812 | if (Utils.inArray(identifiers, touch.identifier) === -1) {
|
813 | touchList.push(touch);
|
814 | }
|
815 | identifiers.push(touch.identifier);
|
816 | });
|
817 |
|
818 | return touchList;
|
819 | }
|
820 |
|
821 | // make fake touchList from mouse position
|
822 | ev.identifier = 1;
|
823 | return [ev];
|
824 | },
|
825 |
|
826 | /**
|
827 | * collect basic event data
|
828 | * @param {HTMLElement} element
|
829 | * @param {String} eventType matches `EVENT_START|MOVE|END`
|
830 | * @param {Array} touches
|
831 | * @param {Object} ev
|
832 | * @return {Object} ev
|
833 | */
|
834 | collectEventData: function collectEventData(element, eventType, touches, ev) {
|
835 | // find out pointerType
|
836 | var pointerType = POINTER_TOUCH;
|
837 | if (Utils.inStr(ev.type, 'mouse') || PointerEvent.matchType(POINTER_MOUSE, ev)) {
|
838 | pointerType = POINTER_MOUSE;
|
839 | } else if (PointerEvent.matchType(POINTER_PEN, ev)) {
|
840 | pointerType = POINTER_PEN;
|
841 | }
|
842 |
|
843 | return {
|
844 | center: Utils.getCenter(touches),
|
845 | timeStamp: Date.now(),
|
846 | target: ev.target,
|
847 | touches: touches,
|
848 | eventType: eventType,
|
849 | pointerType: pointerType,
|
850 | srcEvent: ev,
|
851 |
|
852 | /**
|
853 | * prevent the browser default actions
|
854 | * mostly used to disable scrolling of the browser
|
855 | */
|
856 | preventDefault: function() {
|
857 | var srcEvent = this.srcEvent;
|
858 | srcEvent.preventManipulation && srcEvent.preventManipulation();
|
859 | srcEvent.preventDefault && srcEvent.preventDefault();
|
860 | },
|
861 |
|
862 | /**
|
863 | * stop bubbling the event up to its parents
|
864 | */
|
865 | stopPropagation: function() {
|
866 | this.srcEvent.stopPropagation();
|
867 | },
|
868 |
|
869 | /**
|
870 | * immediately stop gesture detection
|
871 | * might be useful after a swipe was detected
|
872 | * @return {*}
|
873 | */
|
874 | stopDetect: function() {
|
875 | return Detection.stopDetect();
|
876 | }
|
877 | };
|
878 | }
|
879 | };
|
880 |
|
881 |
|
882 | /**
|
883 | * @module GestureDetector
|
884 | *
|
885 | * @class PointerEvent
|
886 | * @static
|
887 | */
|
888 | PointerEvent = GestureDetector.PointerEvent = {
|
889 | /**
|
890 | * holds all pointers, by `identifier`
|
891 | * @property pointers
|
892 | * @type {Object}
|
893 | */
|
894 | pointers: {},
|
895 |
|
896 | /**
|
897 | * get the pointers as an array
|
898 | * @return {Array} touchlist
|
899 | */
|
900 | getTouchList: function getTouchList() {
|
901 | var touchlist = [];
|
902 | // we can use forEach since pointerEvents only is in IE10
|
903 | Utils.each(this.pointers, function(pointer) {
|
904 | touchlist.push(pointer);
|
905 | });
|
906 | return touchlist;
|
907 | },
|
908 |
|
909 | /**
|
910 | * update the position of a pointer
|
911 | * @param {String} eventType matches `EVENT_START|MOVE|END`
|
912 | * @param {Object} pointerEvent
|
913 | */
|
914 | updatePointer: function updatePointer(eventType, pointerEvent) {
|
915 | if (eventType == EVENT_END || (eventType != EVENT_END && pointerEvent.buttons !== 1)) {
|
916 | delete this.pointers[pointerEvent.pointerId];
|
917 | } else {
|
918 | pointerEvent.identifier = pointerEvent.pointerId;
|
919 | this.pointers[pointerEvent.pointerId] = pointerEvent;
|
920 | }
|
921 | },
|
922 |
|
923 | /**
|
924 | * check if ev matches pointertype
|
925 | * @param {String} pointerType matches `POINTER_MOUSE|TOUCH|PEN`
|
926 | * @param {PointerEvent} ev
|
927 | */
|
928 | matchType: function matchType(pointerType, ev) {
|
929 | if (!ev.pointerType) {
|
930 | return false;
|
931 | }
|
932 |
|
933 | var pt = ev.pointerType,
|
934 | types = {};
|
935 |
|
936 | types[POINTER_MOUSE] = (pt === (ev.MSPOINTER_TYPE_MOUSE || POINTER_MOUSE));
|
937 | types[POINTER_TOUCH] = (pt === (ev.MSPOINTER_TYPE_TOUCH || POINTER_TOUCH));
|
938 | types[POINTER_PEN] = (pt === (ev.MSPOINTER_TYPE_PEN || POINTER_PEN));
|
939 | return types[pointerType];
|
940 | },
|
941 |
|
942 | /**
|
943 | * reset the stored pointers
|
944 | */
|
945 | reset: function resetList() {
|
946 | this.pointers = {};
|
947 | }
|
948 | };
|
949 |
|
950 |
|
951 | /**
|
952 | * @module GestureDetector
|
953 | *
|
954 | * @class Detection
|
955 | * @static
|
956 | */
|
957 | Detection = GestureDetector.detection = {
|
958 | // contains all registered GestureDetector.gestures in the correct order
|
959 | gestures: [],
|
960 |
|
961 | // data of the current GestureDetector.gesture detection session
|
962 | current: null,
|
963 |
|
964 | // the previous GestureDetector.gesture session data
|
965 | // is a full clone of the previous gesture.current object
|
966 | previous: null,
|
967 |
|
968 | // when this becomes true, no gestures are fired
|
969 | stopped: false,
|
970 |
|
971 | /**
|
972 | * start GestureDetector.gesture detection
|
973 | * @param {GestureDetector.Instance} inst
|
974 | * @param {Object} eventData
|
975 | */
|
976 | startDetect: function startDetect(inst, eventData) {
|
977 | // already busy with a GestureDetector.gesture detection on an element
|
978 | if (this.current) {
|
979 | return;
|
980 | }
|
981 |
|
982 | this.stopped = false;
|
983 |
|
984 | // holds current session
|
985 | this.current = {
|
986 | inst: inst, // reference to GestureDetectorInstance we're working for
|
987 | startEvent: Utils.extend({}, eventData), // start eventData for distances, timing etc
|
988 | lastEvent: false, // last eventData
|
989 | lastCalcEvent: false, // last eventData for calculations.
|
990 | futureCalcEvent: false, // last eventData for calculations.
|
991 | lastCalcData: {}, // last lastCalcData
|
992 | name: '' // current gesture we're in/detected, can be 'tap', 'hold' etc
|
993 | };
|
994 |
|
995 | this.detect(eventData);
|
996 | },
|
997 |
|
998 | /**
|
999 | * GestureDetector.gesture detection
|
1000 | * @param {Object} eventData
|
1001 | * @return {any}
|
1002 | */
|
1003 | detect: function detect(eventData) {
|
1004 | if (!this.current || this.stopped) {
|
1005 | return;
|
1006 | }
|
1007 |
|
1008 | // extend event data with calculations about scale, distance etc
|
1009 | eventData = this.extendEventData(eventData);
|
1010 |
|
1011 | // GestureDetector instance and instance options
|
1012 | var inst = this.current.inst,
|
1013 | instOptions = inst.options;
|
1014 |
|
1015 | // call GestureDetector.gesture handlers
|
1016 | Utils.each(this.gestures, function triggerGesture(gesture) {
|
1017 | // only when the instance options have enabled this gesture
|
1018 | if (!this.stopped && inst.enabled && instOptions[gesture.name]) {
|
1019 | gesture.handler.call(gesture, eventData, inst);
|
1020 | }
|
1021 | }, this);
|
1022 |
|
1023 | // store as previous event event
|
1024 | if (this.current) {
|
1025 | this.current.lastEvent = eventData;
|
1026 | }
|
1027 |
|
1028 | if (eventData.eventType == EVENT_END) {
|
1029 | this.stopDetect();
|
1030 | }
|
1031 |
|
1032 | return eventData; // eslint-disable-line consistent-return
|
1033 | },
|
1034 |
|
1035 | /**
|
1036 | * clear the GestureDetector.gesture vars
|
1037 | * this is called on endDetect, but can also be used when a final GestureDetector.gesture has been detected
|
1038 | * to stop other GestureDetector.gestures from being fired
|
1039 | */
|
1040 | stopDetect: function stopDetect() {
|
1041 | // clone current data to the store as the previous gesture
|
1042 | // used for the double tap gesture, since this is an other gesture detect session
|
1043 | this.previous = Utils.extend({}, this.current);
|
1044 |
|
1045 | // reset the current
|
1046 | this.current = null;
|
1047 | this.stopped = true;
|
1048 | },
|
1049 |
|
1050 | /**
|
1051 | * calculate velocity, angle and direction
|
1052 | * @param {Object} ev
|
1053 | * @param {Object} center
|
1054 | * @param {Number} deltaTime
|
1055 | * @param {Number} deltaX
|
1056 | * @param {Number} deltaY
|
1057 | */
|
1058 | getCalculatedData: function getCalculatedData(ev, center, deltaTime, deltaX, deltaY) {
|
1059 | var cur = this.current,
|
1060 | recalc = false,
|
1061 | calcEv = cur.lastCalcEvent,
|
1062 | calcData = cur.lastCalcData;
|
1063 |
|
1064 | if (calcEv && ev.timeStamp - calcEv.timeStamp > GestureDetector.CALCULATE_INTERVAL) {
|
1065 | center = calcEv.center;
|
1066 | deltaTime = ev.timeStamp - calcEv.timeStamp;
|
1067 | deltaX = ev.center.clientX - calcEv.center.clientX;
|
1068 | deltaY = ev.center.clientY - calcEv.center.clientY;
|
1069 | recalc = true;
|
1070 | }
|
1071 |
|
1072 | if (ev.eventType == EVENT_TOUCH || ev.eventType == EVENT_RELEASE) {
|
1073 | cur.futureCalcEvent = ev;
|
1074 | }
|
1075 |
|
1076 | if (!cur.lastCalcEvent || recalc) {
|
1077 | calcData.velocity = Utils.getVelocity(deltaTime, deltaX, deltaY);
|
1078 | calcData.angle = Utils.getAngle(center, ev.center);
|
1079 | calcData.direction = Utils.getDirection(center, ev.center);
|
1080 |
|
1081 | cur.lastCalcEvent = cur.futureCalcEvent || ev;
|
1082 | cur.futureCalcEvent = ev;
|
1083 | }
|
1084 |
|
1085 | ev.velocityX = calcData.velocity.x;
|
1086 | ev.velocityY = calcData.velocity.y;
|
1087 | ev.interimAngle = calcData.angle;
|
1088 | ev.interimDirection = calcData.direction;
|
1089 | },
|
1090 |
|
1091 | /**
|
1092 | * extend eventData for GestureDetector.gestures
|
1093 | * @param {Object} ev
|
1094 | * @return {Object} ev
|
1095 | */
|
1096 | extendEventData: function extendEventData(ev) {
|
1097 | var cur = this.current,
|
1098 | startEv = cur.startEvent,
|
1099 | lastEv = cur.lastEvent || startEv;
|
1100 |
|
1101 | // update the start touchlist to calculate the scale/rotation
|
1102 | if (ev.eventType == EVENT_TOUCH || ev.eventType == EVENT_RELEASE) {
|
1103 | startEv.touches = [];
|
1104 | Utils.each(ev.touches, function(touch) {
|
1105 | startEv.touches.push({
|
1106 | clientX: touch.clientX,
|
1107 | clientY: touch.clientY
|
1108 | });
|
1109 | });
|
1110 | }
|
1111 |
|
1112 | var deltaTime = ev.timeStamp - startEv.timeStamp,
|
1113 | deltaX = ev.center.clientX - startEv.center.clientX,
|
1114 | deltaY = ev.center.clientY - startEv.center.clientY;
|
1115 |
|
1116 | this.getCalculatedData(ev, lastEv.center, deltaTime, deltaX, deltaY);
|
1117 |
|
1118 | Utils.extend(ev, {
|
1119 | startEvent: startEv,
|
1120 |
|
1121 | deltaTime: deltaTime,
|
1122 | deltaX: deltaX,
|
1123 | deltaY: deltaY,
|
1124 |
|
1125 | distance: Utils.getDistance(startEv.center, ev.center),
|
1126 | angle: Utils.getAngle(startEv.center, ev.center),
|
1127 | direction: Utils.getDirection(startEv.center, ev.center),
|
1128 | scale: Utils.getScale(startEv.touches, ev.touches),
|
1129 | rotation: Utils.getRotation(startEv.touches, ev.touches)
|
1130 | });
|
1131 |
|
1132 | return ev;
|
1133 | },
|
1134 |
|
1135 | /**
|
1136 | * register new gesture
|
1137 | * @param {Object} gesture object, see `gestures/` for documentation
|
1138 | * @return {Array} gestures
|
1139 | */
|
1140 | register: function register(gesture) {
|
1141 | // add an enable gesture options if there is no given
|
1142 | var options = gesture.defaults || {};
|
1143 | if (options[gesture.name] === undefined) {
|
1144 | options[gesture.name] = true;
|
1145 | }
|
1146 |
|
1147 | // extend GestureDetector default options with the GestureDetector.gesture options
|
1148 | Utils.extend(GestureDetector.defaults, options, true);
|
1149 |
|
1150 | // set its index
|
1151 | gesture.index = gesture.index || 1000;
|
1152 |
|
1153 | // add GestureDetector.gesture to the list
|
1154 | this.gestures.push(gesture);
|
1155 |
|
1156 | // sort the list by index
|
1157 | this.gestures.sort(function(a, b) {
|
1158 | if (a.index < b.index) {
|
1159 | return -1;
|
1160 | }
|
1161 | if (a.index > b.index) {
|
1162 | return 1;
|
1163 | }
|
1164 | return 0;
|
1165 | });
|
1166 |
|
1167 | return this.gestures;
|
1168 | }
|
1169 | };
|
1170 |
|
1171 |
|
1172 | /**
|
1173 | * @module GestureDetector
|
1174 | */
|
1175 |
|
1176 | /**
|
1177 | * create new GestureDetector instance
|
1178 | * all methods should return the instance itself, so it is chainable.
|
1179 | *
|
1180 | * @class Instance
|
1181 | * @constructor
|
1182 | * @param {HTMLElement} element
|
1183 | * @param {Object} [options={}] options are merged with `GestureDetector.defaults`
|
1184 | * @return {GestureDetector.Instance}
|
1185 | */
|
1186 | GestureDetector.Instance = function(element, options) {
|
1187 | var self = this;
|
1188 | var listenerOptions = (options && options.passive) ? { passive: true } : undefined;
|
1189 |
|
1190 | // setup GestureDetectorJS window events and register all gestures
|
1191 | // this also sets up the default options
|
1192 | setup(listenerOptions);
|
1193 |
|
1194 | /**
|
1195 | * @property element
|
1196 | * @type {HTMLElement}
|
1197 | */
|
1198 | this.element = element;
|
1199 |
|
1200 | /**
|
1201 | * @property enabled
|
1202 | * @type {Boolean}
|
1203 | * @protected
|
1204 | */
|
1205 | this.enabled = true;
|
1206 |
|
1207 | /**
|
1208 | * options, merged with the defaults
|
1209 | * options with an _ are converted to camelCase
|
1210 | * @property options
|
1211 | * @type {Object}
|
1212 | */
|
1213 | Utils.each(options, function(value, name) {
|
1214 | delete options[name];
|
1215 | options[Utils.toCamelCase(name)] = value;
|
1216 | });
|
1217 |
|
1218 | this.options = Utils.extend(Utils.extend({}, GestureDetector.defaults), options || {});
|
1219 | this.options.listenerOptions = listenerOptions;
|
1220 |
|
1221 | // add some css to the element to prevent the browser from doing its native behavior
|
1222 | if (this.options.behavior) {
|
1223 | Utils.toggleBehavior(this.element, this.options.behavior, true);
|
1224 | }
|
1225 |
|
1226 | /**
|
1227 | * event start handler on the element to start the detection
|
1228 | * @property eventStartHandler
|
1229 | * @type {Object}
|
1230 | */
|
1231 | this.eventStartHandler = Event.onTouch(element, EVENT_START, function(ev) {
|
1232 | if (self.enabled && ev.eventType == EVENT_START) {
|
1233 | Detection.startDetect(self, ev);
|
1234 | } else if (ev.eventType == EVENT_TOUCH) {
|
1235 | Detection.detect(ev);
|
1236 | }
|
1237 | }, listenerOptions);
|
1238 |
|
1239 | /**
|
1240 | * keep a list of user event handlers which needs to be removed when calling 'dispose'
|
1241 | * @property eventHandlers
|
1242 | * @type {Array}
|
1243 | */
|
1244 | this.eventHandlers = [];
|
1245 | };
|
1246 |
|
1247 | GestureDetector.Instance.prototype = {
|
1248 | /**
|
1249 | * @method on
|
1250 | * @signature on(gestures, handler)
|
1251 | * @description
|
1252 | * [en]Adds an event handler for a gesture. Available gestures are: drag, dragleft, dragright, dragup, dragdown, hold, release, swipe, swipeleft, swiperight, swipeup, swipedown, tap, doubletap, touch, transform, pinch, pinchin, pinchout and rotate. [/en]
|
1253 | * [ja]ジェスチャに対するイベントハンドラを追加します。指定できるジェスチャ名は、drag dragleft dragright dragup dragdown hold release swipe swipeleft swiperight swipeup swipedown tap doubletap touch transform pinch pinchin pinchout rotate です。[/ja]
|
1254 | * @param {String} gestures
|
1255 | * [en]A space separated list of gestures.[/en]
|
1256 | * [ja]検知するジェスチャ名を指定します。スペースで複数指定することができます。[/ja]
|
1257 | * @param {Function} handler
|
1258 | * [en]An event handling function.[/en]
|
1259 | * [ja]イベントハンドラとなる関数オブジェクトを指定します。[/ja]
|
1260 | */
|
1261 | on: function onEvent(gestures, handler, opt) {
|
1262 | var self = this;
|
1263 |
|
1264 | Event.on(self.element, gestures, handler, util.extend({}, self.options.listenerOptions, opt), function(type) {
|
1265 | self.eventHandlers.push({ gesture: type, handler: handler });
|
1266 | });
|
1267 | return self;
|
1268 | },
|
1269 |
|
1270 | /**
|
1271 | * @method off
|
1272 | * @signature off(gestures, handler)
|
1273 | * @description
|
1274 | * [en]Remove an event listener.[/en]
|
1275 | * [ja]イベントリスナーを削除します。[/ja]
|
1276 | * @param {String} gestures
|
1277 | * [en]A space separated list of gestures.[/en]
|
1278 | * [ja]ジェスチャ名を指定します。スペースで複数指定することができます。[/ja]
|
1279 | * @param {Function} handler
|
1280 | * [en]An event handling function.[/en]
|
1281 | * [ja]イベントハンドラとなる関数オブジェクトを指定します。[/ja]
|
1282 | */
|
1283 | off: function offEvent(gestures, handler, opt) {
|
1284 | var self = this;
|
1285 |
|
1286 | Event.off(self.element, gestures, handler, util.extend({}, self.options.listenerOptions, opt), function(type) {
|
1287 | var index = Utils.inArray(self.eventHandlers, { gesture: type, handler: handler }, true);
|
1288 | if (index >= 0) {
|
1289 | self.eventHandlers.splice(index, 1);
|
1290 | }
|
1291 | });
|
1292 | return self;
|
1293 | },
|
1294 |
|
1295 | /**
|
1296 | * trigger gesture event
|
1297 | * @method trigger
|
1298 | * @signature trigger(gesture, eventData)
|
1299 | * @param {String} gesture
|
1300 | * @param {Object} [eventData]
|
1301 | */
|
1302 | trigger: function triggerEvent(gesture, eventData) {
|
1303 | // optional
|
1304 | if (!eventData) {
|
1305 | eventData = {};
|
1306 | }
|
1307 |
|
1308 | // create DOM event
|
1309 | var event = GestureDetector.DOCUMENT.createEvent('Event');
|
1310 | event.initEvent(gesture, true, true);
|
1311 | event.gesture = eventData;
|
1312 |
|
1313 | // trigger on the target if it is in the instance element,
|
1314 | // this is for event delegation tricks
|
1315 | var element = this.element;
|
1316 | if (Utils.hasParent(eventData.target, element)) {
|
1317 | element = eventData.target;
|
1318 | }
|
1319 |
|
1320 | element.dispatchEvent(event);
|
1321 | return this;
|
1322 | },
|
1323 |
|
1324 | /**
|
1325 | * @method enable
|
1326 | * @signature enable(state)
|
1327 | * @description
|
1328 | * [en]Enable or disable gesture detection.[/en]
|
1329 | * [ja]ジェスチャ検知を有効化/無効化します。[/ja]
|
1330 | * @param {Boolean} state
|
1331 | * [en]Specify if it should be enabled or not.[/en]
|
1332 | * [ja]有効にするかどうかを指定します。[/ja]
|
1333 | */
|
1334 | enable: function enable(state) {
|
1335 | this.enabled = state;
|
1336 | return this;
|
1337 | },
|
1338 |
|
1339 | /**
|
1340 | * @method dispose
|
1341 | * @signature dispose()
|
1342 | * @description
|
1343 | * [en]Remove and destroy all event handlers for this instance.[/en]
|
1344 | * [ja]このインスタンスでのジェスチャの検知や、イベントハンドラを全て解除して廃棄します。[/ja]
|
1345 | */
|
1346 | dispose: function dispose() {
|
1347 | var i, eh;
|
1348 |
|
1349 | // undo all changes made by stop_browser_behavior
|
1350 | Utils.toggleBehavior(this.element, this.options.behavior, false);
|
1351 |
|
1352 | // unbind all custom event handlers
|
1353 | for (i = -1; (eh = this.eventHandlers[++i]);) { // eslint-disable-line no-cond-assign
|
1354 | Utils.off(this.element, eh.gesture, eh.handler);
|
1355 | }
|
1356 |
|
1357 | this.eventHandlers = [];
|
1358 |
|
1359 | // unbind the start event listener
|
1360 | Event.off(this.element, EVENT_TYPES[EVENT_START], this.eventStartHandler);
|
1361 |
|
1362 | return null;
|
1363 | }
|
1364 | };
|
1365 |
|
1366 |
|
1367 | /**
|
1368 | * @module gestures
|
1369 | */
|
1370 | /**
|
1371 | * Move with x fingers (default 1) around on the page.
|
1372 | * Preventing the default browser behavior is a good way to improve feel and working.
|
1373 | * ````
|
1374 | * GestureDetectortime.on("drag", function(ev) {
|
1375 | * console.log(ev);
|
1376 | * ev.gesture.preventDefault();
|
1377 | * });
|
1378 | * ````
|
1379 | *
|
1380 | * @class Drag
|
1381 | * @static
|
1382 | */
|
1383 | /**
|
1384 | * @event drag
|
1385 | * @param {Object} ev
|
1386 | */
|
1387 | /**
|
1388 | * @event dragstart
|
1389 | * @param {Object} ev
|
1390 | */
|
1391 | /**
|
1392 | * @event dragend
|
1393 | * @param {Object} ev
|
1394 | */
|
1395 | /**
|
1396 | * @event drapleft
|
1397 | * @param {Object} ev
|
1398 | */
|
1399 | /**
|
1400 | * @event dragright
|
1401 | * @param {Object} ev
|
1402 | */
|
1403 | /**
|
1404 | * @event dragup
|
1405 | * @param {Object} ev
|
1406 | */
|
1407 | /**
|
1408 | * @event dragdown
|
1409 | * @param {Object} ev
|
1410 | */
|
1411 |
|
1412 | /**
|
1413 | * @param {String} name
|
1414 | */
|
1415 | (function(name) {
|
1416 | var triggered = false;
|
1417 |
|
1418 | function dragGesture(ev, inst) {
|
1419 | var cur = Detection.current;
|
1420 |
|
1421 | // max touches
|
1422 | if (inst.options.dragMaxTouches > 0 &&
|
1423 | ev.touches.length > inst.options.dragMaxTouches) {
|
1424 | return;
|
1425 | }
|
1426 |
|
1427 | switch (ev.eventType) {
|
1428 | case EVENT_START:
|
1429 | triggered = false;
|
1430 | break;
|
1431 |
|
1432 | case EVENT_MOVE:
|
1433 | // when the distance we moved is too small we skip this gesture
|
1434 | // or we can be already in dragging
|
1435 | if (ev.distance < inst.options.dragMinDistance &&
|
1436 | cur.name != name) {
|
1437 | return;
|
1438 | }
|
1439 |
|
1440 | var startCenter = cur.startEvent.center;
|
1441 |
|
1442 | // we are dragging!
|
1443 | if (cur.name != name) {
|
1444 | cur.name = name;
|
1445 | if (inst.options.dragDistanceCorrection && ev.distance > 0) {
|
1446 | // When a drag is triggered, set the event center to dragMinDistance pixels from the original event center.
|
1447 | // Without this correction, the dragged distance would jumpstart at dragMinDistance pixels instead of at 0.
|
1448 | // It might be useful to save the original start point somewhere
|
1449 | var factor = Math.abs(inst.options.dragMinDistance / ev.distance);
|
1450 | startCenter.pageX += ev.deltaX * factor;
|
1451 | startCenter.pageY += ev.deltaY * factor;
|
1452 | startCenter.clientX += ev.deltaX * factor;
|
1453 | startCenter.clientY += ev.deltaY * factor;
|
1454 |
|
1455 | // recalculate event data using new start point
|
1456 | ev = Detection.extendEventData(ev);
|
1457 | }
|
1458 | }
|
1459 |
|
1460 | // lock drag to axis?
|
1461 | if (cur.lastEvent.dragLockToAxis ||
|
1462 | ( inst.options.dragLockToAxis &&
|
1463 | inst.options.dragLockMinDistance <= ev.distance
|
1464 | )) {
|
1465 | ev.dragLockToAxis = true;
|
1466 | }
|
1467 |
|
1468 | // keep direction on the axis that the drag gesture started on
|
1469 | var lastDirection = cur.lastEvent.direction;
|
1470 | if (ev.dragLockToAxis && lastDirection !== ev.direction) {
|
1471 | if (Utils.isVertical(lastDirection)) {
|
1472 | ev.direction = (ev.deltaY < 0) ? DIRECTION_UP : DIRECTION_DOWN;
|
1473 | } else {
|
1474 | ev.direction = (ev.deltaX < 0) ? DIRECTION_LEFT : DIRECTION_RIGHT;
|
1475 | }
|
1476 | }
|
1477 |
|
1478 | // first time, trigger dragstart event
|
1479 | if (!triggered) {
|
1480 | inst.trigger(name + 'start', ev);
|
1481 | triggered = true;
|
1482 | }
|
1483 |
|
1484 | // trigger events
|
1485 | inst.trigger(name, ev);
|
1486 | inst.trigger(name + ev.direction, ev);
|
1487 |
|
1488 | var isVertical = Utils.isVertical(ev.direction);
|
1489 |
|
1490 | // block the browser events
|
1491 | if ((inst.options.dragBlockVertical && isVertical) ||
|
1492 | (inst.options.dragBlockHorizontal && !isVertical)) {
|
1493 | ev.preventDefault();
|
1494 | }
|
1495 | break;
|
1496 |
|
1497 | case EVENT_RELEASE:
|
1498 | if (triggered && ev.changedLength <= inst.options.dragMaxTouches) {
|
1499 | inst.trigger(name + 'end', ev);
|
1500 | triggered = false;
|
1501 | }
|
1502 | break;
|
1503 |
|
1504 | case EVENT_END:
|
1505 | triggered = false;
|
1506 | break;
|
1507 | }
|
1508 | }
|
1509 |
|
1510 | GestureDetector.gestures.Drag = {
|
1511 | name: name,
|
1512 | index: 50,
|
1513 | handler: dragGesture,
|
1514 | defaults: {
|
1515 | /**
|
1516 | * minimal movement that have to be made before the drag event gets triggered
|
1517 | * @property dragMinDistance
|
1518 | * @type {Number}
|
1519 | * @default 10
|
1520 | */
|
1521 | dragMinDistance: 10,
|
1522 |
|
1523 | /**
|
1524 | * Set dragDistanceCorrection to true to make the starting point of the drag
|
1525 | * be calculated from where the drag was triggered, not from where the touch started.
|
1526 | * Useful to avoid a jerk-starting drag, which can make fine-adjustments
|
1527 | * through dragging difficult, and be visually unappealing.
|
1528 | * @property dragDistanceCorrection
|
1529 | * @type {Boolean}
|
1530 | * @default true
|
1531 | */
|
1532 | dragDistanceCorrection: true,
|
1533 |
|
1534 | /**
|
1535 | * set 0 for unlimited, but this can conflict with transform
|
1536 | * @property dragMaxTouches
|
1537 | * @type {Number}
|
1538 | * @default 1
|
1539 | */
|
1540 | dragMaxTouches: 1,
|
1541 |
|
1542 | /**
|
1543 | * prevent default browser behavior when dragging occurs
|
1544 | * be careful with it, it makes the element a blocking element
|
1545 | * when you are using the drag gesture, it is a good practice to set this true
|
1546 | * @property dragBlockHorizontal
|
1547 | * @type {Boolean}
|
1548 | * @default false
|
1549 | */
|
1550 | dragBlockHorizontal: false,
|
1551 |
|
1552 | /**
|
1553 | * same as `dragBlockHorizontal`, but for vertical movement
|
1554 | * @property dragBlockVertical
|
1555 | * @type {Boolean}
|
1556 | * @default false
|
1557 | */
|
1558 | dragBlockVertical: false,
|
1559 |
|
1560 | /**
|
1561 | * dragLockToAxis keeps the drag gesture on the axis that it started on,
|
1562 | * It disallows vertical directions if the initial direction was horizontal, and vice versa.
|
1563 | * @property dragLockToAxis
|
1564 | * @type {Boolean}
|
1565 | * @default false
|
1566 | */
|
1567 | dragLockToAxis: false,
|
1568 |
|
1569 | /**
|
1570 | * drag lock only kicks in when distance > dragLockMinDistance
|
1571 | * This way, locking occurs only when the distance has become large enough to reliably determine the direction
|
1572 | * @property dragLockMinDistance
|
1573 | * @type {Number}
|
1574 | * @default 25
|
1575 | */
|
1576 | dragLockMinDistance: 25
|
1577 | }
|
1578 | };
|
1579 | })('drag');
|
1580 |
|
1581 | /**
|
1582 | * @module gestures
|
1583 | */
|
1584 | /**
|
1585 | * trigger a simple gesture event, so you can do anything in your handler.
|
1586 | * only usable if you know what your doing...
|
1587 | *
|
1588 | * @class Gesture
|
1589 | * @static
|
1590 | */
|
1591 | /**
|
1592 | * @event gesture
|
1593 | * @param {Object} ev
|
1594 | */
|
1595 | GestureDetector.gestures.Gesture = {
|
1596 | name: 'gesture',
|
1597 | index: 1337,
|
1598 | handler: function releaseGesture(ev, inst) {
|
1599 | inst.trigger(this.name, ev);
|
1600 | }
|
1601 | };
|
1602 |
|
1603 | /**
|
1604 | * @module gestures
|
1605 | */
|
1606 | /**
|
1607 | * Touch stays at the same place for x time
|
1608 | *
|
1609 | * @class Hold
|
1610 | * @static
|
1611 | */
|
1612 | /**
|
1613 | * @event hold
|
1614 | * @param {Object} ev
|
1615 | */
|
1616 |
|
1617 | /**
|
1618 | * @param {String} name
|
1619 | */
|
1620 | (function(name) {
|
1621 | var timer;
|
1622 |
|
1623 | function holdGesture(ev, inst) {
|
1624 | var options = inst.options,
|
1625 | current = Detection.current;
|
1626 |
|
1627 | switch (ev.eventType) {
|
1628 | case EVENT_START:
|
1629 | clearTimeout(timer);
|
1630 |
|
1631 | // set the gesture so we can check in the timeout if it still is
|
1632 | current.name = name;
|
1633 |
|
1634 | // set timer and if after the timeout it still is hold,
|
1635 | // we trigger the hold event
|
1636 | timer = setTimeout(function() {
|
1637 | if (current && current.name == name) {
|
1638 | inst.trigger(name, ev);
|
1639 | }
|
1640 | }, options.holdTimeout);
|
1641 | break;
|
1642 |
|
1643 | case EVENT_MOVE:
|
1644 | if (ev.distance > options.holdThreshold) {
|
1645 | clearTimeout(timer);
|
1646 | }
|
1647 | break;
|
1648 |
|
1649 | case EVENT_RELEASE:
|
1650 | clearTimeout(timer);
|
1651 | break;
|
1652 | }
|
1653 | }
|
1654 |
|
1655 | GestureDetector.gestures.Hold = {
|
1656 | name: name,
|
1657 | index: 10,
|
1658 | defaults: {
|
1659 | /**
|
1660 | * @property holdTimeout
|
1661 | * @type {Number}
|
1662 | * @default 500
|
1663 | */
|
1664 | holdTimeout: 500,
|
1665 |
|
1666 | /**
|
1667 | * movement allowed while holding
|
1668 | * @property holdThreshold
|
1669 | * @type {Number}
|
1670 | * @default 2
|
1671 | */
|
1672 | holdThreshold: 2
|
1673 | },
|
1674 | handler: holdGesture
|
1675 | };
|
1676 | })('hold');
|
1677 |
|
1678 | /**
|
1679 | * @module gestures
|
1680 | */
|
1681 | /**
|
1682 | * when a touch is being released from the page
|
1683 | *
|
1684 | * @class Release
|
1685 | * @static
|
1686 | */
|
1687 | /**
|
1688 | * @event release
|
1689 | * @param {Object} ev
|
1690 | */
|
1691 | GestureDetector.gestures.Release = {
|
1692 | name: 'release',
|
1693 | index: Infinity,
|
1694 | handler: function releaseGesture(ev, inst) {
|
1695 | if (ev.eventType == EVENT_RELEASE) {
|
1696 | inst.trigger(this.name, ev);
|
1697 | }
|
1698 | }
|
1699 | };
|
1700 |
|
1701 | /**
|
1702 | * @module gestures
|
1703 | */
|
1704 | /**
|
1705 | * triggers swipe events when the end velocity is above the threshold
|
1706 | * for best usage, set `preventDefault` (on the drag gesture) to `true`
|
1707 | * ````
|
1708 | * GestureDetectortime.on("dragleft swipeleft", function(ev) {
|
1709 | * console.log(ev);
|
1710 | * ev.gesture.preventDefault();
|
1711 | * });
|
1712 | * ````
|
1713 | *
|
1714 | * @class Swipe
|
1715 | * @static
|
1716 | */
|
1717 | /**
|
1718 | * @event swipe
|
1719 | * @param {Object} ev
|
1720 | */
|
1721 | /**
|
1722 | * @event swipeleft
|
1723 | * @param {Object} ev
|
1724 | */
|
1725 | /**
|
1726 | * @event swiperight
|
1727 | * @param {Object} ev
|
1728 | */
|
1729 | /**
|
1730 | * @event swipeup
|
1731 | * @param {Object} ev
|
1732 | */
|
1733 | /**
|
1734 | * @event swipedown
|
1735 | * @param {Object} ev
|
1736 | */
|
1737 | GestureDetector.gestures.Swipe = {
|
1738 | name: 'swipe',
|
1739 | index: 40,
|
1740 | defaults: {
|
1741 | /**
|
1742 | * @property swipeMinTouches
|
1743 | * @type {Number}
|
1744 | * @default 1
|
1745 | */
|
1746 | swipeMinTouches: 1,
|
1747 |
|
1748 | /**
|
1749 | * @property swipeMaxTouches
|
1750 | * @type {Number}
|
1751 | * @default 1
|
1752 | */
|
1753 | swipeMaxTouches: 1,
|
1754 |
|
1755 | /**
|
1756 | * horizontal swipe velocity
|
1757 | * @property swipeVelocityX
|
1758 | * @type {Number}
|
1759 | * @default 0.6
|
1760 | */
|
1761 | swipeVelocityX: 0.6,
|
1762 |
|
1763 | /**
|
1764 | * vertical swipe velocity
|
1765 | * @property swipeVelocityY
|
1766 | * @type {Number}
|
1767 | * @default 0.6
|
1768 | */
|
1769 | swipeVelocityY: 0.6
|
1770 | },
|
1771 |
|
1772 | handler: function swipeGesture(ev, inst) {
|
1773 | if (ev.eventType == EVENT_RELEASE) {
|
1774 | var touches = ev.touches.length,
|
1775 | options = inst.options;
|
1776 |
|
1777 | // max touches
|
1778 | if (touches < options.swipeMinTouches ||
|
1779 | touches > options.swipeMaxTouches) {
|
1780 | return;
|
1781 | }
|
1782 |
|
1783 | // when the distance we moved is too small we skip this gesture
|
1784 | // or we can be already in dragging
|
1785 | if (ev.velocityX > options.swipeVelocityX ||
|
1786 | ev.velocityY > options.swipeVelocityY) {
|
1787 | // trigger swipe events
|
1788 | inst.trigger(this.name, ev);
|
1789 | inst.trigger(this.name + ev.direction, ev);
|
1790 | }
|
1791 | }
|
1792 | }
|
1793 | };
|
1794 |
|
1795 | /**
|
1796 | * @module gestures
|
1797 | */
|
1798 | /**
|
1799 | * Single tap and a double tap on a place
|
1800 | *
|
1801 | * @class Tap
|
1802 | * @static
|
1803 | */
|
1804 | /**
|
1805 | * @event tap
|
1806 | * @param {Object} ev
|
1807 | */
|
1808 | /**
|
1809 | * @event doubletap
|
1810 | * @param {Object} ev
|
1811 | */
|
1812 |
|
1813 | /**
|
1814 | * @param {String} name
|
1815 | */
|
1816 | (function(name) {
|
1817 | var hasMoved = false;
|
1818 |
|
1819 | function tapGesture(ev, inst) {
|
1820 | var options = inst.options,
|
1821 | current = Detection.current,
|
1822 | prev = Detection.previous,
|
1823 | sincePrev,
|
1824 | didDoubleTap;
|
1825 |
|
1826 | switch (ev.eventType) {
|
1827 | case EVENT_START:
|
1828 | hasMoved = false;
|
1829 | break;
|
1830 |
|
1831 | case EVENT_MOVE:
|
1832 | hasMoved = hasMoved || (ev.distance > options.tapMaxDistance);
|
1833 | break;
|
1834 |
|
1835 | case EVENT_END:
|
1836 | if (!Utils.inStr(ev.srcEvent.type, 'cancel') && ev.deltaTime < options.tapMaxTime && !hasMoved) {
|
1837 | // previous gesture, for the double tap since these are two different gesture detections
|
1838 | sincePrev = prev && prev.lastEvent && ev.timeStamp - prev.lastEvent.timeStamp;
|
1839 | didDoubleTap = false;
|
1840 |
|
1841 | // check if double tap
|
1842 | if (prev && prev.name == name &&
|
1843 | (sincePrev && sincePrev < options.doubleTapInterval) &&
|
1844 | ev.distance < options.doubleTapDistance) {
|
1845 | inst.trigger('doubletap', ev);
|
1846 | didDoubleTap = true;
|
1847 | }
|
1848 |
|
1849 | // do a single tap
|
1850 | if (!didDoubleTap || options.tapAlways) {
|
1851 | current.name = name;
|
1852 | inst.trigger(current.name, ev);
|
1853 | }
|
1854 | }
|
1855 | break;
|
1856 | }
|
1857 | }
|
1858 |
|
1859 | GestureDetector.gestures.Tap = {
|
1860 | name: name,
|
1861 | index: 100,
|
1862 | handler: tapGesture,
|
1863 | defaults: {
|
1864 | /**
|
1865 | * max time of a tap, this is for the slow tappers
|
1866 | * @property tapMaxTime
|
1867 | * @type {Number}
|
1868 | * @default 250
|
1869 | */
|
1870 | tapMaxTime: 250,
|
1871 |
|
1872 | /**
|
1873 | * max distance of movement of a tap, this is for the slow tappers
|
1874 | * @property tapMaxDistance
|
1875 | * @type {Number}
|
1876 | * @default 10
|
1877 | */
|
1878 | tapMaxDistance: 10,
|
1879 |
|
1880 | /**
|
1881 | * always trigger the `tap` event, even while double-tapping
|
1882 | * @property tapAlways
|
1883 | * @type {Boolean}
|
1884 | * @default true
|
1885 | */
|
1886 | tapAlways: true,
|
1887 |
|
1888 | /**
|
1889 | * max distance between two taps
|
1890 | * @property doubleTapDistance
|
1891 | * @type {Number}
|
1892 | * @default 20
|
1893 | */
|
1894 | doubleTapDistance: 20,
|
1895 |
|
1896 | /**
|
1897 | * max time between two taps
|
1898 | * @property doubleTapInterval
|
1899 | * @type {Number}
|
1900 | * @default 300
|
1901 | */
|
1902 | doubleTapInterval: 300
|
1903 | }
|
1904 | };
|
1905 | })('tap');
|
1906 |
|
1907 | /**
|
1908 | * @module gestures
|
1909 | */
|
1910 | /**
|
1911 | * when a touch is being touched at the page
|
1912 | *
|
1913 | * @class Touch
|
1914 | * @static
|
1915 | */
|
1916 | /**
|
1917 | * @event touch
|
1918 | * @param {Object} ev
|
1919 | */
|
1920 | GestureDetector.gestures.Touch = {
|
1921 | name: 'touch',
|
1922 | index: -Infinity,
|
1923 | defaults: {
|
1924 | /**
|
1925 | * call preventDefault at touchstart, and makes the element blocking by disabling the scrolling of the page,
|
1926 | * but it improves gestures like transforming and dragging.
|
1927 | * be careful with using this, it can be very annoying for users to be stuck on the page
|
1928 | * @property preventDefault
|
1929 | * @type {Boolean}
|
1930 | * @default false
|
1931 | */
|
1932 | preventDefault: false,
|
1933 |
|
1934 | /**
|
1935 | * disable mouse events, so only touch (or pen!) input triggers events
|
1936 | * @property preventMouse
|
1937 | * @type {Boolean}
|
1938 | * @default false
|
1939 | */
|
1940 | preventMouse: false
|
1941 | },
|
1942 | handler: function touchGesture(ev, inst) {
|
1943 | if (inst.options.preventMouse && ev.pointerType == POINTER_MOUSE) {
|
1944 | ev.stopDetect();
|
1945 | return;
|
1946 | }
|
1947 |
|
1948 | if (inst.options.preventDefault) {
|
1949 | ev.preventDefault();
|
1950 | }
|
1951 |
|
1952 | if (ev.eventType == EVENT_TOUCH) {
|
1953 | inst.trigger('touch', ev);
|
1954 | }
|
1955 | }
|
1956 | };
|
1957 |
|
1958 | /**
|
1959 | * @module gestures
|
1960 | */
|
1961 | /**
|
1962 | * User want to scale or rotate with 2 fingers
|
1963 | * Preventing the default browser behavior is a good way to improve feel and working. This can be done with the
|
1964 | * `preventDefault` option.
|
1965 | *
|
1966 | * @class Transform
|
1967 | * @static
|
1968 | */
|
1969 | /**
|
1970 | * @event transform
|
1971 | * @param {Object} ev
|
1972 | */
|
1973 | /**
|
1974 | * @event transformstart
|
1975 | * @param {Object} ev
|
1976 | */
|
1977 | /**
|
1978 | * @event transformend
|
1979 | * @param {Object} ev
|
1980 | */
|
1981 | /**
|
1982 | * @event pinchin
|
1983 | * @param {Object} ev
|
1984 | */
|
1985 | /**
|
1986 | * @event pinchout
|
1987 | * @param {Object} ev
|
1988 | */
|
1989 | /**
|
1990 | * @event rotate
|
1991 | * @param {Object} ev
|
1992 | */
|
1993 |
|
1994 | /**
|
1995 | * @param {String} name
|
1996 | */
|
1997 | (function(name) {
|
1998 | var triggered = false;
|
1999 |
|
2000 | function transformGesture(ev, inst) {
|
2001 | switch (ev.eventType) {
|
2002 | case EVENT_START:
|
2003 | triggered = false;
|
2004 | break;
|
2005 |
|
2006 | case EVENT_MOVE:
|
2007 | // at least multitouch
|
2008 | if (ev.touches.length < 2) {
|
2009 | return;
|
2010 | }
|
2011 |
|
2012 | var scaleThreshold = Math.abs(1 - ev.scale);
|
2013 | var rotationThreshold = Math.abs(ev.rotation);
|
2014 |
|
2015 | // when the distance we moved is too small we skip this gesture
|
2016 | // or we can be already in dragging
|
2017 | if (scaleThreshold < inst.options.transformMinScale &&
|
2018 | rotationThreshold < inst.options.transformMinRotation) {
|
2019 | return;
|
2020 | }
|
2021 |
|
2022 | // we are transforming!
|
2023 | Detection.current.name = name;
|
2024 |
|
2025 | // first time, trigger dragstart event
|
2026 | if (!triggered) {
|
2027 | inst.trigger(name + 'start', ev);
|
2028 | triggered = true;
|
2029 | }
|
2030 |
|
2031 | inst.trigger(name, ev); // basic transform event
|
2032 |
|
2033 | // trigger rotate event
|
2034 | if (rotationThreshold > inst.options.transformMinRotation) {
|
2035 | inst.trigger('rotate', ev);
|
2036 | }
|
2037 |
|
2038 | // trigger pinch event
|
2039 | if (scaleThreshold > inst.options.transformMinScale) {
|
2040 | inst.trigger('pinch', ev);
|
2041 | inst.trigger('pinch' + (ev.scale < 1 ? 'in' : 'out'), ev);
|
2042 | }
|
2043 | break;
|
2044 |
|
2045 | case EVENT_RELEASE:
|
2046 | if (triggered && ev.changedLength < 2) {
|
2047 | inst.trigger(name + 'end', ev);
|
2048 | triggered = false;
|
2049 | }
|
2050 | break;
|
2051 | }
|
2052 | }
|
2053 |
|
2054 | GestureDetector.gestures.Transform = {
|
2055 | name: name,
|
2056 | index: 45,
|
2057 | defaults: {
|
2058 | /**
|
2059 | * minimal scale factor, no scale is 1, zoomin is to 0 and zoomout until higher then 1
|
2060 | * @property transformMinScale
|
2061 | * @type {Number}
|
2062 | * @default 0.01
|
2063 | */
|
2064 | transformMinScale: 0.01,
|
2065 |
|
2066 | /**
|
2067 | * rotation in degrees
|
2068 | * @property transformMinRotation
|
2069 | * @type {Number}
|
2070 | * @default 1
|
2071 | */
|
2072 | transformMinRotation: 1
|
2073 | },
|
2074 |
|
2075 | handler: transformGesture
|
2076 | };
|
2077 | })('transform');
|
2078 |
|
2079 | export default GestureDetector;
|