ldc-cocotte/resources/ts/modules/sweetalert.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

165 lines
4.8 KiB
TypeScript

import type { SweetAlertResult } from "sweetalert2";
import { confirm } from "./sweetalert-confirm";
import { trans } from "./trans";
function resolveTargetForm(target: EventTarget | null, errorMessage: string): HTMLFormElement {
if (!target || !(target instanceof HTMLFormElement)) {
throw new Error(errorMessage);
}
return target;
}
function resolveTargetElement(target: EventTarget | null): HTMLElement {
if (!target || !(target instanceof HTMLElement)) {
throw new Error("confirmJS can only be executed on a html element");
}
return target;
}
function openConfirmDialog(
title: string,
text: string,
target: HTMLElement,
icon: "warning" | "error" | "info" = "warning"
): Promise<boolean> {
return new Promise((resolve) => {
confirm(
title,
text,
target,
(response: SweetAlertResult<unknown>) => {
resolve(response.isConfirmed);
},
{ icon }
);
});
}
document.addEventListener("DOMContentLoaded", () => {
let elements = document.getElementsByClassName("confirmDeleteJSWithAssociated");
for (const element of elements) {
element.addEventListener("submit", function (e) {
e.preventDefault();
const el = resolveTargetForm(e.target, "confirmDoneJS can only be executed on an exising form");
confirm(
trans("Confirm"),
trans("Do you really want to delete this item?"),
el,
function () {
confirm(
trans("Warning"),
trans("Deleting this item will also delete all associated items. You will not be able to undo this action. Do you really want to continue?"),
el,
function () {
el.submit();
},
{
icon: "warning",
reverseButtons: false,
background: getComputedStyle(document.body).getPropertyValue("--bs-body-bg"),
customClass: {
confirmButton: "btn btn-warning mx-1",
cancelButton: "btn btn-primary mx-1",
},
}
);
},
{ icon: "error" }
);
return false;
});
}
elements = document.getElementsByClassName("confirmDeleteJS");
for (const element of elements) {
element.addEventListener("submit", function (e) {
e.preventDefault();
const el = resolveTargetForm(e.target, "confirmDoneJS can only be executed on an exising form");
confirm(
trans("Confirm"),
trans("Do you really want to delete this item?"),
el,
function () {
el.submit();
},
{ icon: "error" }
);
return false;
});
}
elements = document.getElementsByClassName("confirmCancelJS");
for (const element of elements) {
element.addEventListener("submit", function (e) {
e.preventDefault();
const el = resolveTargetForm(e.target, "confirmCancelJS can only be executed on an exising form");
confirm(
trans("Confirm"),
trans("Do you really want to cancel this item?"),
el,
function () {
el.submit();
},
{ icon: "error" }
);
return false;
});
}
elements = document.getElementsByClassName("confirmDoneJS");
for (const element of elements) {
element.addEventListener("submit", function (e) {
e.preventDefault();
const el = resolveTargetForm(e.target, "confirmDoneJS can only be executed on an exising form");
confirm(
trans("Confirm"),
trans("Do you really want to mark this item as processed?"),
el,
function () {
el.submit();
},
{ icon: "info" }
);
return false;
});
}
elements = document.getElementsByClassName("confirmSubmitJS");
for (const element of elements) {
element.addEventListener("submit", function (e) {
e.preventDefault();
const el = resolveTargetForm(e.target, "confirmSubmitJS can only be executed on an exising form");
confirm(
trans("Confirm"),
trans("Do you want to save this item as is?"),
el,
function () {
el.submit();
},
{ icon: "warning" }
);
return false;
});
}
elements = document.getElementsByClassName("confirmJS");
for (const element of elements) {
element.addEventListener(
"click",
async function (e) {
if (element.getAttribute("confirmJS")) {
return;
}
e.preventDefault();
const el = resolveTargetElement(e.target);
const confirmed = await openConfirmDialog(trans("Confirm"), trans("Are you sure?"), el, "warning");
if (confirmed) {
element.setAttribute("confirmJS", "true");
el.click();
element.removeAttribute("confirmJS");
}
},
false
);
}
});