1
|
|
|
// Directives |
2
|
|
|
|
3
|
|
|
import {noop, clickNode, cloneObj} 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
|
|
|
}) |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
Directives.prototype.getConfirmMessage = function(binding) { |
17
|
|
|
if (binding.value && binding.value.message) { |
18
|
|
|
return binding.value.message |
19
|
|
|
} |
20
|
|
|
return typeof binding.value === 'string' ? binding.value : null |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
Directives.prototype.getOptions = function(binding) { |
24
|
|
|
let options = typeof binding.value === 'object' ? cloneObj(binding.value) : {} |
25
|
|
|
|
26
|
|
|
delete options['ok'] |
27
|
|
|
delete options['cancel'] |
28
|
|
|
|
29
|
|
|
if(binding.arg && CONFIRM_TYPES.hasOwnProperty(binding.arg.toUpperCase())){ |
30
|
|
|
options.type = CONFIRM_TYPES[binding.arg.toUpperCase()] |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return options |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
Directives.prototype.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
|
|
|
Directives.prototype.getCatchCallback = function(binding) { |
52
|
|
|
if (binding.value && binding.value.cancel) { |
53
|
|
|
return binding.value.cancel |
54
|
|
|
} |
55
|
|
|
return noop |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
Directives.prototype.clickHandler = function(event, el, binding) { |
59
|
|
|
event.preventDefault() |
60
|
|
|
event.stopImmediatePropagation() |
61
|
|
|
|
62
|
|
|
let options = this.getOptions(binding) |
63
|
|
|
let confirmMessage = this.getConfirmMessage(binding) |
64
|
|
|
let thenCallback = this.getThenCallback(binding, el) |
65
|
|
|
let catchCallback = this.getCatchCallback(binding) |
66
|
|
|
|
67
|
|
|
this.Vue.dialog |
68
|
|
|
.confirm(confirmMessage, options) |
69
|
|
|
.then(thenCallback) |
70
|
|
|
.catch(catchCallback) |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
Directives.prototype.defineConfirm = function () { |
74
|
|
|
const DirectiveDefinition = {} |
75
|
|
|
|
76
|
|
|
DirectiveDefinition.bind = (el, binding) => { |
77
|
|
|
el.VuejsDialog = el.VuejsDialog || {} |
78
|
|
|
|
79
|
|
|
el.VuejsDialog.clickHandler = event => this.clickHandler(event, el, binding) |
80
|
|
|
|
81
|
|
|
el.addEventListener('click', el.VuejsDialog.clickHandler, true) |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
DirectiveDefinition.unbind = (el) => { |
85
|
|
|
el.removeEventListener('click', el.VuejsDialog.clickHandler, true) |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return DirectiveDefinition |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
export default Directives |
92
|
|
|
|