-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
174 lines (158 loc) · 4.56 KB
/
index.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
const directive = {
bind: function($el, binding) {
// any , horizonal, vertical, right, left, up, down
// modifiers: lock, capture
var argument = [
'any',
'horizonal',
'vertical',
'right',
'left',
'up',
'down'
];
var lock = binding.modifiers.lock;
var capture = binding.modifiers.capture;
var processor = binding.value;
var startX;
var startY;
var movingX;
var movingY;
var directionFour;
var directionTwo;
var offset;
var directionCheckDone;
var continuePropagation;
var startWidthTwo;
var startWidthFour;
var canceled;
var enable =
binding.value.enable !== undefined ? binding.value.enable : true;
function getInfo(srcEvt) {
return {
element: $el,
scrEvt: srcEvt,
offset: offset,
startX: startX,
startY: startY,
movingX: movingX,
movingY: movingY,
directionTwo: directionTwo,
directionFour: directionFour,
startWidthTwo: startWidthTwo,
startWidthFour: startWidthFour
};
}
// offset的含义由directionTwo来确定的
if (argument.includes(binding.arg) && enable) {
$el.addEventListener(
'touchstart',
function(e) {
startX = e.touches[0].clientX;
startY = e.touches[0].clientY;
directionTwo = null;
directionCheckDone = null;
startWidthTwo = null;
startWidthFour = null;
continuePropagation = false;
canceled = false;
},
capture
);
$el.addEventListener(
'touchmove',
function(e) {
movingX = e.touches[0].clientX;
movingY = e.touches[0].clientY;
var x = movingX - startX;
var y = movingY - startY;
var lockCheck = false;
var check;
(directionTwo == null || binding.arg === 'any') &&
(directionTwo =
Math.abs(y) <= Math.abs(x) ? 'horizonal' : 'vertical');
if (directionTwo === 'vertical') {
offset = y;
directionFour = y < 0 ? 'up' : 'down';
} else {
offset = x;
directionFour = x > 0 ? 'right' : 'left';
}
check =
[directionFour, directionTwo].includes(binding.arg) ||
binding.arg === 'any';
if (directionCheckDone === null) {
if (check === true) {
startWidthTwo = directionTwo;
startWidthFour = directionFour;
processor.start instanceof Function &&
processor.start(
getInfo(e),
setTo => {
lockCheck = setTo;
},
setTo => {
continuePropagation = setTo;
}
);
}
directionCheckDone = check;
}
if (directionCheckDone) {
lock && (lockCheck = true);
processor.move instanceof Function &&
processor.move(
getInfo(e),
setTo => {
lockCheck = setTo;
},
setTo => {
continuePropagation = setTo;
}
);
!continuePropagation && e.stopPropagation();
lockCheck && e.cancelable && e.preventDefault();
} else if (!canceled) {
canceled = true;
processor.cancel instanceof Function &&
processor.cancel(
getInfo(e),
setTo => {
lockCheck = setTo;
},
setTo => {
continuePropagation = setTo;
}
);
}
},
capture
);
$el.addEventListener(
'touchend',
function(e) {
var lockCheck = false;
continuePropagation = true;
lock && directionCheckDone && (lockCheck = true);
directionCheckDone &&
processor.end instanceof Function &&
processor.end(
getInfo(e),
setTo => {
lockCheck = setTo;
},
setTo => {
continuePropagation = setTo;
}
);
!continuePropagation && e.stopPropagation();
lockCheck && e.cancelable && e.preventDefault();
},
capture
);
} else {
enable && console.log(`未知自定义swipe位置参数:${binding.arg}`);
}
}
};
export default directive;