-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmylib-touch.js
More file actions
94 lines (73 loc) · 3.02 KB
/
mylib-touch.js
File metadata and controls
94 lines (73 loc) · 3.02 KB
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
// My Library Touch add-on
// Uses mousedown/mousemove/mouseup to provide a basic simulation for non-touch browsers
// Passes the event as the first argument and a reference to the listening element as the second
// NOTE: Ignore event.target as it is inconsistent between touch and non-touch
// Move listeners are passed the mouse/finger position as a third argument
// Test page at http://www.cinsoft.net/mylib-touch.html
// Also note that this is just a first draft and is largely untested!
// Use at your own risk.
var API;
if (typeof API != 'undefined' && API.attachListener && Function.prototype.call) {
(function() {
var attachListener = API.attachListener, detachListener = API.detachListener;
var touchEventType, tapEventType;
var attachTouchListeners = function(el, fnStart, fnMove, fnEnd, thisObject) {
var fnMoveWrapped = function(e) {
// TODO: Make sure this is the right touch (store and check identifier)
var touch = e.changedTouches[0], pos = [touch.pageY, touch.pageX];
fnMove.call(thisObject || el, e, el, pos);
};
attachListener(el, 'touchstart', fnStart, thisObject);
attachListener(el, 'touchmove', fnMoveWrapped, thisObject);
attachListener(el, 'touchend', fnEnd, thisObject);
};
var attachMouseListeners = function(el, fnStart, fnMove, fnEnd, thisObject, touched) {
var doc = API.getElementDocument(el);
var fnStartWrapped = function(e) {
touched = true;
fnStart.call(thisObject || el, e, el);
};
var fnMoveWrapped = function(e) {
if (touched) {
fnMove.call(thisObject || el, e, el, API.getMousePosition(e));
}
};
var fnEndWrapped = function(e) {
if (touched) {
touched = false;
fnEnd.call(thisObject || el, e, el);
}
};
// TODO: Use one pair of mousemove/up listeners for all
attachListener(el, 'mousedown', fnStartWrapped, thisObject);
attachListener(doc, 'mousemove', fnMoveWrapped, thisObject);
attachListener(doc, 'mouseup', fnEndWrapped, thisObject);
};
var touchDownListener = function(el, fnStart, fnMove, fnEnd, thisObject) {
var fnWrapped;
return (fnWrapped = function(e) {
var type = e.type;
detachListener(el, 'touchstart', fnWrapped);
detachListener(el, 'mousedown', fnWrapped);
fnStart.call(thisObject || el, e, el);
if (!type.indexOf('mouse')) {
touchEventType = 'mouse';
attachTouchListeners = attachMouseListeners;
} else {
touchEventType = 'touch';
}
attachTouchListeners(el, fnStart, fnMove, fnEnd, thisObject, true);
});
};
API.attachTouchListeners = function(el, fnStart, fnMove, fnEnd, thisObject) {
var fnWrapped;
if (typeof touchEventType == 'undefined') {
fnWrapped = touchDownListener(el, fnStart, fnMove, fnEnd, thisObject);
attachListener(el, 'touchstart', fnWrapped, thisObject);
attachListener(el, 'mousedown', fnWrapped, thisObject);
} else {
attachTouchListeners(el, fnStart, fnMove, fnEnd, thisObject);
}
};
})();
}