ldc-supportweb/resources/ts/modules/sweetalertConfirm.ts
2025-06-19 11:46:01 +02:00

114 lines
4.1 KiB
TypeScript

// SweetAlert
import type { SweetAlertOptions, SweetAlertResult } from "sweetalert2";
import Swal from "sweetalert2";
import * as trans from "../modules/trans";
export default {
methods: {
confirm(
title: string,
text: string = "",
self: Object,
after: (response: SweetAlertResult<any>) => void = () => {},
options: SweetAlertOptions = {},
html: Boolean = false
) {
if (!self) {
throw Error("self is needed to set \"this\" on callback");
}
if (!after) {
throw Error("callback is needed when using confirm");
}
const icon = options.icon ?? "warning";
// define options to fire
const saOptions = {
title: title,
color: getComputedStyle(document.body).getPropertyValue("--bs-body-color"),
icon: icon,
showCancelButton: true,
confirmButtonText:
options.confirmButtonText ??
trans.default.methods.__("Oui"),
cancelButtonText:
options.cancelButtonText ??
trans.default.methods.__("Annuler"),
reverseButtons: true,
// * Bootstrap Styling
customClass: {
confirmButton: "btn btn-primary mx-1",
cancelButton: "btn btn-danger mx-1",
},
buttonsStyling: false,
background: getComputedStyle(document.body).getPropertyValue("--bs-body-bg"),
};
// Enable HTML content interpretation in the modal options
let message = {};
if (html) {
message = {html: text};
} else {
message = { text: text };
}
const fullOptions = {...saOptions, ...message};
Swal.fire(fullOptions).then((result) => {
if (result.isConfirmed) {
if (after) {
after.apply(self, [result]);
}
} else if (
/* Read more about handling dismissals below */
result.dismiss === Swal.DismissReason.cancel
) {
Swal.fire({
title: trans.default.methods.__(
"L'opération n'as pas été effectuée"
),
color: getComputedStyle(document.body).getPropertyValue("--bs-body-color"),
confirmButtonText: trans.default.methods.__("Ok"),
// * Bootstrap Styling
customClass: {
confirmButton: "btn btn-primary",
},
buttonsStyling: false,
background: getComputedStyle(document.body).getPropertyValue("--bs-body-bg"),
});
}
});
return false;
},
alert(
title: string,
text: string = "",
self: HTMLElement | null = null,
after: ((response: SweetAlertResult<any>) => void) | null = null,
options: SweetAlertOptions = {}
) {
const icon = options.icon ?? "info";
Swal.fire({
title: title,
text: text,
color: getComputedStyle(document.body).getPropertyValue("--bs-body-color"),
icon: icon,
// * Bootstrap Styling
customClass: {
confirmButton: "btn btn-lg btn-primary",
},
buttonsStyling: false,
background: getComputedStyle(document.body).getPropertyValue("--bs-body-bg"),
}).then((result) => {
if (after) {
if (self) {
after.apply(self, [result]);
} else {
after(result);
}
}
});
return false;
},
},
};