forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-scroll.js
More file actions
121 lines (98 loc) · 2.91 KB
/
use-scroll.js
File metadata and controls
121 lines (98 loc) · 2.91 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
import { scroll } from 'quasar'
import { watch, onMounted, onBeforeUnmount } from 'vue'
import { useRouter } from 'vue-router'
const { setVerticalScrollPosition, getVerticalScrollPosition } = scroll
let preventTocUpdate = false
let scrollTimer
const scrollDuration = 500
function scrollPage (el, delay) {
const { top } = el.getBoundingClientRect()
const offset = Math.max(0, top + getVerticalScrollPosition(window) - 66)
clearTimeout(scrollTimer)
preventTocUpdate = true
setVerticalScrollPosition(window, offset, delay)
scrollTimer = setTimeout(() => {
preventTocUpdate = false
}, delay + 10)
}
export default function useScroll (scope, $route) {
const $router = useRouter()
preventTocUpdate = $route.hash.length > 1
watch(() => $route.fullPath, (newRoute, oldRoute) => {
setTimeout(() => {
scrollToCurrentAnchor(newRoute.path !== oldRoute.path)
})
})
function changeRouterHash (hash) {
if ($route.hash !== hash) {
$router.replace({ hash }).catch(() => {})
}
else {
scrollToCurrentAnchor()
}
}
function scrollTo (id) {
clearTimeout(scrollTimer)
const hashtag = '#' + id
if (scope.rightDrawerOnLayout.value !== true) {
scope.rightDrawerState.value = false
scrollTimer = setTimeout(() => {
changeRouterHash(hashtag)
}, 300)
}
else {
changeRouterHash(hashtag)
}
setTimeout(() => {
scope.setActiveToc(getVerticalScrollPosition(window))
}, scrollDuration + 50)
}
function onScroll ({ position }) {
if (
preventTocUpdate !== true &&
(scope.rightDrawerOnLayout.value === true || scope.rightDrawerState.value !== true) &&
document.qScrollPrevented !== true
) {
scope.setActiveToc(position.vertical)
}
}
function scrollToCurrentAnchor (immediate) {
const hash = window.location.hash
const el = hash.length > 1
? document.getElementById(hash.substring(1))
: null
if (el !== null) {
if (immediate === true) {
let anchorEl = el
while (anchorEl.parentElement !== null && anchorEl.parentElement.classList.contains('q-page') !== true) {
anchorEl = anchorEl.parentElement
}
document.body.classList.add('q-scroll--lock')
anchorEl.classList.add('q-scroll--anchor')
setTimeout(() => {
document.body.classList.remove('q-scroll--lock')
anchorEl && anchorEl.classList.remove('q-scroll--anchor')
}, 2000)
}
scrollPage(el, immediate === true ? 0 : scrollDuration)
}
else {
preventTocUpdate = false
scope.setActiveToc()
}
}
onMounted(() => {
// TODO: to be moved outside setTimeout once
// QLayout updates QPageContainer spacing on time
setTimeout(() => {
scrollToCurrentAnchor(true)
})
})
onBeforeUnmount(() => {
clearTimeout(scrollTimer)
})
Object.assign(scope, {
scrollTo,
onScroll
})
}