/
CPromptToast.vue
257 lines (223 loc) · 5.89 KB
/
CPromptToast.vue
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<template>
<div v-if="!hideToasts">
<b-toast
v-for="({ prompt, component, passive }) in toasts"
:id="'wfPromptToast-'+prompt.stateID"
:key="'wfPromptToast-'+prompt.stateID"
:variant="pVal(prompt, 'variant', 'primary')"
:visible="!isActive"
solid
:no-auto-hide="!passive"
:auto-hide-delay="pVal(prompt, 'timeout', defaultTimeout) * 1000"
@hide="onToastHide({ prompt, passive })"
>
<template #toast-title>
<strong>{{ pVal(prompt, 'title', 'Workflow prompt') }}</strong>
</template>
<component
v-if="component"
:is="component"
:payload="prompt.payload"
:loading="isLoading"
@submit="resumeToast({ input: $event, prompt })"
/>
</b-toast>
</div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex'
import definitions from './kinds/index.ts'
import { pVal } from './utils.ts'
export default {
name: 'c-prompt-toast',
props: {
hideToasts: {
type: Boolean,
},
},
data () {
return {
passive: new Set(),
hasFocus: null,
hasFocusObserver: 0,
}
},
computed: {
...mapGetters({
prompts: 'wfPrompts/all',
isActive: 'wfPrompts/isActive',
isLoading: 'wfPrompts/isLoading',
}),
/**
* Prompts with handlers, observed with "watch"
*
* Prompts are only returned when document has focus!
*
* @returns {*}
*/
withHandlers () {
return (this.hasFocus ? this.prompts : [])
.filter(({ ref }) => !!definitions[ref] && !!definitions[ref].handler)
.map(prompt => ({ ...definitions[prompt.ref], prompt }))
},
/**
* Prompts with components
*
* Prompts are only returned when document has focus!
*
* @returns {*}
*/
withComponents () {
return (this.hasFocus ? this.prompts : [])
.filter(({ ref }) => !!definitions[ref] && !!definitions[ref].component)
.map(prompt => ({ ...definitions[prompt.ref], prompt }))
},
/**
* All non-passive prompts with components
*/
active() {
return this.withComponents.filter(({ passive }) => !passive)
},
/**
* Returns list of prompts that we can interpret as toasts: display component is defined
*
* Toasts (prompts with components) are displayed in order received but
* passive (no feedback or input from user required) first and the rest later
*/
toasts () {
return this.hideToasts ? [] : [
...this.passive.values(),
...this.active
]
},
defaultTimeout () {
return 7
}
},
watch: {
// watch prompts with handlers and when a new one arrives
// shift it from the stack, resume the prompt and handle it
withHandlers (hh) {
if (hh.length > 0) {
const { handler, prompt } = hh.shift()
this.resume({ input: {}, prompt }).then(() => {
handler.call(this, prompt.payload)
})
}
},
/**
* Make a copy of prompt if it is defined as passive
*
* We do this because we do not want it to be removed right away
* but through a toast component's timeout
*/
withComponents: {
immediate: true,
handler (wc) {
wc.forEach(p => {
if (p.passive) {
this.passive.add(p)
}
})
},
},
},
mounted () {
this.setDocumentFocusObserver()
},
beforeDestroy () {
this.clearDocumentFocusObserver()
this.setDefaultValues()
},
methods: {
...mapActions({
resume: 'wfPrompts/resume',
cancel: 'wfPrompts/cancel',
activate: 'wfPrompts/activate',
}),
resumeToast (values) {
// Only reset input if prompt is kept open
if (values.input && values.input.keep) {
values.input = {}
}
this.resume(values)
},
onToastHide ({ prompt, passive}) {
if (passive) return
this.cancel(prompt)
},
pVal (prompt, k, def = undefined) {
return pVal(prompt.payload, k, def)
},
clearDocumentFocusObserver() {
if (this.hasFocusObserver) {
window.clearInterval(this.hasFocusObserver)
}
},
/**
* Interval check if window has focus
*/
setDocumentFocusObserver() {
this.clearDocumentFocusObserver()
this.hasFocusObserver = window.setInterval(() => {
const f = document.hasFocus()
if (this.hasFocus !== f) {
this.hasFocus = f
}
}, 1000)
},
setDefaultValues () {
this.toasts = []
this.hasFocus = null
this.hasFocusObserver = 0
},
},
}
</script>
<style lang="scss">
.toast-header {
align-items: start;
padding: 0.375rem 0.75rem;
strong {
word-break: break-word;
}
.close {
margin-bottom: 0 !important;
}
}
// .b-toaster-leave-active {
// width: 100%;
// }
.b-toaster {
&.b-toaster-top-right,
&.b-toaster-top-left,
&.b-toaster-bottom-right,
&.b-toaster-bottom-left {
.b-toast {
&.b-toaster-enter-active,
&.b-toaster-leave-active,
&.b-toaster-move {
transition: transform 0.3s ease-in-out; /* Adjust the timing function for smoother transitions */
opacity: 1; /* Ensure opacity is set to avoid flickering during transition */
}
&.b-toaster-enter {
transform: translate(0, -100%); /* Start off-screen when entering */
opacity: 0; /* Start with 0 opacity */
}
&.b-toaster-enter-to,
&.b-toaster-enter-active {
transform: translate(0, 0); /* Move to the visible position */
opacity: 1; /* Fade in during the transition */
}
&.b-toaster-leave-active {
position: absolute;
transform: translate(0, -100%); /* Move off-screen when leaving */
opacity: 0; /* Fade out during the transition */
}
&.b-toaster-leave-to {
opacity: 0; /* Ensure 0 opacity at the end of the transition */
}
}
}
}
</style>