Merge commit 'ead85a5652f2c935ebbbd6b24486b57e9839dffd' into HEAD

This commit is contained in:
zong 2025-10-02 13:12:20 +08:00
commit b3f89ab5fe
4 changed files with 311 additions and 240 deletions

View File

@ -1,12 +1,6 @@
<script setup> <script setup>
import { twMerge } from "tailwind-merge"; import { twMerge } from "tailwind-merge";
import { computed, defineProps, onMounted, ref, watch } from "vue"; import { defineProps } from "vue";
/* ----------------------------------------------------------------
id名.showModal(): 開啟 modal
id名.close(): 關閉 modal
詳細請參考 daisyUI
------------------------------------------------------------------- */
const props = defineProps({ const props = defineProps({
id: String, id: String,
@ -14,52 +8,40 @@ const props = defineProps({
onCancel: Function, onCancel: Function,
modalClass: String, modalClass: String,
width: Number || String, width: Number || String,
draggable: { draggable: { type: Boolean, default: false },
type: Boolean, backdrop: { type: Boolean, default: true },
default: false, modalStyle: { type: Object, default: () => ({}) },
},
backdrop: {
type: Boolean,
default: true,
},
modalStyle: {
type: Object,
default: {},
},
}); });
const dom = ref(null)
onMounted(() => {
document.querySelector(`#${props.id}[open]`)?.addEventListener("load",()=>{
console.log("loading")
})
document.querySelector(`#${props.id}`).addEventListener("load",()=>{
console.log("loading")
})
})
</script> </script>
<template> <template>
<!-- Open the modal using ID.showModal() method --> <dialog
<!-- :class="twMerge('modal', open && innerOpen ? 'modal-open' : 'modal-close')" --> :id="id"
<dialog ref="dom" :id="id" :class="twMerge( :class="
twMerge(
'modal', 'modal',
backdrop backdrop ? '' : 'focus-visible:outline-none backdrop:bg-transparent'
? '' )
: 'focus-visible:outline-none backdrop:bg-transparent', "
)" :style="modalStyle" v-draggable="draggable"> :style="modalStyle"
<div :class="twMerge( v-draggable="
'modal-box static rounded-md border border-info py-5 px-6 overflow-y-scroll bg-normal', draggable ? { handle: '[data-drag-handle]', target: '.modal-box' } : false
"
>
<div
:class="
twMerge(
'modal-box static rounded-md border border-info py-5 px-6 overflow-y-auto bg-normal',
modalClass modalClass
) )
" :style="{ maxWidth: isNaN(width) ? width : `${width}px` }"> "
<div class="text-2xl font-bold"> :style="{ maxWidth: isNaN(width) ? width : `${width}px` }"
<slot name="modalTitle"> >
{{ title }} <!-- 把手只在這裡按下才可拖動 -->
</slot> <div class="text-2xl font-bold select-none" data-drag-handle>
<slot name="modalTitle">{{ title }}</slot>
</div> </div>
<div class="min-h-[200px]"> <div class="min-h-[200px]">
<slot name="modalContent"></slot> <slot name="modalContent"></slot>
</div> </div>
@ -68,25 +50,17 @@ onMounted(() => {
<slot name="modalAction"></slot> <slot name="modalAction"></slot>
</div> </div>
</div> </div>
<form v-if="backdrop" method="dialog" class="modal-backdrop"> <form v-if="backdrop" method="dialog" class="modal-backdrop">
<button @click="() => { <button
onCancel ? onCancel() : cancel(); @click="
() => {
onCancel && onCancel();
} }
"> "
>
close close
</button> </button>
</form> </form>
</dialog> </dialog>
</template> </template>
<style lang="css" scoped>
.modal-box::before {
@apply fixed top-1 right-1 h-5 w-5 rotate-90 bg-no-repeat z-10 bg-[url('../../assets/img/table/content-box-background01.svg')] bg-center;
content: "";
}
.modal-action::after {
@apply absolute -bottom-3 -left-4 h-5 w-5 rotate-90 bg-no-repeat z-10 bg-[url('../../assets/img/table/content-box-background05.svg')] bg-center;
content: "";
}
</style>

View File

@ -1,65 +1,97 @@
const moveModal = (elmnt) => { // src/directives/draggable.js
console.log(elmnt); // 用法:在 Modal 外層加 v-draggable="{ handle: '[data-drag-handle]', target: '.modal-box' }"
var pos1 = 0, // - handle只在這個節點按下才可拖動
pos2 = 0, // - target實際被拖動的 DOM預設用 el 本身)
pos3 = 0,
pos4 = 0;
elmnt.addEventListener("mousedown", dragMouseDown, {
passive: false,
});
function dragMouseDown(e) { function createDraggable(el, options = {}) {
console.log("dragMouseDown", e); const cancelSelector =
e = e || window.event; 'input, textarea, select, button, [contenteditable="true"], [data-drag-cancel]';
const target =
(options.target && el.querySelector(options.target)) ||
el.querySelector('.modal-box') ||
el;
const handle =
(options.handle && el.querySelector(options.handle)) || target;
let startX = 0,
startY = 0,
originLeft = 0,
originTop = 0,
dragging = false;
// 只在「左鍵」且「不是表單控制項」時啟動拖曳
function onMouseDown(e) {
if (e.button !== 0) return; if (e.button !== 0) return;
e.preventDefault();
// get the mouse cursor position at startup: // 點到可互動元件就不要拖(也不要 preventDefault讓它能聚焦
pos3 = e.clientX; if (e.target.matches(cancelSelector) || e.target.closest(cancelSelector)) {
pos4 = e.clientY; return;
document.body.addEventListener("mouseup", closeDragElement, {
passive: false,
});
// call a function whenever the cursor moves:
document.body.addEventListener("mousemove", elementDrag, {
passive: false,
});
} }
function elementDrag(e) { // 這裡才開始準備拖曳
e = e || window.event; dragging = true;
const rect = target.getBoundingClientRect();
originLeft = rect.left;
originTop = rect.top;
startX = e.clientX;
startY = e.clientY;
// 只有在真的要拖時才阻止預設,避免選字/圖片拖移
e.preventDefault(); e.preventDefault();
// calculate the new cursor position:
pos1 = pos3 - e.clientX; // 以視窗為座標系比較直觀
pos2 = pos4 - e.clientY; target.style.position = 'fixed';
pos3 = e.clientX; target.style.left = '0px';
pos4 = e.clientY; target.style.top = '0px';
// set the element's new position: target.style.transform = `translate(${originLeft}px, ${originTop}px)`;
elmnt.style.top = elmnt.offsetTop - pos2 + "px";
elmnt.style.left = elmnt.offsetLeft - pos1 + "px"; window.addEventListener('mousemove', onMouseMove, { passive: false });
window.addEventListener('mouseup', onMouseUp, { passive: true });
} }
function closeDragElement() { function onMouseMove(e) {
// stop moving when mouse button is released: if (!dragging) return;
document.body.removeEventListener("mouseup", closeDragElement); const dx = e.clientX - startX;
document.body.removeEventListener("mousemove", elementDrag); const dy = e.clientY - startY;
target.style.transform = `translate(${originLeft + dx}px, ${originTop + dy}px)`;
} }
function onMouseUp() {
dragging = false;
window.removeEventListener('mousemove', onMouseMove);
window.removeEventListener('mouseup', onMouseUp);
}
handle.style.cursor = 'move';
handle.addEventListener('mousedown', onMouseDown, { passive: false });
// 提供清理函式給 unmounted 用
return () => {
handle.removeEventListener('mousedown', onMouseDown);
window.removeEventListener('mousemove', onMouseMove);
window.removeEventListener('mouseup', onMouseUp);
}; };
}
export const draggable = { export const draggable = {
install(app) { install(app) {
app.directive("draggable", { app.directive('draggable', {
mounted: (el, binding, vnode, prevVnode) => { mounted(el, binding) {
console.log("draggable", $(`#${el.id}`).draggable); // 允許 v-draggable 或 v-draggable="true" 或 v-draggable="{ handle, target }"
if (binding.value) { const enabled =
if ($(`#${el.id}`).draggable) { binding.value === '' || binding.value === true || typeof binding.value === 'object';
$(`#${el.id}`).draggable({ if (!enabled) return;
cursor: "move",
scroll: true, // 以前用 jQuery UI 的判斷會在沒有 $ 或 .draggable 時噴錯,直接移除
container: ".app-container", const options = typeof binding.value === 'object' ? binding.value : {};
}); el.__dragCleanup__ = createDraggable(el, options);
} else { },
moveModal(el); unmounted(el) {
} if (el.__dragCleanup__) {
el.__dragCleanup__();
delete el.__dragCleanup__;
} }
}, },
}); });

View File

@ -1,38 +1,22 @@
<script setup> <script setup>
import Table from "@/components/customUI/Table.vue"; import Table from "@/components/customUI/Table.vue";
import VendorModal from "./VendorModal.vue"; import VendorModal from "./VendorModal.vue";
import { getOperationCompanyList,deleteOperationCompany } from "@/apis/operation"; import {
getOperationCompanyList,
deleteOperationCompany,
} from "@/apis/operation";
import { onMounted, ref, inject, computed } from "vue"; import { onMounted, ref, inject, computed } from "vue";
import { useI18n } from "vue-i18n"; import { useI18n } from "vue-i18n";
const { t } = useI18n(); const { t } = useI18n();
const { openToast, cancelToastOpen } = inject("app_toast"); const { openToast, cancelToastOpen } = inject("app_toast");
const columns = computed(() => [ const columns = computed(() => [
{ { title: t("operation.vendor"), key: "name" },
title: t("operation.vendor"), { title: t("operation.contact_person"), key: "contact_person" },
key: "name", { title: t("operation.phone"), key: "phone" },
}, { title: t("operation.email"), key: "email" },
{ { title: t("operation.created_at"), key: "created_at" },
title: t("operation.contact_person"), { title: t("operation.operation"), key: "operation", width: 200 },
key: "contact_person",
},
{
title: t("operation.phone"),
key: "phone",
},
{
title: t("operation.email"),
key: "email",
},
{
title: t("operation.created_at"),
key: "created_at",
},
{
title: t("operation.operation"),
key: "operation",
width: 200,
},
]); ]);
const dataSource = ref([]); const dataSource = ref([]);
@ -49,7 +33,9 @@ onMounted(() => {
getDataSource(); getDataSource();
}); });
const formState = ref({ // ====== Modal ======
const MODAL_ID = "company_modal";
const emptyForm = () => ({
contact_person: "", contact_person: "",
email: "", email: "",
name: "", name: "",
@ -60,22 +46,36 @@ const formState = ref({
address: "", address: "",
}); });
const openModal = (record) => { const formState = ref(emptyForm());
if (record.id) {
formState.value = { ...record }; // (/)
} else { const openModal = (payload) => {
formState.value = { const el = document.getElementById(MODAL_ID);
contact_person: "",
email: "", //
name: "", if (payload === false) {
phone: "", try {
remark: "", el?.close?.();
tax_id_number: "", } catch {}
city: "", return;
address: "", }
};
// true
if (payload === true || payload == null) {
formState.value = emptyForm();
try {
el?.showModal?.();
} catch {}
return;
}
// record
if (payload && typeof payload === "object") {
formState.value = { ...emptyForm(), ...payload }; //
try {
el?.showModal?.();
} catch {}
} }
company_modal.showModal();
}; };
const remove = async (id) => { const remove = async (id) => {
@ -95,15 +95,19 @@ const remove = async (id) => {
<template> <template>
<div class="flex justify-start items-center mt-10 mb-5"> <div class="flex justify-start items-center mt-10 mb-5">
<h3 class="text-xl mr-5">{{ $t("assetManagement.company") }}</h3> <h3 class="text-xl mr-5">{{ $t("assetManagement.company") }}</h3>
<!-- 子層會渲染新增按鈕照你的原樣 -->
<VendorModal <VendorModal
:formState="formState" :formState="formState"
:getData="getDataSource" :getData="getDataSource"
:openModal="openModal" :openModal="openModal"
/> />
</div> </div>
<Table :columns="columns" :dataSource="dataSource" :loading="loading"> <Table :columns="columns" :dataSource="dataSource" :loading="loading">
<template #bodyCell="{ record, column, index }"> <template #bodyCell="{ record, column, index }">
<template v-if="column.key === 'index'">{{ index + 1 }}</template> <template v-if="column.key === 'index'">{{ index + 1 }}</template>
<template v-else-if="column.key === 'operation'"> <template v-else-if="column.key === 'operation'">
<button <button
class="btn btn-sm btn-success text-white mr-2" class="btn btn-sm btn-success text-white mr-2"
@ -118,6 +122,7 @@ const remove = async (id) => {
{{ $t("button.delete") }} {{ $t("button.delete") }}
</button> </button>
</template> </template>
<template v-else> <template v-else>
{{ record[column.key] }} {{ record[column.key] }}
</template> </template>

View File

@ -1,23 +1,21 @@
<script setup> <script setup>
import { ref, onMounted, defineProps, inject, watch } from "vue"; import { ref, inject } from "vue";
import * as yup from "yup"; import * as yup from "yup";
import "yup-phone-lite"; import "yup-phone-lite";
import useFormErrorMessage from "@/hooks/useFormErrorMessage"; import useFormErrorMessage from "@/hooks/useFormErrorMessage";
import { import { postOperationCompany, updateOperationCompany } from "@/apis/operation";
postOperationCompany,
updateOperationCompany,
} from "@/apis/operation";
import { useI18n } from "vue-i18n"; import { useI18n } from "vue-i18n";
const { t } = useI18n(); const { t } = useI18n();
const { openToast } = inject("app_toast"); const { openToast } = inject("app_toast");
const props = defineProps({ const props = defineProps({
formState: Object, formState: Object,
getData: Function, getData: Function,
openModal: Function openModal: Function, // / modal
}); });
const deptScheme = yup.object({ const schema = yup.object({
name: yup.string().required(t("button.required")), name: yup.string().required(t("button.required")),
contact_person: yup.string().nullable(true), contact_person: yup.string().nullable(true),
email: yup.string().email().nullable(true), email: yup.string().email().nullable(true),
@ -28,34 +26,104 @@ const deptScheme = yup.object({
remark: yup.string().nullable(true), remark: yup.string().nullable(true),
}); });
const { formErrorMsg, handleSubmit, handleErrorReset, updateScheme } = const { formErrorMsg, handleSubmit, handleErrorReset } =
useFormErrorMessage(deptScheme); useFormErrorMessage(schema);
const loading = ref(false);
const MODAL_ID = "company_modal";
/** ====== 關閉 Modal多重保險 ====== */
const closeModal = () => {
// 1) boolean
if (typeof props.openModal === "function") {
try {
if (props.openModal.length >= 1) {
// openModal(false)
props.openModal(false);
} else {
// toggler false
try {
props.openModal(false);
} catch {
props.openModal();
}
}
return;
} catch (_) {}
}
// 2) <dialog> close() Modal
const el = document.getElementById(MODAL_ID);
if (el?.close) {
try {
el.close();
return;
} catch (_) {}
}
// 3) Modal
try {
el?.dispatchEvent?.(new CustomEvent("close", { bubbles: true }));
} catch (_) {}
};
/** ====== 開啟 Modal呼叫父層 ====== */
const openModal = () => {
if (typeof props.openModal === "function") {
try {
if (props.openModal.length >= 1) props.openModal(true);
else props.openModal();
} catch {
//
}
}
};
const onCancel = () => { const onCancel = () => {
handleErrorReset(); //
company_modal.close(); //
closeModal();
}; };
const onOk = async () => { const onOk = async () => {
const value = await handleSubmit(deptScheme, props.formState); try {
loading.value = true;
const value = await handleSubmit(schema, props.formState);
let res;
if (props.formState?.id) { if (props.formState?.id) {
// API id updateOperationCompany(props.formState.id, value)
res = await updateOperationCompany(value); res = await updateOperationCompany(value);
} else { } else {
res = await postOperationCompany(value); res = await postOperationCompany(value);
} }
if (res.isSuccess) {
props.getData(); if (res?.isSuccess) {
onCancel(); await props.getData?.();
props.openModal(false);
//
closeModal();
//
handleErrorReset();
props.openModal(false);
openToast?.("success", t("common.success"), `#${MODAL_ID}`);
} else { } else {
openToast("error", res.msg, "#company_modal"); openToast?.("error", res?.msg ?? t("common.failed"), `#${MODAL_ID}`);
}
} catch (err) {
openToast?.("error", err?.message ?? t("common.failed"), `#${MODAL_ID}`);
} finally {
loading.value = false;
} }
}; };
</script> </script>
<template> <template>
<button class="btn btn-sm btn-add " @click.stop.prevent="props.openModal"> <button class="btn btn-sm btn-add" @click.stop.prevent="openModal">
<font-awesome-icon :icon="['fas', 'plus']" />{{ $t("button.add") }} <font-awesome-icon :icon="['fas', 'plus']" />{{ $t("button.add") }}
</button> </button>
<Modal <Modal
id="company_modal" id="company_modal"
:title="props.formState?.id ? t('button.edit') : t('button.add')" :title="props.formState?.id ? t('button.edit') : t('button.add')"
@ -63,87 +131,79 @@ const onOk = async () => {
width="710" width="710"
> >
<template #modalContent> <template #modalContent>
<form ref="form" class="mt-5 w-full flex flex-wrap justify-between"> <form class="mt-5 w-full flex flex-wrap justify-between">
<Input :value="formState" class="my-2" name="name"> <Input :value="props.formState" class="my-2" name="name">
<template #topLeft>{{ $t("operation.name") }}</template> <template #topLeft>{{ $t("operation.name") }}</template>
<template #bottomLeft <template #bottomLeft>
><span class="text-error text-base"> <span class="text-error text-base">{{ formErrorMsg.name }}</span>
{{ formErrorMsg.name }} </template>
</span></template </Input>
></Input <Input :value="props.formState" class="my-2" name="contact_person">
>
<Input :value="formState" class="my-2" name="contact_person">
<template #topLeft>{{ $t("operation.contact_person") }}</template> <template #topLeft>{{ $t("operation.contact_person") }}</template>
<template #bottomLeft <template #bottomLeft>
><span class="text-error text-base"> <span class="text-error text-base">{{
{{ formErrorMsg.contact_person }} formErrorMsg.contact_person
</span></template }}</span>
></Input </template>
> </Input>
<Input :value="formState" class="my-2" name="phone"> <Input :value="props.formState" class="my-2" name="phone">
<template #topLeft>{{ $t("operation.phone") }}</template> <template #topLeft>{{ $t("operation.phone") }}</template>
<template #bottomLeft <template #bottomLeft>
><span class="text-error text-base"> <span class="text-error text-base">{{ formErrorMsg.phone }}</span>
{{ formErrorMsg.phone }} </template>
</span></template </Input>
></Input <Input :value="props.formState" class="my-2" name="email">
>
<Input :value="formState" class="my-2" name="email">
<template #topLeft>{{ $t("operation.email") }}</template> <template #topLeft>{{ $t("operation.email") }}</template>
<template #bottomLeft <template #bottomLeft>
><span class="text-error text-base"> <span class="text-error text-base">{{ formErrorMsg.email }}</span>
{{ formErrorMsg.email }} </template>
</span></template </Input>
></Input <Input :value="props.formState" class="my-2" name="city">
>
<Input :value="formState" class="my-2" name="city">
<template #topLeft>{{ $t("operation.city") }}</template> <template #topLeft>{{ $t("operation.city") }}</template>
<template #bottomLeft <template #bottomLeft>
><span class="text-error text-base"> <span class="text-error text-base">{{ formErrorMsg.city }}</span>
{{ formErrorMsg.city }} </template>
</span></template </Input>
></Input <Input :value="props.formState" class="my-2" name="address">
>
<Input :value="formState" class="my-2" name="address">
<template #topLeft>{{ $t("operation.address") }}</template> <template #topLeft>{{ $t("operation.address") }}</template>
<template #bottomLeft <template #bottomLeft>
><span class="text-error text-base"> <span class="text-error text-base">{{ formErrorMsg.address }}</span>
{{ formErrorMsg.address }} </template>
</span></template </Input>
></Input <Input :value="props.formState" class="my-2" name="tax_id_number">
>
<Input :value="formState" class="my-2" name="tax_id_number">
<template #topLeft>{{ $t("operation.tax_id_number") }}</template> <template #topLeft>{{ $t("operation.tax_id_number") }}</template>
<template #bottomLeft <template #bottomLeft>
><span class="text-error text-base"> <span class="text-error text-base">{{
{{ formErrorMsg.tax_id_number }} formErrorMsg.tax_id_number
</span></template }}</span>
></Input </template>
> </Input>
<Input :value="formState" class="my-2" name="remark"> <Input :value="props.formState" class="my-2" name="remark">
<template #topLeft>{{ $t("operation.remark") }}</template> <template #topLeft>{{ $t("operation.remark") }}</template>
<template #bottomLeft <template #bottomLeft>
><span class="text-error text-base"> <span class="text-error text-base">{{ formErrorMsg.remark }}</span>
{{ formErrorMsg.remark }} </template>
</span></template </Input>
></Input
>
</form> </form>
</template> </template>
<template #modalAction> <template #modalAction>
<button <button
type="reset" type="button"
class="btn btn-outline-success mr-2" class="btn btn-outline-success mr-2"
@click.prevent="onCancel" @click.prevent="onCancel"
:disabled="loading"
> >
{{ $t("button.cancel") }} {{ $t("button.cancel") }}
</button> </button>
<button <button
type="submit" type="button"
class="btn btn-outline-success" class="btn btn-outline-success"
@click.stop.prevent="onOk" @click.stop.prevent="onOk"
:disabled="loading"
> >
<span v-if="loading" class="loading loading-spinner loading-xs mr-2" />
{{ $t("button.submit") }} {{ $t("button.submit") }}
</button> </button>
</template> </template>