Files
PIbd-21_Permyakov_R.G._IP/components/board-helper.js

44 lines
1.7 KiB
JavaScript

import { buttonIcon } from "./button-helper";
function newsItemButton(item, icon, color, callback) {
const button = buttonIcon(icon, color);
button.addEventListener("click", () => callback(item));
return button;
};
function newsItem(item, callbacks, tegs) {
const news = document.createElement("div");
const element1 = document.createElement("div");
const element2 = document.createElement("div");
const element3 = document.createElement("div");
news.classList.add("newsItem", "d-flex", "flex-column");
element1.appendChild(newsItemButton(item, "pencil-fill", "warning", callbacks.edit));
element1.appendChild(newsItemButton(item, "trash-fill", "danger", callbacks.delete));
element1.classList.add("d-flex", "flex-row", "align-self-end");
let teg = tegs.filter((f) => f.id === item.teg)[0];
element2.innerText = teg.name;
element2.classList.add("color2");
let text = `Дата: ${item.bdate}`;
text += `\n${item.description}`;
element3.innerText = text;
news.appendChild(element1);
news.appendChild(element2);
news.appendChild(element3);
return news;
}
export function board(data, callbacks, tegs) {
if (!data || !Array.isArray(data)){
throw new Error("Data is not defined");
}
const bodyElement = document.createElement("div");
bodyElement.classList.add("d-flex", "flex-row", "flex-wrap");
data.forEach((item) => bodyElement.appendChild(newsItem(item, callbacks, tegs)));
return bodyElement;
}
export function populateBoard(boardElement, data, callbacks, tegs) {
boardElement.innerHTML = "";
data.forEach((item) => boardElement.appendChild(newsItem(item, callbacks, tegs)));
}