forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreen.js
More file actions
174 lines (146 loc) · 4.08 KB
/
Screen.js
File metadata and controls
174 lines (146 loc) · 4.08 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import Vue from 'vue'
import { isSSR, fromSSR, client } from './Platform.js'
import { listenOpts, noop } from '../utils/event.js'
import debounce from '../utils/debounce.js'
const SIZE_LIST = ['sm', 'md', 'lg', 'xl']
const { passive } = listenOpts
export default {
width: 0,
height: 0,
name: 'xs',
sizes: {
sm: 600,
md: 1024,
lg: 1440,
xl: 1920
},
lt: {
sm: true,
md: true,
lg: true,
xl: true
},
gt: {
xs: false,
sm: false,
md: false,
lg: false
},
xs: true,
sm: false,
md: false,
lg: false,
xl: false,
setSizes: noop,
setDebounce: noop,
install ($q, queues, cfg) {
if (isSSR === true) {
$q.screen = this
return
}
const { visualViewport } = window
const target = visualViewport || window
const scrollingElement = document.scrollingElement || document.documentElement
const getSize = visualViewport === void 0 || client.is.mobile === true
? () => [
Math.max(window.innerWidth, scrollingElement.clientWidth),
Math.max(window.innerHeight, scrollingElement.clientHeight)
]
: () => [
visualViewport.width * visualViewport.scale + window.innerWidth - scrollingElement.clientWidth,
visualViewport.height * visualViewport.scale + window.innerHeight - scrollingElement.clientHeight
]
const classes = cfg.screen !== void 0 && cfg.screen.bodyClasses === true
const update = force => {
const [ w, h ] = getSize()
if (h !== this.height) {
this.height = h
}
if (w !== this.width) {
this.width = w
}
else if (force !== true) {
return
}
let s = this.sizes
this.gt.xs = w >= s.sm
this.gt.sm = w >= s.md
this.gt.md = w >= s.lg
this.gt.lg = w >= s.xl
this.lt.sm = w < s.sm
this.lt.md = w < s.md
this.lt.lg = w < s.lg
this.lt.xl = w < s.xl
this.xs = this.lt.sm
this.sm = this.gt.xs === true && this.lt.md === true
this.md = this.gt.sm === true && this.lt.lg === true
this.lg = this.gt.md === true && this.lt.xl === true
this.xl = this.gt.lg
s = (this.xs === true && 'xs') ||
(this.sm === true && 'sm') ||
(this.md === true && 'md') ||
(this.lg === true && 'lg') ||
'xl'
if (s !== this.name) {
if (classes === true) {
document.body.classList.remove(`screen--${this.name}`)
document.body.classList.add(`screen--${s}`)
}
this.name = s
}
}
let updateEvt, updateSizes = {}, updateDebounce = 16
this.setSizes = sizes => {
SIZE_LIST.forEach(name => {
if (sizes[name] !== void 0) {
updateSizes[name] = sizes[name]
}
})
}
this.setDebounce = deb => {
updateDebounce = deb
}
const start = () => {
const style = getComputedStyle(document.body)
// if css props available
if (style.getPropertyValue('--q-size-sm')) {
SIZE_LIST.forEach(name => {
this.sizes[name] = parseInt(style.getPropertyValue(`--q-size-${name}`), 10)
})
}
this.setSizes = sizes => {
SIZE_LIST.forEach(name => {
if (sizes[name]) {
this.sizes[name] = sizes[name]
}
})
update(true)
}
this.setDebounce = delay => {
updateEvt !== void 0 && target.removeEventListener('resize', updateEvt, passive)
updateEvt = delay > 0
? debounce(update, delay)
: update
target.addEventListener('resize', updateEvt, passive)
}
this.setDebounce(updateDebounce)
if (Object.keys(updateSizes).length > 0) {
this.setSizes(updateSizes)
updateSizes = void 0 // free up memory
}
else {
update()
}
// due to optimizations, this would be left out otherwise
classes === true && this.name === 'xs' &&
document.body.classList.add(`screen--xs`)
}
if (fromSSR === true) {
queues.takeover.push(start)
}
else {
start()
}
Vue.util.defineReactive($q, 'screen', this)
}
}