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

76 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-10-16 16:16:58 +08:00
import { storageLocal } from "/@/utils/storage";
import { deviceDetection } from "/@/utils/deviceDetection";
import { defineStore } from "pinia";
import { store } from "/@/store";
import { getConfig } from "/@/config";
2021-10-16 16:16:58 +08:00
interface AppState {
sidebar: {
opened: boolean;
withoutAnimation: boolean;
2021-11-25 12:37:56 +08:00
// 判断是否手动点击Hamburger
isClickHamburger: boolean;
2021-10-16 16:16:58 +08:00
};
layout: string;
device: string;
}
export const useAppStore = defineStore({
id: "pure-app",
state: (): AppState => ({
sidebar: {
opened: storageLocal.getItem("sidebarStatus")
? !!+storageLocal.getItem("sidebarStatus")
: true,
2021-11-25 12:37:56 +08:00
withoutAnimation: false,
isClickHamburger: false
2021-10-16 16:16:58 +08:00
},
// 这里的layout用于监听容器拖拉后恢复对应的导航模式
2021-10-16 16:16:58 +08:00
layout:
storageLocal.getItem("responsive-layout")?.layout ?? getConfig().Layout,
2021-10-16 16:16:58 +08:00
device: deviceDetection() ? "mobile" : "desktop"
}),
getters: {
getSidebarStatus() {
return this.sidebar.opened;
},
getDevice() {
return this.device;
}
},
actions: {
2021-11-25 12:37:56 +08:00
TOGGLE_SIDEBAR(opened?: boolean, resize?: string) {
if (opened && resize) {
this.sidebar.withoutAnimation = true;
this.sidebar.opened = true;
storageLocal.setItem("sidebarStatus", true);
} else if (!opened && resize) {
this.sidebar.withoutAnimation = true;
this.sidebar.opened = false;
storageLocal.setItem("sidebarStatus", false);
} else if (!opened && !resize) {
this.sidebar.withoutAnimation = false;
this.sidebar.opened = !this.sidebar.opened;
this.sidebar.isClickHamburger = !this.sidebar.opened;
storageLocal.setItem("sidebarStatus", this.sidebar.opened);
2021-10-16 16:16:58 +08:00
}
},
TOGGLE_DEVICE(device: string) {
this.device = device;
},
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
},
toggleDevice(device) {
this.TOGGLE_DEVICE(device);
},
setLayout(layout) {
this.layout = layout;
}
}
});
export function useAppStoreHook() {
return useAppStore(store);
}