Completed
Push — master ( 33fc86...4cc37a )
by Ajeh
28s
created

Directives.defineConfirm   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 2
dl 0
loc 14
rs 9.4285
nop 2
1
// Directives
2
3
import {noop, clickNode} from './utilities'
4
5
6
let Directives = function (Vue) {
7
    Object.defineProperties(this, {
8
        Vue: {get: () => Vue},
9
        confirmDefinition: {
10
            get: this.defineConfirm
11
        },
12
        alertDefinition: {
13
            get: this.defineAlert
14
        }
15
    })
16
}
17
18
Directives.prototype.defineConfirm = function () {
19
    const _this = this
20
    const DirectiveDefinition = {}
21
22
    const getConfirmMessage = function(binding) {
23
        if (binding.value && binding.value.message) {
24
            return binding.value.message
25
        }
26
        return typeof binding.value === 'string' ? binding.value : null
27
    }
28
29
    const getCatchCallback = function(binding) {
30
        if (binding.value && binding.value.cancel) {
31
            return binding.value.cancel
32
        }
33
        return noop
34
    }
35
36
    const getThenCallback = function(binding, el){
37
        if (binding.value && binding.value.ok) {
38
            return binding.value.ok
39
        } else {
40
            return () => {
41
                // Unbind to allow original event
42
                el.removeEventListener('click', el.VuejsDialog.clickHandler, true)
43
                // Trigger original event
44
                clickNode(el)
45
                // Re-bind listener
46
                el.addEventListener('click', el.VuejsDialog.clickHandler, true)
47
            }
48
        }
49
    }
50
51
    const clickHandler = function (event, el, binding) {
52
        event.preventDefault()
53
        event.stopImmediatePropagation()
54
55
        let confirmMessage = getConfirmMessage(binding)
56
        let thenCallback = getThenCallback(binding, el)
57
        let catchCallback = getCatchCallback(binding)
58
59
        _this.Vue.dialog
60
            .confirm(confirmMessage)
61
            .then(thenCallback)
62
            .catch(catchCallback)
63
    }
64
65
66
    DirectiveDefinition.bind = (el, binding) => {
67
        if (el.VuejsDialog === undefined) {
68
            el.VuejsDialog = {}
69
        }
70
71
        el.VuejsDialog.clickHandler = function clickEventHandler(event) {
72
            clickHandler(event, el, binding)
73
        }
74
75
        el.addEventListener('click', el.VuejsDialog.clickHandler, true)
76
    }
77
78
    DirectiveDefinition.unbind = (el) => {
79
        el.removeEventListener('click', el.VuejsDialog.clickHandler, true)
80
    }
81
82
    return DirectiveDefinition
83
}
84
85
Directives.prototype.defineAlert = function () {
86
    //
87
}
88
89
export default Directives
90