แผนผังองค์กร

This commit is contained in:
STW_TTTY\stwtt 2024-06-06 13:58:15 +07:00
parent 3e213f0681
commit 43772f8d0a
5 changed files with 306 additions and 2 deletions

View file

@ -31,7 +31,7 @@ const items = ref<any>([
title: "แผนผังองค์กร",
sub: "ดูแผนผังองค์กร",
color: "blue-3",
path: "",
path: "/organization-chart",
active: false,
},
{

View file

@ -0,0 +1,13 @@
const organizationChart = () =>
import("@/modules/12_organization/views/main.vue");
export default [
{
path: "/organization-chart",
name: "organizationChart",
component: organizationChart,
meta: {
Auth: true,
Key: [7],
},
},
];

View file

@ -0,0 +1,282 @@
<script setup lang="ts">
import { ref, computed, onMounted, watch } from "vue";
import { useQuasar } from "quasar";
import { useRouter } from "vue-router";
import http from "@/plugins/http";
import config from "@/app.config";
import { StructChart } from "structure-chart";
import "structure-chart/structure-chart.css";
import { useCounterMixin } from "@/stores/mixin";
/** use*/
const mixin = useCounterMixin();
const { showLoader, hideLoader, messageError } = mixin;
const router = useRouter();
const $q = useQuasar();
const chartRef = ref(); // chart
const theBreadcrumb = ref();
const rootOrgID = ref(); // org id root
const dataSource = ref(); // Chart
const idActive = ref<string>("");
const dataSourceLock = ref(); // Chart home
/**
* สราง Path Breadcrumbs
*/
function breadcrumbsGen() {
if (rootOrgID.value !== 0) {
theBreadcrumb.value = [];
const newPath = findPath(rootOrgID.value);
theBreadcrumb.value = newPath;
}
}
/**
* === กระบวนการสราง 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 ?? [])];
}
}
}
}
}
function 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 ?? [])];
}
}
function savePNG() {
chartRef.value.savePNG();
}
function savePDF() {
chartRef.value.savePDF();
}
/**
* เมอมการคลกท Chart ใหาน ID ของหนวยงานทกคล แลวดงขอม Chart ของหนวยงานน จาก API
* @param data
*/
async function refreshChart(data: any, type: number) {
console.log(data, type);
if (data.value === undefined) {
fetchStructChart(data, type.toString());
rootOrgID.value = data;
} else {
searchAndReplace(dataSource.value, data.value);
}
}
async function fetchStructChart(
id: string,
type: string,
status: boolean = false
) {
console.log(status);
showLoader();
await http
.get(config.API.orgStructChart(id, type))
.then((res) => {
if (res.data.result.length > 0) {
const data = 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();
});
}
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);
}
}
}
function getActive() {
http
.get(config.API.orgActive)
.then(async (res) => {
const data = await res.data.result;
idActive.value = data.activeId;
fetchStructChart(data.activeId, "0");
})
.catch((e) => {
messageError($q, e);
})
.finally(() => {});
}
/** hook lifecycle*/
onMounted(async () => {
await getActive();
});
</script>
<template>
<div class="col-12 row justify-center">
<div class="col-xs-12 col-sm-12 col-md-11">
<div class="toptitle text-white col-12 row items-center">
<q-btn
icon="mdi-arrow-left"
unelevated
round
dense
flat
color="primary"
class="q-mr-sm"
@click="router.go(-1)"
/>
<div>แผนผงองคกร</div>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-11 row q-col-gutter-md">
<div class="col-12 row">
<q-card bordered class="col-12 row caedNone">
<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>
<div class="col-12">
<q-separator/>
</div>
<div style="overflow-x: auto; overflow-y: auto" class="q-pt-md">
<StructChart
style="height: 70vh"
ref="chartRef"
class="struct"
:dataSource="dataSource"
@onElementClick="refreshChart"
/>
</div>
</q-card>
</div>
</div>
</div>
</template>
<style scoped>
.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>