2023-12-06 22:05:45 +04:00
|
|
|
|
// модуль для работы с элементами управления
|
|
|
|
|
|
|
|
|
|
// объект для удобного получения элементов
|
|
|
|
|
// при обращении к атрибуту объекта вызывается
|
|
|
|
|
// нужная функция для поиска элемента
|
|
|
|
|
export const cntrls = {
|
2023-12-22 10:03:01 +04:00
|
|
|
|
container: document.getElementById("my-id-for-text"),
|
2023-12-07 22:11:53 +04:00
|
|
|
|
button: document.getElementById("items-add"),
|
|
|
|
|
table: document.querySelector("#items-table tbody"),
|
|
|
|
|
form: document.getElementById("items-form"),
|
|
|
|
|
lineId: document.getElementById("items-line-id"),
|
2023-12-22 10:03:01 +04:00
|
|
|
|
genresType: document.getElementById("genre"),
|
|
|
|
|
nameBook: document.getElementById("nameBook"),
|
|
|
|
|
authorsType: document.getElementById("author"),
|
|
|
|
|
year: document.getElementById("yearPicker"),
|
|
|
|
|
description: document.getElementById("description"),
|
2023-12-07 22:11:53 +04:00
|
|
|
|
count: document.getElementById("count"),
|
|
|
|
|
date: document.getElementById("date"),
|
|
|
|
|
image: document.getElementById("image"),
|
|
|
|
|
imagePreview: document.getElementById("image-preview"),
|
|
|
|
|
};
|
2023-12-06 22:05:45 +04:00
|
|
|
|
|
2023-12-07 22:11:53 +04:00
|
|
|
|
// Дефолтное превью
|
|
|
|
|
export const imagePlaceholder = "https://via.placeholder.com/200";
|
2023-12-06 22:05:45 +04:00
|
|
|
|
|
2023-12-07 22:11:53 +04:00
|
|
|
|
// функция создает тег option для select
|
|
|
|
|
// <option value="" selected>name</option>
|
2023-12-22 10:03:01 +04:00
|
|
|
|
export function createGenresOption(name, value = "", isSelected = false) {
|
|
|
|
|
const option = document.createElement("option");
|
|
|
|
|
option.value = value || "";
|
|
|
|
|
option.selected = isSelected;
|
|
|
|
|
option.text = name;
|
|
|
|
|
return option;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createAuthorsOption(name, value = "", isSelected = false) {
|
2023-12-07 22:11:53 +04:00
|
|
|
|
const option = document.createElement("option");
|
|
|
|
|
option.value = value || "";
|
|
|
|
|
option.selected = isSelected;
|
|
|
|
|
option.text = name;
|
|
|
|
|
return option;
|
|
|
|
|
}
|
2023-12-06 22:05:45 +04:00
|
|
|
|
|
2023-12-07 22:11:53 +04:00
|
|
|
|
// функция создает ссылку (a) для таблицы
|
|
|
|
|
// содержимое тега a заполняется необходимой иконкой (icon)
|
|
|
|
|
// при нажатии вызывается callback
|
|
|
|
|
// ссылка "оборачивается" тегом td
|
|
|
|
|
// <td><a href="#" onclick="callback()"><i class="fa-solid icon"></i></a></td>
|
|
|
|
|
function createTableAnchor(icon, callback) {
|
|
|
|
|
const i = document.createElement("i");
|
|
|
|
|
i.classList.add("fa-solid", icon);
|
2023-12-06 22:05:45 +04:00
|
|
|
|
|
2023-12-07 22:11:53 +04:00
|
|
|
|
const a = document.createElement("a");
|
|
|
|
|
a.href = "#";
|
|
|
|
|
a.appendChild(i);
|
|
|
|
|
a.onclick = (event) => {
|
2023-12-22 10:03:01 +04:00
|
|
|
|
// чтобы в URL не добавлялась решетка
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
callback();
|
2023-12-07 22:11:53 +04:00
|
|
|
|
};
|
2023-12-06 22:05:45 +04:00
|
|
|
|
|
2023-12-07 22:11:53 +04:00
|
|
|
|
const td = document.createElement("td");
|
|
|
|
|
td.appendChild(a);
|
|
|
|
|
return td;
|
|
|
|
|
}
|
2023-12-06 22:05:45 +04:00
|
|
|
|
|
2023-12-07 22:11:53 +04:00
|
|
|
|
// функция создает колонку таблицы с текстом value
|
|
|
|
|
// <td>value</td>
|
|
|
|
|
function createTableColumn(value) {
|
|
|
|
|
const td = document.createElement("td");
|
|
|
|
|
td.textContent = value;
|
|
|
|
|
return td;
|
|
|
|
|
}
|
2023-12-06 22:05:45 +04:00
|
|
|
|
|
2023-12-07 22:11:53 +04:00
|
|
|
|
// функция создает строку таблицы
|
|
|
|
|
// <tr>
|
|
|
|
|
// <th scope="row">index + 1</th>
|
|
|
|
|
// <td>item.items.name</td>
|
|
|
|
|
// <td>parseFloat(item.price).toFixed(2))</td>
|
|
|
|
|
// <td>item.count</td>
|
|
|
|
|
// <td>parseFloat(item.sum).toFixed(2))</td>
|
|
|
|
|
// <td><a href="#" onclick="editCallback()"><i class="fa-solid fa-pencil"></i></a></td>
|
|
|
|
|
// <td><a href="#" onclick="editPageCallback()"><i class="fa-solid fa-pen-to-square"></i></a></td>
|
|
|
|
|
// <td><a href="#" onclick="deleteCallback()"><i class="fa-solid fa-trash"></i></a></td>
|
|
|
|
|
// </tr>
|
2023-12-22 10:03:01 +04:00
|
|
|
|
export function createTableRow(item, index, editCallback, deleteCallback) {
|
2023-12-07 22:11:53 +04:00
|
|
|
|
const rowNumber = document.createElement("th");
|
|
|
|
|
rowNumber.scope = "row";
|
|
|
|
|
rowNumber.textContent = index + 1;
|
2023-12-06 22:05:45 +04:00
|
|
|
|
|
2023-12-07 22:11:53 +04:00
|
|
|
|
const row = document.createElement("tr");
|
|
|
|
|
row.id = `line-${item.id}`;
|
2023-12-06 22:05:45 +04:00
|
|
|
|
|
2023-12-07 22:11:53 +04:00
|
|
|
|
row.appendChild(rowNumber);
|
2023-12-22 10:03:01 +04:00
|
|
|
|
row.appendChild(createTableColumn(item.nameBook));
|
2023-12-07 22:11:53 +04:00
|
|
|
|
row.appendChild(createTableColumn(item.authors.name));
|
2023-12-22 10:03:01 +04:00
|
|
|
|
row.appendChild(createTableColumn(item.genres.name));
|
|
|
|
|
row.appendChild(createTableColumn(item.year));
|
|
|
|
|
row.appendChild(createTableColumn(item.description));
|
2023-12-07 22:11:53 +04:00
|
|
|
|
row.appendChild(createTableColumn(item.count));
|
|
|
|
|
row.appendChild(createTableColumn(item.date));
|
2023-12-22 10:03:01 +04:00
|
|
|
|
|
2023-12-07 22:11:53 +04:00
|
|
|
|
// редактировать в модальном окне
|
|
|
|
|
row.appendChild(createTableAnchor("fa-pencil", editCallback));
|
|
|
|
|
// удаление
|
|
|
|
|
row.appendChild(createTableAnchor("fa-trash", deleteCallback));
|
|
|
|
|
return row;
|
|
|
|
|
}
|
2023-12-22 10:03:01 +04:00
|
|
|
|
|
|
|
|
|
export function createTableRowOnIndex(item) {
|
|
|
|
|
console.log(item);
|
|
|
|
|
|
|
|
|
|
const bookCard = createBookCard(item);
|
|
|
|
|
return bookCard;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createBookCard(item) {
|
|
|
|
|
const card = document.createElement("div");
|
|
|
|
|
card.classList.add("book");
|
|
|
|
|
|
|
|
|
|
const image = document.createElement("img");
|
|
|
|
|
image.src = item.image ? item.image : imagePlaceholder;
|
|
|
|
|
image.alt = "Обложка книги";
|
|
|
|
|
|
|
|
|
|
const title = document.createElement("h3");
|
|
|
|
|
title.textContent = `${item.nameBook}, ${item.authors.name}`;
|
|
|
|
|
|
|
|
|
|
card.appendChild(image);
|
|
|
|
|
card.appendChild(title);
|
|
|
|
|
|
|
|
|
|
const div = document.createElement("div");
|
|
|
|
|
div.classList.add("col-12");
|
|
|
|
|
div.classList.add("col-md-4");
|
|
|
|
|
div.classList.add("col-lg-4");
|
|
|
|
|
div.classList.add("col-xl-3");
|
|
|
|
|
div.appendChild(card);
|
|
|
|
|
return div;
|
|
|
|
|
}
|