ldc-legaulois/resources/ts/bo/base/common.ts

118 lines
3.8 KiB
TypeScript

import { confirm } from "@modules/sweetalert-confirm";
import * as bootstrap from "bootstrap";
window.addEventListener("DOMContentLoaded", () => {
/**
* Popup delete item.
*/
const elements = document.getElementsByClassName("confirmDeleteTS") as HTMLCollectionOf<Element>;
for (const element of elements) {
element.addEventListener("submit", function (e) {
e.preventDefault();
const el = e.target;
if (!el || !(el instanceof HTMLFormElement)) {
throw new Error(
"confirmDoneJS can only be executed on an exising form"
);
}
confirm(
"Êtes-vous sûr ?",
"Toutes les données vont être perdu",
el as HTMLFormElement,
function () {
(el as HTMLFormElement).submit();
},
{ icon: "warning" }
);
return false;
});
}
/**
* Popup delete item.
*/
const btnElements = document.getElementsByClassName(
"confirmPublishUnpublishTS"
) as HTMLCollectionOf<Element>;
for (const element of btnElements) {
element.addEventListener("submit", function (e) {
e.preventDefault();
const el = e.target;
if (!el || !(el instanceof HTMLFormElement)) {
throw new Error(
"confirmDoneJS can only be executed on an exising form"
);
}
confirm(
"Êtes-vous sûr ?",
"Toutes les données associées seront altérées",
el as HTMLFormElement,
function () {
(el as HTMLFormElement).submit();
},
{
icon: "warning",
confirmButtonText: "Oui, continuer",
confirmButtonColor: "btn btn-primary mx-1",
}
);
return false;
});
}
});
const tooltipTriggerList = document.querySelectorAll("[data-bs-toggle=\"tooltip\"]");
tooltipTriggerList.forEach(el => new bootstrap.Tooltip(el));
// for active tab for block
document.addEventListener("DOMContentLoaded", () => {
document.querySelectorAll<HTMLElement>("[data-bs-target$=\"-t4\"]").forEach((trigger) => {
trigger.addEventListener("shown.bs.tab", (event) => {
const target = event.currentTarget as HTMLElement;
const sectionId = target.getAttribute("data-bs-target")!
.replace("#s", "")
.replace("-t4", "");
const firstTab = document.querySelector<HTMLElement>(
`#bienfait-tabs-${sectionId} .nav-link:first-child`
);
if (firstTab) {
new bootstrap.Tab(firstTab).show();
}
});
});
});
// // Opens the accordion and activates the tab based on URL parameters (section & tab)
document.addEventListener("DOMContentLoaded", (): void => {
const params = new URLSearchParams(window.location.search);
const sectionId: string | null = params.get("section");
const tab: string = params.get("tab") ?? "t3";
if (!sectionId) return;
const collapseEl: HTMLElement | null = document.getElementById("collapse-" + sectionId);
if (!collapseEl) return;
const bsCollapse = new bootstrap.Collapse(collapseEl, { toggle: false });
bsCollapse.show();
collapseEl.addEventListener("shown.bs.collapse", (): void => {
const tabTrigger: Element | null = document.querySelector(
`[data-bs-target="#s${sectionId}-${tab}"]`
);
if (tabTrigger) {
bootstrap.Tab.getOrCreateInstance(tabTrigger).show();
}
}, { once: true });
});