Completed
Push — master ( 53b8d5...da1ee2 )
by Ajeh
28s
created

Plugin.open   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 11
rs 9.4285
nop 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 9 1
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.globalOptions = globalOptions
12
13
    this.Dialog = (() => {
14
        let DialogConstructor = Vue.extend(DialogComponent)
15
        let node = document.createElement("div")
16
        document.querySelector('body').appendChild(node)
17
18
        return (new DialogConstructor()).$mount(node)
19
    })()
20
}
21
22
Plugin.prototype.alert = function(message = null, options = {}){
23
	message && (options.message = message)
24
	return this.open(DIALOG_TYPES.ALERT, options)
25
}
26
27
Plugin.prototype.confirm = function(message = null, options = {}){
28
    message && (options.message = message)
29
    return this.open(DIALOG_TYPES.CONFIRM, options)
30
}
31
32
Plugin.prototype.open = function(type, localOptions = {}){
33
	return new Promise((resolve, reject) => {
34
35
        localOptions.id = 'dialog.' + Date.now()
36
        localOptions.window = type
37
        localOptions.promiseResolver = resolve
38
        localOptions.promiseRejecter = reject
39
40
		this.Dialog.commit(mergeObjs(DEFAULT_OPTIONS, this.globalOptions, localOptions))
41
	})
42
}
43
44
Plugin.install = function (Vue, options) {
45
46
    let DirectivesObj = new Directives(Vue)
47
48
	Vue.directive('confirm', DirectivesObj.confirmDefinition)
49
50
	//Vue.directive('alert', DirectivesObj.alertDefinition)
51
52
	Vue.dialog = new Plugin(Vue, options)
53
54
	Object.defineProperties(Vue.prototype, {
55
		$dialog: {
56
			get () {
57
				return Vue.dialog
58
			}
59
		}
60
	})
61
62
}
63
64
export default Plugin