|
1 | | -import { h, render, createVNode } from 'vue'; |
2 | | -import Dialog from './Dialog.vue'; |
| 1 | +import { render, createVNode } from 'vue'; |
| 2 | +import AlertDialog from './MessageDialog.vue'; |
| 3 | +import { isBlank } from 'ph-utils'; |
3 | 4 |
|
4 | | -export default { |
5 | | - alert() { |
6 | | - return new Promise((resolve, reject) => { |
7 | | - const vnode = createVNode( |
8 | | - Dialog, |
9 | | - { |
10 | | - title: 'Alert', |
11 | | - modelValue: true, |
12 | | - 'onUpdate:modelValue': onUpdate, |
13 | | - }, |
14 | | - () => 'Alert', |
15 | | - ); |
16 | | - function onUpdate(val: boolean) { |
17 | | - if (vnode.component != null) { |
18 | | - vnode.component.props.modelValue = val; |
19 | | - } |
| 5 | +function getContainer() { |
| 6 | + return document.createElement('div'); |
| 7 | +} |
| 8 | + |
| 9 | +type MessageOption = { |
| 10 | + showCancel?: boolean; |
| 11 | + type?: string; |
| 12 | +}; |
| 13 | + |
| 14 | +function alert(message: string, title?: string, option?: MessageOption) { |
| 15 | + return new Promise((resolve) => { |
| 16 | + const opts = { showCancel: false, ...option }; |
| 17 | + let container = getContainer(); |
| 18 | + const onClose = (action: 'close' | 'ok') => { |
| 19 | + resolve(action === 'ok' ? true : false); |
| 20 | + if (vnode != null && vnode.component != null) { |
| 21 | + vnode.component.props.show = false; |
20 | 22 | } |
21 | | - render(vnode, document.body); |
22 | | - }); |
23 | | - }, |
| 23 | + vnode = undefined as any; |
| 24 | + container = undefined as any; |
| 25 | + }; |
| 26 | + const props: any = { |
| 27 | + message, |
| 28 | + show: true, |
| 29 | + showCancel: opts.showCancel, |
| 30 | + onClose, |
| 31 | + to: container, |
| 32 | + }; |
| 33 | + |
| 34 | + if (!isBlank(title)) { |
| 35 | + props.title = title; |
| 36 | + } |
| 37 | + |
| 38 | + if (!isBlank(opts.type)) { |
| 39 | + props.type = opts.type; |
| 40 | + } |
| 41 | + |
| 42 | + let vnode = createVNode(AlertDialog, props); |
| 43 | + render(vnode, container); |
| 44 | + document.body.appendChild(container.firstElementChild as any); |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +function confirm(message: string, title?: string, option?: MessageOption) { |
| 49 | + const opts = { type: 'info', showCancel: true, ...option }; |
| 50 | + return alert(message, title, opts); |
| 51 | +} |
| 52 | + |
| 53 | +export default { |
| 54 | + alert, |
| 55 | + confirm, |
24 | 56 | }; |
0 commit comments