2022-12-23 19:18:33 +03:00
|
|
|
import { customStylesFormat, nativeOptionMultiple } from './utils';
|
2022-10-19 20:33:37 +03:00
|
|
|
/**
|
|
|
|
* @module createBreadcrumb
|
|
|
|
*/
|
2022-10-17 16:32:14 +03:00
|
|
|
|
2022-10-19 12:05:41 +03:00
|
|
|
/**
|
|
|
|
* Метод который создает и отвечает за поведение chips
|
|
|
|
* @param {object} data объект в котором содержатся настройки и элементы селекта
|
|
|
|
* @param {string} title имя выбранного элемента для отрисовки chips
|
|
|
|
* @param {number} index индекс выбранного элемента для отрисовки chips
|
|
|
|
* @param {string} id уникальное id выбранного элемента
|
2022-10-19 20:33:37 +03:00
|
|
|
* @returns {HTMLElement} возвращает сформированный HTMLElement chips item
|
2022-10-19 12:05:41 +03:00
|
|
|
*/
|
2022-10-12 10:36:17 +03:00
|
|
|
export function createBreadcrumb(data, title, index, id) {
|
|
|
|
const { element, option, indexes, selectedItems } = data;
|
2022-10-17 16:32:14 +03:00
|
|
|
const { placeholder, styles } = option;
|
2022-10-10 20:05:02 +03:00
|
|
|
|
|
|
|
const selected = element.querySelector('.selected');
|
2022-12-23 19:18:33 +03:00
|
|
|
const nativeOption = element.querySelectorAll('.nativeSelect__nativeOption');
|
2022-10-21 14:43:10 +03:00
|
|
|
|
2022-10-17 16:32:14 +03:00
|
|
|
const liChip = document.createElement('li');
|
|
|
|
const textNode = document.createTextNode(title);
|
|
|
|
const svgIcon = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
2022-10-10 20:05:02 +03:00
|
|
|
const path1 = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
|
|
|
const path2 = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
|
|
|
|
2022-10-17 16:32:14 +03:00
|
|
|
svgIcon.setAttribute('viewBox', '0 0 10 10');
|
2022-10-10 20:05:02 +03:00
|
|
|
path1.setAttribute('d', 'M3,7 L7,3');
|
|
|
|
path2.setAttribute('d', 'M3,3 L7,7');
|
2022-10-18 14:13:14 +03:00
|
|
|
liChip.setAttribute('id', `tag-${index}-${id}`);
|
2022-10-10 20:05:02 +03:00
|
|
|
|
2022-10-17 16:32:14 +03:00
|
|
|
svgIcon.classList.add('svg-icon');
|
2022-10-10 20:05:02 +03:00
|
|
|
|
2022-10-17 16:32:14 +03:00
|
|
|
svgIcon.appendChild(path1);
|
|
|
|
svgIcon.appendChild(path2);
|
|
|
|
liChip.appendChild(textNode);
|
|
|
|
liChip.appendChild(svgIcon);
|
2022-10-10 20:05:02 +03:00
|
|
|
|
2022-10-18 14:13:14 +03:00
|
|
|
if (styles) {
|
|
|
|
const { chips } = styles;
|
|
|
|
customStylesFormat(chips, liChip);
|
|
|
|
}
|
2022-10-17 16:32:14 +03:00
|
|
|
|
|
|
|
svgIcon.addEventListener('click', (event) => {
|
2022-10-31 19:32:59 +03:00
|
|
|
event.preventDefault();
|
2022-10-10 20:05:02 +03:00
|
|
|
event.stopPropagation();
|
2022-12-23 19:18:33 +03:00
|
|
|
nativeOptionMultiple(nativeOption, title, false);
|
2022-10-10 20:05:02 +03:00
|
|
|
|
|
|
|
const deleteIcon = indexes.indexOf(index);
|
2022-10-21 14:43:10 +03:00
|
|
|
let checkBox = '';
|
2022-10-10 20:05:02 +03:00
|
|
|
|
|
|
|
indexes.splice(deleteIcon, 1);
|
|
|
|
selectedItems.splice(deleteIcon, 1);
|
|
|
|
|
|
|
|
if (id) {
|
|
|
|
checkBox = document.getElementById(`chbox-${id}`);
|
|
|
|
} else {
|
|
|
|
checkBox = document.getElementById(`chbox-${index}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
checkBox.checked = false;
|
|
|
|
checkBox.parentElement.classList.remove('active');
|
|
|
|
|
|
|
|
if (!selectedItems.length) {
|
|
|
|
selected.innerText = placeholder;
|
|
|
|
}
|
|
|
|
|
2022-10-17 16:32:14 +03:00
|
|
|
liChip.parentElement.removeChild(liChip);
|
2022-10-10 20:05:02 +03:00
|
|
|
});
|
|
|
|
|
2022-10-17 16:32:14 +03:00
|
|
|
return liChip;
|
2022-10-10 20:05:02 +03:00
|
|
|
}
|
2022-10-21 16:17:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Метод который создает нативный селект
|
|
|
|
* @returns {HTMLSelectElement} Возвращает созданный нативный селект
|
|
|
|
*/
|
|
|
|
export function createNativeSelect() {
|
2022-12-23 19:18:33 +03:00
|
|
|
const nativeSelect = document.createElement('select');
|
2022-10-21 16:17:37 +03:00
|
|
|
|
2022-12-23 19:18:33 +03:00
|
|
|
nativeSelect.setAttribute('name', 'dataSelect');
|
|
|
|
nativeSelect.classList.add('nativeSelect');
|
|
|
|
return nativeSelect;
|
2022-10-21 16:17:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Метод который создает Options для нативного селекта
|
|
|
|
* @returns {HTMLOptionElement} Возвращает созданные Options нативного селекта
|
|
|
|
*/
|
2022-12-23 19:18:33 +03:00
|
|
|
export function createNativeSelectOption() {
|
|
|
|
const nativeOption = document.createElement('option');
|
2022-10-21 16:17:37 +03:00
|
|
|
|
2022-12-23 19:18:33 +03:00
|
|
|
nativeOption.classList.add('nativeSelect__nativeOption');
|
|
|
|
return nativeOption;
|
2022-10-21 16:17:37 +03:00
|
|
|
}
|
2022-10-25 16:07:12 +03:00
|
|
|
|
2022-10-25 21:03:10 +03:00
|
|
|
/**
|
|
|
|
* Метод который создает поиск элементов в селекте
|
|
|
|
* @param {string} random уникальное значение для input элемента.
|
2022-11-07 19:56:10 +03:00
|
|
|
* @param {string} lenguage текст на определенном языке переданный из файла language.js
|
2022-10-25 21:03:10 +03:00
|
|
|
* @returns {HTMLInputElement} Возвращает сформированный input елемент.
|
|
|
|
*/
|
2022-11-07 19:56:10 +03:00
|
|
|
export function createInputSearch(random, lenguage) {
|
2022-12-23 19:18:33 +03:00
|
|
|
const inputSearch = document.createElement('input');
|
2022-10-25 16:07:12 +03:00
|
|
|
|
2022-12-23 19:18:33 +03:00
|
|
|
inputSearch.type = 'text';
|
|
|
|
inputSearch.classList.add('inputSearch');
|
|
|
|
inputSearch.setAttribute('id', `searchSelect-${random}`);
|
2022-11-07 19:56:10 +03:00
|
|
|
|
|
|
|
if (lenguage) {
|
2022-12-23 19:18:33 +03:00
|
|
|
inputSearch.setAttribute('placeholder', `${lenguage}`);
|
2022-11-07 19:56:10 +03:00
|
|
|
} else {
|
2022-12-23 19:18:33 +03:00
|
|
|
inputSearch.setAttribute('placeholder', 'Search...');
|
2022-11-07 19:56:10 +03:00
|
|
|
}
|
|
|
|
|
2022-12-23 19:18:33 +03:00
|
|
|
inputSearch.addEventListener('click', (e) => {
|
2022-11-07 19:56:10 +03:00
|
|
|
e.preventDefault();
|
|
|
|
});
|
2022-10-25 16:07:12 +03:00
|
|
|
|
2022-12-23 19:18:33 +03:00
|
|
|
return inputSearch;
|
2022-10-25 16:07:12 +03:00
|
|
|
}
|