-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathuse-fullscreen.js
More file actions
130 lines (105 loc) · 2.69 KB
/
Copy pathuse-fullscreen.js
File metadata and controls
130 lines (105 loc) · 2.69 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
import {
ref,
watch,
onBeforeMount,
onMounted,
onBeforeUnmount,
getCurrentInstance
} from 'vue'
import { getRootElement, getRootTarget } from '../../utils/private.dom/root.js'
import History from '../../plugins/private.history/History.js'
import { vmHasRouter } from '../../utils/private.vm/vm.js'
let counter = 0
export const useFullscreenProps = {
fullscreen: Boolean,
noRouteFullscreenExit: Boolean
}
export const useFullscreenEmits = ['update:fullscreen', 'fullscreen']
export default function useFullscreen() {
const vm = getCurrentInstance()
const { props, emit, proxy } = vm
let historyEntry, fullscreenFillerNode, container
const inFullscreen = ref(false)
if (vmHasRouter(vm) === true) {
watch(
() => proxy.$route.fullPath,
() => {
if (props.noRouteFullscreenExit !== true) {
exitFullscreen()
}
}
)
}
watch(
() => props.fullscreen,
v => {
if (inFullscreen.value !== v) {
toggleFullscreen()
}
}
)
watch(inFullscreen, v => {
emit('update:fullscreen', v)
emit('fullscreen', v)
})
function toggleFullscreen() {
if (inFullscreen.value === true) {
exitFullscreen()
} else {
setFullscreen()
}
}
function setFullscreen() {
if (inFullscreen.value === true) return
inFullscreen.value = true
container = proxy.$el.parentNode
container.replaceChild(fullscreenFillerNode, proxy.$el)
let root = getRootElement()
root.appendChild(proxy.$el)
counter++
if (counter === 1) {
const target = getRootTarget()
if (target?.classList) target.classList.add('q-body--fullscreen-mixin')
}
historyEntry = {
handler: exitFullscreen
}
History.add(historyEntry)
}
function exitFullscreen() {
if (inFullscreen.value !== true) return
if (historyEntry !== void 0) {
History.remove(historyEntry)
historyEntry = void 0
}
container.replaceChild(proxy.$el, fullscreenFillerNode)
inFullscreen.value = false
counter = Math.max(0, counter - 1)
if (counter === 0) {
const target = getRootTarget()
if (target?.classList) target.classList.remove('q-body--fullscreen-mixin')
if (proxy.$el.scrollIntoView !== void 0) {
setTimeout(() => {
proxy.$el.scrollIntoView()
})
}
}
}
onBeforeMount(() => {
fullscreenFillerNode = document.createElement('span')
})
onMounted(() => {
if (props.fullscreen === true) setFullscreen()
})
onBeforeUnmount(exitFullscreen)
// expose public methods
Object.assign(proxy, {
toggleFullscreen,
setFullscreen,
exitFullscreen
})
return {
inFullscreen,
toggleFullscreen
}
}