管理系统PC前端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dm-manage-web/src/layout/mixin/ResizeHandler.js

46 lines
1.2 KiB

2 years ago
import store from '@/store';
2 years ago
2 years ago
const { body } = document;
const WIDTH = 992; // refer to Bootstrap's responsive design
2 years ago
export default {
watch: {
$route(route) {
if (this.device === 'mobile' && this.sidebar.opened) {
2 years ago
store.dispatch('app/closeSideBar', { withoutAnimation: false });
2 years ago
}
}
},
beforeMount() {
2 years ago
window.addEventListener('resize', this.$_resizeHandler);
2 years ago
},
beforeDestroy() {
2 years ago
window.removeEventListener('resize', this.$_resizeHandler);
2 years ago
},
mounted() {
2 years ago
const isMobile = this.$_isMobile();
2 years ago
if (isMobile) {
2 years ago
store.dispatch('app/toggleDevice', 'mobile');
store.dispatch('app/closeSideBar', { withoutAnimation: true });
2 years ago
}
},
methods: {
// use $_ for mixins properties
// https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential
$_isMobile() {
2 years ago
const rect = body.getBoundingClientRect();
return rect.width - 1 < WIDTH;
2 years ago
},
$_resizeHandler() {
if (!document.hidden) {
2 years ago
const isMobile = this.$_isMobile();
store.dispatch('app/toggleDevice', isMobile ? 'mobile' : 'desktop');
2 years ago
if (isMobile) {
2 years ago
store.dispatch('app/closeSideBar', { withoutAnimation: true });
2 years ago
}
}
}
}
2 years ago
};