2022-08-31 03:01:44 +00:00
|
|
|
|
import {useSettingStore} from "@/store/modules/setting"
|
2022-04-06 07:48:24 +00:00
|
|
|
|
import {computed, onMounted, onUnmounted, watch} from "vue";
|
|
|
|
|
|
import {useRoute} from "vue-router";
|
|
|
|
|
|
|
|
|
|
|
|
const { body } = document
|
|
|
|
|
|
|
2023-02-28 06:19:13 +00:00
|
|
|
|
const WIDTH = 800 // refer to Bootstrap's responsive design
|
|
|
|
|
|
const MAX_WIDTH = 1200
|
2022-04-06 07:48:24 +00:00
|
|
|
|
|
|
|
|
|
|
export const useResizeHandler = ()=>{
|
2022-08-30 11:17:40 +00:00
|
|
|
|
const SettingStore = useSettingStore()
|
2022-04-06 07:48:24 +00:00
|
|
|
|
const route = useRoute()
|
|
|
|
|
|
const device = computed(()=>{
|
2022-08-30 11:17:40 +00:00
|
|
|
|
return SettingStore.device
|
2022-04-06 07:48:24 +00:00
|
|
|
|
})
|
|
|
|
|
|
function $_isMobile(){
|
|
|
|
|
|
const rect = body.getBoundingClientRect()
|
|
|
|
|
|
return rect.width - 1 < WIDTH
|
|
|
|
|
|
}
|
2023-02-28 06:19:13 +00:00
|
|
|
|
|
|
|
|
|
|
function collapse(){
|
|
|
|
|
|
const rect = body.getBoundingClientRect()
|
|
|
|
|
|
if(rect.width - 1 > MAX_WIDTH){
|
|
|
|
|
|
return true
|
|
|
|
|
|
}else {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-06 07:48:24 +00:00
|
|
|
|
function $_resizeHandler(){
|
|
|
|
|
|
if (!document.hidden) { // bool型,表示页面是否处于隐藏状态。页面隐藏包括页面在后台标签页或者浏览器最小化
|
|
|
|
|
|
const isMobile = $_isMobile()
|
2023-02-28 06:19:13 +00:00
|
|
|
|
const isCollapse = collapse()
|
2022-08-30 11:17:40 +00:00
|
|
|
|
SettingStore.toggleDevice(isMobile ? 'mobile' : 'desktop')
|
2022-04-06 07:48:24 +00:00
|
|
|
|
|
2023-02-28 06:19:13 +00:00
|
|
|
|
|
2022-04-06 07:48:24 +00:00
|
|
|
|
if (isMobile) {
|
2022-08-30 11:17:40 +00:00
|
|
|
|
SettingStore.closeSideBar({ withoutAnimation: true })
|
2022-04-06 07:48:24 +00:00
|
|
|
|
}
|
2023-02-28 06:19:13 +00:00
|
|
|
|
|
|
|
|
|
|
if(!isMobile){
|
|
|
|
|
|
SettingStore.setCollapse(isCollapse)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-04-06 07:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
onMounted(()=>{
|
|
|
|
|
|
const isMobile = $_isMobile()
|
|
|
|
|
|
if (isMobile) {
|
2022-08-30 11:17:40 +00:00
|
|
|
|
SettingStore.toggleDevice('mobile')
|
|
|
|
|
|
SettingStore.closeSideBar({ withoutAnimation: true })
|
2022-04-06 07:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
window.addEventListener('resize', $_resizeHandler)
|
|
|
|
|
|
|
|
|
|
|
|
watch(route,()=>{
|
2022-08-30 11:17:40 +00:00
|
|
|
|
if (device.value === 'mobile' && SettingStore.isCollapse) {
|
|
|
|
|
|
SettingStore.closeSideBar({ withoutAnimation: false })
|
2022-04-06 07:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
onUnmounted(()=>{
|
|
|
|
|
|
window.removeEventListener('resize', $_resizeHandler)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {device}
|
|
|
|
|
|
}
|