init.js
13.2 KB
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
import {
hasPerspective,
hasTransition,
hasTransform,
hasTouch,
style,
offset,
addEvent,
removeEvent,
getRect,
preventDefaultException
} from '../util/dom'
import { extend, isUndef } from '../util/lang'
const DEFAULT_OPTIONS = {
startX: 0,
startY: 0,
scrollX: false,
scrollY: true,
freeScroll: false,
directionLockThreshold: 5,
eventPassthrough: '',
click: false,
tap: false,
/**
* support any side
* bounce: {
* top: true,
* bottom: true,
* left: true,
* right: true
* }
*/
bounce: true,
bounceTime: 800,
momentum: true,
momentumLimitTime: 300,
momentumLimitDistance: 15,
swipeTime: 2500,
swipeBounceTime: 500,
deceleration: 0.0015,
flickLimitTime: 200,
flickLimitDistance: 100,
resizePolling: 60,
probeType: 0,
preventDefault: true,
preventDefaultException: {
tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT|AUDIO)$/
},
HWCompositing: true,
useTransition: true,
useTransform: true,
bindToWrapper: false,
disableMouse: hasTouch,
disableTouch: !hasTouch,
observeDOM: true,
autoBlur: true,
/**
* for picker
* wheel: {
* selectedIndex: 0,
* rotate: 25,
* adjustTime: 400
* wheelWrapperClass: 'wheel-scroll',
* wheelItemClass: 'wheel-item'
* }
*/
wheel: false,
/**
* for slide
* snap: {
* loop: false,
* el: domEl,
* threshold: 0.1,
* stepX: 100,
* stepY: 100,
* speed: 400,
* easing: {
* style: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)',
* fn: function (t) {
* return t * (2 - t)
* }
* }
* listenFlick: true
* }
*/
snap: false,
/**
* for scrollbar
* scrollbar: {
* fade: true,
* interactive: false
* }
*/
scrollbar: false,
/**
* for pull down and refresh
* pullDownRefresh: {
* threshold: 50,
* stop: 20
* }
*/
pullDownRefresh: false,
/**
* for pull up and load
* pullUpLoad: {
* threshold: 50
* }
*/
pullUpLoad: false,
/**
* for mouse wheel
* mouseWheel: {
* speed: 20,
* invert: false,
* easeTime: 300
* }
*/
mouseWheel: false,
stopPropagation: false,
/**
* for zoom
* zoom: {
* start: 1,
* min: 1,
* max: 4
* }
*/
zoom: false,
/**
* for infinity
* infinity: {
* render(item, div) {
* },
* createTombstone() {
* },
* fetch(count) {
* }
* }
*/
infinity: false,
/**
* for double click
* dblclick: {
* delay: 300
* }
*/
dblclick: false
}
export function initMixin (BScroll) {
BScroll.prototype._init = function (options) {
this._handleOptions(options)
// init private custom events
this._events = {}
this.x = 0
this.y = 0
this.directionX = 0
this.directionY = 0
this.setScale(1)
this._addDOMEvents()
this._initExtFeatures()
this._watchTransition()
if (this.options.observeDOM) {
this._initDOMObserver()
}
if (this.options.autoBlur) {
this._handleAutoBlur()
}
this.refresh()
if (!this.options.snap) {
this.scrollTo(this.options.startX, this.options.startY)
}
this.enable()
}
BScroll.prototype.setScale = function (scale) {
this.lastScale = isUndef(this.scale) ? scale : this.scale
this.scale = scale
}
BScroll.prototype._handleOptions = function (options) {
this.options = extend({}, DEFAULT_OPTIONS, options)
this.translateZ = this.options.HWCompositing && hasPerspective ? ' translateZ(0)' : ''
this.options.useTransition = this.options.useTransition && hasTransition
this.options.useTransform = this.options.useTransform && hasTransform
this.options.preventDefault = !this.options.eventPassthrough && this.options.preventDefault
// If you want eventPassthrough I have to lock one of the axes
this.options.scrollX = this.options.eventPassthrough === 'horizontal' ? false : this.options.scrollX
this.options.scrollY = this.options.eventPassthrough === 'vertical' ? false : this.options.scrollY
// With eventPassthrough we also need lockDirection mechanism
this.options.freeScroll = this.options.freeScroll && !this.options.eventPassthrough
this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold
if (this.options.tap === true) {
this.options.tap = 'tap'
}
}
BScroll.prototype._addDOMEvents = function () {
let eventOperation = addEvent
this._handleDOMEvents(eventOperation)
}
BScroll.prototype._removeDOMEvents = function () {
let eventOperation = removeEvent
this._handleDOMEvents(eventOperation)
}
BScroll.prototype._handleDOMEvents = function (eventOperation) {
let target = this.options.bindToWrapper ? this.wrapper : window
eventOperation(window, 'orientationchange', this)
eventOperation(window, 'resize', this)
if (this.options.click) {
eventOperation(this.wrapper, 'click', this, true)
}
if (!this.options.disableMouse) {
eventOperation(this.wrapper, 'mousedown', this)
eventOperation(target, 'mousemove', this)
eventOperation(target, 'mousecancel', this)
eventOperation(target, 'mouseup', this)
}
if (hasTouch && !this.options.disableTouch) {
eventOperation(this.wrapper, 'touchstart', this)
eventOperation(target, 'touchmove', this)
eventOperation(target, 'touchcancel', this)
eventOperation(target, 'touchend', this)
}
eventOperation(this.scroller, style.transitionEnd, this)
}
BScroll.prototype._initExtFeatures = function () {
if (this.options.snap) {
this._initSnap()
}
if (this.options.scrollbar) {
this._initScrollbar()
}
if (this.options.pullUpLoad) {
this._initPullUp()
}
if (this.options.pullDownRefresh) {
this._initPullDown()
}
if (this.options.wheel) {
this._initWheel()
}
if (this.options.mouseWheel) {
this._initMouseWheel()
}
if (this.options.zoom) {
this._initZoom()
}
if (this.options.infinity) {
this._initInfinite()
}
}
BScroll.prototype._watchTransition = function () {
if (typeof Object.defineProperty !== 'function') {
return
}
let me = this
let isInTransition = false
let key = this.options.useTransition ? 'isInTransition' : 'isAnimating'
Object.defineProperty(this, key, {
get () {
return isInTransition
},
set (newVal) {
isInTransition = newVal
// fix issue #359
let el = me.scroller.children.length ? me.scroller.children : [me.scroller]
let pointerEvents = (isInTransition && !me.pulling) ? 'none' : 'auto'
for (let i = 0; i < el.length; i++) {
el[i].style.pointerEvents = pointerEvents
}
}
})
}
BScroll.prototype._handleAutoBlur = function () {
this.on('scrollStart', () => {
let activeElement = document.activeElement
if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) {
activeElement.blur()
}
})
}
BScroll.prototype._initDOMObserver = function () {
if (typeof MutationObserver !== 'undefined') {
let timer
let observer = new MutationObserver((mutations) => {
// don't do any refresh during the transition, or outside of the boundaries
if (this._shouldNotRefresh()) {
return
}
let immediateRefresh = false
let deferredRefresh = false
for (let i = 0; i < mutations.length; i++) {
const mutation = mutations[i]
if (mutation.type !== 'attributes') {
immediateRefresh = true
break
} else {
if (mutation.target !== this.scroller) {
deferredRefresh = true
break
}
}
}
if (immediateRefresh) {
this.refresh()
} else if (deferredRefresh) {
// attributes changes too often
clearTimeout(timer)
timer = setTimeout(() => {
if (!this._shouldNotRefresh()) {
this.refresh()
}
}, 60)
}
})
const config = {
attributes: true,
childList: true,
subtree: true
}
observer.observe(this.scroller, config)
this.on('destroy', () => {
observer.disconnect()
})
} else {
this._checkDOMUpdate()
}
}
BScroll.prototype._shouldNotRefresh = function () {
let outsideBoundaries = this.x > this.minScrollX || this.x < this.maxScrollX || this.y > this.minScrollY || this.y < this.maxScrollY
return this.isInTransition || this.stopFromTransition || outsideBoundaries
}
BScroll.prototype._checkDOMUpdate = function () {
let scrollerRect = getRect(this.scroller)
let oldWidth = scrollerRect.width
let oldHeight = scrollerRect.height
function check () {
if (this.destroyed) {
return
}
scrollerRect = getRect(this.scroller)
let newWidth = scrollerRect.width
let newHeight = scrollerRect.height
if (oldWidth !== newWidth || oldHeight !== newHeight) {
this.refresh()
}
oldWidth = newWidth
oldHeight = newHeight
next.call(this)
}
function next () {
setTimeout(() => {
check.call(this)
}, 1000)
}
next.call(this)
}
BScroll.prototype.handleEvent = function (e) {
switch (e.type) {
case 'touchstart':
case 'mousedown':
this._start(e)
if (this.options.zoom && e.touches && e.touches.length > 1) {
this._zoomStart(e)
}
break
case 'touchmove':
case 'mousemove':
if (this.options.zoom && e.touches && e.touches.length > 1) {
this._zoom(e)
} else {
this._move(e)
}
break
case 'touchend':
case 'mouseup':
case 'touchcancel':
case 'mousecancel':
if (this.scaled) {
this._zoomEnd(e)
} else {
this._end(e)
}
break
case 'orientationchange':
case 'resize':
this._resize()
break
case 'transitionend':
case 'webkitTransitionEnd':
case 'oTransitionEnd':
case 'MSTransitionEnd':
this._transitionEnd(e)
break
case 'click':
if (this.enabled && !e._constructed) {
if (!preventDefaultException(e.target, this.options.preventDefaultException)) {
e.preventDefault()
e.stopPropagation()
}
}
break
case 'wheel':
case 'DOMMouseScroll':
case 'mousewheel':
this._onMouseWheel(e)
break
}
}
BScroll.prototype.refresh = function () {
const isWrapperStatic = window.getComputedStyle(this.wrapper, null).position === 'static'
let wrapperRect = getRect(this.wrapper)
this.wrapperWidth = wrapperRect.width
this.wrapperHeight = wrapperRect.height
let scrollerRect = getRect(this.scroller)
this.scrollerWidth = Math.round(scrollerRect.width * this.scale)
this.scrollerHeight = Math.round(scrollerRect.height * this.scale)
this.relativeX = scrollerRect.left
this.relativeY = scrollerRect.top
if (isWrapperStatic) {
this.relativeX -= wrapperRect.left
this.relativeY -= wrapperRect.top
}
this.minScrollX = 0
this.minScrollY = 0
const wheel = this.options.wheel
if (wheel) {
this.items = this.scroller.children
this.options.itemHeight = this.itemHeight = this.items.length ? this.scrollerHeight / this.items.length : 0
if (this.selectedIndex === undefined) {
this.selectedIndex = wheel.selectedIndex || 0
}
this.options.startY = -this.selectedIndex * this.itemHeight
this.maxScrollX = 0
this.maxScrollY = -this.itemHeight * (this.items.length - 1)
} else {
this.maxScrollX = this.wrapperWidth - this.scrollerWidth
if (!this.options.infinity) {
this.maxScrollY = this.wrapperHeight - this.scrollerHeight
}
if (this.maxScrollX < 0) {
this.maxScrollX -= this.relativeX
this.minScrollX = -this.relativeX
} else if (this.scale > 1) {
this.maxScrollX = (this.maxScrollX / 2 - this.relativeX)
this.minScrollX = this.maxScrollX
}
if (this.maxScrollY < 0) {
this.maxScrollY -= this.relativeY
this.minScrollY = -this.relativeY
} else if (this.scale > 1) {
this.maxScrollY = (this.maxScrollY / 2 - this.relativeY)
this.minScrollY = this.maxScrollY
}
}
this.hasHorizontalScroll = this.options.scrollX && this.maxScrollX < this.minScrollX
this.hasVerticalScroll = this.options.scrollY && this.maxScrollY < this.minScrollY
if (!this.hasHorizontalScroll) {
this.maxScrollX = this.minScrollX
this.scrollerWidth = this.wrapperWidth
}
if (!this.hasVerticalScroll) {
this.maxScrollY = this.minScrollY
this.scrollerHeight = this.wrapperHeight
}
this.endTime = 0
this.directionX = 0
this.directionY = 0
this.wrapperOffset = offset(this.wrapper)
this.trigger('refresh')
!this.scaled && this.resetPosition()
}
BScroll.prototype.enable = function () {
this.enabled = true
}
BScroll.prototype.disable = function () {
this.enabled = false
}
}