-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpulltorefresh.js
414 lines (319 loc) · 12.5 KB
/
pulltorefresh.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
/*!
* pulltorefreshjs v0.1.22
* (c) Rafael Soto
* Released under the MIT License.
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.PullToRefresh = factory());
}(this, function () { 'use strict';
var _shared = {
pullStartY: null,
pullMoveY: null,
handlers: [],
styleEl: null,
events: null,
dist: 0,
state: 'pending',
timeout: null,
distResisted: 0,
supportsPassive: false,
supportsPointerEvents: typeof window !== 'undefined' && !!window.PointerEvent
};
try {
window.addEventListener('test', null, {
get passive() {
// eslint-disable-line getter-return
_shared.supportsPassive = true;
}
});
} catch (e) {// do nothing
}
function setupDOM(handler) {
if (!handler.ptrElement) {
var ptr = document.createElement('div');
if (handler.mainElement !== document.body) {
handler.mainElement.parentNode.insertBefore(ptr, handler.mainElement);
} else {
document.body.insertBefore(ptr, document.body.firstChild);
}
ptr.classList.add(((handler.classPrefix) + "ptr"));
ptr.innerHTML = handler.getMarkup().replace(/__PREFIX__/g, handler.classPrefix);
handler.ptrElement = ptr;
if (typeof handler.onInit === 'function') {
handler.onInit(handler);
} // Add the css styles to the style node, and then
// insert it into the dom
if (!_shared.styleEl) {
_shared.styleEl = document.createElement('style');
_shared.styleEl.setAttribute('id', 'pull-to-refresh-js-style');
document.head.appendChild(_shared.styleEl);
}
_shared.styleEl.textContent = handler.getStyles().replace(/__PREFIX__/g, handler.classPrefix).replace(/\s+/g, ' ');
}
return handler;
}
function onReset(handler) {
if (!handler.ptrElement) { return; }
handler.ptrElement.classList.remove(((handler.classPrefix) + "refresh"));
handler.ptrElement.style[handler.cssProp] = '0px';
setTimeout(function () {
// remove previous ptr-element from DOM
if (handler.ptrElement && handler.ptrElement.parentNode) {
handler.ptrElement.parentNode.removeChild(handler.ptrElement);
handler.ptrElement = null;
} // reset state
_shared.state = 'pending';
}, handler.refreshTimeout);
}
function update(handler) {
var iconEl = handler.ptrElement.querySelector(("." + (handler.classPrefix) + "icon"));
var textEl = handler.ptrElement.querySelector(("." + (handler.classPrefix) + "text"));
if (iconEl) {
if (_shared.state === 'refreshing') {
iconEl.innerHTML = handler.iconRefreshing;
} else {
iconEl.innerHTML = handler.iconArrow;
}
}
if (textEl) {
if (_shared.state === 'releasing') {
textEl.innerHTML = handler.instructionsReleaseToRefresh;
}
if (_shared.state === 'pulling' || _shared.state === 'pending') {
textEl.innerHTML = handler.instructionsPullToRefresh;
}
if (_shared.state === 'refreshing') {
textEl.innerHTML = handler.instructionsRefreshing;
}
}
}
var _ptr = {
setupDOM: setupDOM,
onReset: onReset,
update: update
};
var _timeout;
var screenY = function screenY(event) {
if (_shared.pointerEventsEnabled && _shared.supportsPointerEvents) {
return event.screenY;
}
return event.touches[0].screenY;
};
var _setupEvents = (function () {
var _el;
function _onTouchStart(e) {
// here, we must pick a handler first, and then append their html/css on the DOM
var target = _shared.handlers.filter(function (h) { return h.contains(e.target); })[0];
_shared.enable = !!target;
if (target && _shared.state === 'pending') {
_el = _ptr.setupDOM(target);
if (target.shouldPullToRefresh()) {
_shared.pullStartY = screenY(e);
}
clearTimeout(_shared.timeout);
_ptr.update(target);
}
}
function _onTouchMove(e) {
if (!(_el && _el.ptrElement && _shared.enable)) {
return;
}
if (!_shared.pullStartY) {
if (_el.shouldPullToRefresh()) {
_shared.pullStartY = screenY(e);
}
} else {
_shared.pullMoveY = screenY(e);
}
if (_shared.state === 'refreshing') {
if (e.cancelable && _el.shouldPullToRefresh() && _shared.pullStartY < _shared.pullMoveY) {
e.preventDefault();
}
return;
}
if (_shared.state === 'pending') {
_el.ptrElement.classList.add(((_el.classPrefix) + "pull"));
_shared.state = 'pulling';
_ptr.update(_el);
}
if (_shared.pullStartY && _shared.pullMoveY) {
_shared.dist = _shared.pullMoveY - _shared.pullStartY;
}
_shared.distExtra = _shared.dist - _el.distIgnore;
if (_shared.distExtra > 0) {
if (e.cancelable) {
e.preventDefault();
}
_el.ptrElement.style[_el.cssProp] = (_shared.distResisted) + "px";
_shared.distResisted = _el.resistanceFunction(_shared.distExtra / _el.distThreshold) * Math.min(_el.distMax, _shared.distExtra);
if (_shared.state === 'pulling' && _shared.distResisted > _el.distThreshold) {
_el.ptrElement.classList.add(((_el.classPrefix) + "release"));
_shared.state = 'releasing';
_ptr.update(_el);
}
if (_shared.state === 'releasing' && _shared.distResisted < _el.distThreshold) {
_el.ptrElement.classList.remove(((_el.classPrefix) + "release"));
_shared.state = 'pulling';
_ptr.update(_el);
}
}
}
function _onTouchEnd() {
if (!(_el && _el.ptrElement && _shared.enable)) {
return;
} // wait 1/2 sec before unmounting...
clearTimeout(_timeout);
_timeout = setTimeout(function () {
if (_el && _el.ptrElement && _shared.state === 'pending') {
_ptr.onReset(_el);
}
}, 500);
if (_shared.state === 'releasing' && _shared.distResisted > _el.distThreshold) {
_shared.state = 'refreshing';
_el.ptrElement.style[_el.cssProp] = (_el.distReload) + "px";
_el.ptrElement.classList.add(((_el.classPrefix) + "refresh"));
_shared.timeout = setTimeout(function () {
var retval = _el.onRefresh(function () { return _ptr.onReset(_el); });
if (retval && typeof retval.then === 'function') {
retval.then(function () { return _ptr.onReset(_el); });
}
if (!retval && !_el.onRefresh.length) {
_ptr.onReset(_el);
}
}, _el.refreshTimeout);
} else {
if (_shared.state === 'refreshing') {
return;
}
_el.ptrElement.style[_el.cssProp] = '0px';
_shared.state = 'pending';
}
_ptr.update(_el);
_el.ptrElement.classList.remove(((_el.classPrefix) + "release"));
_el.ptrElement.classList.remove(((_el.classPrefix) + "pull"));
_shared.pullStartY = _shared.pullMoveY = null;
_shared.dist = _shared.distResisted = 0;
}
function _onScroll() {
if (_el) {
_el.mainElement.classList.toggle(((_el.classPrefix) + "top"), _el.shouldPullToRefresh());
}
}
var _passiveSettings = _shared.supportsPassive ? {
passive: _shared.passive || false
} : undefined;
if (_shared.pointerEventsEnabled && _shared.supportsPointerEvents) {
window.addEventListener('pointerup', _onTouchEnd);
window.addEventListener('pointerdown', _onTouchStart);
window.addEventListener('pointermove', _onTouchMove, _passiveSettings);
} else {
window.addEventListener('touchend', _onTouchEnd);
window.addEventListener('touchstart', _onTouchStart);
window.addEventListener('touchmove', _onTouchMove, _passiveSettings);
}
window.addEventListener('scroll', _onScroll);
return {
onTouchEnd: _onTouchEnd,
onTouchStart: _onTouchStart,
onTouchMove: _onTouchMove,
onScroll: _onScroll,
destroy: function destroy() {
if (_shared.pointerEventsEnabled && _shared.supportsPointerEvents) {
window.removeEventListener('pointerdown', _onTouchStart);
window.removeEventListener('pointerup', _onTouchEnd);
window.removeEventListener('pointermove', _onTouchMove, _passiveSettings);
} else {
window.removeEventListener('touchstart', _onTouchStart);
window.removeEventListener('touchend', _onTouchEnd);
window.removeEventListener('touchmove', _onTouchMove, _passiveSettings);
}
window.removeEventListener('scroll', _onScroll);
}
};
});
var _ptrMarkup = "\n<div class=\"__PREFIX__box\">\n <div class=\"__PREFIX__content\">\n <div class=\"__PREFIX__icon\"></div>\n <div class=\"__PREFIX__text\"></div>\n </div>\n</div>\n";
var _ptrStyles = "\n.__PREFIX__ptr {\n box-shadow: inset 0 -3px 5px rgba(0, 0, 0, 0.12);\n pointer-events: none;\n font-size: 0.85em;\n font-weight: bold;\n top: 0;\n height: 0;\n transition: height 0.3s, min-height 0.3s;\n text-align: center;\n width: 100%;\n overflow: hidden;\n display: flex;\n align-items: flex-end;\n align-content: stretch;\n}\n\n.__PREFIX__box {\n padding: 10px;\n flex-basis: 100%;\n}\n\n.__PREFIX__pull {\n transition: none;\n}\n\n.__PREFIX__text {\n margin-top: .33em;\n color: rgba(0, 0, 0, 0.3);\n}\n\n.__PREFIX__icon {\n color: rgba(0, 0, 0, 0.3);\n transition: transform .3s;\n}\n\n/*\nWhen at the top of the page, disable vertical overscroll so passive touch\nlisteners can take over.\n*/\n.__PREFIX__top {\n touch-action: pan-x pan-down pinch-zoom;\n}\n\n.__PREFIX__release .__PREFIX__icon {\n transform: rotate(180deg);\n}\n";
var _defaults = {
distThreshold: 60,
distMax: 80,
distReload: 50,
distIgnore: 0,
mainElement: 'body',
triggerElement: 'body',
ptrElement: '.ptr',
classPrefix: 'ptr--',
cssProp: 'min-height',
iconArrow: '⇣',
iconRefreshing: '…',
instructionsPullToRefresh: 'Pull down to refresh',
instructionsReleaseToRefresh: 'Release to refresh',
instructionsRefreshing: 'Refreshing',
refreshTimeout: 500,
getMarkup: function () { return _ptrMarkup; },
getStyles: function () { return _ptrStyles; },
onInit: function () {},
onRefresh: function () { return location.reload(); },
resistanceFunction: function (t) { return Math.min(1, t / 2.5); },
shouldPullToRefresh: function () { return !window.scrollY; }
};
var _methods = ['mainElement', 'ptrElement', 'triggerElement'];
var _setupHandler = (function (options) {
var _handler = {}; // merge options with defaults
Object.keys(_defaults).forEach(function (key) {
_handler[key] = options[key] || _defaults[key];
}); // normalize timeout value, even if it is zero
_handler.refreshTimeout = typeof options.refreshTimeout === 'number' ? options.refreshTimeout : _defaults.refreshTimeout; // normalize elements
_methods.forEach(function (method) {
if (typeof _handler[method] === 'string') {
_handler[method] = document.querySelector(_handler[method]);
}
}); // attach events lazily
if (!_shared.events) {
_shared.events = _setupEvents();
}
_handler.contains = function (target) {
return _handler.triggerElement.contains(target);
};
_handler.destroy = function () {
// stop pending any pending callbacks
clearTimeout(_shared.timeout); // remove handler from shared state
var offset = _shared.handlers.indexOf(_handler);
_shared.handlers.splice(offset, 1);
};
return _handler;
});
var index = {
setPassiveMode: function setPassiveMode(isPassive) {
_shared.passive = isPassive;
},
setPointerEventsMode: function setPointerEventsMode(isEnabled) {
_shared.pointerEventsEnabled = isEnabled;
},
destroyAll: function destroyAll() {
if (_shared.events) {
_shared.events.destroy();
_shared.events = null;
}
_shared.handlers.forEach(function (h) {
h.destroy();
});
},
init: function init(options) {
if ( options === void 0 ) options = {};
var handler = _setupHandler(options);
_shared.handlers.push(handler);
return handler;
},
// export utils for testing
_: {
setupHandler: _setupHandler,
setupEvents: _setupEvents,
setupDOM: _ptr.setupDOM,
onReset: _ptr.onReset,
update: _ptr.update
}
};
return index;
}));