2024-04-14 12:41:22 +00:00
|
|
|
import { Behavior, registerBehavior } from "./index";
|
2023-08-21 21:12:00 +00:00
|
|
|
|
2024-04-14 12:41:22 +00:00
|
|
|
class ConfirmButtonBehavior extends Behavior {
|
2023-08-21 21:12:00 +00:00
|
|
|
constructor(element) {
|
2024-04-14 12:41:22 +00:00
|
|
|
super(element);
|
|
|
|
element.dataset.type = element.type;
|
|
|
|
element.dataset.name = element.name;
|
|
|
|
element.dataset.value = element.value;
|
|
|
|
element.removeAttribute("type");
|
|
|
|
element.removeAttribute("name");
|
|
|
|
element.removeAttribute("value");
|
|
|
|
element.addEventListener("click", this.onClick.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
Behavior.interacting = false;
|
2023-08-21 21:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onClick(event) {
|
|
|
|
event.preventDefault();
|
2024-04-14 12:41:22 +00:00
|
|
|
Behavior.interacting = true;
|
2023-08-21 21:12:00 +00:00
|
|
|
|
2023-08-24 17:11:36 +00:00
|
|
|
const container = document.createElement("span");
|
|
|
|
container.className = "confirmation";
|
|
|
|
|
2024-04-14 12:41:22 +00:00
|
|
|
const icon = this.element.getAttribute("ld-confirm-icon");
|
2023-08-24 17:11:36 +00:00
|
|
|
if (icon) {
|
|
|
|
const iconElement = document.createElementNS(
|
|
|
|
"http://www.w3.org/2000/svg",
|
|
|
|
"svg",
|
|
|
|
);
|
|
|
|
iconElement.style.width = "16px";
|
|
|
|
iconElement.style.height = "16px";
|
|
|
|
iconElement.innerHTML = `<use xlink:href="#${icon}"></use>`;
|
|
|
|
container.append(iconElement);
|
|
|
|
}
|
|
|
|
|
2024-04-14 12:41:22 +00:00
|
|
|
const question = this.element.getAttribute("ld-confirm-question");
|
2023-08-24 17:11:36 +00:00
|
|
|
if (question) {
|
|
|
|
const questionElement = document.createElement("span");
|
|
|
|
questionElement.innerText = question;
|
|
|
|
container.append(question);
|
|
|
|
}
|
|
|
|
|
2024-04-14 12:41:22 +00:00
|
|
|
const buttonClasses = Array.from(this.element.classList.values())
|
2024-03-29 11:37:20 +00:00
|
|
|
.filter((cls) => cls.startsWith("btn"))
|
|
|
|
.join(" ");
|
|
|
|
|
2024-04-14 12:41:22 +00:00
|
|
|
const cancelButton = document.createElement(this.element.nodeName);
|
2023-08-21 21:12:00 +00:00
|
|
|
cancelButton.type = "button";
|
2023-08-24 17:11:36 +00:00
|
|
|
cancelButton.innerText = question ? "No" : "Cancel";
|
2024-03-29 11:37:20 +00:00
|
|
|
cancelButton.className = `${buttonClasses} mr-1`;
|
2023-08-21 21:12:00 +00:00
|
|
|
cancelButton.addEventListener("click", this.reset.bind(this));
|
|
|
|
|
2024-04-14 12:41:22 +00:00
|
|
|
const confirmButton = document.createElement(this.element.nodeName);
|
|
|
|
confirmButton.type = this.element.dataset.type;
|
|
|
|
confirmButton.name = this.element.dataset.name;
|
|
|
|
confirmButton.value = this.element.dataset.value;
|
2023-08-24 17:11:36 +00:00
|
|
|
confirmButton.innerText = question ? "Yes" : "Confirm";
|
2024-03-29 11:37:20 +00:00
|
|
|
confirmButton.className = buttonClasses;
|
2023-08-21 21:12:00 +00:00
|
|
|
confirmButton.addEventListener("click", this.reset.bind(this));
|
|
|
|
|
|
|
|
container.append(cancelButton, confirmButton);
|
|
|
|
this.container = container;
|
|
|
|
|
2024-04-14 12:41:22 +00:00
|
|
|
this.element.before(container);
|
|
|
|
this.element.classList.add("d-none");
|
2023-08-21 21:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
setTimeout(() => {
|
2024-04-14 12:41:22 +00:00
|
|
|
Behavior.interacting = false;
|
2023-08-21 21:12:00 +00:00
|
|
|
this.container.remove();
|
2024-04-14 12:41:22 +00:00
|
|
|
this.element.classList.remove("d-none");
|
2023-08-21 21:12:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
registerBehavior("ld-confirm-button", ConfirmButtonBehavior);
|