|
@@ -0,0 +1,103 @@
|
|
|
+<script setup lang="ts">
|
|
|
+import {DialogState, dialogStore} from "./index";
|
|
|
+import {ElButton} from "element-plus";
|
|
|
+import XEUtils from "xe-utils";
|
|
|
+import sleep from "@/utils/sleep";
|
|
|
+import {nextTick} from "vue";
|
|
|
+
|
|
|
+function next(item: DialogState, data: any) {
|
|
|
+ if (item.closeValue === "confirm") {
|
|
|
+ item.resolve(data);
|
|
|
+ } else {
|
|
|
+ item.reject({value: item.closeValue, data});
|
|
|
+ }
|
|
|
+ item.visible = false;
|
|
|
+}
|
|
|
+
|
|
|
+async function handleCancel(item: DialogState, emits = false, value = null) {
|
|
|
+ if (emits) {
|
|
|
+ item.closeValue = "cancel";
|
|
|
+ next(item, value);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ const fn = item.componentRef?.cancel ?? XEUtils.noop;
|
|
|
+ const data = await fn();
|
|
|
+ item.closeValue = "cancel";
|
|
|
+ next(item, data);
|
|
|
+ } catch {
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+async function handleConfirm(item: DialogState, emits = false, value = null) {
|
|
|
+ if (emits) {
|
|
|
+ item.closeValue = "confirm";
|
|
|
+ next(item, value);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const fn = item.componentRef?.confirm ?? XEUtils.noop;
|
|
|
+ try {
|
|
|
+ const data = await fn();
|
|
|
+ item.closeValue = "confirm";
|
|
|
+ next(item, data);
|
|
|
+ } catch {
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+async function closed(item: DialogState, index: number) {
|
|
|
+ item.reject({value: item.closeValue, data: null});
|
|
|
+ await nextTick();
|
|
|
+ await sleep();
|
|
|
+ dialogStore.value.splice(index, 1);
|
|
|
+}
|
|
|
+
|
|
|
+function setRef(el, item) {
|
|
|
+ item.componentRef = el;
|
|
|
+}
|
|
|
+</script>
|
|
|
+
|
|
|
+<template>
|
|
|
+ <el-dialog
|
|
|
+ v-for="(item, index) in dialogStore"
|
|
|
+ :key="`Cy-dialog_${item.dialogKey}`"
|
|
|
+ v-model="item.visible"
|
|
|
+ draggable
|
|
|
+ v-bind="item.dialogProps"
|
|
|
+ @closed="closed(item, index)"
|
|
|
+ >
|
|
|
+ <component
|
|
|
+ :is="item.component"
|
|
|
+ :ref="el => setRef(el, item)"
|
|
|
+ v-bind="item.propsData"
|
|
|
+ @cyDialogCancel="value => handleCancel(item, true, value)"
|
|
|
+ @cyDialogConfirm="value => handleConfirm(item, true, value)"
|
|
|
+ />
|
|
|
+
|
|
|
+ <template v-if="item.showCancel || item.showConfirm" #footer>
|
|
|
+ <el-button size="default" @click="handleCancel(item)">
|
|
|
+ {{ item.cancelText || "取消" }}
|
|
|
+ </el-button>
|
|
|
+
|
|
|
+ <el-button
|
|
|
+ type="primary"
|
|
|
+ size="default"
|
|
|
+ color="hsl(240 5.9% 10%)"
|
|
|
+ @click="handleConfirm(item)"
|
|
|
+ >
|
|
|
+ {{ item.confirmText || "确认" }}
|
|
|
+ </el-button>
|
|
|
+ </template>
|
|
|
+ </el-dialog>
|
|
|
+</template>
|
|
|
+
|
|
|
+<style lang="scss">
|
|
|
+.el-dialog.is-fullscreen {
|
|
|
+ display: flex;
|
|
|
+ flex-direction: column;
|
|
|
+
|
|
|
+ .el-dialog__body {
|
|
|
+ flex: 1;
|
|
|
+ height: 0;
|
|
|
+ }
|
|
|
+}
|
|
|
+</style>
|