ab19e851d8e5197b7054cbf3926c5c4c812b26c0.svn-base 1.2 KB

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