ldc-cocotte/resources/ts/modules/sweetalert-confirm.ts
Jérémy Munsch bf3e7a93fd
refactor(i18n): harmonize translation keys to English and complete French translations
- Convert hardcoded strings and French translation keys to English keys across Blade templates, Vue components, TypeScript modules, models, and routes
- Complete and synchronize lang/fr.json and lang/en.json translation dictionaries
- Add timezone attribute in lang/fr/validation.php and lang/en/validation.php
- Add missing English lang files for parity: crud.php, models.php, search.php, and vue.php
2026-10-27 09:58:47 +01:00

95 lines
2.6 KiB
TypeScript

// SweetAlert
import type { SweetAlertOptions, SweetAlertResult } from "sweetalert2";
import Swal from "sweetalert2";
import { trans } from "./trans";
function confirm(
title: string,
text: string,
self: object,
after: (response: SweetAlertResult<unknown>) => void = () => void 0,
options: SweetAlertOptions = {}
) {
const dialogText = text ?? "";
if (!self) {
throw new Error("self is needed to set \"this\" on callback");
}
if (!after) {
throw new Error("callback is needed when using confirm");
}
Swal.fire({
title: title,
text: dialogText,
color: getComputedStyle(document.body).getPropertyValue("--bs-body-color"),
icon: "warning",
showCancelButton: true,
confirmButtonText:
options.confirmButtonText ??
trans("Yes"),
cancelButtonText:
options.cancelButtonText ??
trans("Cancel"),
reverseButtons: true,
// * Bootstrap Styling
customClass: {
confirmButton: "btn btn-danger mx-1",
cancelButton: "btn btn-primary mx-1",
},
buttonsStyling: false,
background: getComputedStyle(document.body).getPropertyValue("--bs-body-bg"),
...options,
}).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("The operation was not performed"),
color: getComputedStyle(document.body).getPropertyValue("--bs-body-color"),
confirmButtonText: trans("OK"),
// * Bootstrap Styling
customClass: {confirmButton: "btn btn-primary",},
buttonsStyling: false,
background: getComputedStyle(document.body).getPropertyValue("--bs-body-bg"),
});
}
});
return false;
}
function alert(
title: string,
text = "",
self: HTMLElement | null = null,
after: ((response: SweetAlertResult<unknown>) => 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;
}
export { alert, confirm };