Files
wireguard-dashboard-admin/src/store/modules/app.ts

90 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-04-22 14:15:05 +08:00
import {
type appType,
store,
getConfig,
defineStore,
storageLocal,
deviceDetection,
responsiveStorageNameSpace
} from "../utils";
2021-10-16 16:16:58 +08:00
export const useAppStore = defineStore({
id: "pure-app",
2021-12-06 17:11:15 +08:00
state: (): appType => ({
2021-10-16 16:16:58 +08:00
sidebar: {
2022-01-05 14:17:06 +08:00
opened:
2023-05-12 13:27:40 +08:00
storageLocal().getItem<StorageConfigs>(
`${responsiveStorageNameSpace()}layout`
)?.sidebarStatus ?? getConfig().SidebarStatus,
2021-11-25 12:37:56 +08:00
withoutAnimation: false,
2022-08-22 21:34:55 +08:00
isClickCollapse: false
2021-10-16 16:16:58 +08:00
},
// 这里的layout用于监听容器拖拉后恢复对应的导航模式
2021-10-16 16:16:58 +08:00
layout:
2023-05-12 13:27:40 +08:00
storageLocal().getItem<StorageConfigs>(
`${responsiveStorageNameSpace()}layout`
)?.layout ?? getConfig().Layout,
2024-03-23 08:58:13 +08:00
device: deviceDetection() ? "mobile" : "desktop",
// 浏览器窗口的可视区域大小
viewportSize: {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight
}
2021-10-16 16:16:58 +08:00
}),
getters: {
2023-05-12 13:27:40 +08:00
getSidebarStatus(state) {
return state.sidebar.opened;
2021-10-16 16:16:58 +08:00
},
2023-05-12 13:27:40 +08:00
getDevice(state) {
return state.device;
2024-03-23 08:58:13 +08:00
},
getViewportWidth(state) {
return state.viewportSize.width;
},
getViewportHeight(state) {
return state.viewportSize.height;
2021-10-16 16:16:58 +08:00
}
},
actions: {
2021-11-25 12:37:56 +08:00
TOGGLE_SIDEBAR(opened?: boolean, resize?: string) {
2023-05-12 13:27:40 +08:00
const layout = storageLocal().getItem<StorageConfigs>(
`${responsiveStorageNameSpace()}layout`
);
2021-11-25 12:37:56 +08:00
if (opened && resize) {
this.sidebar.withoutAnimation = true;
this.sidebar.opened = true;
2022-01-05 14:17:06 +08:00
layout.sidebarStatus = true;
2021-11-25 12:37:56 +08:00
} else if (!opened && resize) {
this.sidebar.withoutAnimation = true;
this.sidebar.opened = false;
2022-01-05 14:17:06 +08:00
layout.sidebarStatus = false;
2021-11-25 12:37:56 +08:00
} else if (!opened && !resize) {
this.sidebar.withoutAnimation = false;
this.sidebar.opened = !this.sidebar.opened;
2022-08-22 21:34:55 +08:00
this.sidebar.isClickCollapse = !this.sidebar.opened;
2022-01-05 14:17:06 +08:00
layout.sidebarStatus = this.sidebar.opened;
2021-10-16 16:16:58 +08:00
}
2023-05-12 13:27:40 +08:00
storageLocal().setItem(`${responsiveStorageNameSpace()}layout`, layout);
2021-10-16 16:16:58 +08:00
},
2021-11-25 12:37:56 +08:00
async toggleSideBar(opened?: boolean, resize?: string) {
await this.TOGGLE_SIDEBAR(opened, resize);
2021-10-16 16:16:58 +08:00
},
2022-08-22 21:34:55 +08:00
toggleDevice(device: string) {
this.device = device;
2021-10-16 16:16:58 +08:00
},
setLayout(layout) {
this.layout = layout;
2024-03-23 08:58:13 +08:00
},
setViewportSize(size) {
this.viewportSize = size;
},
setSortSwap(val) {
this.sortSwap = val;
2021-10-16 16:16:58 +08:00
}
}
});
export function useAppStoreHook() {
return useAppStore(store);
}