// модуль для работы с элементами управления
// объект для удобного получения элементов
// при обращении к атрибуту объекта вызывается
// нужная функция для поиска элемента
export const cntrls = {
button: document.getElementById("items-add"),
table: document.querySelector("#items-table tbody"),
form: document.getElementById("items-form"),
lineId: document.getElementById("items-line-id"),
itemsType: document.getElementById("item"),
food: document.getElementById("food"),
date: document.getElementById("date"),
price: document.getElementById("price"),
count: document.getElementById("count"),
image: document.getElementById("image"),
imagePreview: document.getElementById("image-preview"),
catalogMenu: document.getElementById("menu-container"),
};
// Дефолтное превью
export const imagePlaceholder = "https://via.placeholder.com/200";
// функция создает тег option для select
//
export function createItemsOption(name, value = "", isSelected = false) {
const option = document.createElement("option");
option.value = value || "";
option.selected = isSelected;
option.text = name;
return option;
}
// функция создает ссылку (a) для таблицы
// содержимое тега a заполняется необходимой иконкой (icon)
// при нажатии вызывается callback
// ссылка "оборачивается" тегом td
//
|
function createTableAnchor(icon, callback) {
const i = document.createElement("i");
i.classList.add("fa-solid", icon);
const a = document.createElement("a");
a.href = "#";
a.appendChild(i);
a.onclick = (event) => {
// чтобы в URL не добавлялась решетка
event.preventDefault();
event.stopPropagation();
callback();
};
const td = document.createElement("td");
td.appendChild(a);
return td;
}
// функция создает колонку таблицы с текстом value
// value |
function createTableColumn(value) {
const td = document.createElement("td");
td.textContent = value;
return td;
}
// функция создает строку таблицы
//
// index + 1 |
// item.items.name |
// parseFloat(item.price).toFixed(2)) |
// item.count |
// parseFloat(item.sum).toFixed(2)) |
// |
// |
// |
//
export function createTableRow(item, index, editPageCallback, deleteCallback) {
const rowNumber = document.createElement("th");
rowNumber.scope = "row";
rowNumber.textContent = index + 1;
const row = document.createElement("tr");
row.id = `line-${item.id}`;
row.appendChild(rowNumber);
row.appendChild(createTableColumn(item.items.name));
row.appendChild(createTableColumn(item.food));
row.appendChild(createTableColumn(item.date));
row.appendChild(createTableColumn(parseFloat(item.price).toFixed(2)));
row.appendChild(createTableColumn(item.count));
row.appendChild(createTableColumn(parseFloat(item.sum).toFixed(2)));
// редактировать на странице page-edit
row.appendChild(createTableAnchor("fa-pen-to-square", editPageCallback));
// удаление
row.appendChild(createTableAnchor("fa-trash", deleteCallback));
return row;
}
export function createCatalogItem(item) {
const col = document.createElement("div");
col.className = 'col-3 my-2';
const imgDiv = document.createElement("div");
imgDiv.className = 'd-flex justify-content-center my-2';
const img = document.createElement("img");
img.src = item.image;
img.className = 'Menu';
img.width = '155';
img.height = '245';
imgDiv.appendChild(img);
const food = document.createElement("div");
food.className = 'text-center';
const price = document.createElement("p");
price.className = 'text-center';
const priceValue = parseInt(item.price, 10);
price.textContent = `${priceValue} ₽`;
const name = document.createElement("p");
name.className = 'text-center';
name.textContent = item.food;
food.appendChild(price);
food.appendChild(name);
const btnDiv = document.createElement("div");
btnDiv.className = 'text-center';
const btn = document.createElement("btn");
btn.className = 'btn-danger btn';
btn.textContent = 'Купить';
btnDiv.appendChild(btn);
col.appendChild(imgDiv);
col.appendChild(food);
col.appendChild(btnDiv);
return col;
}