1 |
|
2 | var EventUtil = function () {
|
3 | |
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | var _countClickEventHandler = function _countClickEventHandler(e) {
|
12 |
|
13 | var target = e.currentTarget;
|
14 |
|
15 | target.prevCount += 1;
|
16 | e.count = target.prevCount;
|
17 | e.code = '0';
|
18 |
|
19 |
|
20 | if (target.prevCount === 1) {
|
21 | target.prevTime = new Date().getTime();
|
22 | }
|
23 |
|
24 |
|
25 | if (target.prevCount >= target.count) {
|
26 |
|
27 | if (new Date().getTime() - target.prevTime <= target.timeout) {
|
28 | e.code = '1';
|
29 | }
|
30 | target.prevCount = 0;
|
31 | }
|
32 |
|
33 | target.handler(e);
|
34 | };
|
35 | var _countClickEvent = function _countClickEvent(element, type, handler, isDetach, params) {
|
36 |
|
37 | element.prevCount = 0;
|
38 | element.prevTime = new Date().getTime();
|
39 | element.count = params && params.count ? params.count : 10;
|
40 | element.timeout = params && params.timeout ? params.timeout : 5000;
|
41 | element.handler = handler;
|
42 |
|
43 | function attach() {
|
44 | element.addEventListener('click', _countClickEventHandler, false);
|
45 | }
|
46 | function detach() {
|
47 | element.removeEventListener('click', _countClickEventHandler, false);
|
48 | }
|
49 | if (isDetach) {
|
50 | detach();
|
51 | } else {
|
52 | attach();
|
53 | }
|
54 | };
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | var _touchEvent = function _touchEvent(element, type, handler, isDetach) {
|
63 | var params = {
|
64 | threshold: 0
|
65 | |
66 |
|
67 |
|
68 | };var touches = {
|
69 | direction: 0,
|
70 | vertical: 0,
|
71 | horizontal: 0,
|
72 | startX: 0,
|
73 | startY: 0,
|
74 | endX: 0,
|
75 | endY: 0,
|
76 | diffX: 0,
|
77 | diffY: 0
|
78 | |
79 |
|
80 |
|
81 | };var onTouchStart = function onTouchStart(e) {
|
82 | touches.startX = e.touches[0].clientX;
|
83 | touches.startY = e.touches[0].clientY;
|
84 | };
|
85 | var onTouchEnd = function onTouchEnd(e) {
|
86 | var eventName = '';
|
87 | touches.endX = e.changedTouches[0].clientX;
|
88 | touches.endY = e.changedTouches[0].clientY;
|
89 | touches.diffX = touches.startX - touches.endX;
|
90 | touches.diffY = touches.startY - touches.endY;
|
91 |
|
92 | if (Math.abs(touches.diffX) < 6 && Math.abs(touches.diffY) < 6) {
|
93 | eventName = 'tap';
|
94 | }
|
95 |
|
96 |
|
97 | if (touches.direction === 0) {
|
98 |
|
99 | touches.direction = Math.abs(touches.diffX) > Math.abs(touches.diffY) ? 1 : -1;
|
100 | }
|
101 | if (touches.direction === -1) {
|
102 |
|
103 | touches.vertical = touches.diffY < 0 ? 1 : -1;
|
104 | }
|
105 | if (touches.direction === 1) {
|
106 |
|
107 | touches.horizontal = touches.diffX < 0 ? 1 : -1;
|
108 | }
|
109 |
|
110 |
|
111 | if (touches.vertical === -1) {
|
112 |
|
113 | if (Math.abs(touches.diffY) > params.threshold) {
|
114 | eventName = 'swipeup';
|
115 | }
|
116 | } else if (touches.vertical === 1) {
|
117 |
|
118 | if (Math.abs(touches.diffY) > params.threshold) {
|
119 | eventName = 'swipedown';
|
120 | }
|
121 | } else if (touches.horizontal === -1) {
|
122 |
|
123 | if (Math.abs(touches.diffY) > params.threshold) {
|
124 | eventName = 'swipeleft';
|
125 | }
|
126 | } else if (touches.horizontal === 1) {
|
127 |
|
128 | if (Math.abs(touches.diffY) > params.threshold) {
|
129 | eventName = 'swiperight';
|
130 | }
|
131 | }
|
132 |
|
133 | touches.direction = 0;
|
134 | touches.vertical = 0;
|
135 | touches.horizontal = 0;
|
136 |
|
137 | for (var n in element.touchEvents) {
|
138 | if (eventName === n) element.touchEvents[n](e);
|
139 | }
|
140 | };
|
141 | |
142 |
|
143 |
|
144 |
|
145 | var attach = function attach() {
|
146 | if (Object.keys(element.touchEvents || {}).length === 0) {
|
147 | element.touchEvents = {};
|
148 | element['addEventListener']('touchstart', onTouchStart, false);
|
149 | element['addEventListener']('touchend', onTouchEnd, false);
|
150 | }
|
151 | element.touchEvents[type] = handler;
|
152 | };
|
153 |
|
154 | var detach = function detach() {
|
155 | if (element.touchEvents) delete element.touchEvents[type];
|
156 | if (Object.keys(element.touchEvents || {}).length === 0) {
|
157 | element['removeEventListener']('touchstart', onTouchStart, false);
|
158 | element['removeEventListener']('touchend', onTouchEnd, false);
|
159 | }
|
160 | };
|
161 |
|
162 | if (isDetach) {
|
163 | detach();
|
164 | } else {
|
165 | attach();
|
166 | }
|
167 | };
|
168 | |
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | var _shake_handler = null;
|
176 | var _shake_threshold = 3000;
|
177 | var _shake_lastUpdate = 0;
|
178 | var _shake_curShakeX = 0;
|
179 | var _shake_curShakeY = 0;
|
180 | var _shake_curShakeZ = 0;
|
181 | var _shake_lastShakeX = 0;
|
182 | var _shake_lastShakeY = 0;
|
183 | var _shake_lastShakeZ = 0;
|
184 | var _shakeEventHandler = function _shakeEventHandler(e) {
|
185 | var acceleration = e.accelerationIncludingGravity;
|
186 | var curTime = new Date().getTime();
|
187 | if (curTime - _shake_lastUpdate > 100) {
|
188 | var diffTime = curTime - _shake_lastUpdate;
|
189 | _shake_lastUpdate = curTime;
|
190 | _shake_curShakeX = acceleration.x;
|
191 | _shake_curShakeY = acceleration.y;
|
192 | _shake_curShakeZ = acceleration.z;
|
193 | var speed = Math.abs(_shake_curShakeX + _shake_curShakeY + _shake_curShakeZ - _shake_lastShakeX - _shake_lastShakeY - _shake_lastShakeZ) / diffTime * 10000;
|
194 | if (speed > _shake_threshold) {
|
195 | e.speed = speed;
|
196 | if (_shake_handler) _shake_handler(e);
|
197 | }
|
198 | _shake_lastShakeX = _shake_curShakeX;
|
199 | _shake_lastShakeY = _shake_curShakeY;
|
200 | _shake_lastShakeZ = _shake_curShakeZ;
|
201 | }
|
202 | };
|
203 | var _shakeEvent = function _shakeEvent(element, type, handler, isDetach) {
|
204 | _shake_handler = handler;
|
205 | function attach() {
|
206 | window.addEventListener('devicemotion', _shakeEventHandler, false);
|
207 | }
|
208 | function detach() {
|
209 | window.removeEventListener('devicemotion', _shakeEventHandler, false);
|
210 | }
|
211 |
|
212 | if (isDetach) {
|
213 | detach();
|
214 | } else {
|
215 | attach();
|
216 | }
|
217 | };
|
218 | |
219 |
|
220 |
|
221 | return {
|
222 | addHandler: function addHandler(element, type, handler, params) {
|
223 |
|
224 |
|
225 | if (type === 'countclick') {
|
226 | _countClickEvent(element, 'countclick', handler, false, params);
|
227 | return;
|
228 | }
|
229 |
|
230 | var isSupportTouch = 'ontouchstart' in window;
|
231 | if (!isSupportTouch) {
|
232 | if (type === 'touchstart') type = 'mousedown';
|
233 | if (type === 'touchmove') type = 'mousemove';
|
234 | if (type === 'touchend') type = 'mouseup';
|
235 | if (type === 'touchcancel') type = 'mouseup';
|
236 | if (type === 'tap') type = 'click';
|
237 | } else {
|
238 |
|
239 | if (type === 'tap' || type === 'swipeleft' || type === 'swiperight' || type === 'swipedown' || type === 'swipeup') {
|
240 | _touchEvent(element, type, handler);
|
241 | return;
|
242 | }
|
243 | }
|
244 |
|
245 | if (type === 'shake') {
|
246 | if (window.DeviceMotionEvent) {
|
247 | _shakeEvent(element, 'shake', handler);
|
248 | } else {
|
249 | console.warn('此设备不支持重力感应');
|
250 | }
|
251 | return;
|
252 | }
|
253 |
|
254 | if (element.addEventListener) {
|
255 | element.addEventListener(type, handler, false);
|
256 | } else if (element.attachEvent) {
|
257 | element.attachEvent('on' + type, handler);
|
258 | } else {
|
259 | element['on' + type] = handler;
|
260 | }
|
261 | },
|
262 | removeHandler: function removeHandler(element, type, handler) {
|
263 |
|
264 | if (type === 'countclick') {
|
265 | _countClickEvent(element, 'countclick', null, true);
|
266 | return;
|
267 | }
|
268 |
|
269 | var isSupportTouch = 'ontouchstart' in window;
|
270 | if (!isSupportTouch) {
|
271 | if (type === 'touchstart') type = 'mousedown';
|
272 | if (type === 'touchmove') type = 'mousemove';
|
273 | if (type === 'touchend') type = 'mouseup';
|
274 | if (type === 'touchcancel') type = 'mouseup';
|
275 | if (type === 'tap') type = 'click';
|
276 | } else {
|
277 |
|
278 | if (type === 'tap' || type === 'swipeleft' || type === 'swiperight' || type === 'swipedown' || type === 'swipeup') {
|
279 | _touchEvent(element, type, handler, true);
|
280 | return;
|
281 | }
|
282 | }
|
283 |
|
284 | if (type === 'shake') {
|
285 | if (window.DeviceMotionEvent) {
|
286 | _shakeEvent(element, 'shake', handler, true);
|
287 | } else {
|
288 | console.warn('此设备不支持重力感应');
|
289 | }
|
290 | return;
|
291 | }
|
292 |
|
293 | if (element.removeEventListener) {
|
294 | element.removeEventListener(type, handler, false);
|
295 | } else if (element.detachEvent) {
|
296 | element.detachEvent('on' + type, handler);
|
297 | } else {
|
298 | element['on' + type] = null;
|
299 | }
|
300 | },
|
301 | preventDefault: function preventDefault(e) {
|
302 | if (e.preventDefault) {
|
303 | e.preventDefault();
|
304 | } else {
|
305 | e.returnValue = false;
|
306 | }
|
307 | },
|
308 | stopPropagation: function stopPropagation(e) {
|
309 | if (e.stopPropagation) {
|
310 | e.stopPropagation();
|
311 | } else {
|
312 | e.cancelBubble = true;
|
313 | }
|
314 | },
|
315 | event: function event(e) {
|
316 | return e || window.e;
|
317 | },
|
318 | type: function type(e) {
|
319 | return e.type;
|
320 | },
|
321 | target: function target(e) {
|
322 | return e.target || e.srcElement;
|
323 | }
|
324 | };
|
325 | }();
|
326 |
|
327 | export default EventUtil; |
\ | No newline at end of file |