ldc-supportweb/resources/ts/modules/sweetalertConfirm.ts
2023-10-12 15:56:11 +02:00

100 lines
3.8 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 = {}
) {
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";
Swal.fire({
title: title,
text: text,
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"),
}).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;
},
},
};