Skip to content

Commit 62838ce

Browse files
authored
Merge pull request #626 from devforth/feature/AdminForth/1629/search-bulk-ai-pluggin-problem
fix: add beforeCancelFunction logic
2 parents 1732342 + 9785bbe commit 62838ce

2 files changed

Lines changed: 43 additions & 17 deletions

File tree

adminforth/spa/src/afcl/Dialog.vue

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
:closeByClickOutside="clickToCloseOutside || closeByClickOutside"
66
:closeByEsc="closeByEsc || closable"
77
:beforeCloseFunction="beforeCloseFunction"
8+
:beforeCancelFunction="beforeCancelFunction"
89
:beforeOpenFunction="beforeOpenFunction"
910
:askForCloseConfirmation="askForCloseConfirmation"
1011
:closeConfirmationText="closeConfirmationText"
@@ -26,7 +27,7 @@
2627
v-if="headerCloseButton"
2728
type="button"
2829
class="text-lightDialogCloseButton bg-transparent hover:bg-lightDialogCloseButtonHoverBackground hover:text-lightDialogCloseButtonHover rounded-lg text-sm w-8 h-8 ms-auto inline-flex justify-center items-center dark:text-darkDialogCloseButton dark:hover:bg-darkDialogCloseButtonHoverBackground dark:hover:text-darkDialogCloseButtonHover"
29-
@click="tryToHideModal"
30+
@click="tryToCancelModal"
3031
>
3132
<svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 14 14">
3233
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6"/>
@@ -118,10 +119,15 @@ function close() {
118119
modalRef.value.close();
119120
}
120121
122+
function tryToCancelModal() {
123+
modalRef.value?.cancel();
124+
}
125+
121126
defineExpose({
122127
open: open,
123128
close: close,
124129
tryToHideModal: tryToHideModal,
130+
tryToCancelModal: tryToCancelModal,
125131
})
126132
127133
function tryToHideModal() {
@@ -171,6 +177,11 @@ interface DialogProps {
171177
*/
172178
beforeOpenFunction?: (() => void | Promise<void>) | null
173179
180+
/**
181+
* Function that will be called before the dialog is canceled.
182+
*/
183+
beforeCancelFunction?: (() => void | Promise<void | boolean>) | null
184+
174185
/**
175186
* Disables close on Ecs button
176187
*
@@ -201,19 +212,21 @@ interface DialogProps {
201212
202213
/********** for the backward compatibility ***************/
203214
class Dialog implements IDialogInsideButtonClickHandler {
204-
hide: () => void
205-
constructor( hide: () => void ) {
206-
this.hide = hide;
215+
hide: (isCancel?: boolean) => void
216+
constructor(hideFn: (isCancel?: boolean) => void) {
217+
this.hide = hideFn;
207218
}
208219
}
209220
const dialog: Ref<Dialog> = ref(
210-
new Dialog(
211-
() => {
212-
if (dialog.value) {
213-
tryToHideModal();
221+
new Dialog((isCancel = false) => {
222+
if (dialog.value) {
223+
if (isCancel) {
224+
modalRef.value?.cancel();
225+
} else {
226+
modalRef.value?.close();
214227
}
215228
}
216-
)
229+
})
217230
);
218231
/*************************************************************/
219232

adminforth/spa/src/afcl/Modal.vue

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ interface DialogProps {
7070
closeByEsc?: boolean
7171
beforeCloseFunction?: (() => void | Promise<void | boolean>) | null
7272
beforeOpenFunction?: (() => void | Promise<void>) | null
73+
beforeCancelFunction?: (() => void | Promise<void | boolean>) | null
7374
askForCloseConfirmation?: boolean
7475
closeConfirmationText?: string
7576
removeFromDomOnClose?: boolean
@@ -82,6 +83,7 @@ const props = withDefaults(defineProps<DialogProps>(), {
8283
closeByEsc: true,
8384
beforeCloseFunction: null,
8485
beforeOpenFunction: null,
86+
beforeCancelFunction: null,
8587
askForCloseConfirmation: false,
8688
closeConfirmationText: 'Are you sure you want to close this dialog?',
8789
removeFromDomOnClose: false,
@@ -109,19 +111,32 @@ async function close() {
109111
isModalOpen.value = false;
110112
}
111113
114+
async function cancel() {
115+
if (props.beforeCancelFunction) {
116+
const shouldCancel = await props.beforeCancelFunction?.();
117+
if (shouldCancel === false) return;
118+
}
119+
isModalOpen.value = false;
120+
}
121+
112122
defineOptions({
113123
inheritAttrs: false,
114124
})
115125
116126
defineExpose({
117127
open: open,
118128
close: close,
129+
cancel: cancel,
119130
tryToHideModal: tryToHideModal,
120131
})
121132
122-
function tryToHideModal() {
123-
if (!props.askForCloseConfirmation ) {
124-
close();
133+
function tryToHideModal(isCancelAction = false) {
134+
if (!props.askForCloseConfirmation) {
135+
if (isCancelAction) {
136+
cancel();
137+
} else {
138+
close();
139+
}
125140
} else {
126141
showConfirmationOnClose.value = true;
127142
}
@@ -136,10 +151,8 @@ function toggleModal() {
136151
}
137152
138153
function onEsc(event: KeyboardEvent) {
139-
if (event.key === 'Escape') {
140-
if (props.closeByEsc) {
141-
tryToHideModal();
142-
}
154+
if (event.key === 'Escape' && props.closeByEsc) {
155+
tryToHideModal(true);
143156
}
144157
}
145158
@@ -154,7 +167,7 @@ onUnmounted(() => {
154167
155168
function backdropClick(e: MouseEvent) {
156169
if (props.closeByClickOutside && e.target === e.currentTarget) {
157-
tryToHideModal();
170+
tryToHideModal(true);
158171
}
159172
}
160173

0 commit comments

Comments
 (0)