|
@@ -9,6 +9,7 @@ import {
|
|
|
DeleteFilled
|
|
|
} from '@element-plus/icons-vue'
|
|
|
import {IElMessageBox, MessageBoxType} from "./message-box.type";
|
|
|
+import {uuid} from '../../../utils/getUuid'
|
|
|
|
|
|
const iconData = {
|
|
|
success: {
|
|
@@ -38,6 +39,7 @@ const iconData = {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+const messageInstance = new Map <string, { close: () => void }>
|
|
|
|
|
|
const MESSAGE_BOX_VARIANTS = ['alert', 'confirm', 'prompt'] as const
|
|
|
const MESSAGE_BOX_DEFAULT_OPTS: {
|
|
@@ -75,10 +77,12 @@ function MessageBox(options: MessageBoxType) {
|
|
|
const div = document.createElement('div');
|
|
|
document.body.appendChild(div);
|
|
|
const icon = getIcon(options)
|
|
|
+ const id = options.boxId || 'cy' + uuid(5);
|
|
|
|
|
|
function clearDiv() {
|
|
|
render(null, div);
|
|
|
div.remove();
|
|
|
+ messageInstance.delete(id);
|
|
|
}
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
@@ -92,17 +96,31 @@ function MessageBox(options: MessageBoxType) {
|
|
|
},
|
|
|
onVanish: () => {
|
|
|
clearDiv();
|
|
|
+ },
|
|
|
+ setClose: (close: () => void) => {
|
|
|
+ if (messageInstance.has(id)) {
|
|
|
+ messageInstance.get(id)?.close()
|
|
|
+ }
|
|
|
+ messageInstance.set(id, {close})
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Object.assign(data, options, {...icon});
|
|
|
-
|
|
|
const vNode = createVNode(MessageBoxConstructor,
|
|
|
data,
|
|
|
null);
|
|
|
render(vNode, div);
|
|
|
});
|
|
|
+}
|
|
|
|
|
|
+MessageBox.close = (id: string) => {
|
|
|
+ if (id) {
|
|
|
+ messageInstance.get(id)?.close()
|
|
|
+ } else {
|
|
|
+ messageInstance.forEach((value, key) => {
|
|
|
+ messageInstance.get(key)?.close()
|
|
|
+ })
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
export default MessageBox as IElMessageBox;
|