|
1
|
import store from '@/store'
|
|
2
|
|
|
3
4
|
const { body } = document
const WIDTH = 992 // refer to Bootstrap's responsive design
|
|
5
6
7
8
|
export default {
watch: {
$route(route) {
|
|
9
10
|
if (this.device === 'mobile' && this.sidebar.opened) {
store.dispatch('app/closeSideBar', { withoutAnimation: false })
|
|
11
|
}
|
|
12
|
}
|
|
13
14
|
},
beforeMount() {
|
|
15
|
window.addEventListener('resize', this.$_resizeHandler)
|
|
16
17
|
},
beforeDestroy() {
|
|
18
|
window.removeEventListener('resize', this.$_resizeHandler)
|
|
19
20
|
},
mounted() {
|
|
21
|
const isMobile = this.$_isMobile()
|
|
22
|
if (isMobile) {
|
|
23
24
|
store.dispatch('app/toggleDevice', 'mobile')
store.dispatch('app/closeSideBar', { withoutAnimation: true })
|
|
25
26
27
28
29
30
|
}
},
methods: {
// use $_ for mixins properties
// https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential
$_isMobile() {
|
|
31
32
|
const rect = body.getBoundingClientRect()
return rect.width - 1 < WIDTH
|
|
33
34
35
|
},
$_resizeHandler() {
if (!document.hidden) {
|
|
36
37
|
const isMobile = this.$_isMobile()
store.dispatch('app/toggleDevice', isMobile ? 'mobile' : 'desktop')
|
|
38
39
|
if (isMobile) {
|
|
40
|
store.dispatch('app/closeSideBar', { withoutAnimation: true })
|
|
41
42
|
}
}
|
|
43
44
45
|
}
}
}
|