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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
<template>
<b-modal
id="global-confirm-modal"
ref="modal"
v-model="show"
:title="titleToShow"
:focus="focusTarget"
no-close-on-backdrop
no-close-on-esc
no-header-close
@shown="onShown"
@hidden="onHidden"
>
<!-- Processing view (optional) -->
<div v-if="processing" class="text-center" aria-live="polite" role="status">
<div class="mb-3">{{ processingText }}</div>
<b-progress
:min="1"
:max="processingMax"
:value="processingCounter"
variant="success"
height="3px"
:aria-label="processingText"
/>
</div>
<!-- Confirmation content -->
<template v-else>
<!-- Support for multiple message lines -->
<template v-if="messageLines && messageLines.length > 0">
<p
v-for="(line, index) in messageLines"
:key="index"
class="mb-2"
:class="{ 'fw-bold': index === 0 }"
>
{{ line }}
</p>
</template>
<p v-else class="mb-3">{{ message }}</p>
<!-- Optional checkbox confirmation -->
<b-form-checkbox
v-if="confirmationText"
v-model="confirmed"
aria-describedby="confirm-checkbox-help"
>
{{ confirmationText }}
</b-form-checkbox>
<span
v-if="confirmationText"
id="confirm-checkbox-help"
class="visually-hidden"
>
{{ $t('global.ariaLabel.checkboxToEnableButton') }}
</span>
</template>
<template #footer>
<b-button
:variant="cancelVariant"
:disabled="processing"
@click="onCancel"
>
{{ cancelTitleToShow }}
</b-button>
<b-button
:variant="okVariant"
:disabled="isOkDisabled"
:aria-describedby="
isOkDisabled && confirmationText ? 'ok-disabled-reason' : undefined
"
@click="onOk"
>
{{ okTitleToShow }}
</b-button>
<span
v-if="confirmationText && !confirmed"
id="ok-disabled-reason"
class="visually-hidden"
>
{{ $t('global.ariaLabel.buttonDisabledUntilChecked') }}
</span>
</template>
</b-modal>
</template>
<script>
import eventBus from '@/eventBus';
import i18n from '@/i18n';
export default {
name: 'ConfirmModal',
data() {
return {
show: false,
message: '',
messageLines: [], // Array of strings for multi-paragraph messages
title: '',
okTitle: '',
cancelTitle: '',
okVariant: 'primary',
cancelVariant: 'secondary',
resolver: null,
requestQueue: [],
// Processing state (optional)
processing: false,
processingCounter: 1,
processingMax: 20,
processingIntervalId: null,
processingText: 'Processing...',
currentRequestProcessing: false,
autoFocusButton: 'cancel', // 'ok' or 'cancel'
// Checkbox confirmation (optional)
confirmationText: '',
confirmed: false,
};
},
computed: {
titleToShow() {
return this.title || i18n.global.t('global.action.confirm') || 'Confirm';
},
okTitleToShow() {
return this.okTitle || i18n.global.t('global.action.confirm') || 'OK';
},
cancelTitleToShow() {
return (
this.cancelTitle || i18n.global.t('global.action.cancel') || 'Cancel'
);
},
isOkDisabled() {
if (this.processing) return true;
// Only require checkbox confirmation if confirmationText is set
if (this.confirmationText && this.confirmationText.length > 0) {
return !this.confirmed;
}
return false;
},
focusTarget() {
// If there's a checkbox, focus it; otherwise use the autoFocusButton setting
if (this.confirmationText && this.confirmationText.length > 0) {
return '#global-confirm-modal .form-check-input';
}
return this.autoFocusButton === 'ok' ? 'ok' : 'cancel';
},
},
created() {
eventBus.$on('confirm:open', this.enqueue);
},
beforeUnmount() {
eventBus.$off('confirm:open', this.enqueue);
// Clean up keydown listener if still attached
if (this._onKeydown) {
document.removeEventListener('keydown', this._onKeydown, {
capture: true,
});
this._onKeydown = null;
}
},
methods: {
enqueue(payload) {
const request =
typeof payload === 'string' ? { message: payload } : payload;
this.requestQueue.push(request);
if (!this.show) this.next();
},
next() {
const req = this.requestQueue.shift();
if (!req) return;
this.message = req.message || '';
this.messageLines = req.messageLines || [];
this.title = req.title || '';
this.okTitle = req.okTitle || '';
this.cancelTitle = req.cancelTitle || '';
this.okVariant = req.okVariant || 'primary';
this.cancelVariant = req.cancelVariant || 'secondary';
this.resolver = req.resolve || null;
this.processing = false;
this.processingCounter = 1;
this.processingMax = req.processingMax || 20;
this.processingText = req.processingText || 'Processing...';
this.currentRequestProcessing = !!req.processing;
this.autoFocusButton = req.autoFocusButton || 'cancel';
// Checkbox confirmation
this.confirmationText = req.confirmationText || '';
this.confirmed = false;
this.show = true;
},
onOk() {
// Don't allow OK during processing
if (this.processing) return;
// If processing is requested, show progress before resolving
if (this.requestWantsProcessing()) {
this.startProcessing(() => {
if (this.resolver) this.resolver(true);
this.resolver = null;
this.show = false;
});
return;
}
if (this.resolver) this.resolver(true);
this.resolver = null;
this.show = false;
},
onCancel() {
// Don't allow cancel during processing
if (this.processing) return;
if (this.resolver) this.resolver(false);
this.resolver = null;
this.show = false;
},
onShown() {
// Focus is now handled by the modal's focus prop via focusTarget computed
// Bind ESC to cancel (we disabled built-in ESC handling for control)
this._onKeydown = (e) => {
// Don't handle keys during processing
if (this.processing) return;
if (e.key === 'Escape' || e.key === 'Esc') {
e.preventDefault();
e.stopPropagation();
this.onCancel();
}
};
document.addEventListener('keydown', this._onKeydown, { capture: true });
},
onHidden() {
// If user closed without OK, resolve false
if (this.resolver) this.resolver(false);
this.resolver = null;
this.stopProcessing();
// Reset state
this.confirmationText = '';
this.confirmed = false;
this.messageLines = [];
// Unbind ESC handler
if (this._onKeydown) {
document.removeEventListener('keydown', this._onKeydown, {
capture: true,
});
this._onKeydown = null;
}
// Process next in queue after a brief delay to allow modal to fully close
this.$nextTick(() => {
this.next();
});
},
requestWantsProcessing() {
// Enable processing only if explicitly requested via payload.processing === true
return this.currentRequestProcessing === true;
},
startProcessing(done) {
this.processing = true;
this.stopProcessing();
this.processingIntervalId = setInterval(() => {
if (this.processingCounter < this.processingMax) {
this.processingCounter += 1;
} else {
this.stopProcessing();
this.processing = false;
if (typeof done === 'function') done();
}
}, 150);
},
stopProcessing() {
if (this.processingIntervalId) {
clearInterval(this.processingIntervalId);
this.processingIntervalId = null;
}
},
},
};
</script>
|