-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathindex.js
107 lines (83 loc) · 2.55 KB
/
index.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict'
import Promise from 'promise-polyfill'
import DialogComponent from '../components/dialog.vue'
import { DIALOG_TYPES, DEFAULT_OPTIONS } from './constants'
import Directives from './directives'
import { mergeObjs } from './utilities'
const registeredViews = {}
let Plugin = function (Vue, globalOptions = {}) {
this.Vue = Vue
this.mounted = false
this.$root = {} // The root component
this.registeredViews = {} // Custom components
this.globalOptions = mergeObjs(DEFAULT_OPTIONS, globalOptions)
}
Plugin.prototype.mountIfNotMounted = function () {
if (this.mounted === true) {
return
}
this.$root = (() => {
let DialogConstructor = this.Vue.extend(DialogComponent)
let node = document.createElement('div')
document.querySelector('body').appendChild(node)
let Vm = new DialogConstructor(this.globalOptions.forwardPlugin)
Vm.registeredViews = this.registeredComponents()
return Vm.$mount(node)
})()
this.mounted = true
}
Plugin.prototype.registeredComponents = function () {
return registeredViews
}
Plugin.prototype.registerComponent = function (name, definition) {
if (this.mounted) {
this.destroy()
}
// registeredViews[name] = this.Vue.extend(definition)
registeredViews[name] = definition
}
Plugin.prototype.destroy = function () {
if (this.mounted === true) {
this.$root.forceCloseAll()
let elem = this.$root.$el
this.$root.$destroy()
this.$root.$off()
elem.remove()
this.mounted = false
}
}
Plugin.prototype.alert = function (message = null, options = {}) {
message && (options.message = message)
return this.open(DIALOG_TYPES.ALERT, options)
}
Plugin.prototype.prompt = function (message = null, options = {}) {
message && (options.message = message)
return this.open(DIALOG_TYPES.PROMPT, options)
}
Plugin.prototype.confirm = function (message = null, options = {}) {
message && (options.message = message)
return this.open(DIALOG_TYPES.CONFIRM, options)
}
Plugin.prototype.open = function (type, localOptions = {}) {
this.mountIfNotMounted()
return new Promise((resolve, reject) => {
localOptions.id = 'dialog.' + Date.now()
localOptions.window = type
localOptions.promiseResolver = resolve
localOptions.promiseRejecter = reject
this.$root.commit(mergeObjs(this.globalOptions, localOptions))
})
}
Plugin.install = function (Vue, options) {
let DirectivesObj = new Directives(Vue)
Vue.directive('confirm', DirectivesObj.confirmDefinition)
Vue.dialog = new Plugin(Vue, options)
Object.defineProperties(Vue.prototype, {
$dialog: {
get () {
return Vue.dialog
}
}
})
}
export default Plugin