1
|
|
|
'use strict' |
2
|
|
|
|
3
|
|
|
import Promise from 'promise-polyfill' |
4
|
|
|
import DialogComponent from '../components/dialog.vue' |
5
|
|
|
import {DIALOG_TYPES, DEFAULT_OPTIONS} from './constants' |
6
|
|
|
import Directives from './directives' |
7
|
|
|
import {mergeObjs} from './utilities' |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
let Plugin = function(Vue, globalOptions = {}){ |
11
|
|
|
this.Vue = Vue |
12
|
|
|
this.mounted = false |
13
|
|
|
this.Dialog = {} |
14
|
|
|
this.globalOptions = mergeObjs(DEFAULT_OPTIONS, globalOptions) |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
Plugin.prototype.mountIfNotMounted = function(){ |
18
|
|
|
if(this.mounted === true){ |
19
|
|
|
return |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
this.Dialog = (() => { |
23
|
|
|
let DialogConstructor = this.Vue.extend(DialogComponent) |
24
|
|
|
let node = document.createElement("div") |
25
|
|
|
document.querySelector('body').appendChild(node) |
26
|
|
|
|
27
|
|
|
return (new DialogConstructor()).$mount(node) |
28
|
|
|
})() |
29
|
|
|
|
30
|
|
|
this.mounted = true |
31
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
Plugin.prototype.alert = function(message = null, options = {}){ |
35
|
|
|
message && (options.message = message) |
36
|
|
|
return this.open(DIALOG_TYPES.ALERT, options) |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
Plugin.prototype.confirm = function(message = null, options = {}){ |
40
|
|
|
message && (options.message = message) |
41
|
|
|
return this.open(DIALOG_TYPES.CONFIRM, options) |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
Plugin.prototype.open = function(type, localOptions = {}){ |
45
|
|
|
this.mountIfNotMounted() |
46
|
|
|
return new Promise((resolve, reject) => { |
47
|
|
|
|
48
|
|
|
localOptions.id = 'dialog.' + Date.now() |
49
|
|
|
localOptions.window = type |
50
|
|
|
localOptions.promiseResolver = resolve |
51
|
|
|
localOptions.promiseRejecter = reject |
52
|
|
|
|
53
|
|
|
this.Dialog.commit(mergeObjs(this.globalOptions, localOptions)) |
54
|
|
|
}) |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
Plugin.install = function (Vue, options) { |
58
|
|
|
|
59
|
|
|
let DirectivesObj = new Directives(Vue) |
60
|
|
|
|
61
|
|
|
Vue.directive('confirm', DirectivesObj.confirmDefinition) |
62
|
|
|
|
63
|
|
|
Vue.dialog = new Plugin(Vue, options) |
64
|
|
|
|
65
|
|
|
Object.defineProperties(Vue.prototype, { |
66
|
|
|
$dialog: { |
67
|
|
|
get () { |
68
|
|
|
return Vue.dialog |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
}) |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
export default Plugin |