-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathdialog-mixin.js
102 lines (97 loc) · 2.22 KB
/
dialog-mixin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'use strict'
/**
* Created by Emmy on 3/23/2018.
*/
import { DIALOG_TYPES, CONFIRM_TYPES } from '../../js/constants'
import MessageMixin from '../../js/mixins/message-mixin'
import ButtonMixin from '../../js/mixins/btn-mixin'
export default {
data: function () {
return {
input: '',
loading: false
}
},
props: {
options: {
type: Object,
required: true
}
},
computed: {
loaderEnabled () {
return !!this.options.loader
},
isHardConfirm () {
return this.options.window === DIALOG_TYPES.CONFIRM &&
this.options.type === CONFIRM_TYPES.HARD
},
isPrompt () {
return (this.options.window === DIALOG_TYPES.PROMPT)
},
leftBtnComponent () {
return (this.options.reverse === false) ? 'cancel-btn' : 'ok-btn'
},
rightBtnComponent () {
return (this.options.reverse === true) ? 'cancel-btn' : 'ok-btn'
},
hardConfirmHelpText () {
return this.options.verificationHelp
.replace(/\[\+:(\w+)]/g, (match, $1) => {
return this.options[$1] || match
})
},
promptHelpText () {
return this.options.promptHelp
.replace(/\[\+:(\w+)]/g, (match, $1) => {
return this.options[$1] || match
})
}
},
mounted () {
this.isHardConfirm && this.$refs.inputElem && this.$refs.inputElem.focus()
},
methods: {
clickRightBtn () {
this.options.reverse ? this.cancel() : this.proceed(this.getDefaultData())
},
clickLeftBtn () {
this.options.reverse ? this.proceed(this.getDefaultData()) : this.cancel()
},
submitDialogForm () {
this.okBtnDisabled || this.proceed(this.getDefaultData())
},
getDefaultData () {
return this.isPrompt ? this.input : null
},
proceed (withData = null) {
if (this.loaderEnabled) {
this.switchLoadingState(true)
this.options.promiseResolver({
close: this.close,
loading: this.switchLoadingState,
data: withData
})
} else {
this.options.promiseResolver({
data: withData
})
this.close()
}
},
cancel () {
if (this.loading === true) { return }
this.close()
},
switchLoadingState (loading = null) {
if (loading === null) {
loading = !this.loading
}
this.loading = !!loading
},
close () {
this.$emit('close')
}
},
mixins: [MessageMixin, ButtonMixin]
}