forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQScrollObserver.js
More file actions
140 lines (110 loc) · 3.3 KB
/
QScrollObserver.js
File metadata and controls
140 lines (110 loc) · 3.3 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
import { watch, onMounted, onBeforeUnmount, getCurrentInstance } from 'vue'
import { createComponent } from '../../utils/private/create.js'
import { getScrollTarget, getVerticalScrollPosition, getHorizontalScrollPosition } from '../../utils/scroll.js'
import { listenOpts, noop } from '../../utils/event.js'
const { passive } = listenOpts
const axisValues = [ 'both', 'horizontal', 'vertical' ]
export default createComponent({
name: 'QScrollObserver',
props: {
axis: {
type: String,
validator: v => axisValues.includes(v),
default: 'vertical'
},
debounce: [ String, Number ],
scrollTarget: {
default: void 0
}
},
emits: [ 'scroll' ],
setup (props, { emit }) {
const scroll = {
position: {
top: 0,
left: 0
},
direction: 'down',
directionChanged: false,
delta: {
top: 0,
left: 0
},
inflectionPoint: {
top: 0,
left: 0
}
}
let clearTimer = null, localScrollTarget, parentEl
watch(() => props.scrollTarget, () => {
unconfigureScrollTarget()
configureScrollTarget()
})
function emitEvent () {
clearTimer !== null && clearTimer()
const top = Math.max(0, getVerticalScrollPosition(localScrollTarget))
const left = getHorizontalScrollPosition(localScrollTarget)
const delta = {
top: top - scroll.position.top,
left: left - scroll.position.left
}
if (
(props.axis === 'vertical' && delta.top === 0)
|| (props.axis === 'horizontal' && delta.left === 0)
) {
return
}
const curDir = Math.abs(delta.top) >= Math.abs(delta.left)
? (delta.top < 0 ? 'up' : 'down')
: (delta.left < 0 ? 'left' : 'right')
scroll.position = { top, left }
scroll.directionChanged = scroll.direction !== curDir
scroll.delta = delta
if (scroll.directionChanged === true) {
scroll.direction = curDir
scroll.inflectionPoint = scroll.position
}
emit('scroll', { ...scroll })
}
function configureScrollTarget () {
localScrollTarget = getScrollTarget(parentEl, props.scrollTarget)
localScrollTarget.addEventListener('scroll', trigger, passive)
trigger(true)
}
function unconfigureScrollTarget () {
if (localScrollTarget !== void 0) {
localScrollTarget.removeEventListener('scroll', trigger, passive)
localScrollTarget = void 0
}
}
function trigger (immediately) {
if (immediately === true || props.debounce === 0 || props.debounce === '0') {
emitEvent()
}
else if (clearTimer === null) {
const [ timer, fn ] = props.debounce
? [ setTimeout(emitEvent, props.debounce), clearTimeout ]
: [ requestAnimationFrame(emitEvent), cancelAnimationFrame ]
clearTimer = () => {
fn(timer)
clearTimer = null
}
}
}
const vm = getCurrentInstance()
onMounted(() => {
parentEl = vm.proxy.$el.parentNode
configureScrollTarget()
})
onBeforeUnmount(() => {
clearTimer !== null && clearTimer()
unconfigureScrollTarget()
})
// expose public methods
Object.assign(vm.proxy, {
trigger,
getPosition: () => scroll
})
return noop
}
})