ResizeHandler.js 1.45 KB
Newer Older
纪泽龙's avatar
纪泽龙 committed
1
import store from "@/store";
耿迪迪's avatar
耿迪迪 committed
2

纪泽龙's avatar
纪泽龙 committed
3 4
const { body } = document;
const WIDTH = 992; // refer to Bootstrap's responsive design
耿迪迪's avatar
耿迪迪 committed
5 6 7 8

export default {
  watch: {
    $route(route) {
纪泽龙's avatar
纪泽龙 committed
9 10
      if (this.device === "mobile" && this.sidebar.opened) {
        store.dispatch("app/closeSideBar", { withoutAnimation: false });
耿迪迪's avatar
耿迪迪 committed
11 12 13 14
      }
    }
  },
  beforeMount() {
纪泽龙's avatar
纪泽龙 committed
15
    window.addEventListener("resize", this.$_resizeHandler);
耿迪迪's avatar
耿迪迪 committed
16 17
  },
  beforeDestroy() {
纪泽龙's avatar
纪泽龙 committed
18
    window.removeEventListener("resize", this.$_resizeHandler);
耿迪迪's avatar
耿迪迪 committed
19 20
  },
  mounted() {
纪泽龙's avatar
纪泽龙 committed
21
    const isMobile = this.$_isMobile();
耿迪迪's avatar
耿迪迪 committed
22
    if (isMobile) {
纪泽龙's avatar
纪泽龙 committed
23 24
      store.dispatch("app/toggleDevice", "mobile");
      store.dispatch("app/closeSideBar", { withoutAnimation: true });
耿迪迪's avatar
耿迪迪 committed
25 26 27 28 29 30
    }
  },
  methods: {
    // use $_ for mixins properties
    // https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential
    $_isMobile() {
纪泽龙's avatar
纪泽龙 committed
31 32
      const rect = body.getBoundingClientRect();
      return rect.width - 1 < WIDTH;
耿迪迪's avatar
耿迪迪 committed
33 34 35
    },
    $_resizeHandler() {
      if (!document.hidden) {
纪泽龙's avatar
纪泽龙 committed
36 37 38 39 40 41 42 43
        const isMobile = this.$_isMobile();
        // console.log(this.$route.path)
        // 当是gis地图的时候左边隐藏
        if (this.$route.path == "/enterprise/mapView") {
          store.dispatch("app/toggleDevice", "mobile");
        } else {
          store.dispatch("app/toggleDevice", isMobile ? "mobile" : "desktop");
        }
耿迪迪's avatar
耿迪迪 committed
44 45

        if (isMobile) {
纪泽龙's avatar
纪泽龙 committed
46
          store.dispatch("app/closeSideBar", { withoutAnimation: true });
耿迪迪's avatar
耿迪迪 committed
47 48 49 50
        }
      }
    }
  }
纪泽龙's avatar
纪泽龙 committed
51
};