Files
EnuivTools/web/src/shell/Shell.vue
T

175 lines
5.9 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { onMounted, onUnmounted, ref } from "vue";
import { NButton, NIcon, NLayout, NLayoutSider, NMenu, NModal, NRadio, NRadioGroup, NTooltip } from "naive-ui";
import { LogOutOutline, MoonOutline, PersonOutline, SunnyOutline } from "@vicons/ionicons5";
import { isDark, toggleTheme } from "../shared/theme";
import { navOptions, type PageKey } from "./navigation";
import HomeView from "../features/home/HomeView.vue";
import HoursHistoryView from "../features/history/HoursHistoryView.vue";
import ExamsView from "../features/exams/ExamsView.vue";
import TrainingsView from "../features/trainings/TrainingsView.vue";
const props = defineProps<{ employeeId: string }>();
const emit = defineEmits<{ logout: []; revoke: [] }>();
const page = ref<PageKey>(pageFromHash());
const collapsed = ref(window.innerWidth < 800);
const holdOpen = ref(false);
const leaveOpen = ref(false);
const leaveChoice = ref<"logout" | "revoke">("logout");
const pages = new Set<PageKey>(["home", "history", "exams", "trainings"]);
function hashName() {
return window.location.hash.replace(/^#\/?/, "");
}
function pageFromHash(): PageKey {
const name = hashName();
if (name === "exams" || name === "trainings" || name === "history" || name === "home") return name;
return "home";
}
function knownHash(name: string) {
return pages.has(name as PageKey);
}
function show(next: PageKey) {
page.value = next;
const hash = `#/${next}`;
if (window.location.hash !== hash) window.location.hash = hash;
}
function onHashChange() {
const name = hashName();
if (name && !knownHash(name)) {
history.replaceState(null, "", "#/home");
page.value = "home";
return;
}
page.value = pageFromHash();
}
function onResize() {
if (holdOpen.value) return;
if (window.innerWidth < 800) collapsed.value = true;
}
function onCollapse() {
collapsed.value = true;
holdOpen.value = false;
}
function onExpand() {
collapsed.value = false;
holdOpen.value = true;
}
onMounted(() => {
const name = hashName();
if (!name || !knownHash(name)) history.replaceState(null, "", "#/home");
window.addEventListener("resize", onResize);
window.addEventListener("hashchange", onHashChange);
});
onUnmounted(() => {
window.removeEventListener("resize", onResize);
window.removeEventListener("hashchange", onHashChange);
});
function onMenu(key: string) {
if (key === "home" || key === "history" || key === "exams" || key === "trainings") show(key);
}
function openLeave() {
leaveChoice.value = "logout";
leaveOpen.value = true;
}
function confirmLeave() {
const choice = leaveChoice.value;
if (!choice) return;
leaveOpen.value = false;
if (choice === "revoke") emit("revoke");
else emit("logout");
}
</script>
<template>
<n-layout has-sider class="shell">
<n-layout-sider
bordered
collapse-mode="width"
:collapsed-width="64"
:width="220"
:collapsed="collapsed"
show-trigger
:native-scrollbar="false"
:content-style="{ height: '100%', display: 'flex', flexDirection: 'column' }"
@collapse="onCollapse"
@expand="onExpand"
>
<div class="sider-body">
<div class="sider-brand" :class="{ compact: collapsed }">
<div class="brand-mark" aria-hidden="true">E</div>
<strong v-if="!collapsed">EunivTools</strong>
</div>
<n-menu
:collapsed="collapsed"
:collapsed-width="64"
:collapsed-icon-size="22"
:value="page"
:options="navOptions"
@update:value="onMenu"
/>
<div class="sider-user" :class="{ compact: collapsed }">
<n-icon :size="18"><PersonOutline /></n-icon>
<div v-if="!collapsed" class="who">
<strong>{{ props.employeeId }}</strong>
<span>已登录</span>
</div>
<n-tooltip>
<template #trigger>
<n-button quaternary circle size="small" :aria-label="isDark ? '切换到日间' : '切换到夜间'" @click="toggleTheme">
<template #icon><n-icon><MoonOutline v-if="!isDark" /><SunnyOutline v-else /></n-icon></template>
</n-button>
</template>
{{ isDark ? "切换到日间" : "切换到夜间" }}
</n-tooltip>
<n-button class="logout-btn" quaternary circle size="small" aria-label="退出" @click="openLeave">
<template #icon><n-icon><LogOutOutline /></n-icon></template>
</n-button>
</div>
</div>
</n-layout-sider>
<n-layout class="content" :native-scrollbar="false" content-style="height: 100%; min-height: 0; overflow: hidden; display: flex; flex-direction: column;">
<HomeView v-if="page === 'home'" :employee-id="props.employeeId" />
<HoursHistoryView v-else-if="page === 'history'" />
<ExamsView v-else-if="page === 'exams'" />
<TrainingsView v-else />
</n-layout>
<n-modal
v-model:show="leaveOpen"
preset="card"
title="退出"
:style="{ width: 'min(440px, calc(100vw - 32px))' }"
:mask-closable="false"
>
<n-radio-group v-model:value="leaveChoice" class="leave-choices">
<n-radio value="logout">退出系统</n-radio>
<n-radio value="revoke">注销账号</n-radio>
</n-radio-group>
<p v-if="leaveChoice === 'logout'" class="leave-note">定时计划和已开始的任务不会受影响。</p>
<p v-else-if="leaveChoice === 'revoke'" class="leave-note">会从平台删除这个账号,定时任务和已开始的任务会一并删除。</p>
<template #footer>
<div class="modal-actions">
<n-button @click="leaveOpen = false">取消</n-button>
<n-button
:type="leaveChoice === 'revoke' ? 'error' : 'primary'"
@click="confirmLeave"
>
{{ leaveChoice === "revoke" ? "注销账号" : "退出系统" }}
</n-button>
</div>
</template>
</n-modal>
</n-layout>
</template>