forked from Clouda-team/touch.code.baidu.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
touch-0.2.6.js
1100 lines (985 loc) · 44.1 KB
/
touch-0.2.6.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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var touch = touch || {};
(function(exports){
//工具类接口
var isPlainObject = function(obj) {
if (!obj || type(obj) !== "object" || obj.nodeType || obj === obj.window ) {
return false;
}
var hasOwnProperty = Object.prototype.hasOwnProperty;
// Not own constructor property must be Object
if (obj.constructor && !hasOwnProperty.call(obj, "constructor") &&
!hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf")) {
return false;
}
var key;
for (key in obj) {}
return key === undefined || hasOwnProperty.call(obj, key);
};
var typeMap = {};
"Boolean Number String Function Array Date RegExp Object".split(' ').forEach(function(item){
typeMap["[object " + item + "]"] = item.toLowerCase();
});
var type = function(obj){
return obj == null ?
'null' :
typeMap[ Object.prototype.toString.call(obj)] || "object";
},
isObject = function(obj){
return type(obj) === 'object';
},
isArray = function(obj){
return type(obj) === 'array';
},
isFunction = function(obj){
return type(obj) === 'function';
},
isString = function(obj){
return type(obj) === 'string';
},
isBoolean = function(obj){
return type(obj) === 'bollean';
},
isNumber = function(obj){
return type(obj) === 'number';
},
isDate = function(obj){
return type(obj) === 'date';
},
isRegExp = function(obj){
return type(obj) === 'regexp';
},
extend = function(){
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
//Handle a deep copy situation
if(typeof target === "boolean") {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
//Handle case when target is a string or something (possible in deep copy)
if ( typeof target !== "object" && !isFunction(target) ) {
target = {};
}
// if only one argument is passed, do nothing
if (length === i) {
return target;
}
for ( ; i < length; i++) {
// Only deal with non-null/undefined values
if ((options = arguments[i]) != null) {
// Extend the base object
for (name in options) {
src = target[name];
copy = options[name];
// Prevent never-ending loop
if (target === copy) {
continue;
}
// Recurse if we're merging plain objects or arrays
if (deep && copy && (isPlainObject(copy) || (copyIsArray = isArray(copy)) ) ) {
if (copyIsArray) {
copyIsArray = false;
clone = src && isArray(src) ? src : [];
} else {
clone = src && isPlainObject(src) ? src : {};
}
// Never move original objects, clone them
target[name] = extend(deep, clone, copy);
// Don't bring in undefined values
} else if (copy !== undefined) {
target[ name ] = copy;
}
}
}
}
//Return the modified object
return target;
};
var _utils = (function(){
var randomStr = '1234567890abcdefghijklmno'+
'pqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ';
var randomStrLen = randomStr.length - 1;
return {
query: function(selector){
return document.querySelectorAll(selector);
},
randomStr: function(max){
var rv = '';
max = (typeof max === 'undefined') ? 8 : max;
for(var i=0; i < max; i++){
rv += randomStr[Math.floor(Math.random() * (randomStrLen + 1))];
}
return rv;
},
//获取某个元素的Data ID, 如果该元素不存在该id, 则随机生成一个id, 并分配给
//该元素的uid attribute。
//return {uid}
getElUID: function(el){
var uid = el.getAttribute('data-uid');
if(!uid){
uid = this.randomStr(18);
el.setAttribute('data-uid', uid);
}
return uid;
},
addEvents: function(el, types, callback){
types = types ? types.split(" ") : [];
for(var i= 0,len=types.length; i<len; i++) {
if(el.addEventListener){
callback && el.addEventListener(types[i], callback, false);
}
}
},
removeEvents: function(el, types, callback){
types = types ? types.split(" ") : [];
for(var i= 0,len=types.length; i<len; i++) {
if(el.removeEventListener){
el.removeEventListener(types[i], callback, false);
}
}
},
deepCopy: function(obj1, obj2){
if(!obj2){
obj2 = obj1;
obj1 = {};
}
extend(true, obj1, obj2);
return obj1;
}
}
})();
var _touchData = (function(){
var dataMap = {};//key是一个uid, value是data object, 代表uid的是一个数据集合。
return {
get: function(el, key){
var uid = _utils.getElUID(el);
dataMap[uid] = dataMap[uid] || {};
if(arguments.length === 1){
return dataMap[uid];
}else{
return dataMap[uid][key];
}
},
set: function(el, key, value){
var uid = _utils.getElUID(el);
dataMap[uid] = dataMap[uid] || {};
dataMap[uid][key] = value;
}
}
})();
/*
*事件管理器,托管由用户定义的事件和处理程序。
*/
var eventManager = (function(){
var eventHanlderMap = {};
var hdInterMap = {};
return {
trigger: function(el, type, paras){
var uid = _utils.getElUID(el);
var handlers = eventHanlderMap[uid][type]['handler'];
var smrEv = smrEventList;
var swipeList = [
smrEv.SWIPE_START, smrEv.SWIPING,
smrEv.SWIPE_END, smrEv.SWIPE_LEFT,
smrEv.SWIPE_RIGHT, smrEv.SWIPE_UP,
smrEv.SWIPE_DOWN, smrEv.SWIPE
];
var execHandler = function(func, el, ops, pr){
//当配置interval大于0, 对event handler进行切片处理
if(ops && ops.interval){
hdInterMap[func] = hdInterMap[func] || {};
hdInterMap[func]['list'] = hdInterMap[func]['list'] || [];
hdInterMap[func]['list'].push((function(h,e,p){
return function(){
h.call(e, p);
}
})(func, el, pr));
if(!hdInterMap[func]['interval']){
hdInterMap[func]['interval'] = setInterval((function(h){
return function(){
var prx = hdInterMap[h]['list'].shift();
if(prx){
prx();
}else{
clearInterval(hdInterMap[h]['interval']);
hdInterMap[h]['interval'] = null;
}
}
})(func), ops.interval);
}
}else{
func.call(el, pr);
}
};
for(var i=0, len=handlers.length; i < len; i++){
var ops = handlers[i].options;
//如果是sumeru swipe类事件, 加速度处理
if(swipeList.indexOf(type) != -1){
if(ops && ops.swipeFactor){
var duration = paras.duration/1000;
paras.factor = (10 - ops.swipeFactor) * duration * duration * 10;
}
}
if(ops && ops.__binding_live){
var target = paras.originEvent.target;
//兼容ios4, 文本节点的父节点为target
if(target && target.nodeType === 3){
target = target.parentNode;
}
var liveEls = _utils.query(ops.__binding_live);
if(liveEls && liveEls.length > 0){
liveEls = Array.prototype.slice.apply(liveEls, [0]);
liveEls.forEach(function(le){
if(le.contains(target)){
execHandler(handlers[i], le, ops, paras);
}
});
}
}else{
execHandler(handlers[i], el, ops, paras);
}
}
},
off: function(){
if(typeof arguments[0] === 'undefined'){
return;
}
var uid = _utils.getElUID(arguments[0]);
//解除live的绑定
if(typeof arguments[3] === 'string'){
var handlers = [];
try{
handlers = eventHanlderMap[uid][arguments[1]]['handler'];
}catch(e){
handlers = [];
}
if(arguments[2]){
var index = handlers.indexOf(arguments[2]);
if(index > -1){
var h = handlers.splice(index, 1);
delete h.options;
}
}else{
for( var i=0; i < handlers.length; i++){
var ops = handlers[i].options;
if(ops && ops.__binding_live
&& ops.__binding_live === arguments[3]){
var h = handlers.splice(i, 1);
delete h.options;
i--;
}
}
}
return false;
}
//解除on绑定
if(arguments.length == 1){
eventHanlderMap[uid] = null;
}else if(arguments.length === 2){
eventHanlderMap[uid] && (eventHanlderMap[uid][arguments[1]] = null);
}else{
if( eventHanlderMap[uid] &&
eventHanlderMap[uid][arguments[1]] &&
eventHanlderMap[uid][arguments[1]]['handler'] ){
var handlers = eventHanlderMap[uid][arguments[1]]['handler'];
var index = handlers.indexOf(arguments[2]);
if(index > -1){
var h = handlers.splice(index, 1);
delete h.options;
}
}
}
},
add: function(el, type, hanlder, options){
var uid = _utils.getElUID(el);
eventHanlderMap[uid] = eventHanlderMap[uid] || {};
eventHanlderMap[uid][type] = eventHanlderMap[uid][type] || {};
eventHanlderMap[uid][type]['handler'] = eventHanlderMap[uid][type]['handler'] || [];
!options || (hanlder.options = options);
typeof hanlder === 'function' ? eventHanlderMap[uid][type]['handler'].push(hanlder) : null;
},
hasHandler: function(el, type){
try{
var uid = _utils.getElUID(el);
var handlers = eventHanlderMap[uid][type]['handler'];
return handlers && handlers.length > 0 ? true : false;
}catch(e){
return false;
}
},
isEmpty: function(el){
var uid = _utils.getElUID(el);
var thisEvent = eventHanlderMap[uid];
for(var type in thisEvent){
if(thisEvent[type] &&
thisEvent[type]['handler'] &&
thisEvent[type]['handler']['length'] !== 0){
return false;
}
}
return true;
}
}
})();
var config = {
tap: true,
doubleTap: true,
tapMaxDistance: 10,
hold: true,
holdTime: 650,//ms
maxDoubleTapInterval: 300,
//swipe
swipe: true,
swipeTime: 300,
swipeMinDistance: 18,
swipeFactor: 5,
drag: true,
//pinch config, minScaleRate与minRotationAngle先指定为0
pinch: true,
minScaleRate: 0,
minRotationAngle: 0
};
/*
*@constructor SmrEvent: one element, one SmrEvent obj. 这个对象
*封装了旋转、pinchin、pinchout、swipe、tap等事件。
*/
var SmrEvent = (function(){
var _hasTouch = ('ontouchstart' in window);
/**
* 获取事件的位置信息
* @param ev, 原生事件对象
* @return array [{ x: int, y: int }]
*/
function getPosOfEvent(ev){
//多指触摸, 返回多个手势位置信息
if(_hasTouch) {
var pos = [];
var src = null;
for(var t=0, len=ev.touches.length; t<len; t++) {
src = ev.touches[t];
pos.push({ x: src.pageX, y: src.pageY });
}
return pos;
}//处理PC浏览器的情况
else {
return [{
x: ev.pageX,
y: ev.pageY
}];
}
};
/**
*获取两点之间的距离
*/
function getDistance(pos1, pos2){
var x = pos2.x - pos1.x, y = pos2.y - pos1.y;
return Math.sqrt((x * x) + (y * y));
};
/**
*计算事件的手势个数
*@param ev {Event}
*/
function getFingers(ev){
return ev.touches ? ev.touches.length : 1;
};
//计算收缩的比例
function calScale(pstart/*开始位置*/, pmove/*移动中的位置*/){
if(pstart.length >= 2 && pmove.length >= 2) {
var disStart = getDistance(pstart[1], pstart[0]);
var disEnd = getDistance(pmove[1], pmove[0]);
return disEnd / disStart;
}
return 1;
};
//return 角度,范围为{-180-0,0-180}, 用来识别swipe方向。
function getAngle(p1, p2){
return Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Math.PI;
};
//return 角度, 范围在{0-180}, 用来识别旋转角度
function _getAngle180(p1, p2){
var agl = Math.atan((p2.y - p1.y) * -1 / (p2.x - p1.x)) * (180 / Math.PI);
return (agl < 0 ? (agl + 180) : agl);
};
//根据角度计算方位
//@para agl {int} 是调用getAngle获取的。
function getDirectionFromAngle(agl) {
var directions = {
up: agl < -45 && agl > -135,
down: agl >= 45 && agl < 135,
left: agl >= 135 || agl <= -135,
right: agl >= -45 && agl <= 45
};
for(var key in directions){
if(directions[key])return key;
}
return null;
};
//取消事件的默认行为和冒泡
function preventDefault(ev){
ev.preventDefault();
ev.stopPropagation();
};
function getXYByElement(el){
var left =0, top = 0;
while (el.offsetParent) {
left += el.offsetLeft;
top += el.offsetTop;
el = el.offsetParent;
}
return { left: left, top: top };
};
return function(el){
var me = this;
var pos = {start : null, move: null, end: null};
var startTime = 0;
var fingers = 0;
var startEvent = null;
var moveEvent = null;
var endEvent = null;
var startSwiping = false;
var startPinch = false;
var startDrag = false;
var __offset = {};
var __touchStart = false;
var __holdTimer = null;
var __tapped = false;
var __lastTapEndTime = null;
function triggerEvent(name, paras){
if(typeof me["on"+ name] === 'function'){
me["on"+ name](paras);
}
};
function reset(){
startEvent = moveEvent = endEvent = null;
__tapped = __touchStart = startSwiping = startPinch = false;
startDrag = false;
pos = {};
__rotation_single_finger = false;
};
function isTouchStart(ev){
return (ev.type === 'touchstart' || ev.type === 'mousedown');
};
function isTouchMove(ev){
return (ev.type === 'touchmove' || ev.type === 'mousemove');
};
function isTouchEnd(ev){
return (ev.type === 'touchend' || ev.type === 'mouseup' || ev.type === 'touchcancel');
};
function triggerCustomEvent(el, customEventName, eventObj, copy){
if(eventManager.hasHandler(el, customEventName)){
copy = typeof copy == 'undefind' ? true : copy;
if(copy){
eventObj = _utils.deepCopy(eventObj);
}
eventObj.type = customEventName;
eventObj.startRotate = function(){
me.startRotate();
}
triggerEvent(customEventName, eventObj);
}
};
var __scale_last_rate = 1;
var __rotation_single_finger = false;
var __rotation_single_start = [];//元素坐标中心位置
var __initial_angle = 0;
var __rotation = 0;
var __prev_tapped_end_time = 0;
var __prev_tapped_pos = null;
var gestures = {
_getAngleDiff: function(currentPos){
var diff = parseInt(__initial_angle - _getAngle180(currentPos[0], currentPos[1]), 10);
var count = 0;
while(Math.abs(diff - __rotation) > 90 && count++ < 50) {
if(__rotation < 0){
diff -= 180;
}else{
diff += 180;
}
}
__rotation = parseInt(diff, 10);
return __rotation;
},
pinch: function(ev){
if(config.pinch){
//touchend进入此时的getFinger(ev) < 2
if(!__touchStart)return;
if(getFingers(ev) < 2){
if(!isTouchEnd(ev))return;
}
var em = eventManager;
var scale = calScale(pos.start, pos.move);
var rotation = this._getAngleDiff(pos.move);
var eventObj = {
type: '',
originEvent: ev,
scale: scale,
rotation: rotation,
direction: (rotation > 0 ? 'right' : 'left'),
fingersCount: getFingers(ev),
startRotate: function(){
me.startRotate();
}
};
if(!startPinch){
startPinch = true;
eventObj.fingerStatus = "start";
triggerCustomEvent(el, smrEventList.PINCH_START, eventObj);
}else if(isTouchMove(ev)){
eventObj.fingerStatus = "move";
}else if(isTouchEnd(ev)){
eventObj.fingerStatus = "end";
triggerCustomEvent(el, smrEventList.PINCH_END, eventObj);
}
triggerCustomEvent(el, smrEventList.PINCH, eventObj);
if(Math.abs(1-scale) > config.minScaleRate){
var scaleEv = _utils.deepCopy(eventObj);
//手势放大, 触发pinchout事件
var scale_diff = 0.00000000001;//防止touchend的scale与__scale_last_rate相等,不触发事件的情况。
if(scale > __scale_last_rate){
__scale_last_rate = scale - scale_diff;
triggerCustomEvent(el, smrEventList.PINCH_OUT, scaleEv, false);
}//手势缩小,触发pinchin事件
else if(scale < __scale_last_rate){
__scale_last_rate = scale + scale_diff;
triggerCustomEvent(el, smrEventList.PINCH_IN, scaleEv, false);
}
if(isTouchEnd(ev)){
__scale_last_rate = 1;
}
}
if(Math.abs(rotation) > config.minRotationAngle){
var rotationEv = _utils.deepCopy(eventObj), eventType;
eventType = rotation > 0 ? smrEventList.ROTATION_RIGHT : smrEventList.ROTATION_LEFT;
triggerCustomEvent(el, eventType, rotationEv, false);
triggerCustomEvent(el, smrEventList.ROTATION, eventObj);
}
//preventDefault(ev);
}
},
rotateSingleFinger: function(ev){
if(__rotation_single_finger && getFingers(ev) < 2){
if(!pos.move)return;
if(__rotation_single_start.length < 2){
var docOff = getXYByElement(el);
__rotation_single_start = [{
x: docOff.left + el.offsetWidth/2,
y: docOff.top + el.offsetHeight/2
}, pos.move[0]];
__initial_angle = parseInt(_getAngle180(__rotation_single_start[0], __rotation_single_start[1]), 10);
}
var move = [__rotation_single_start[0], pos.move[0]];
var rotation = this._getAngleDiff(move);
var eventObj = {
type: '',
originEvent: ev,
rotation: rotation,
direction: (rotation > 0 ? 'right' : 'left'),
fingersCount: getFingers(ev)
};
if(isTouchMove(ev)){
eventObj.fingerStatus = "move";
}else if(isTouchEnd(ev) || ev.type === 'mouseout'){
eventObj.fingerStatus = "end";
triggerCustomEvent(el, smrEventList.PINCH_END, eventObj);
}
eventType = rotation > 0 ? smrEventList.ROTATION_RIGHT : smrEventList.ROTATION_LEFT;
triggerCustomEvent(el, eventType, eventObj);
triggerCustomEvent(el, smrEventList.ROTATION, eventObj);
}
},
swipe: function(ev){
//目前swipe只存在一个手势上
if(!__touchStart || !pos.move || getFingers(ev) > 1){
return;
}
var em = eventManager;
var now = Date.now();
var touchTime = now - startTime;
var distance = getDistance(pos.start[0], pos.move[0]);
var position = {
x: pos.move[0].x - __offset.left,
y: pos.move[0].y - __offset.top
};
var angle = getAngle(pos.start[0], pos.move[0]);
var direction = getDirectionFromAngle(angle);
var touchSecond = touchTime/1000;
var factor = ((10 - config.swipeFactor) * 10 * touchSecond * touchSecond);
var eventObj = {
type: smrEventList.SWIPE,//DEFAULT: smrEventList.SWIPE event.
originEvent: ev,
position: position,
direction: direction,
distance: distance,
distanceX: pos.move[0].x - pos.start[0].x,
distanceY: pos.move[0].y - pos.start[0].y,
angle: angle,
duration: touchTime,
fingersCount: getFingers(ev),
factor: factor
};
if(config.swipe){
var swipeTo = function(){
var elt = smrEventList;
switch(direction){
case 'up': triggerCustomEvent(el, elt.SWIPE_UP, eventObj);break;
case 'down': triggerCustomEvent(el, elt.SWIPE_DOWN, eventObj);break;
case 'left': triggerCustomEvent(el, elt.SWIPE_LEFT, eventObj);break;
case 'right': triggerCustomEvent(el, elt.SWIPE_RIGHT, eventObj);break;
}
};
if(!startSwiping){
eventObj.fingerStatus = eventObj.swipe = 'start';
startSwiping = true;
triggerCustomEvent(el, smrEventList.SWIPE_START, eventObj);
}else if(isTouchMove(ev)){
eventObj.fingerStatus = eventObj.swipe = 'move';
triggerCustomEvent(el, smrEventList.SWIPING, eventObj);
if(touchTime > config.swipeTime &&
touchTime < config.swipeTime + 50 &&
distance > config.swipeMinDistance){
swipeTo();
triggerCustomEvent(el, smrEventList.SWIPE, eventObj, false);
}
}else if(isTouchEnd(ev) || ev.type === 'mouseout'){
eventObj.fingerStatus = eventObj.swipe = 'end';
triggerCustomEvent(el, smrEventList.SWIPE_END, eventObj);
if(config.swipeTime > touchTime &&
distance > config.swipeMinDistance){
swipeTo();
triggerCustomEvent(el, smrEventList.SWIPE, eventObj, false);
}
}
}
if(config.drag){
if(!startDrag){
eventObj.fingerStatus = eventObj.swipe = 'start';
startDrag = true;
}else if(isTouchMove(ev)){
eventObj.fingerStatus = eventObj.swipe = 'move';
}else if(isTouchEnd(ev)){
eventObj.fingerStatus = eventObj.swipe = 'end';
}
triggerCustomEvent(el, smrEventList.DRAG, eventObj);
}
},
tap: function(ev){
if(config.tap){
var em = eventManager;
var now = Date.now();
var touchTime = now - startTime;
var distance = getDistance(pos.start[0], pos.move ? pos.move[0]:pos.start[0]);
clearTimeout(__holdTimer);//去除hold事件
var isDoubleTap = (function(){
if(__prev_tapped_pos && config.doubleTap &&
(startTime - __prev_tapped_end_time) < config.maxDoubleTapInterval){
var doubleDis = getDistance(__prev_tapped_pos, pos.start[0]);
if(doubleDis < 16)return true;
}
return false;
})();
if(isDoubleTap){
triggerEvent(smrEventList.DOUBLE_TAP, {
type: smrEventList.DOUBLE_TAP,
originEvent : ev,
position : pos.start[0]
});
return;
}
if(config.tapMaxDistance < distance)return;
if(config.holdTime > touchTime && getFingers(ev) <= 1){
//clearTimeout在ios上有时不work(alert引起的), 先用__tapped顶一下
__tapped = true;
__prev_tapped_end_time = now;
__prev_tapped_pos = pos.start[0];
if(em.hasHandler(el, smrEventList.TAP)){
triggerEvent(smrEventList.TAP, {
type: smrEventList.TAP,
originEvent : ev,
fingersCount: getFingers(ev),
position : pos.start[0]
});
}
if(em.hasHandler(el, smrEventList.CLICK)){
triggerEvent(smrEventList.CLICK, {
type: smrEventList.CLICK,
originEvent : ev,
fingersCount: getFingers(ev),
position : pos.start[0]
});
}
}
}
},
hold: function(ev){
if(config.hold) {
clearTimeout(__holdTimer);
__holdTimer = setTimeout(function() {
if(!pos.start)return;
var distance = getDistance(pos.start[0], pos.move ? pos.move[0]:pos.start[0]);
if(config.tapMaxDistance < distance)return;
if(!__tapped){
triggerEvent("hold", {
type: 'hold',
originEvent: ev,
fingersCount: getFingers(ev),
position: pos.start[0]
});
}
}, config.holdTime);
}
}
};
var handlerOriginEvent = function(ev){
switch(ev.type){
case 'touchstart':
case 'mousedown':
__rotation_single_finger = false;
__rotation_single_start = [];
triggerCustomEvent(el, ev.type, {
originEvent: ev
});
__touchStart = true;
if(!pos.start || pos.start.length < 2){
pos.start = getPosOfEvent(ev);
}
if(getFingers(ev) >= 2){
__initial_angle = parseInt(_getAngle180(pos.start[0], pos.start[1]), 10);
}
startTime = Date.now();
startEvent = ev;
__offset = {};
//来自jquery offset的写法: https://github.com/jquery/jquery/blob/master/src/offset.js
var box = el.getBoundingClientRect();
var docEl = document.documentElement;
__offset = {
top: box.top + ( window.pageYOffset || docEl.scrollTop ) - ( docEl.clientTop || 0 ),
left: box.left + ( window.pageXOffset || docEl.scrollLeft ) - ( docEl.clientLeft || 0 )
};
gestures.hold(ev);
break;
case 'touchmove':
case 'mousemove':
triggerCustomEvent(el, ev.type, {
originEvent: ev
});
if(!__touchStart || !pos.start)return;
pos.move = getPosOfEvent(ev);
if(getFingers(ev) >= 2){
gestures.pinch(ev);
}else if(__rotation_single_finger){
gestures.rotateSingleFinger(ev);
}else{
gestures.swipe(ev);
}
break;
case 'touchend':
case 'touchcancel':
case 'mouseup':
case 'mouseout':
triggerCustomEvent(el, ev.type, {
originEvent: ev
});
if(!__touchStart)return;
endEvent = ev;
if(startPinch){
gestures.pinch(ev);
}else if(__rotation_single_finger){
gestures.rotateSingleFinger(ev);
}else{
if(startSwiping){
gestures.swipe(ev);
}
}
gestures.tap(ev);
reset();
__initial_angle = 0;
__rotation = 0;
if(ev.touches && ev.touches.length === 1){
__touchStart = true;
__rotation_single_finger = true;
}
break;
}
};
var eventNames = _hasTouch ? 'touchstart touchmove touchend touchcancel':
'mouseup mousedown mousemove mouseout';
_utils.addEvents(el, eventNames, handlerOriginEvent);
this.tearDown = function(){
_utils.removeEvents(el, eventNames, handlerOriginEvent);
}
this.startRotate = function(){
__rotation_single_finger = true;
}
}
})();
/*
*@param el {Element}
*@param types {String} 事件类型, 可以空格分割多个事件。
*@param handler {Function} 事件处理函数
*@param options {Object}
*{
* swipeFactor: int (1-10) 加速度因子, 值越大速率越大。
* interval: 0 //单位ms, 用来对handler的回调进行切片。
*}
*/
var _on = function(){
if(typeof arguments.length < 3)throw 'Please specify complete argments';
var element = typeof arguments[0] === 'string' ?
_utils.query(arguments[0]) : [arguments[0]];
var types = arguments[1].split(' ');
var handler = arguments[arguments.length - 1];
var options = arguments.length > 3 ? arguments[arguments.length - 2] : undefined;
element = Array.prototype.slice.apply(element, [0]);
element.forEach(function(el){
for(var i=0; i < types.length; i++){
var eventName = mapEvent(types[i]);
if(event.special[eventName]){
eventManager.add(el, eventName, handler, options);
event.special[eventName].setUp.apply(this, [el, eventName]);
}else{
_utils.addEvents(el, eventName, handler);
}
}
});
};
var _live = function(){
if(arguments.length < 3){
throw new Error('wrong argument');
}
//如果指定的第一个元素不为选择器, 则交给on处理。
if(typeof arguments[0] != 'string'){
_on.apply(exports, arguments);
return;
}
var options = arguments.length > 3 ? arguments[arguments.length - 2] : {};
var types = arguments[1];
var handler = arguments[arguments.length - 1];
options.__binding_live = arguments[0];
_on.apply(exports, [document.body, types, options, handler]);
};
var _off = function(selector, types, handler, liveSelector){
if(typeof selector === 'undefined'){
throw 'Please specify the selector.';
}
selector = typeof selector === 'string' ? _utils.query(selector) : [selector];
selector = Array.prototype.slice.apply(selector, [0]);
if(!types){
eventManager.off(selector);
_utils.removeEvents(selector);
}else{
types = types.split(' ');
selector.forEach(function(el){
types.forEach(function(type, index){
var args = [el, mapEvent(type)];
handler ? args.push(handler) : null;
liveSelector ? args.push(liveSelector) : null;
if(event.special[type]){
eventManager.off.apply(eventManager, args);
if(eventManager.isEmpty(el, type)){
event.special[type].tearDown(el);
}
}else{
_utils.removeEvents.apply(this, args);
}
});
});
}
};
var _die = function(selector, types, handler){
_off(document.body, types, handler, selector);
}
var touchEvent = {
'touchstart': 'mousedown',
'touchmove': 'mousemove',