-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathindex.js
276 lines (224 loc) · 8.01 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
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
'use strict';
(function () {
var getReactFCInitializer = function (React) {
return function initializeReactFastclick () {
var originalCreateElement = React.createElement;
// Moved if Math.abs(downX - upX) > MOVE_THRESHOLD;
var MOVE_THRESHOLD = 8;
var TOUCH_DELAY = 1000;
var touchKeysToStore = [
'clientX',
'clientY',
'pageX',
'pageY',
'screenX',
'screenY',
'radiusX',
'radiusY'
];
var touchEvents = {
downPos: {},
lastPos: {}
};
var isDisabled = function (element) {
if (!element) {
return false;
}
var disabled = element.getAttribute('disabled');
return disabled !== false && disabled !== null;
};
var focus = function (event, target) {
var myTarget = target || event.currentTarget;
if (!myTarget || isDisabled(myTarget)) {
return;
}
myTarget.focus();
};
var handleType = {
input: function (event) {
focus(event);
event.stopPropagation();
},
textarea: function (event) {
focus(event);
event.stopPropagation();
},
select: function (event) {
focus(event);
event.stopPropagation();
},
label: function (event) {
var input;
var forTarget = event.currentTarget.getAttribute('for');
if (forTarget) {
input = document.getElementById(forTarget);
} else {
input = event.currentTarget.querySelectorAll('input, textarea, select')[0];
}
if (input) {
focus(event, input);
}
}
};
var fakeClickEvent = function (event) {
if (typeof event.persist === 'function') {
event.persist();
}
event.fastclick = true;
event.type = 'click';
event.button = 0;
};
var copyTouchKeys = function (touch, target) {
if (typeof target.persist === 'function') {
target.persist();
}
if (touch) {
for (var i = 0; i < touchKeysToStore.length; i += 1) {
var key = touchKeysToStore[i];
target[key] = touch[key];
}
}
};
var noTouchHappened = function () {
return !touchEvents.touched && (
!touchEvents.lastTouchDate || new Date().getTime() > touchEvents.lastTouchDate + TOUCH_DELAY
);
};
var invalidateIfMoreThanOneTouch = function (event) {
touchEvents.invalid = event.touches && event.touches.length > 1 || touchEvents.invalid;
};
var onMouseEvent = function (callback, event) {
var touched = !noTouchHappened();
// Prevent mouse events on other elements
if (touched && event.target !== touchEvents.target) {
event.preventDefault();
}
// Prevent any mouse events if we touched recently
if (typeof callback === 'function' && !touched) {
callback(event);
}
if (event.type === 'click') {
touchEvents.invalid = false;
touchEvents.touched = false;
touchEvents.moved = false;
}
};
var onTouchStart = function (callback, event) {
touchEvents.invalid = false;
touchEvents.moved = false;
touchEvents.touched = true;
touchEvents.target = event.target;
touchEvents.lastTouchDate = new Date().getTime();
copyTouchKeys(event.touches[0], touchEvents.downPos);
copyTouchKeys(event.touches[0], touchEvents.lastPos);
invalidateIfMoreThanOneTouch(event);
if (typeof callback === 'function') {
callback(event);
}
};
var onTouchMove = function (callback, event) {
touchEvents.touched = true;
touchEvents.lastTouchDate = new Date().getTime();
copyTouchKeys(event.touches[0], touchEvents.lastPos);
invalidateIfMoreThanOneTouch(event);
if (Math.abs(touchEvents.downPos.clientX - touchEvents.lastPos.clientX) > MOVE_THRESHOLD ||
Math.abs(touchEvents.downPos.clientY - touchEvents.lastPos.clientY) > MOVE_THRESHOLD) {
touchEvents.moved = true;
}
if (typeof callback === 'function') {
callback(event);
}
};
var onTouchEnd = function (callback, onClick, type, event) {
touchEvents.touched = true;
touchEvents.lastTouchDate = new Date().getTime();
invalidateIfMoreThanOneTouch(event);
if (typeof callback === 'function') {
callback(event);
}
if (!touchEvents.invalid && !touchEvents.moved) {
var box = event.currentTarget.getBoundingClientRect();
if (touchEvents.lastPos.clientX - (touchEvents.lastPos.radiusX || 0) <= box.right &&
touchEvents.lastPos.clientX + (touchEvents.lastPos.radiusX || 0) >= box.left &&
touchEvents.lastPos.clientY - (touchEvents.lastPos.radiusY || 0) <= box.bottom &&
touchEvents.lastPos.clientY + (touchEvents.lastPos.radiusY || 0) >= box.top) {
if (!isDisabled(event.currentTarget)) {
if (typeof onClick === 'function') {
copyTouchKeys(touchEvents.lastPos, event);
fakeClickEvent(event);
onClick(event);
}
if (!event.defaultPrevented && handleType[type]) {
handleType[type](event);
}
}
}
}
};
var propsWithFastclickEvents = function (type, props) {
var newProps = {};
// Loop over props
for (var key in props) {
// Copy props to newProps
newProps[key] = props[key];
}
// Apply our wrapped mouse and touch handlers
newProps.onClick = onMouseEvent.bind(null, props.onClick);
newProps.onMouseDown = onMouseEvent.bind(null, props.onMouseDown);
newProps.onMouseMove = onMouseEvent.bind(null, props.onMouseMove);
newProps.onMouseUp = onMouseEvent.bind(null, props.onMouseUp);
newProps.onTouchStart = onTouchStart.bind(null, props.onTouchStart);
newProps.onTouchMove = onTouchMove.bind(null, props.onTouchMove);
newProps.onTouchEnd = onTouchEnd.bind(null, props.onTouchEnd, props.onClick, type);
if (typeof Object.freeze === 'function') {
Object.freeze(newProps);
}
return newProps;
};
React.createElement = function () {
// Convert arguments to array
var args = Array.prototype.slice.call(arguments);
var type = args[0];
var props = args[1];
// Check if basic element & has onClick prop
if (type && typeof type === 'string' && (
(props && typeof props.onClick === 'function') || handleType[type]
)) {
// Add our own events to props
args[1] = propsWithFastclickEvents(type, props || {});
}
// Apply args to original createElement function
return originalCreateElement.apply(null, args);
};
if (typeof React.DOM === 'object') {
for (var key in React.DOM) {
React.DOM[key] = React.createElement.bind(null, key);
}
}
};
};
/* istanbul ignore next */
// Export for commonjs / browserify
if (typeof exports === 'object' && typeof module !== 'undefined') {
var React = require('react');
module.exports = getReactFCInitializer(React);
// Export for amd / require
} else if (typeof define === 'function' && define.amd) { // eslint-disable-line no-undef
define(['react'], function (ReactAMD) { // eslint-disable-line no-undef
return getReactFCInitializer(ReactAMD);
});
// Export globally
} else {
var root;
if (typeof window !== 'undefined') {
root = window;
} else if (typeof global !== 'undefined') {
root = global;
} else if (typeof self !== 'undefined') {
root = self;
} else {
root = this;
}
root.Reorder = getReactFCInitializer(root.React);
}
})();