import {createApplicationServiceComponent} from "../Application/service/createApplicationServiceComponent"; import {closeDialogServiceHook, destroyDialogServiceHook, dialogCustomOptionKeys, DialogServiceEditType, iDialogServiceCustomOption, iDialogServiceOption} from "./dialog.service.utils"; import {computed, createHooks, getComponentCls, onBeforeUnmount, reactive, ref, toRaw, useRefs} from "@peryl/react-compose"; import {Dialog} from "../Dialog"; import {i18n} from "../../i18n"; import {DialogAlign, DialogCloseType, DialogPositions} from "../Dialog/utils/dialog.utils"; import './dialog-service.scss'; import {Button} from "../Button"; import Icon from "../Icon"; import {ThemeStatusIcons} from "../Application/theme/theme.utils"; import Input from "../Input"; import InputNumber from "../InputNumber"; import $message from "../$message"; /** * 弹框服务单个组件 * @author 韦胜健 * @date 2022.5.3 14:29 */ export const DialogService = createApplicationServiceComponent({ name: 'dialog-service', setup: ({ option, destroy }) => { const onClose = createHooks<() => void>(); const { refs, onRef } = useRefs({ input: Input, number: InputNumber, }); const isShow = ref(false); const isOpen = ref(false); const state = reactive({ option, editValue: null as any, }); const targetOption = computed(() => { const customOption: iDialogServiceCustomOption = {}; const dialogProps: Partial = {}; Object.keys(state.option).forEach((key) => { if (dialogCustomOptionKeys.indexOf(key) > -1) { (customOption as any)[key] = (state.option as any)[key]; } else { (dialogProps as any)[key] = (state.option as any)[key]; } }); if (!dialogProps.position) {dialogProps.position = DialogPositions.center;} /** * 以下设置只会在弹框为居中位置的时候才会启用 * @author 韦胜健 * @date 2022.5.4 21:54 */ if (dialogProps.position === DialogPositions.center) { if (!dialogProps.headAlign) {dialogProps.headAlign = DialogAlign.center;} if (!dialogProps.contentAlign && !customOption.render) {dialogProps.contentAlign = DialogAlign.center;} if (!dialogProps.footAlign) {dialogProps.footAlign = DialogAlign.center;} if (dialogProps.closeType === undefined) {dialogProps.closeType = DialogCloseType.triangle;} if (dialogProps.maxWidth == null) {dialogProps.maxWidth = '50vw';} if (dialogProps.maxHeight == null) {dialogProps.maxHeight = '80vh';} if (dialogProps.movable == null) {dialogProps.movable = true;} } dialogProps.externalClass = `${dialogProps.externalClass || ''} ${getComponentCls('dialog-service')}`.trim(); if (!!customOption.status) {dialogProps.externalClass = `${dialogProps.externalClass || ''} dialog-service-status-${customOption.status}`;} return { customOption, dialogProps, }; }); async function service(option: iDialogServiceOption) { state.option = option; state.editValue = option.editValue; isShow.value = true; } function hide() {isShow.value = false;} /** * 关闭dialog service观察者对象,没有id表示关闭所有弹框服务 * @author 韦胜健 * @date 2022.5.4 21:46 */ onBeforeUnmount(closeDialogServiceHook.use(id => {(!id || (id === state.option.id)) && (hide());})); /** * 销毁dialog service观察者对象,用来销毁hold类型的服务 * @author 韦胜健 * @date 2022/6/2 18:00 */ onBeforeUnmount(destroyDialogServiceHook.use(opt => { if (opt === state.option || opt === toRaw(state.option)) { if (!isOpen.value) { /*如果已经关闭,则直接销毁*/ destroy(); } else { /*没有关闭则等待关闭之后销毁*/ isShow.value = false; const eject = onClose.use(() => { eject(); destroy(); }); } } })); onBeforeUnmount(onClose.use(() => { targetOption.value.dialogProps.onClose?.(); /*如果服务为持有服务,则关闭时不销毁*/ !targetOption.value.customOption.hold && destroy(); })); return { refer: { service, hide, isShow, isOpen, }, render: () => { /*---------------------------------------head-------------------------------------------*/ const head = targetOption.value.customOption.renderHead || (() => (
{!!targetOption.value.customOption.status && } {targetOption.value.dialogProps.title || i18n.$it('base.defaultTitle').d('提示')}
)); /*---------------------------------------foot-------------------------------------------*/ const foot = targetOption.value.customOption.renderFoot || (() => !targetOption.value.dialogProps.confirmButton && !targetOption.value.dialogProps.cancelButton && (