hrms-mgt/src/modules/02_organization/components/StructureMain.vue
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 d092e8311d fix โครงสร้าง
2024-12-17 18:24:04 +07:00

318 lines
8.6 KiB
Vue

<script setup lang="ts">
import { ref, onMounted, watch, nextTick } from "vue";
import { useQuasar } from "quasar";
import { StructChart } from "structure-chart";
import "structure-chart/structure-chart.css";
import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";
import { useOrganizational } from "@/modules/02_organization/store/organizational";
const $q = useQuasar();
const mixin = useCounterMixin();
const store = useOrganizational();
const { showLoader, hideLoader, messageError } = mixin;
const dataSource = ref(); // ข้อมูล Chart
const rootOrgID = ref(); // org id ของ root ตัวปัจจุบันที่เลือกอยู่
const dataSourceLock = ref(); // ข้อมูลตั้งต้นของ Chart ใช้ให้กดกลับไปที่ home
const chartRef = ref(); // อ้างอิงไปที่ตัว chart
const loader = ref<boolean>(false); // Loader
const scrollContainer = ref<HTMLElement | null>(null);
/** function ดาวน์โหลดไฟล์โครงสร้าง PNG*/
async function savePNG() {
try {
showLoader();
await scrollToCenter();
await chartRef.value.savePNG();
} catch {
messageError($q);
} finally {
hideLoader();
}
}
/** function ดาวน์โหลดไฟล์โครงสร้าง PDF*/
async function savePDF() {
try {
showLoader();
await scrollToCenter();
await chartRef.value.savePDF();
} catch {
messageError($q);
} finally {
hideLoader();
}
}
/**
* function ดึงข้อมูลโครงสร้าง
* @param id id โครงสร้าง
* @param type
* @param status
*/
async function fetchStructChart(
id: string,
type: string,
status: boolean = false
) {
showLoader();
await http
.get(config.API.orgStructChart(id, type))
.then(async (res) => {
if (res.data.result.length > 0) {
const data = await res.data.result[0];
const struct = [];
struct.push({ ...data, officer: [], heads: [] });
dataSource.value = struct[0];
if (dataSourceLock.value === undefined || status)
dataSourceLock.value = dataSource.value;
breadcrumbsGen();
}
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
/**
* เมื่อมีการคลิกที่ Chart ให้อ่าน ID ของหน่วยงานที่ถูกคลิก แล้วดึงข้อมูล Chart ของหน่วยงานนั้นๆ จาก API
* @param data
*/
async function refreshChart(data: any, type: number) {
if (data.value === undefined) {
fetchStructChart(data, type.toString());
rootOrgID.value = data;
} else {
searchAndReplace(dataSource.value, data.value);
}
}
function searchAndReplace(data: any, id: string) {
if (data.deptID === id) {
fetchStructChart(data.deptID, data.type.toString());
rootOrgID.value = data.deptID;
} else if (data.children) {
for (const child of data.children) {
searchAndReplace(child, id);
}
}
}
/**
* === กระบวนการสร้าง Path ===
*/
/**
* Recursive เพื่อหา Path ที่ผู้ใช้คลิก
* @param id OrgID ของหน่วยงานที่ต้องการหา
* @param chart Array ของ Object ของหน่วยงานย่อยใน Tree นั้นๆ
*/
function chartTraverse(id: any, chart: any): any {
let _returnPath = [];
for (const child of chart) {
if (child.deptID === id) {
_returnPath.push({
label: child.departmentName,
id: child.deptID,
type: child.type,
});
return _returnPath;
} else {
if (
typeof child.children !== "undefined" &&
child.children !== null &&
child.children.length > 0
) {
let _result = chartTraverse(id, child.children);
if (typeof _result !== "undefined" && _result !== null) {
_returnPath.push({
label: child.departmentName,
id: child.deptID,
type: child.type,
});
return [..._returnPath, ...(_result ?? [])];
}
}
}
}
}
const findPath = (id: any) => {
let _path = [];
_path.push({
label: dataSourceLock.value.departmentName,
id: dataSourceLock.value.deptID,
type: dataSourceLock.value.type,
});
if (dataSourceLock.value.deptID === id) return _path;
if (dataSourceLock.value.children.length > 0) {
let _returnPath = chartTraverse(id, dataSourceLock.value.children);
return [..._path, ...(_returnPath ?? [])];
}
};
const theBreadcrumb = ref();
/**
* สร้าง Path Breadcrumbs
*/
function breadcrumbsGen() {
if (rootOrgID.value !== 0) {
theBreadcrumb.value = [];
const newPath = findPath(rootOrgID.value);
theBreadcrumb.value = newPath;
}
}
watch(
() => store.typeOrganizational,
async () => {
store.historyId = "";
const id =
store.typeOrganizational === "current"
? store.activeId
: store.typeOrganizational === "draft"
? store.draftId
: "";
id && (await fetchStructChart(id, "0", true));
}
);
watch(
() => store.historyId,
async () => {
store.historyId && (await fetchStructChart(store.historyId, "0", true));
}
);
/** ฟังก์ชันเลื่อน scroll ไปที่กึ่งกลาง*/
async function scrollToCenter() {
const container = scrollContainer.value;
if (container) {
container.scrollLeft = (container.scrollWidth - container.clientWidth) / 2;
}
}
// Watch เมื่อข้อมูลใน dataSource เปลี่ยนแปลง
watch(
dataSource,
() => {
// ใช้ nextTick เพื่อให้ DOM อัปเดตเสร็จก่อนค่อยเลื่อน scroll
nextTick(() => {
scrollToCenter();
});
},
{ deep: true } // ตรวจจับการเปลี่ยนแปลงภายในข้อมูล
);
onMounted(async () => {
const id =
store.typeOrganizational === "current"
? store.activeId
: store.typeOrganizational === "draft"
? store.draftId
: store.historyId;
id && (await fetchStructChart(id, "0"));
});
</script>
<template>
<q-card flat bordered>
<div class="q-pa-sm row wrap items-center">
<q-btn flat round color="primary" @click="savePNG()" icon="mdi-image">
<q-tooltip> ดาวนโหลด PNG </q-tooltip>
</q-btn>
<q-btn
flat
round
color="red-7"
@click="savePDF()"
icon="mdi-file-pdf-box"
>
<q-tooltip> ดาวนโหลด PDF </q-tooltip>
</q-btn>
<div class="bg-grey-2 q-py-xs q-px-sm rounded-borders">
<q-breadcrumbs>
<template v-slot:separator>
<q-icon size="1.5em" name="chevron_right" color="primary" />
</template>
<template v-for="link in theBreadcrumb" :key="link.id">
<q-breadcrumbs-el
:label="link.label"
@click="refreshChart(link.id, link.type)"
class="breadcrumbs-link"
/>
</template>
</q-breadcrumbs>
</div>
</div>
<q-separator />
<div
ref="scrollContainer"
style="
overflow-x: auto;
overflow-y: auto;
height: 70vh;
position: relative;
"
class="q-pt-md"
>
<StructChart
style="height: 70vh"
ref="chartRef"
class="struct"
:dataSource="dataSource"
@onElementClick="refreshChart"
/>
</div>
</q-card>
<full-loader :visibility="loader"></full-loader>
</template>
<style>
.breadcrumbs-link {
cursor: pointer;
}
.struct .section-primary .header,
.struct .section-secondary .header,
.struct .section-tertiary .header {
font-size: 1rem !important;
}
.struct .section-primary .column-side .side-button,
.struct .section-secondary .column-side .side-button,
.struct .section-list .column-side .side-button {
background-color: #d9d9d96b !important;
color: #545459 !important;
width: 25px !important;
height: 25px !important;
font-size: 11px !important;
padding: 0px;
}
.struct .section-primary,
.section-secondary,
.struct .section-list {
padding: 6px 15px 6px 15px;
}
.struct .section-primary .header {
font-weight: 600 !important;
}
.struct .section-secondary .header {
font-weight: 400 !important;
}
.struct .subchild-more {
background-position: center !important;
background-size: 14px !important;
}
</style>