ResizeHandler.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. $_isMobile() {
  27. const rect = body.getBoundingClientRect()
  28. return rect.width - 1 < WIDTH
  29. },
  30. $_resizeHandler() {
  31. if (!document.hidden) {
  32. const isMobile = this.$_isMobile()
  33. store.dispatch('app/toggleDevice', isMobile ? 'mobile' : 'desktop')
  34. if (isMobile) {
  35. store.dispatch('app/closeSideBar', { withoutAnimation: true })
  36. }
  37. }
  38. }
  39. }
  40. }