-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpull-element.js
499 lines (442 loc) · 11.9 KB
/
pull-element.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
/*!
* @license
* pull-element.js v1.1.4
* (c) 2017 Jade Gu
* Released under the MIT License.
* https://github.com/Lucifier129/pull-element
*/
;(function(global, factory) {
typeof exports === 'object' && typeof module !== 'undefined'
? (module.exports = factory())
: typeof define === 'function' && define.amd
? define(factory)
: (global.PullElement = factory())
})(this, function() {
'use strict'
function extend(target) {
var args = arguments
var argsLen = args.length
for (var i = 1; i < argsLen; i++) {
var source = args[i]
for (var key in source) {
target[key] = source[key]
}
}
return target
}
function isNumber(obj) {
return typeof obj === 'number' && !isNaN(obj)
}
function isFunction(obj) {
return typeof obj === 'function'
}
function getScrollEndingInfo(scroller, isGlobalScroller) {
var scrollTop, scrollLeft, offsetWidth, offsetHeight
var scrollWidth = scroller.scrollWidth
var scrollHeight = scroller.scrollHeight
if (isGlobalScroller) {
var documentElement = document.documentElement
var documentBody = document.body
offsetWidth = documentElement.clientWidth
offsetHeight = documentElement.clientHeight
scrollTop = documentBody.scrollTop || documentElement.scrollTop
scrollLeft = documentBody.scrollLeft || documentElement.scrollLeft
} else {
offsetWidth = scroller.offsetWidth
offsetHeight = scroller.offsetHeight
scrollTop = scroller.scrollTop
scrollLeft = scroller.scrollLeft
}
return {
isScrollTopEnd: scrollTop <= 0,
isScrollBottomEnd: offsetHeight + scrollTop >= scrollHeight,
isScrollLeftEnd: scrollLeft <= 0,
isScrollRightEnd: offsetWidth + scrollLeft >= scrollWidth
}
}
function getElem(elem) {
return typeof elem === 'string' ? document.querySelector(elem) : elem
}
function getElems(elems) {
if (typeof elems === 'string') {
return Array.prototype.slice.call(document.querySelectorAll(elems))
} else if (elems.length) {
return Array.prototype.slice.call(elems)
} else {
return [elems]
}
}
function addEvent(elem, type, handler, options) {
elem.addEventListener(type, handler, options)
}
function removeEvent(elem, type, handler) {
elem.removeEventListener(type, handler)
}
function getCoor(event) {
var targetEvent = event.touches[0]
return {
x: targetEvent.clientX,
y: targetEvent.clientY
}
}
function transformValueByDamping(value, damping) {
if (isFunction(damping)) {
return damping(value)
}
if (isNumber(damping)) {
return value / damping
}
return value
}
function getTranslateStyle(translateX, translateY) {
var translateValue =
'translate(' + translateX + 'px,' + translateY + 'px) translateZ(0)'
return {
transform: translateValue,
webkitTransform: translateValue
}
}
// https://developers.google.com/web/updates/2017/01/scrolling-intervention
var eventHandlerOptions = {
passive: false
}
var emptyStyle = {
transition: '',
transform: '',
webkitTransform: '',
webkitTransition: ''
}
var emptyTransitionStyle = {
transition: '',
webkitTransition: ''
}
var eventMap = {
pullDown: 'onPullDown',
pullUp: 'onPullUp',
pullRight: 'onPullRight',
pullLeft: 'onPullLeft'
}
var propMap = {
pullDown: 'isScrollTopEnd',
pullUp: 'isScrollBottomEnd',
pullRight: 'isScrollLeftEnd',
pullLeft: 'isScrollRightEnd'
}
var defaultOffsetState = {
action: '',
axis: '',
translateX: 0,
translateY: 0
}
var defaultState = extend(
{
isScrollTopEnd: true,
isScrollLeftEnd: true,
isScrollBottomEnd: true,
isScrollRightEnd: true,
clientX: 0,
clientY: 0
},
defaultOffsetState
)
var defaultOptions = {
target: 'body',
scroller: '',
trigger: '',
damping: 1.6,
wait: true,
pullUp: false,
pullDown: false,
pullLeft: false,
pullRight: false,
detectScroll: false,
detectScrollOnStart: false,
detectBoundary: false,
stopPropagation: false,
drag: false,
transitionProperty: 'transform',
transitionDuration: '0.3s',
transitionTimingFunction: 'ease-out'
}
var isSupportPromise = typeof Promise === 'function'
function PullElement(options) {
this.options = extend({}, defaultOptions, options)
this.state = extend({}, defaultState)
this.target = null
this.scroller = null
this.trigger = null
this.transitionStyle = null
this.isTouching = false
this.isPreventDefault = false
this.isWaiting = false
this.isGlobalScroller = false
this.transitionDuration = 0
this.handleTouchStart = this.handleTouchStart.bind(this)
this.handleTouchMove = this.handleTouchMove.bind(this)
this.handleTouchEnd = this.handleTouchEnd.bind(this)
}
extend(PullElement.prototype, {
init: function() {
var options = this.options
var target = getElem(options.target)
var scroller = options.scroller ? getElem(options.scroller) : target
var trigger = options.trigger ? getElems(options.trigger) : [target]
this.target = target
this.scroller = scroller
this.trigger = trigger
this.isGlobalScroller =
scroller === document.body ||
scroller === window ||
scroller === document.documentElement
this.transitionStyle = {
transitionProperty: options.transitionProperty,
transitionDuration: options.transitionDuration,
transitionTimingFunction: options.transitionTimingFunction,
webkitTransitionProperty: options.transitionProperty,
webkitTransitionDuration: options.transitionDuration,
webkitTransitionTimingFunction: options.transitionTimingFunction
}
/**
* in some browser, transitionend dose'nt work as expected
* use setTimeout instead
*/
var transitionDuration = Number(
options.transitionDuration.replace(/[^.\d]+/g, '')
)
// transform 1s to 1000ms
if (/[\d\.]+s$/.test(options.transitionDuration)) {
transitionDuration = transitionDuration * 1000
}
this.transitionDuration = transitionDuration
this.enable()
},
destroy: function() {
this.disable()
},
setTranslate: function(translateX, translateY) {
var state = this.state
state.translateX = translateX
state.translateY = translateY
extend(
this.target.style,
emptyTransitionStyle,
getTranslateStyle(translateX, translateY)
)
},
animateTo: function(translateX, translateY, callback) {
var state = this.state
var target = this.target
var transitionDuration = this.transitionDuration
var transitionStyle = this.transitionStyle
var translateStyle = getTranslateStyle(translateX, translateY)
state.translateX = translateX
state.translateY = translateY
var createTransitionEndHandler = function(callback) {
extend(target.style, transitionStyle, translateStyle)
setTimeout(callback, transitionDuration)
}
if (isSupportPromise) {
return new Promise(createTransitionEndHandler).then(callback)
}
createTransitionEndHandler(callback)
},
animateToOrigin: function(callback) {
var context = this
var finalCallback = function() {
extend(context.target.style, emptyStyle)
extend(context.state, defaultOffsetState)
context.isWaiting = false
callback && callback()
}
return this.animateTo(0, 0, finalCallback)
},
enable: function() {
for (var i = 0; i < this.trigger.length; i++) {
addEvent(this.trigger[i], 'touchstart', this.handleTouchStart)
}
addEvent(document, 'touchmove', this.handleTouchMove, eventHandlerOptions)
addEvent(document, 'touchend', this.handleTouchEnd)
addEvent(document, 'touchcancel', this.handleTouchEnd)
},
disable: function() {
for (var i = 0; i < this.trigger.length; i++) {
removeEvent(this.trigger[i], 'touchstart', this.handleTouchStart)
}
removeEvent(
document,
'touchmove',
this.handleTouchMove,
eventHandlerOptions
)
removeEvent(document, 'touchend', this.handleTouchEnd)
removeEvent(document, 'touchcancel', this.handleTouchEnd)
},
preventDefault: function() {
this.isPreventDefault = true
},
getScrollInfo: function() {
return getScrollEndingInfo(this.scroller, this.isGlobalScroller)
},
isActiveAction: function(action) {
var options = this.options
var eventName = eventMap[action]
return options[eventName] || options[eventName + 'End'] || options[action]
},
emit: function(eventName, data) {
var listener = this.options[eventName]
if (isFunction(listener)) {
listener.call(this, data)
}
},
detectBoundary: function(action, deltaX, deltaY, translateX, translateY) {
var isBoundary = false
if (action === 'pullDown') {
if (translateY <= 0 && deltaY <= 0) {
isBoundary = true
}
} else if (action === 'pullUp') {
if (translateY >= 0 && deltaY >= 0) {
isBoundary = true
}
} else if (action == 'pullLeft') {
if (translateX >= 0 && deltaX >= 0) {
isBoundary = true
}
} else if (action === 'pullRight') {
if (translateX <= 0 && deltaX <= 0) {
isBoundary = true
}
}
if (isBoundary) {
this.handleTouchEnd()
return true
}
return false
},
handleTouchStart: function(event) {
if (this.isTouching || this.isWaiting) {
return
}
var options = this.options
var coor = getCoor(event)
extend(this.state, {
clientX: coor.x,
clientY: coor.y,
axis: '',
action: ''
})
if (options.detectScroll || options.detectScrollOnStart) {
extend(this.state, this.getScrollInfo())
}
if (options.stopPropagation) {
event.stopPropagation()
}
this.isTouching = true
},
handleTouchMove: function(event) {
if (!this.isTouching) {
return
}
var coor = getCoor(event)
var options = this.options
var state = this.state
var clientX = coor.x
var clientY = coor.y
var deltaX = clientX - state.clientX
var deltaY = clientY - state.clientY
var translateX = state.translateX
var translateY = state.translateY
var axis = state.axis
var action = state.action
// only check the axis once
if (!axis) {
axis = Math.abs(deltaY) >= Math.abs(deltaX) ? 'y' : 'x'
}
// only check the action once
if (!action) {
if (axis === 'y') {
if (deltaY > 0) {
action = 'pullDown'
} else if (deltaY < 0) {
action = 'pullUp'
}
} else if (axis === 'x') {
if (deltaX > 0) {
action = 'pullRight'
} else if (deltaX < 0) {
action = 'pullLeft'
}
}
}
if (options.detectBoundary) {
var isBoundary = this.detectBoundary(action, deltaX, deltaY, translateX, translateY)
if (isBoundary) return
}
var isActiveAction = this.isActiveAction(action)
if (isActiveAction && options.detectScroll && !state[propMap[action]]) {
extend(state, this.getScrollInfo())
if (state[propMap[action]]) {
deltaX = 0
deltaY = 0
}
}
var isActiveAndEnding = isActiveAction && state[propMap[action]]
if (isActiveAndEnding) {
translateX += transformValueByDamping(deltaX, options.damping)
translateY += transformValueByDamping(deltaY, options.damping)
}
extend(state, {
clientX: clientX,
clientY: clientY,
translateX: translateX,
translateY: translateY,
action: action,
axis: axis
})
if (!isActiveAndEnding) {
return
}
if (!options.drag) {
if (axis === 'y') {
translateX = 0
} else if (axis === 'x') {
translateY = 0
}
}
this.emit(eventMap[action], {
translateX: translateX,
translateY: translateY
})
if (this.isPreventDefault) {
this.isPreventDefault = false
return
}
if (options.wait) {
this.isWaiting = true
}
event.preventDefault()
this.setTranslate(translateX, translateY)
},
handleTouchEnd: function() {
if (!this.isTouching) {
return
}
this.isTouching = false
var state = this.state
var action = state.action
if (!this.isActiveAction(action) || !state[propMap[action]]) {
return
}
this.emit(eventMap[action] + 'End', {
translateX: state.translateX,
translateY: state.translateY
})
if (this.isPreventDefault) {
this.isPreventDefault = false
return
}
this.animateToOrigin()
}
})
return PullElement
})