forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQResizeObservable.js
More file actions
100 lines (89 loc) · 2.27 KB
/
QResizeObservable.js
File metadata and controls
100 lines (89 loc) · 2.27 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
import { listenOpts } from '../../utils/event'
export default {
name: 'QResizeObservable',
props: {
debounce: {
type: Number,
default: 100
}
},
data () {
return this.hasObserver
? {}
: { url: this.$q.platform.is.ie ? null : 'about:blank' }
},
methods: {
onResize () {
this.timer = null
if (!this.$el || !this.$el.parentNode) {
return
}
const
parent = this.$el.parentNode,
size = {
width: parent.offsetWidth,
height: parent.offsetHeight
}
if (size.width === this.size.width && size.height === this.size.height) {
return
}
this.size = size
this.$emit('resize', this.size)
},
trigger (immediately) {
if (immediately || this.debounce === 0) {
this.onResize()
}
else if (!this.timer) {
this.timer = setTimeout(this.onResize, this.debounce)
}
}
},
render (h) {
if (this.hasObserver) {
return
}
return h('object', {
style: this.style,
attrs: {
type: 'text/html',
data: this.url,
'aria-hidden': true
},
on: {
load: () => {
this.$el.contentDocument.defaultView.addEventListener('resize', this.trigger, listenOpts.passive)
this.trigger(true)
}
}
})
},
beforeCreate () {
this.size = { width: -1, height: -1 }
this.hasObserver = typeof ResizeObserver !== 'undefined'
if (!this.hasObserver) {
this.style = `${this.$q.platform.is.ie ? 'visibility:hidden;' : ''}display:block;position:absolute;top:0;left:0;right:0;bottom:0;height:100%;width:100%;overflow:hidden;pointer-events:none;z-index:-1;`
}
},
mounted () {
if (this.hasObserver) {
this.observer = new ResizeObserver(this.trigger)
this.observer.observe(this.$el.parentNode)
return
}
this.trigger(true)
if (this.$q.platform.is.ie) {
this.url = 'about:blank'
}
},
beforeDestroy () {
clearTimeout(this.timer)
if (this.hasObserver) {
this.observer.unobserve(this.$el.parentNode)
return
}
if (this.$el.contentDocument) {
this.$el.contentDocument.defaultView.removeEventListener('resize', this.trigger, listenOpts.passive)
}
}
}