120 lines
4.5 KiB
JavaScript
Raw Normal View History

2023-11-10 14:23:55 +04:00
// модуль для работы с элементами управления
// объект для удобного получения элементов
// при обращении к атрибуту объекта вызывается
// нужная функция для поиска элемента
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'),
2023-11-20 23:03:27 +04:00
author: document.getElementById('author'),
name: document.getElementById('name'),
2023-11-21 16:11:09 +04:00
name_book: document.getElementById('name_book'),
2023-11-10 14:23:55 +04:00
price: document.getElementById('price'),
2023-11-21 16:11:09 +04:00
isbn: document.getElementById('isbn'),
2023-11-10 14:23:55 +04:00
image: document.getElementById('image'),
2023-11-21 16:11:09 +04:00
text: document.getElementById('text'),
2023-11-10 14:23:55 +04:00
imagePreview: document.getElementById('image-preview'),
};
// Дефолтное превью
export const imagePlaceholder = 'https://via.placeholder.com/200';
// функция создает тег option для select
// <option value="" selected>name</option>
// eslint-disable-next-line require-jsdoc
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>
// eslint-disable-next-line require-jsdoc
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>
// eslint-disable-next-line require-jsdoc
function createTableColumn(value) {
const td = document.createElement('td');
td.textContent = value;
return td;
}
2023-11-24 15:09:59 +04:00
function createTableColumnImg(value) {
const td = document.createElement('td');
td.appendChild(value);
return td;
}
2023-11-10 14:23:55 +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>
// eslint-disable-next-line max-len
// <td><a href="#" onclick="editCallback()"><i class="fa-solid fa-pencil"></i></a></td>
// eslint-disable-next-line max-len
// <td><a href="#" onclick="editPageCallback()"><i class="fa-solid fa-pen-to-square"></i></a></td>
// eslint-disable-next-line max-len
// <td><a href="#" onclick="deleteCallback()"><i class="fa-solid fa-trash"></i></a></td>
// </tr>
// eslint-disable-next-line max-len, require-jsdoc
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));
2023-11-21 16:11:09 +04:00
row.appendChild(createTableColumn(item.name_book));
2023-11-20 23:03:27 +04:00
row.appendChild(createTableColumn(item.author));
2023-11-21 16:11:09 +04:00
row.appendChild(createTableColumn(item.isbn));
2023-11-20 23:03:27 +04:00
row.appendChild(createTableColumn(parseFloat(item.price).toFixed(2)));
2023-11-24 15:09:59 +04:00
// Добавляем картинку в таблицу
const img = document.createElement('img');
img.src = item.image;
img.classList.add('small-image');
row.appendChild(createTableColumnImg(img));
2023-11-10 14:23:55 +04:00
// редактировать в модальном окне
row.appendChild(createTableAnchor('fa-pencil', editCallback));
// редактировать на странице page-edit
2023-11-24 15:09:59 +04:00
//row.appendChild(createTableAnchor('fa-pen-to-square', editPageCallback));
2023-11-10 14:23:55 +04:00
// удаление
row.appendChild(createTableAnchor('fa-trash', deleteCallback));
return row;
}
2023-11-24 15:09:59 +04:00