resize.ts
1.16 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
import { Component, Vue, Watch } from 'vue-property-decorator'
import { AppModule, DeviceType } from '@/store/modules/app'
const WIDTH = 992
@Component({
name: 'ResizeMixin'
})
export default class extends Vue {
get device() {
return AppModule.device
}
get sidebar() {
return AppModule.sidebar
}
@Watch('$route')
private onRouteChange() {
if (this.device === DeviceType.Mobile && this.sidebar.opened) {
AppModule.CloseSideBar(false)
}
}
beforeMount() {
window.addEventListener('resize', this.resizeHandler)
}
mounted() {
const isMobile = this.isMobile()
if (isMobile) {
AppModule.ToggleDevice(DeviceType.Mobile)
AppModule.CloseSideBar(true)
}
}
beforeDestroy() {
window.removeEventListener('resize', this.resizeHandler)
}
private isMobile() {
const rect = document.body.getBoundingClientRect()
return rect.width - 1 < WIDTH
}
private resizeHandler() {
if (!document.hidden) {
const isMobile = this.isMobile()
AppModule.ToggleDevice(isMobile ? DeviceType.Mobile : DeviceType.Desktop)
if (isMobile) {
AppModule.CloseSideBar(true)
}
}
}
}