Web-programming_Anisin_R.S..../js/lines-ui.js
2024-01-10 20:47:54 +04:00

93 lines
3.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// модуль для работы с элементами управления
// объект для удобного получения элементов
// при обращении к атрибуту объекта вызывается
// нужная функция для поиска элемента
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"),
discountType: document.getElementById("discount"),
price: document.getElementById("price"),
name: document.getElementById("name"),
image: document.getElementById("image"),
imagePreview: document.getElementById("image-preview"),
buttonAddDiscount: document.getElementById("discounts-add"),
tableDiscounts: document.querySelector("#discounts-table tbody"),
formDiscounts: document.getElementById("discounts-form"),
discountLineId: document.getElementById("discounts-line-id"),
percent: document.getElementById("percent"),
discountName: document.getElementById("discount-name"),
};
// Дефолтное превью
export const imagePlaceholder = "https://via.placeholder.com/200";
// функция создает тег option для select
// <option value="" selected>name</option>
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
// <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("bi", 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
// <td>value</td>
function createTableColumn(value) {
const td = document.createElement("td");
td.textContent = value;
return td;
}
// функция создает строку таблицы
export function createTableRow(item, index, editCallback, 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.name));
row.appendChild(createTableColumn(item.items.name));
row.appendChild(createTableColumn(`${parseFloat(item.price).toFixed(2)}`));
row.appendChild(createTableColumn(item.discounts.name));
row.appendChild(createTableColumn(`${item.discounts.percent}%`));
row.appendChild(createTableColumn(`${parseFloat(item.finalPrice).toFixed(2)}`));
// редактировать в модальном окне
row.appendChild(createTableAnchor("bi-pencil-square", editCallback));
// удаление
row.appendChild(createTableAnchor("bi-trash", deleteCallback));
return row;
}