forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQResizeObserver.js
More file actions
141 lines (113 loc) · 3.4 KB
/
QResizeObserver.js
File metadata and controls
141 lines (113 loc) · 3.4 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
import { h, onMounted, onBeforeUnmount, getCurrentInstance, nextTick } from 'vue'
import useCanRender from '../../composables/private/use-can-render.js'
import { createComponent } from '../../utils/private/create.js'
import { listenOpts, noop } from '../../utils/event.js'
const hasObserver = typeof ResizeObserver !== 'undefined'
const resizeProps = hasObserver === true
? {}
: {
style: 'display:block;position:absolute;top:0;left:0;right:0;bottom:0;height:100%;width:100%;overflow:hidden;pointer-events:none;z-index:-1;',
url: 'about:blank'
}
export default createComponent({
name: 'QResizeObserver',
props: {
debounce: {
type: [ String, Number ],
default: 100
}
},
emits: [ 'resize' ],
setup (props, { emit }) {
if (__QUASAR_SSR_SERVER__) { return noop }
let timer = null, targetEl, size = { width: -1, height: -1 }
function trigger (immediately) {
if (immediately === true || props.debounce === 0 || props.debounce === '0') {
emitEvent()
}
else if (timer === null) {
timer = setTimeout(emitEvent, props.debounce)
}
}
function emitEvent () {
clearTimeout(timer)
timer = null
if (targetEl) {
const { offsetWidth: width, offsetHeight: height } = targetEl
if (width !== size.width || height !== size.height) {
size = { width, height }
emit('resize', size)
}
}
}
const vm = getCurrentInstance()
// expose public methods
Object.assign(vm.proxy, { trigger })
if (hasObserver === true) {
let observer
onMounted(() => {
nextTick(() => {
targetEl = vm.proxy.$el.parentNode
if (targetEl) {
observer = new ResizeObserver(trigger)
observer.observe(targetEl)
emitEvent()
}
})
})
onBeforeUnmount(() => {
clearTimeout(timer)
if (observer !== void 0) {
if (observer.disconnect !== void 0) {
observer.disconnect()
}
else if (targetEl) { // FF for Android
observer.unobserve(targetEl)
}
}
})
return noop
}
else { // no observer, so fallback to old iframe method
const canRender = useCanRender()
let curDocView
function cleanup () {
clearTimeout(timer)
if (curDocView !== void 0) {
// iOS is fuzzy, need to check it first
if (curDocView.removeEventListener !== void 0) {
curDocView.removeEventListener('resize', trigger, listenOpts.passive)
}
curDocView = void 0
}
}
function onObjLoad () {
cleanup()
if (targetEl && targetEl.contentDocument) {
curDocView = targetEl.contentDocument.defaultView
curDocView.addEventListener('resize', trigger, listenOpts.passive)
emitEvent()
}
}
onMounted(() => {
nextTick(() => {
targetEl = vm.proxy.$el
targetEl && onObjLoad()
})
})
onBeforeUnmount(cleanup)
return () => {
if (canRender.value === true) {
return h('object', {
style: resizeProps.style,
tabindex: -1, // fix for Firefox
type: 'text/html',
data: resizeProps.url,
'aria-hidden': 'true',
onLoad: onObjLoad
})
}
}
}
}
})