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

src/plugin/js/index.js   A

Complexity

Total Complexity 8
Complexity/F 1

Size

Lines of Code 64
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 8
c 1
b 0
f 0
nc 4
mnd 0
bc 8
fnc 8
dl 0
loc 64
rs 10
bpm 1
cpm 1
noi 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A Plugin.open 0 11 1
A index.js ➔ Plugin 0 11 1
A Plugin.install 0 19 1
A Plugin.confirm 0 4 1
A Plugin.alert 0 4 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