62 lines
1.9 KiB
JavaScript
62 lines
1.9 KiB
JavaScript
import validation from "../../../js/validation";
|
|
import populateSelect from "../../select-helper";
|
|
import { showToast, toastsInit } from "../../toast-helper";
|
|
|
|
function getKey(input) {
|
|
return input.getAttribute("id").replace("-", "_");
|
|
}
|
|
|
|
export default class FormView {
|
|
constructor(root, saveCallback, backCallback) {
|
|
this.root = root;
|
|
this.saveCallback = saveCallback;
|
|
this.backCallback = backCallback;
|
|
}
|
|
|
|
render(model) {
|
|
const template = document.getElementById("news-form-template").content.cloneNode(true);
|
|
|
|
const form = template.querySelector("form");
|
|
form.addEventListener("submit", async () => {
|
|
if (!form.checkValidity()) {
|
|
return;
|
|
}
|
|
this.saveCallback();
|
|
});
|
|
|
|
this.inputs = template.querySelectorAll("input");
|
|
this.inputs.forEach((input) => {
|
|
input.addEventListener("change", (event) => {
|
|
model.setValue(getKey(event.target), event.target.value);
|
|
});
|
|
});
|
|
|
|
this.tegSelector = template.getElementById("teg");
|
|
this.tegSelector.addEventListener("change", (event) => {
|
|
console.log(event.terget.value);
|
|
model.setValue("teg", event.target.value);
|
|
});
|
|
populateSelect(this.tegSelector, model.tegs);
|
|
|
|
const backButton = template.getElementById("btn-back");
|
|
backButton.addEventListener("click", this.backCallback);
|
|
|
|
this.root.appendChild(template);
|
|
|
|
this.toasts = toastsInit();
|
|
|
|
validation();
|
|
}
|
|
|
|
update(model) {
|
|
this.inputs.forEach((input) => {
|
|
const control = input;
|
|
control.value = model.getValue(getKey(input));
|
|
});
|
|
this.groupsSelector.value = model.element.groupId || "";
|
|
}
|
|
|
|
successToast() {
|
|
showToast(this.toasts, "Сохранение", "Сохранение успешно завершено");
|
|
}
|
|
} |