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
|
|
|
|