-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdispatcher.js
192 lines (157 loc) · 3.34 KB
/
dispatcher.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
/**
* Module dependencies.
*/
var Touch = require('./touch');
/**
* Expose `Dispatcher`.
*/
module.exports = Dispatcher;
/**
* Initialize a new `Dispatcher`.
*
* @api private
*/
function Dispatcher(){}
/**
* Handle mousedown.
*
* @api private
*/
Dispatcher.prototype.mousedown = function(e){
this.down = e;
this.emit('touchstart', e);
};
/**
* Handle mousemove.
*
* @api private
*/
Dispatcher.prototype.mousemove = function(e){
if (!this.down) return;
this.emit('touchmove', e);
};
/**
* Handle mouseup.
*
* @api private
*/
Dispatcher.prototype.mouseup = function(e){
this.down = false;
this.emit('touchend', e);
};
/**
* Add a `Touch`.
*
* @param {Event} e
* @return {Touch}
* @api private
*/
Dispatcher.prototype.addTouch = function(e){
var touch = new Touch(e, this.target);
this.touches.push(touch);
touch.finger.show();
return touch;
};
/**
* Emit event `type`.
*
* Assigns `.two` to a boolean in order to
* represent two fingers by holding down the
* alt key.
*
* @param {String} type
* @param {Event} e
* @api private
*/
Dispatcher.prototype.emit = function(type, e){
this.target = this.target || e.target;
this.two = e.altKey;
var event = createEvent(type, e);
this[type](event, e);
};
/**
* Handle touchstart.
*
* @api private
*/
Dispatcher.prototype.touchstart = function(event, e){
this.touches = [];
// one finger
this.addTouch(e);
// two fingers
if (this.two) {
var touch = this.addTouch(e);
touch.pageX = e.pageX - 50;
touch.pageY = e.pageY + 50;
}
// move
var one = this.touches[0];
var two = this.touches[1];
one.finger.moveTo(e.pageX, e.pageY);
if (two) two.finger.moveTo(touch.pageX, touch.pageY);
// dispatch
event.targetTouches = event.touches = this.touches;
this.target.dispatchEvent(event);
};
/**
* Handle touchmove.
*
* @api private
*/
Dispatcher.prototype.touchmove = function(event, e){
var one = this.touches[0];
var two = this.touches[1];
// move
one.update(e);
one.finger.moveTo(e.pageX, e.pageY);
// if (this.two) {
// two.update(e);
// two.pageX = window.innerWidth - e.pageX;
// two.pageY = window.innerHeight - e.pageY;
// two.finger.moveTo(two.pageX, two.pageY);
// }
// dispatch
event.changedTouches = event.targetTouches = event.touches = this.touches;
this.target.dispatchEvent(event);
};
/**
* Handle touchend.
*
* @api private
*/
Dispatcher.prototype.touchend = function(event, e){
var target = this.target;
hideFingers(this.touches);
event.changedTouches = event.targetTouches = this.touches;
event.touches = [];
this.target = this.touches = null;
target.dispatchEvent(event);
};
/**
* Create an `MouseEvent` to masquerade as a `TouchEvent`
* on non-touch devices, given the `type` and original event `e`.
*
* @param {String} type
* @param {MouseEvent} e
* @return {MouseEvent}
* @api private
*/
function createEvent(type, e) {
var event = document.createEvent('MouseEvent');
event.initMouseEvent(type, true, true, window, e.detail,
e.screenX, e.screenY, e.clientX, e.clientY,
e.ctrlKey, e.shiftKey, e.altKey, e.metaKey,
e.button, e.relatedTarget);
return event;
}
/**
* Hide all fingers in `touches`.
*
* @param {Array} touches
* @api private
*/
function hideFingers(touches){
touches.forEach(function(touch){
touch.finger.hide();
});
};