-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteract.js
868 lines (717 loc) · 30.1 KB
/
interact.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
/*
* interact v2.0.02.0.0
* build => 2012-10-11T23:50:41.520Z
*
*
*
*/
// umdjs returnExports pattern: https://github.com/umdjs/umd/blob/master/returnExports.js
(function (root, factory) {
if (typeof exports === 'object') {
module.exports = factory(require('eve'));
} else if (typeof define === 'function' && define.amd) {
define(['eve'], factory);
} else {
root['interact'] = factory(root['eve']);
}
}(this, function (eve) {
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf shim
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
"use strict";
if (this == null) {
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
if (len === 0) {
return -1;
}
var n = 0;
if (arguments.length > 0) {
n = Number(arguments[1]);
if (n != n) { // shortcut for verifying if it's NaN
n = 0;
} else if (n != 0 && n != Infinity && n != -Infinity) {
n = (n > 0 || -1) * Math.floor(Math.abs(n));
}
}
if (n >= len) {
return -1;
}
var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
for (; k < len; k++) {
if (k in t && t[k] === searchElement) {
return k;
}
}
return -1;
}
}
// initialise variables
var interactors = [],
reLastChunk = /.*\.(.*)$/,
supportedEvents = ['down', 'move', 'up', 'zoom'],
lastXY = {};
/* internal functions */
function genBinder(target) {
return function(evtName, callback) {
target.addEventListener(evtName, callback, false);
};
} // bindDoc
function genUnbinder(target) {
return function(evtName, callback, customTarget) {
target.removeEventListener(evtName, callback, false);
};
} // unbindDoc
function genIEBinder(target) {
return function(evtName, callback) {
target.attachEvent('on' + evtName, callback);
};
} // genIEBinder
function genIEUnbinder(target) {
return function(evtName, callback) {
target.detachEvent('on' + evtName, callback);
};
} // genIEUnbinder
function getHandlers(types, capabilities) {
var handlers = [];
// iterate through the interactors in the registry
for (var ii = interactors.length; ii--; ) {
var interactor = interactors[ii],
selected = (! types) || (types.indexOf(interactor.type) >= 0),
checksPass = true;
// TODO: perform capabilities check
for (var checkKey in interactor.checks) {
var check = interactor.checks[checkKey];
// _log('checking ' + checkKey + ' capability. require: ' + check + ', capability = ' + capabilities[checkKey]);
checksPass = checksPass && (check === capabilities[checkKey]);
} // for
if (selected && checksPass) {
handlers[handlers.length] = interactor.handler;
} // if
} // for
return handlers;
} // getHandlers
function point(x, y) {
return {
x: x ? x : 0,
y: y ? y : 0,
count: 1
};
} // point
/* exports */
/*\
* Interact.register
[ function ]
**
* Register an interaction handler
**
> Arguments
**
- typeName (string) the name of the interaction handler being registered
- opts (object) an object containing options for the new interactor
\*/
function register(typeName, opts) {
// initialise options
opts = opts || {};
opts.checks = opts.checks || {};
opts.type = opts.type || typeName;
interactors.push(opts);
} // register
function Interactor(target, opts, caps) {
var handlers, events = {}, ii;
// if the target is a string, then look for the element
if (typeof target == 'string') {
target = document.getElementById(target);
} // if
// initialise options
opts = opts || {};
opts.isIE = typeof window.attachEvent != 'undefined';
// initialise the namespace
this.ns = opts.ns = opts.ns || 'interact';
// initialise the events that we will monitor
opts.events = opts.events || supportedEvents;
// convert the events array into an object
for (ii = 0; ii < supportedEvents.length; ii++) {
events[supportedEvents[ii]] = opts.events.indexOf(supportedEvents[ii]) >= 0;
}
// copy the events object into the opts
opts.events = events;
// init caps
caps = caps || {};
caps.touch = caps.touch || 'ontouchstart' in window;
// initialise the binder and unbinder
opts.binder = (opts.isIE ? genIEBinder : genBinder)(opts.bindTarget || document);
opts.unbinder = (opts.isIE ? genIEBinder : genUnbinder)(opts.bindTarget || document);
// initialise the handlers
handlers = getHandlers(opts.types, caps);
for (ii = 0; ii < handlers.length; ii++) {
handlers[ii].call(target, target, opts);
} // for
}
Interactor.prototype.on = function(name, handler) {
eve.on(this.ns + '.' + name, handler);
return this;
};
function interact(target, opts, caps) {
return new Interactor(target, opts, caps);
} // watch
/* common pointer (mouse, touch, etc) functions */
function getOffset(obj) {
var calcLeft = 0,
calcTop = 0;
if (obj.offsetParent) {
do {
calcLeft += obj.offsetLeft;
calcTop += obj.offsetTop;
obj = obj.offsetParent;
} while (obj);
} // if
return {
left: calcLeft,
top: calcTop
};
} // getOffset
function matchTarget(evt, targetElement) {
var targ = evt.target || evt.srcElement;
// while we have a target, and that target is not the target element continue
// additionally, if we hit an element that has an interactor bound to it (will have the class interactor)
// then also stop
while (targ && (targ !== targetElement)) {
targ = targ.parentNode;
} // while
return targ && (targ === targetElement);
} // matchTarget
function pointerOffset(absPoint, offset) {
return {
x: absPoint.x - (offset ? offset.left : 0),
y: absPoint.y - (offset ? offset.top : 0)
};
} // triggerPositionEvent
function preventDefault(evt, immediate) {
if (evt.preventDefault) {
evt.preventDefault();
evt.stopPropagation();
}
else if (typeof evt.cancelBubble != 'undefined') {
evt.cancelBubble = true;
} // if..else
if (immediate && evt.stopImmediatePropagation) {
evt.stopImmediatePropagation();
} // if
} // preventDefault
var MouseHandler = function(targetElement, opts) {
// initialise opts
opts = opts || {};
// initialise constants
var WHEEL_DELTA_STEP = 120,
WHEEL_DELTA_LEVEL = WHEEL_DELTA_STEP * 8;
// initialise variables
var aggressiveCapture = opts.aggressiveCapture,
ignoreButton = opts.isIE,
isFlashCanvas = typeof FlashCanvas != 'undefined',
buttonDown = false,
start,
currentX,
currentY,
evtPointer = opts.ns,
evtTargetId = targetElement && targetElement.id ? '.' + targetElement.id : '',
evtPointerDown = evtPointer + '.down' + evtTargetId,
evtPointerMove = evtPointer + '.move' + evtTargetId,
evtPointerUp = evtPointer + '.up' + evtTargetId,
evtZoomWheel = opts.ns + '.zoom.wheel' + evtTargetId;
/* internal functions */
function getPagePos(evt) {
if (evt.pageX && evt.pageY) {
return point(evt.pageX, evt.pageY);
}
else {
var doc = document.documentElement,
body = document.body;
// code from jquery event handling:
// https://github.com/jquery/jquery/blob/1.5.1/src/event.js#L493
return point(
evt.clientX +
(doc && doc.scrollLeft || body && body.scrollLeft || 0) -
(doc && doc.clientLeft || body && body.clientLeft || 0),
evt.clientY +
(doc && doc.scrollTop || body && body.scrollTop || 0) -
(doc && doc.clientTop || body && body.clientTop || 0)
);
} // if
} // getPagePos
function handleDoubleClick(evt) {
if (matchTarget(evt, targetElement)) {
var clickXY = getPagePos(evt);
eve(
evtPointer + '.doubletap' + evtTargetId,
targetElement,
evt,
clickXY,
pointerOffset(clickXY, getOffset(targetElement))
);
} // if
} // handleDoubleClick
function handleMouseDown(evt) {
if (matchTarget(evt, targetElement)) {
buttonDown = isLeftButton(evt);
if (aggressiveCapture) {
preventDefault(evt, true);
}
if (buttonDown) {
var pagePos = getPagePos(evt);
// update the cursor and prevent the default
if (typeof targetElement.style != 'undefined') {
targetElement.style.cursor = 'move';
}
start = point(pagePos.x, pagePos.y);
// trigger the pointer down event
eve(
evtPointerDown,
targetElement,
evt,
start,
pointerOffset(start, getOffset(targetElement))
);
}
} // if
} // mouseDown
function handleMouseMove(evt) {
var pagePos = getPagePos(evt);
// capture the current x and current y
currentX = pagePos.x;
currentY = pagePos.y;
if (matchTarget(evt, targetElement)) {
triggerCurrent(evt, evtPointer + '.'+ (buttonDown ? 'move' : 'hover'));
} // if
} // mouseMove
function handleMouseUp(evt) {
// 27/06/2012 (DJO): buttonDown state no longer checked to determine whether or not the event
// should be fired
if (isLeftButton(evt)) {
buttonDown = false;
// if the button was released on this element, then trigger the event
if (matchTarget(evt, targetElement)) {
if (typeof targetElement.style != 'undefined') {
targetElement.style.cursor = 'default';
}
triggerCurrent(evt, evtPointer + '.up');
} // if
} // if
} // mouseUp
function handleWheel(evt) {
if (matchTarget(evt, targetElement)) {
var deltaY;
// handle IE behaviour
evt = evt || window.event;
if (evt.detail) {
if (typeof evt.axis == 'undefined' || evt.axis === 2) {
deltaY = -evt.detail * WHEEL_DELTA_STEP;
} // if
}
else {
deltaY = evt.wheelDeltaY ? evt.wheelDeltaY : evt.wheelDelta;
if (window.opera) {
deltaY = -deltaY;
} // if
} // if..else
if (deltaY) {
var current = point(currentX, currentY);
eve(
evtZoomWheel,
targetElement,
evt,
current,
pointerOffset(current, getOffset(targetElement)),
deltaY / WHEEL_DELTA_LEVEL
);
if (aggressiveCapture) {
preventDefault(evt, true);
}
evt.returnValue = false;
} // if
} // if
} // handleWheel
function isLeftButton(evt) {
evt = evt || window.event;
var button = evt.which || evt.button;
return button == 1;
} // leftPressed
function preventDrag(evt) {
return !matchTarget(evt, targetElement);
} // preventDrag
function triggerCurrent(evt, eventName, overrideX, overrideY, updateLast) {
var evtX = typeof overrideX != 'undefined' ? overrideX : currentX,
evtY = typeof overrideY != 'undefined' ? overrideY : currentY,
current = point(evtX, evtY);
if (aggressiveCapture) {
preventDefault(evt, true);
}
// trigger the event
eve(
eventName + evtTargetId,
targetElement,
evt,
current,
pointerOffset(current, getOffset(targetElement))
);
} // triggerCurrent
/* exports */
function unbind() {
// wire up the event handlers
opts.unbinder('mousedown', handleMouseDown);
opts.unbinder('mousemove', handleMouseMove);
opts.unbinder('mouseup', handleMouseUp);
// bind mouse wheel events
opts.unbinder("mousewheel", handleWheel);
opts.unbinder("DOMMouseScroll", handleWheel);
} // unbind
// wire up the event handlers
if (opts.events.down) {
opts.binder('mousedown', handleMouseDown);
opts.binder('dblclick', handleDoubleClick);
}
if (opts.events.move) {
opts.binder('mousemove', handleMouseMove);
}
if (opts.events.up) {
opts.binder('mouseup', handleMouseUp);
}
// handle drag start and select start events to ensure moves work on ie
opts.binder('selectstart', preventDrag);
opts.binder('dragstart', preventDrag);
// bind mouse wheel events (if we are handling zoom events)
if (opts.events.zoom) {
opts.binder('mousewheel', handleWheel);
opts.binder('DOMMouseScroll', handleWheel);
}
return {
unbind: unbind
};
}; // MouseHandler
// register the mouse pointer
register('pointer', {
handler: MouseHandler,
checks: {
touch: false
}
});
/**
# TouchHandler(targetElement, opts)
## Valid Options
- detailed: boolean (default = false)
- inertia: boolean (default = false)
*/
var TouchHandler = function(targetElement, opts) {
// initialise opts
opts = opts || {};
// initialise constants
var DEFAULT_INERTIA_MAX = 500,
INERTIA_TIMEOUT_MOUSE = 100,
INERTIA_TIMEOUT_TOUCH = 250,
THRESHOLD_DOUBLETAP = 300,
THRESHOLD_PINCHZOOM = 20,
MIN_MOVEDIST = 7,
EMPTY_TOUCH_DATA = {
x: 0,
y: 0
},
// define the touch modes
TOUCH_MODE_UNKNOWN = 0,
TOUCH_MODE_TAP = 1,
TOUCH_MODE_MOVE = 2,
TOUCH_MODE_PINCH = 3,
// initialise variables
aggressiveCapture = opts.aggressiveCapture,
offset,
touchMode,
touchDown = false,
touchesStart,
touchesCurrent,
startDistance,
touchesLast,
detailedEvents = opts.detailed,
scaling = 1,
evtPointer = opts.ns,
evtTargetId = targetElement && targetElement.id ? '.' + targetElement.id : '',
evtPointerDown = evtPointer + '.down' + evtTargetId,
evtPointerMultiDown = evtPointer + '.multi.down' + evtTargetId,
evtPointerMove = evtPointer + '.move' + evtTargetId,
evtPointerMultiMove = evtPointer + '.multi.move' + evtTargetId,
evtPointerUp = evtPointer + '.up' + evtTargetId,
evtPointerMultiUp = evtPointer + '.multi.up' + evtTargetId,
evtZoomPinch = evtPointer + '.zoom.pinch' + evtTargetId;
/* internal functions */
function calcChange(first, second) {
var srcVector = (first && (first.count > 0)) ? first.touches[0] : null;
if (srcVector && second && (second.count > 0)) {
return calcDiff(srcVector, second.touches[0]);
} // if
return null;
} // calcChange
// TODO: modify this function to provide distance between any of the touches
// rather then just the first two
function calcTouchDistance(touchData) {
if (touchData.count < 2) {
return 0;
} // if
var xDist = touchData.x - touchData.next.x,
yDist = touchData.y - touchData.next.y;
// return the floored distance to keep math in the realm of integers...
return ~~Math.sqrt(xDist * xDist + yDist * yDist);
} // touches
function copyTouches(src, adjustX, adjustY) {
// set to 0 if not supplied
adjustX = adjustX ? adjustX : 0;
adjustY = adjustY ? adjustY : 0;
var firstTouch = {
x: src.x - adjustX,
y: src.y - adjustY,
id: src.id,
count: src.count
},
touchData = firstTouch;
while (src.next) {
src = src.next;
touchData = touchData.next = {
x: src.x - adjustX,
y: src.y - adjustY,
id: src.id
};
} // while
return firstTouch;
} // copyTouches
function getTouchCenter(touchData) {
var x1 = touchData.x,
x2 = touchData.next.x,
y1 = touchData.y,
y2 = touchData.next.y,
minX = x1 < x2 ? x1 : x2,
minY = y1 < y2 ? y1 : y2,
width = Math.abs(x1 - x2),
height = Math.abs(y1 - y2);
return {
x: minX + (width >> 1),
y: minY + (height >> 1)
};
} // getTouchCenter
function getTouchData(evt, evtProp) {
var touches = evt[evtProp ? evtProp : 'touches'],
firstTouch, touchData;
if (touches.length === 0) {
return null;
} // if
// assign the first touch and touch data
touchData = firstTouch = {
x: touches[0].pageX,
y: touches[0].pageY,
id: touches[0].identifier,
count: touches.length
};
for (var ii = 1, touchCount = touches.length; ii < touchCount; ii++) {
touchData = touchData.next = {
x: touches[ii].pageX,
y: touches[ii].pageY,
id: touches[ii].identifier
};
} // for
return firstTouch;
} // fillTouchData
function handleTouchStart(evt) {
if (matchTarget(evt, targetElement)) {
// update the offset
offset = getOffset(targetElement);
// initialise variables
var changedTouches = getTouchData(evt, 'changedTouches'),
relTouches = copyTouches(changedTouches, offset.left, offset.top),
evtArgs = [targetElement, evt, changedTouches, relTouches];
// prevent the default action
if (aggressiveCapture) {
preventDefault(evt, true);
}
if (! touchesStart) {
// reset the touch mode to unknown
touchMode = TOUCH_MODE_TAP;
eve.apply(eve, [evtPointerDown].concat(evtArgs));
} // if
// if we are providing detailed events, then trigger the pointer down multi
if (detailedEvents) {
eve.apply(eve, [evtPointerMultiDown].concat(evtArgs));
} // if
touchesStart = getTouchData(evt);
// check the start distance
if (touchesStart.count > 1) {
startDistance = calcTouchDistance(touchesStart);
} // if
// reset the scaling
scaling = 1;
// update the last touches
touchesLast = copyTouches(touchesStart);
} // if
} // handleTouchStart
function handleTouchMove(evt) {
var cancelTap, evtArgs;
if (matchTarget(evt, targetElement)) {
// prevent the default action
if (aggressiveCapture) {
preventDefault(evt, true);
}
// fill the touch data
touchesCurrent = getTouchData(evt);
// if the touch mode is currently tap, then check the distance from the start touch
if (touchMode == TOUCH_MODE_TAP) {
cancelTap =
Math.abs(touchesStart.x - touchesCurrent.x) > MIN_MOVEDIST ||
Math.abs(touchesStart.y - touchesCurrent.y) > MIN_MOVEDIST;
// update the touch mode based on the result
touchMode = cancelTap ? TOUCH_MODE_UNKNOWN : TOUCH_MODE_TAP;
} // if
if (touchMode != TOUCH_MODE_TAP) {
touchMode = touchesCurrent.count > 1 ? TOUCH_MODE_PINCH : TOUCH_MODE_MOVE;
// TOUCH_MODE_PINCH extra checks
// if we had multiple touches, then the touch mode is probably pinch, but we
// need to check this by checking the zoom distance between the start touches
// and the current touches
if (touchMode == TOUCH_MODE_PINCH) {
// check that the first touches have two touches, if not copy the current touches
if (touchesStart.count === 1) {
touchesStart = copyTouches(touchesCurrent);
startDistance = calcTouchDistance(touchesStart);
}
else {
// calculate the current distance
var touchDistance = calcTouchDistance(touchesCurrent),
distanceDelta = Math.abs(startDistance - touchDistance);
// if the distance is not great enough then switch back to move
if (distanceDelta < THRESHOLD_PINCHZOOM) {
touchMode = TOUCH_MODE_MOVE;
}
// otherwise, raise the zoom event
else {
var current = getTouchCenter(touchesCurrent),
currentScaling = touchDistance / startDistance,
scaleChange = currentScaling - scaling;
// trigger the zoom event
eve(evtZoomPinch, targetElement, evt, current,
pointerOffset(current, offset), scaleChange);
// update the scaling
scaling = currentScaling;
} // if..else
} // if..else
} // if
// initialise the event args
evtArgs = [
targetElement,
evt,
touchesCurrent,
copyTouches(touchesCurrent, offset.left, offset.top),
point(touchesCurrent.x - touchesLast.x, touchesCurrent.y - touchesLast.y)
];
// if the touch mode is move, then trigger a pointer move on the first touch
if (touchMode == TOUCH_MODE_MOVE) {
// trigger the pointer move event
eve.apply(eve, [evtPointerMove].concat(evtArgs));
} // if
// fire a touch multi event for custom event handling
if (detailedEvents) {
eve.apply(eve, [evePointerMultiMove].concat(evtArgs));
} // if
} // if
touchesLast = copyTouches(touchesCurrent);
} // if
} // handleTouchMove
function handleTouchEnd(evt) {
if (matchTarget(evt, targetElement)) {
var changedTouches = getTouchData(evt, 'changedTouches'),
offsetTouches = copyTouches(changedTouches, offset.left, offset.top),
evtArgs = [targetElement, evt, changedTouches, offsetTouches];
// get the current touches
touchesCurrent = getTouchData(evt);
// prevent the default action
if (aggressiveCapture) {
preventDefault(evt, true);
}
// if this is the last touch to be removed do some extra checks
if (! touchesCurrent) {
eve.apply(eve, [evtPointerUp].concat(evtArgs));
touchesStart = null;
} // if
// if we are monitoring detailed events, then trigger up multi
if (detailedEvents) {
eve.apply(evt, [evtPointerMultiUp].concat(evtArgs));
} // if..else
} // if
} // handleTouchEnd
function initTouchData() {
return {
x: 0,
y: 0,
next: null
};
} // initTouchData
/* exports */
function unbind() {
opts.unbinder('touchstart', handleTouchStart);
opts.unbinder('touchmove', handleTouchMove);
opts.unbinder('touchend', handleTouchEnd);
} // unbind
// wire up the event handlers
if (opts.events.down) {
opts.binder('touchstart', handleTouchStart);
}
if (opts.events.move) {
opts.binder('touchmove', handleTouchMove);
}
if (opts.events.up) {
opts.binder('touchend', handleTouchEnd);
}
return {
unbind: unbind
};
}; // TouchHandler
// register the mouse pointer
register('pointer', {
handler: TouchHandler,
checks: {
touch: true
}
});
// add some helpful wrappers
// TODO: make work with different event namespaces
eve.on('interact.down', function(evt, absXY, relXY) {
var ctrlName = eve.nt().replace(reLastChunk, '$1');
if (ctrlName) {
lastXY[ctrlName] = {
x: relXY.x,
y: relXY.y
};
} // if
// save the down target
downTarget = this;
});
// handle pointer move events
eve.on('interact.move', function(evt, absXY, relXY) {
var ctrlName = eve.nt().replace(reLastChunk, '$1');
if (ctrlName && lastXY[ctrlName]) {
var deltaX = relXY.x - lastXY[ctrlName].x,
deltaY = relXY.y - lastXY[ctrlName].y;
// trigger the pan event
eve('interact.pan.' + ctrlName, this, evt, deltaX, deltaY, absXY, relXY);
// update the last xy
lastXY[ctrlName] = {
x: relXY.x,
y: relXY.y
};
} // if
});
eve.on('interact.up', function(evt, absXY, relXY) {
var ctrlName = eve.nt().replace(reLastChunk, '$1');
if (this === downTarget) {
eve('interact.tap' + (ctrlName ? '.' + ctrlName : ''), this, evt, absXY, relXY);
} // if
});
interact.register = register;
interact.watch = interact;
return typeof interact != 'undefined' ? interact : undefined;
}));