Completed
Push — master ( 4a55ed...a53e8c )
by Ajeh
26s
created

directives.js ➔ Directives   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

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 5
rs 9.4285
nop 1
1
// Directives
2
3
import {noop} from './utilities'
4
5
6
let Directives = function (Vue) {
7
    this.Vue = Vue
8
    this.confirmDefinition = this.defineConfirm()
9
    this.alertDefinition = this.defineAlert()
10
}
11
12
Directives.prototype.defineConfirm = function () {
13
    const _this = this
14
    const DirectiveDefinition = {}
15
16
    const clickHandler = function (event, el, binding) {
17
        event.preventDefault()
18
        event.stopImmediatePropagation()
19
20
        let confirmMessage = (function () {
21
            if (binding.value && binding.value.message) {
22
                return binding.value.message
23
            }
24
            return typeof binding.value === 'string' ? binding.value : null
25
        })()
26
27
        let thenCallback = (function () {
28
            if (binding.value && binding.value.ok) {
29
                return binding.value.ok
30
            } else {
31
                return () => {
32
                    el.removeEventListener('click', el.VuejsDialog.clickHandler, true)
33
34
                    _this.Vue.nextTick(() => {
35
                        (function (node) {
36
                            if (document.createEvent) {
37
                                let evt = document.createEvent('MouseEvents');
38
                                evt.initEvent('click', true, false);
39
                                node.dispatchEvent(evt);
40
                            } else if (document.createEventObject) {
41
                                node.fireEvent('onclick');
42
                            } else if (typeof node.onclick === 'function') {
43
                                node.onclick();
44
                            }
45
                        })(el)
46
47
                        el.addEventListener('click', el.VuejsDialog.clickHandler, true)
48
                    })
49
                }
50
            }
51
        })()
52
53
        let catchCallback = (function () {
54
            if (binding.value && binding.value.cancel) {
55
                return binding.value.cancel
56
            }
57
            return noop
58
        })()
59
60
        _this.Vue.dialog.confirm(confirmMessage).then(thenCallback).catch(catchCallback)
61
    }
62
63
    DirectiveDefinition.bind = (el, binding) => {
64
        if (el.VuejsDialog === undefined) {
65
            el.VuejsDialog = {}
66
        }
67
68
        el.VuejsDialog.clickHandler = function clickEventHandler(event) {
69
            clickHandler(event, el, binding)
70
        }
71
72
        el.addEventListener('click', el.VuejsDialog.clickHandler, true)
73
    }
74
75
    DirectiveDefinition.unbind = (el) => {
76
        el.removeEventListener('click', el.VuejsDialog.clickHandler, true)
77
    }
78
79
    return DirectiveDefinition
80
}
81
82
Directives.prototype.defineAlert = function () {
83
    //
84
}
85
86
export default Directives
87