-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathpromise.dialog.ts
86 lines (67 loc) · 2.92 KB
/
promise.dialog.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict'
import Promise from 'promise-polyfill'
import DialogComponent from './components/DialogApp.vue'
import { DEFAULT_OPTIONS } from './constants'
import { mergeObjs } from './utilities'
import {createApp} from "vue";
import type {App, ComponentInstance} from "vue";
import {DialogTypeEnum} from "./interface";
import type {DialogWindowOptionsInterface} from "./interface";
interface DialogPluginOptions extends Omit<DialogWindowOptionsInterface, 'id'>{}
export class PromiseDialog {
private dgAppComponentInstance: ComponentInstance<typeof DialogComponent>;
private mounted = false;
constructor(
private readonly app: App,
private readonly globalOptions: Partial<DialogPluginOptions>,
) {
this.globalOptions = mergeObjs(DEFAULT_OPTIONS, globalOptions)
}
public open(type: DialogTypeEnum, message: string = undefined, localOptions: Partial<DialogWindowOptionsInterface> = {}) {
this.mountIfNotMounted()
return new Promise((resolve, reject) => {
localOptions.id = 'dialog.' + Date.now()
localOptions.window = type
localOptions.message = message
localOptions.promiseResolver = resolve
localOptions.promiseRejecter = reject
this.dgAppComponentInstance.commit(mergeObjs(this.globalOptions, localOptions))
})
}
public alert(message?: string, options?: Partial<DialogWindowOptionsInterface>) {
return this.open(DialogTypeEnum.alert, message, {
...(options || {}),
...(message ? {message} : {}),
})
}
public confirm(message?: string, options?: Partial<DialogWindowOptionsInterface>) {
return this.open(DialogTypeEnum.confirm, message, {
...(options || {}),
...(message ? {message} : {}),
})
}
public prompt(message?: string, options?: Partial<DialogWindowOptionsInterface>) {
return this.open(DialogTypeEnum.prompt, message, {
...(options || {}),
...(message ? {message} : {}),
})
}
private mountIfNotMounted(): void {
if (this.mounted) return
this.dgAppComponentInstance = (() => {
const connectAppContext = true
const dialogApp = createApp(DialogComponent)
const node = document.createElement('div')
document.querySelector('body').appendChild(node)
if (connectAppContext) {
dialogApp.config.globalProperties = this.app.config.globalProperties
dialogApp._context.components = this.app._context.components
dialogApp._context.directives = this.app._context.directives
dialogApp._context.mixins = this.app._context.mixins
dialogApp._context.provides = this.app._context.provides
}
return dialogApp.mount(node) as ComponentInstance<DialogComponent>
})()
this.mounted = true
}
}