HandlerProxy.js
11 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
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
import {
addEventListener,
removeEventListener,
normalizeEvent
} from '../core/event';
import * as zrUtil from '../core/util';
import Eventful from '../mixin/Eventful';
import env from '../core/env';
var TOUCH_CLICK_DELAY = 300;
var mouseHandlerNames = [
'click', 'dblclick', 'mousewheel', 'mouseout',
'mouseup', 'mousedown', 'mousemove', 'contextmenu'
];
var touchHandlerNames = [
'touchstart', 'touchend', 'touchmove'
];
var pointerEventNames = {
pointerdown: 1, pointerup: 1, pointermove: 1, pointerout: 1
};
var pointerHandlerNames = zrUtil.map(mouseHandlerNames, function (name) {
var nm = name.replace('mouse', 'pointer');
return pointerEventNames[nm] ? nm : name;
});
function eventNameFix(name) {
return (name === 'mousewheel' && env.browser.firefox) ? 'DOMMouseScroll' : name;
}
// function onMSGestureChange(proxy, event) {
// if (event.translationX || event.translationY) {
// // mousemove is carried by MSGesture to reduce the sensitivity.
// proxy.handler.dispatchToElement(event.target, 'mousemove', event);
// }
// if (event.scale !== 1) {
// event.pinchX = event.offsetX;
// event.pinchY = event.offsetY;
// event.pinchScale = event.scale;
// proxy.handler.dispatchToElement(event.target, 'pinch', event);
// }
// }
/**
* Prevent mouse event from being dispatched after Touch Events action
* @see <https://github.com/deltakosh/handjs/blob/master/src/hand.base.js>
* 1. Mobile browsers dispatch mouse events 300ms after touchend.
* 2. Chrome for Android dispatch mousedown for long-touch about 650ms
* Result: Blocking Mouse Events for 700ms.
*/
function setTouchTimer(instance) {
instance._touching = true;
clearTimeout(instance._touchTimer);
instance._touchTimer = setTimeout(function () {
instance._touching = false;
}, 700);
}
var domHandlers = {
/**
* Mouse move handler
* @inner
* @param {Event} event
*/
mousemove: function (event) {
event = normalizeEvent(this.dom, event);
this.trigger('mousemove', event);
},
/**
* Mouse out handler
* @inner
* @param {Event} event
*/
mouseout: function (event) {
event = normalizeEvent(this.dom, event);
var element = event.toElement || event.relatedTarget;
if (element !== this.dom) {
while (element && element.nodeType !== 9) {
// 忽略包含在root中的dom引起的mouseOut
if (element === this.dom) {
return;
}
element = element.parentNode;
}
}
this.trigger('mouseout', event);
},
/**
* Touch开始响应函数
* @inner
* @param {Event} event
*/
touchstart: function (event) {
// Default mouse behaviour should not be disabled here.
// For example, page may needs to be slided.
event = normalizeEvent(this.dom, event);
// Mark touch, which is useful in distinguish touch and
// mouse event in upper applicatoin.
event.zrByTouch = true;
this._lastTouchMoment = new Date();
this.handler.processGesture(this, event, 'start');
// In touch device, trigger `mousemove`(`mouseover`) should
// be triggered, and must before `mousedown` triggered.
domHandlers.mousemove.call(this, event);
domHandlers.mousedown.call(this, event);
setTouchTimer(this);
},
/**
* Touch移动响应函数
* @inner
* @param {Event} event
*/
touchmove: function (event) {
event = normalizeEvent(this.dom, event);
// Mark touch, which is useful in distinguish touch and
// mouse event in upper applicatoin.
event.zrByTouch = true;
this.handler.processGesture(this, event, 'change');
// Mouse move should always be triggered no matter whether
// there is gestrue event, because mouse move and pinch may
// be used at the same time.
domHandlers.mousemove.call(this, event);
setTouchTimer(this);
},
/**
* Touch结束响应函数
* @inner
* @param {Event} event
*/
touchend: function (event) {
event = normalizeEvent(this.dom, event);
// Mark touch, which is useful in distinguish touch and
// mouse event in upper applicatoin.
event.zrByTouch = true;
this.handler.processGesture(this, event, 'end');
domHandlers.mouseup.call(this, event);
// Do not trigger `mouseout` here, in spite of `mousemove`(`mouseover`) is
// triggered in `touchstart`. This seems to be illogical, but by this mechanism,
// we can conveniently implement "hover style" in both PC and touch device just
// by listening to `mouseover` to add "hover style" and listening to `mouseout`
// to remove "hover style" on an element, without any additional code for
// compatibility. (`mouseout` will not be triggered in `touchend`, so "hover
// style" will remain for user view)
// click event should always be triggered no matter whether
// there is gestrue event. System click can not be prevented.
if (+new Date() - this._lastTouchMoment < TOUCH_CLICK_DELAY) {
domHandlers.click.call(this, event);
}
setTouchTimer(this);
},
pointerdown: function (event) {
domHandlers.mousedown.call(this, event);
// if (useMSGuesture(this, event)) {
// this._msGesture.addPointer(event.pointerId);
// }
},
pointermove: function (event) {
// FIXME
// pointermove is so sensitive that it always triggered when
// tap(click) on touch screen, which affect some judgement in
// upper application. So, we dont support mousemove on MS touch
// device yet.
if (!isPointerFromTouch(event)) {
domHandlers.mousemove.call(this, event);
}
},
pointerup: function (event) {
domHandlers.mouseup.call(this, event);
},
pointerout: function (event) {
// pointerout will be triggered when tap on touch screen
// (IE11+/Edge on MS Surface) after click event triggered,
// which is inconsistent with the mousout behavior we defined
// in touchend. So we unify them.
// (check domHandlers.touchend for detailed explanation)
if (!isPointerFromTouch(event)) {
domHandlers.mouseout.call(this, event);
}
}
};
function isPointerFromTouch(event) {
var pointerType = event.pointerType;
return pointerType === 'pen' || pointerType === 'touch';
}
// function useMSGuesture(handlerProxy, event) {
// return isPointerFromTouch(event) && !!handlerProxy._msGesture;
// }
// Common handlers
zrUtil.each(['click', 'mousedown', 'mouseup', 'mousewheel', 'dblclick', 'contextmenu'], function (name) {
domHandlers[name] = function (event) {
event = normalizeEvent(this.dom, event);
this.trigger(name, event);
};
});
/**
* 为控制类实例初始化dom 事件处理函数
*
* @inner
* @param {module:zrender/Handler} instance 控制类实例
*/
function initDomHandler(instance) {
zrUtil.each(touchHandlerNames, function (name) {
instance._handlers[name] = zrUtil.bind(domHandlers[name], instance);
});
zrUtil.each(pointerHandlerNames, function (name) {
instance._handlers[name] = zrUtil.bind(domHandlers[name], instance);
});
zrUtil.each(mouseHandlerNames, function (name) {
instance._handlers[name] = makeMouseHandler(domHandlers[name], instance);
});
function makeMouseHandler(fn, instance) {
return function () {
if (instance._touching) {
return;
}
return fn.apply(instance, arguments);
};
}
}
function HandlerDomProxy(dom) {
Eventful.call(this);
this.dom = dom;
/**
* @private
* @type {boolean}
*/
this._touching = false;
/**
* @private
* @type {number}
*/
this._touchTimer;
this._handlers = {};
initDomHandler(this);
if (env.pointerEventsSupported) { // Only IE11+/Edge
// 1. On devices that both enable touch and mouse (e.g., MS Surface and lenovo X240),
// IE11+/Edge do not trigger touch event, but trigger pointer event and mouse event
// at the same time.
// 2. On MS Surface, it probablely only trigger mousedown but no mouseup when tap on
// screen, which do not occurs in pointer event.
// So we use pointer event to both detect touch gesture and mouse behavior.
mountHandlers(pointerHandlerNames, this);
// FIXME
// Note: MS Gesture require CSS touch-action set. But touch-action is not reliable,
// which does not prevent defuault behavior occasionally (which may cause view port
// zoomed in but use can not zoom it back). And event.preventDefault() does not work.
// So we have to not to use MSGesture and not to support touchmove and pinch on MS
// touch screen. And we only support click behavior on MS touch screen now.
// MS Gesture Event is only supported on IE11+/Edge and on Windows 8+.
// We dont support touch on IE on win7.
// See <https://msdn.microsoft.com/en-us/library/dn433243(v=vs.85).aspx>
// if (typeof MSGesture === 'function') {
// (this._msGesture = new MSGesture()).target = dom; // jshint ignore:line
// dom.addEventListener('MSGestureChange', onMSGestureChange);
// }
}
else {
if (env.touchEventsSupported) {
mountHandlers(touchHandlerNames, this);
// Handler of 'mouseout' event is needed in touch mode, which will be mounted below.
// addEventListener(root, 'mouseout', this._mouseoutHandler);
}
// 1. Considering some devices that both enable touch and mouse event (like on MS Surface
// and lenovo X240, @see #2350), we make mouse event be always listened, otherwise
// mouse event can not be handle in those devices.
// 2. On MS Surface, Chrome will trigger both touch event and mouse event. How to prevent
// mouseevent after touch event triggered, see `setTouchTimer`.
mountHandlers(mouseHandlerNames, this);
}
function mountHandlers(handlerNames, instance) {
zrUtil.each(handlerNames, function (name) {
addEventListener(dom, eventNameFix(name), instance._handlers[name]);
}, instance);
}
}
var handlerDomProxyProto = HandlerDomProxy.prototype;
handlerDomProxyProto.dispose = function () {
var handlerNames = mouseHandlerNames.concat(touchHandlerNames);
for (var i = 0; i < handlerNames.length; i++) {
var name = handlerNames[i];
removeEventListener(this.dom, eventNameFix(name), this._handlers[name]);
}
};
handlerDomProxyProto.setCursor = function (cursorStyle) {
this.dom.style && (this.dom.style.cursor = cursorStyle || 'default');
};
zrUtil.mixin(HandlerDomProxy, Eventful);
export default HandlerDomProxy;