forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel-toggle.js
More file actions
116 lines (102 loc) · 2.66 KB
/
model-toggle.js
File metadata and controls
116 lines (102 loc) · 2.66 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* eslint prefer-promise-reject-errors: 0 */
import History from '../plugins/history'
export default {
props: {
value: Boolean
},
data () {
return {
showing: false
}
},
watch: {
value (val) {
if (this.disable && val) {
this.$emit('input', false)
return
}
this.$nextTick(() => {
if (this.value !== this.showing) {
this[val ? 'show' : 'hide']()
}
})
}
},
methods: {
toggle (evt) {
return this[this.showing ? 'hide' : 'show'](evt)
},
show (evt) {
if (this.disable || this.showing) {
return this.showPromise || Promise.resolve(evt)
}
this.hidePromise && this.hidePromiseReject()
this.showing = true
if (this.value === false) {
this.$emit('input', true)
}
if (this.$options.modelToggle === void 0 || this.$options.modelToggle.history) {
this.__historyEntry = {
handler: this.hide
}
History.add(this.__historyEntry)
}
if (!this.__show) {
this.$emit('show', evt)
return Promise.resolve(evt)
}
this.showPromise = new Promise((resolve, reject) => {
this.showPromiseResolve = () => {
this.showPromise = null
this.$emit('show', evt)
resolve(evt)
}
this.showPromiseReject = () => {
this.showPromise = null
reject(null) // eslint prefer-promise-reject-errors: 0
}
})
this.__show(evt)
return this.showPromise || Promise.resolve(evt)
},
hide (evt) {
if (this.disable || !this.showing) {
return this.hidePromise || Promise.resolve(evt)
}
this.showPromise && this.showPromiseReject()
this.showing = false
if (this.value === true) {
this.$emit('input', false)
}
if (this.__historyEntry) {
History.remove(this.__historyEntry)
this.__historyEntry = null
}
if (!this.__hide) {
this.$emit('hide', evt)
return Promise.resolve()
}
this.hidePromise = new Promise((resolve, reject) => {
this.hidePromiseResolve = () => {
this.hidePromise = null
this.$emit('hide', evt)
resolve()
}
this.hidePromiseReject = () => {
this.hidePromise = null
reject(null)
}
})
this.__hide(evt)
return this.hidePromise || Promise.resolve(evt)
}
},
beforeDestroy () {
if (this.showing) {
this.showPromise && this.showPromiseReject()
this.hidePromise && this.hidePromiseReject()
this.$emit('input', false)
this.__hide && this.__hide()
}
}
}