Blame view

HHECS.Web/src/layout/mixin/ResizeHandler.js 1.21 KB
胡菁 authored
1
import store from '@/store'
胡菁 authored
2
胡菁 authored
3
4
const { body } = document
const WIDTH = 992 // refer to Bootstrap's responsive design
胡菁 authored
5
6
7
8

export default {
  watch: {
    $route(route) {
胡菁 authored
9
10
      if (this.device === 'mobile' && this.sidebar.opened) {
        store.dispatch('app/closeSideBar', { withoutAnimation: false })
胡菁 authored
11
      }
胡菁 authored
12
    }
胡菁 authored
13
14
  },
  beforeMount() {
胡菁 authored
15
    window.addEventListener('resize', this.$_resizeHandler)
胡菁 authored
16
17
  },
  beforeDestroy() {
胡菁 authored
18
    window.removeEventListener('resize', this.$_resizeHandler)
胡菁 authored
19
20
  },
  mounted() {
胡菁 authored
21
    const isMobile = this.$_isMobile()
胡菁 authored
22
    if (isMobile) {
胡菁 authored
23
24
      store.dispatch('app/toggleDevice', 'mobile')
      store.dispatch('app/closeSideBar', { withoutAnimation: true })
胡菁 authored
25
26
27
28
29
30
    }
  },
  methods: {
    // use $_ for mixins properties
    // https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential
    $_isMobile() {
胡菁 authored
31
32
      const rect = body.getBoundingClientRect()
      return rect.width - 1 < WIDTH
胡菁 authored
33
34
35
    },
    $_resizeHandler() {
      if (!document.hidden) {
胡菁 authored
36
37
        const isMobile = this.$_isMobile()
        store.dispatch('app/toggleDevice', isMobile ? 'mobile' : 'desktop')
胡菁 authored
38
39

        if (isMobile) {
胡菁 authored
40
          store.dispatch('app/closeSideBar', { withoutAnimation: true })
胡菁 authored
41
42
        }
      }
胡菁 authored
43
44
45
    }
  }
}