2023-11-30 21:18:37 +04:00
|
|
|
|
// модуль для работы с элементами управления
|
|
|
|
|
|
|
|
|
|
// объект для удобного получения элементов
|
|
|
|
|
// при обращении к атрибуту объекта вызывается
|
|
|
|
|
// нужная функция для поиска элемента
|
|
|
|
|
|
|
|
|
|
export const cntrls = {
|
|
|
|
|
container: document.getElementById("my-id-for-text"),
|
|
|
|
|
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"),
|
|
|
|
|
name: document.getElementById("name"),
|
|
|
|
|
price: document.getElementById("price"),
|
|
|
|
|
count: document.getElementById("count"),
|
2023-12-06 20:23:44 +04:00
|
|
|
|
discountsType: document.getElementById("discounts"),
|
2023-11-30 21:18:37 +04:00
|
|
|
|
image: document.getElementById("image"),
|
|
|
|
|
imagePreview: document.getElementById("image-preview"),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Дефолтное превью
|
|
|
|
|
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("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
|
|
|
|
|
// <td>value</td>
|
|
|
|
|
function createTableColumn(value) {
|
|
|
|
|
const td = document.createElement("td");
|
|
|
|
|
td.textContent = value;
|
|
|
|
|
return td;
|
|
|
|
|
}
|
|
|
|
|
export function createTableRow(item, index, editCallback, 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(parseFloat(item.price).toFixed(2)));
|
|
|
|
|
row.appendChild(createTableColumn(item.count));
|
2023-12-06 20:23:44 +04:00
|
|
|
|
row.appendChild(createTableColumn(item.discounts.name));
|
2023-11-30 21:18:37 +04:00
|
|
|
|
// редактировать в модальном окне
|
|
|
|
|
row.appendChild(createTableAnchor("fa-pencil", editCallback));
|
|
|
|
|
// удаление
|
|
|
|
|
row.appendChild(createTableAnchor("fa-trash", deleteCallback));
|
|
|
|
|
return row;
|
|
|
|
|
}
|
|
|
|
|
export function createTableRowOnIndex(item, index, editCallback, editPageCallback, deleteCallback) {
|
2023-12-06 20:23:44 +04:00
|
|
|
|
console.log(item);
|
2023-11-30 21:18:37 +04:00
|
|
|
|
|
|
|
|
|
const img = document.createElement("img");
|
|
|
|
|
img.classList.add("objects");
|
|
|
|
|
img.src = item.image;
|
|
|
|
|
img.alt = "object";
|
|
|
|
|
img.style.display = "block";
|
|
|
|
|
img.style.marginLeft = "auto";
|
|
|
|
|
img.style.marginRight = "auto";
|
|
|
|
|
|
|
|
|
|
const name = document.createElement("div");
|
|
|
|
|
name.classList.add("caption");
|
2023-12-06 20:23:44 +04:00
|
|
|
|
name.innerText = item.items.name;
|
2023-11-30 21:18:37 +04:00
|
|
|
|
name.style.padding = "0px";
|
2023-12-06 20:23:44 +04:00
|
|
|
|
name.style.marginBottom = "-10px";
|
2023-11-30 21:18:37 +04:00
|
|
|
|
|
|
|
|
|
const count = document.createElement("div");
|
|
|
|
|
count.classList.add("caption_count");
|
|
|
|
|
count.innerText = `${item.count} ${item.count == 1 ? 'штука' : item.count > 1 && item.count < 5 ? 'штуки' : 'штук'}`;
|
|
|
|
|
count.style.padding = "0px";
|
|
|
|
|
count.style.marginBottom = "-10px";
|
|
|
|
|
|
|
|
|
|
const price = document.createElement("div");
|
|
|
|
|
price.classList.add("caption_2");
|
|
|
|
|
price.innerText = parseInt(item.price) + "₽";
|
|
|
|
|
price.style.padding = "0";
|
|
|
|
|
|
|
|
|
|
const a = document.createElement("a");
|
2023-12-06 20:23:44 +04:00
|
|
|
|
a.href = "page4.html";
|
|
|
|
|
a.style.textDecoration = "none";
|
2023-11-30 21:18:37 +04:00
|
|
|
|
|
2023-12-06 20:23:44 +04:00
|
|
|
|
const discounts = document.createElement("div");
|
|
|
|
|
discounts.classList.add("caption");
|
|
|
|
|
discounts.innerText = item.discounts.name;
|
2023-11-30 21:18:37 +04:00
|
|
|
|
|
|
|
|
|
a.appendChild(img);
|
|
|
|
|
a.appendChild(name);
|
|
|
|
|
a.appendChild(count);
|
|
|
|
|
a.appendChild(price);
|
2023-12-06 20:23:44 +04:00
|
|
|
|
a.appendChild(discounts);
|
2023-11-30 21:18:37 +04:00
|
|
|
|
|
|
|
|
|
const buttonsContainer = document.createElement("div");
|
|
|
|
|
buttonsContainer.style.display = "flex";
|
|
|
|
|
buttonsContainer.style.justifyContent = "center";
|
|
|
|
|
|
|
|
|
|
const editButton = createTableAnchor("fa-pencil", editCallback);
|
|
|
|
|
const deleteButton = createTableAnchor("fa-trash", deleteCallback);
|
2023-12-06 20:23:44 +04:00
|
|
|
|
deleteButton.style.marginLeft = "1.2vw";
|
2023-11-30 21:18:37 +04:00
|
|
|
|
buttonsContainer.appendChild(editButton);
|
|
|
|
|
buttonsContainer.appendChild(deleteButton);
|
2023-12-06 20:23:44 +04:00
|
|
|
|
buttonsContainer.style.marginTop = "-6px";
|
|
|
|
|
buttonsContainer.style.marginBottom = "10px";
|
2023-11-30 21:18:37 +04:00
|
|
|
|
a.appendChild(buttonsContainer);
|
|
|
|
|
|
|
|
|
|
const div = document.createElement("div");
|
|
|
|
|
div.classList.add("col");
|
|
|
|
|
div.classList.add("clear-float");
|
|
|
|
|
div.appendChild(a);
|
|
|
|
|
|
|
|
|
|
const uniqueId = `div-${index + 1}`;
|
|
|
|
|
div.id = uniqueId;
|
|
|
|
|
|
|
|
|
|
div.style.float = "left";
|
|
|
|
|
div.style.width = "33.33%";
|
|
|
|
|
div.style.boxSizing = "border-box";
|
|
|
|
|
return div;
|
|
|
|
|
}
|