Completed
Push — master ( ece39f...21e195 )
by Ajeh
28s
created

Directives.defineConfirm   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
b 0
f 0
nc 1
dl 0
loc 80
rs 8.8387
nop 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A 0 12 4
A 0 6 4
A 0 14 3
A 0 6 3
A 0 11 2
A 0 14 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
// Directives
2
3
import {noop, clickNode} from './utilities'
4
import {CONFIRM_TYPES} from './constants'
5
6
7
let Directives = function (Vue) {
8
    Object.defineProperties(this, {
9
        Vue: {get: () => Vue},
10
        confirmDefinition: {
11
            get: this.defineConfirm
12
        },
13
        alertDefinition: {
14
            get: this.defineAlert
15
        }
16
    })
17
}
18
19
Directives.prototype.defineConfirm = function () {
20
    const _this = this
21
    const DirectiveDefinition = {}
22
23
    const getConfirmMessage = function(binding) {
24
        if (binding.value && binding.value.message) {
25
            return binding.value.message
26
        }
27
        return typeof binding.value === 'string' ? binding.value : null
28
    }
29
30
    const getOptions = function(binding) {
31
        let options = typeof binding.value === 'object' ? binding.value : {}
32
33
        delete options['ok']
34
        delete options['cancel']
35
36
        if(binding.arg && CONFIRM_TYPES.hasOwnProperty(binding.arg.toUpperCase())){
37
            options.type = CONFIRM_TYPES[binding.arg.toUpperCase()]
38
        }
39
40
        return options
41
    }
42
43
    const getCatchCallback = function(binding) {
44
        if (binding.value && binding.value.cancel) {
45
            return binding.value.cancel
46
        }
47
        return noop
48
    }
49
50
    const getThenCallback = function(binding, el){
51
        if (binding.value && binding.value.ok) {
52
            return binding.value.ok
53
        } else {
54
            return () => {
55
                // Unbind to allow original event
56
                el.removeEventListener('click', el.VuejsDialog.clickHandler, true)
57
                // Trigger original event
58
                clickNode(el)
59
                // Re-bind listener
60
                el.addEventListener('click', el.VuejsDialog.clickHandler, true)
61
            }
62
        }
63
    }
64
65
    const clickHandler = function (event, el, binding) {
66
        event.preventDefault()
67
        event.stopImmediatePropagation()
68
69
        let options = getOptions(binding)
70
        let confirmMessage = getConfirmMessage(binding)
71
        let thenCallback = getThenCallback(binding, el)
72
        let catchCallback = getCatchCallback(binding)
73
74
        _this.Vue.dialog
75
            .confirm(confirmMessage, options)
76
            .then(thenCallback)
77
            .catch(catchCallback)
78
    }
79
80
81
    DirectiveDefinition.bind = (el, binding) => {
82
        if (el.VuejsDialog === undefined) {
83
            el.VuejsDialog = {}
84
        }
85
86
        el.VuejsDialog.clickHandler = function (event) {
87
            clickHandler(event, el, binding)
88
        }
89
90
        el.addEventListener('click', el.VuejsDialog.clickHandler, true)
91
    }
92
93
    DirectiveDefinition.unbind = (el) => {
94
        el.removeEventListener('click', el.VuejsDialog.clickHandler, true)
95
    }
96
97
    return DirectiveDefinition
98
}
99
100
Directives.prototype.defineAlert = function () {
101
    // Still Considering it uses case.
102
}
103
104
export default Directives
105