Finished in working to TS
This commit is contained in:
@ -1,121 +0,0 @@
|
||||
import { customStylesFormat, nativeOptionMultiple } from './utils';
|
||||
/**
|
||||
* @module createBreadcrumb
|
||||
*/
|
||||
|
||||
/**
|
||||
* Метод который создает и отвечает за поведение chips
|
||||
* @param {object} data объект в котором содержатся настройки и элементы селекта
|
||||
* @param {string} title имя выбранного элемента для отрисовки chips
|
||||
* @param {number} index индекс выбранного элемента для отрисовки chips
|
||||
* @param {string} id уникальное id выбранного элемента
|
||||
* @returns {HTMLElement} возвращает сформированный HTMLElement chips item
|
||||
*/
|
||||
export function createBreadcrumb(data, title, index, id) {
|
||||
const { element, option, indexes, selectedItems } = data;
|
||||
const { placeholder, styles } = option;
|
||||
|
||||
const selected = element.querySelector('.selected');
|
||||
const nativeOption = element.querySelectorAll('.nativeSelect__nativeOption');
|
||||
|
||||
const liChip = document.createElement('li');
|
||||
const textNode = document.createTextNode(title);
|
||||
const svgIcon = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
||||
const path1 = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
||||
const path2 = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
||||
|
||||
svgIcon.setAttribute('viewBox', '0 0 10 10');
|
||||
path1.setAttribute('d', 'M3,7 L7,3');
|
||||
path2.setAttribute('d', 'M3,3 L7,7');
|
||||
liChip.setAttribute('id', `tag-${index}-${id}`);
|
||||
|
||||
svgIcon.classList.add('svg-icon');
|
||||
|
||||
svgIcon.appendChild(path1);
|
||||
svgIcon.appendChild(path2);
|
||||
liChip.appendChild(textNode);
|
||||
liChip.appendChild(svgIcon);
|
||||
|
||||
if (styles) {
|
||||
const { chips } = styles;
|
||||
customStylesFormat(chips, liChip);
|
||||
}
|
||||
|
||||
svgIcon.addEventListener('click', (event) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
nativeOptionMultiple(nativeOption, title, false);
|
||||
|
||||
const deleteIcon = indexes.indexOf(index);
|
||||
let checkBox = '';
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
liChip.parentElement.removeChild(liChip);
|
||||
});
|
||||
|
||||
return liChip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Метод который создает нативный селект
|
||||
* @returns {HTMLSelectElement} Возвращает созданный нативный селект
|
||||
*/
|
||||
export function createNativeSelect() {
|
||||
const nativeSelect = document.createElement('select');
|
||||
|
||||
nativeSelect.setAttribute('name', 'dataSelect');
|
||||
nativeSelect.classList.add('nativeSelect');
|
||||
return nativeSelect;
|
||||
}
|
||||
|
||||
/**
|
||||
* Метод который создает Options для нативного селекта
|
||||
* @returns {HTMLOptionElement} Возвращает созданные Options нативного селекта
|
||||
*/
|
||||
export function createNativeSelectOption() {
|
||||
const nativeOption = document.createElement('option');
|
||||
|
||||
nativeOption.classList.add('nativeSelect__nativeOption');
|
||||
return nativeOption;
|
||||
}
|
||||
|
||||
/**
|
||||
* Метод который создает поиск элементов в селекте
|
||||
* @param {string} random уникальное значение для input элемента.
|
||||
* @param {string} lenguage текст на определенном языке переданный из файла language.js
|
||||
* @returns {HTMLInputElement} Возвращает сформированный input елемент.
|
||||
*/
|
||||
export function createInputSearch(random, lenguage) {
|
||||
const inputSearch = document.createElement('input');
|
||||
|
||||
inputSearch.type = 'text';
|
||||
inputSearch.classList.add('inputSearch');
|
||||
inputSearch.setAttribute('id', `searchSelect-${random}`);
|
||||
|
||||
if (lenguage) {
|
||||
inputSearch.setAttribute('placeholder', `${lenguage}`);
|
||||
} else {
|
||||
inputSearch.setAttribute('placeholder', 'Search...');
|
||||
}
|
||||
|
||||
inputSearch.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
});
|
||||
|
||||
return inputSearch;
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
import { customStylesFormat, nativeOptionMultiple } from '../utils/utilsTs';
|
||||
import { customStylesFormat, nativeOptionMultiple } from '../utils/utils';
|
||||
import { ICreateBreadCrumb } from './create-element.interface';
|
||||
|
||||
/**
|
@ -1,227 +0,0 @@
|
||||
/**
|
||||
* Utils module
|
||||
* @module Utils
|
||||
*/
|
||||
|
||||
/**
|
||||
* Создание кнопки выбора элементов
|
||||
* @param {HTMLElement} element созданный экземпляр класса DropDown
|
||||
* @param {string} content placeholer передаваемый из настроек селекта
|
||||
* @param {object} styles не обязательный параметр. Объект в котором находяться настройки кастомизации частей селекта
|
||||
*/
|
||||
export function createSelected(element, content, styles) {
|
||||
if (content) {
|
||||
element.innerHTML = `
|
||||
<div class="cg-select">
|
||||
<p class="selected">${content}</p>
|
||||
<div class="caret"></div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
if (styles) {
|
||||
customStyles(element, styles);
|
||||
|
||||
element.innerHTML = `
|
||||
<div class="cg-select" style = "${styles}">
|
||||
<p class="selected" style = "${styles}">${content}</p>
|
||||
<div class="caret" style = "${styles}"></div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Поиск и стилизация елементов полученных из styles экземпляра DropDown
|
||||
* @param {HTMLElement} element созданный экземпляр класса DropDown
|
||||
* @param {object} styles объект в котором находяться настройки кастомизации частей селекта
|
||||
*/
|
||||
export function customStyles(element, styles) {
|
||||
if (!styles) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { head, caret, placeholder, lable } = styles;
|
||||
|
||||
const cgSelect = element.querySelector('.cg-select');
|
||||
const caretSelect = element.querySelector('.caret');
|
||||
const placeholderSelect = element.querySelector('.selected');
|
||||
const lableItem = element.parentElement.querySelector('h1.label');
|
||||
|
||||
customStylesFormat(head, cgSelect);
|
||||
customStylesFormat(caret, caretSelect);
|
||||
customStylesFormat(lable, lableItem);
|
||||
|
||||
if (placeholderSelect) {
|
||||
customStylesFormat(placeholder, placeholderSelect);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Универсальный метод для стилизации селекта
|
||||
* @param {object} elemOption объект полученное из объекта styles у которого мы получаем ключ-значение стилей
|
||||
* @param {HTMLElement} selector HTMLElement подвергающиеся кастомизации
|
||||
*/
|
||||
export function customStylesFormat(elemOption, selector) {
|
||||
if (elemOption) {
|
||||
Object.entries(elemOption).forEach(([key, value]) => {
|
||||
selector.style[key] = value;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверка содержит ли item указанные свойства,
|
||||
* @param {object} item проверяемый на определенную структуру элемент
|
||||
* @returns {boolean} возвращает true/false если item содержит указанные свойства
|
||||
*/
|
||||
export function checkItemStruct(item) {
|
||||
if (item && typeof item !== 'object') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return item.hasOwnProperty('id') && item.hasOwnProperty('title') && item.hasOwnProperty('value');
|
||||
}
|
||||
|
||||
/**
|
||||
* Вставка изначального текста селекта(до выбора)
|
||||
* @param {object} data объект в котором находяться title селекта
|
||||
* @param {HTMLElement} select елемент селекта, куда будет вставляться title
|
||||
* @returns {HTMLElement} возвращает сформированный елемент селекта
|
||||
*/
|
||||
export function getSelectText(data, select) {
|
||||
const { placeholder, selected } = data;
|
||||
|
||||
if (placeholder) {
|
||||
select.innerText = placeholder;
|
||||
} else if (selected) {
|
||||
select.innerText = selected;
|
||||
} else {
|
||||
select.innerText = 'Select...';
|
||||
}
|
||||
return select;
|
||||
}
|
||||
|
||||
/**
|
||||
* Преобразование каждого елемента полученного из поля Items;
|
||||
* @param {object | string} dataItem полученный елемент переданный при создании селекта может быть как object/string
|
||||
* @param {number} index индекс этого элемента
|
||||
* @returns {object} возвращает сформированный объект
|
||||
*/
|
||||
export function getFormatItem(dataItem, index) {
|
||||
const random = Math.random().toString(36).substring(2, 10);
|
||||
let item = {};
|
||||
|
||||
if (checkItemStruct(dataItem)) {
|
||||
item = {
|
||||
id: dataItem.id,
|
||||
title: dataItem.title,
|
||||
value: index,
|
||||
};
|
||||
} else {
|
||||
item = {
|
||||
id: random,
|
||||
title: dataItem,
|
||||
value: index,
|
||||
};
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Поведение нативного(одинарного) селекта при выборе кастомного
|
||||
* @param {NodeList} element NodeList нативного селекта
|
||||
* @param {object} item выбранный элемент в кастомном селекте
|
||||
*/
|
||||
export function nativeOptionOrdinary(element, item) {
|
||||
element.forEach((option) => {
|
||||
option.removeAttribute('selected');
|
||||
if (option.textContent === item) {
|
||||
option.setAttribute('selected', 'selected');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Поведение нативного(Multiple) селекта при выборе в кастомном
|
||||
* @param {NodeList} element NodeList нативного селекта
|
||||
* @param {object} item выбранный элемент в кастомном селекте
|
||||
* @param {boolean} condition специальный флаг при котором добавляются/убераются атрибуты у нативного селекта
|
||||
*/
|
||||
export function nativeOptionMultiple(element, item, condition) {
|
||||
element.forEach((option) => {
|
||||
if (condition == true) {
|
||||
if (option.textContent === item) {
|
||||
option.setAttribute('selected', 'selected');
|
||||
}
|
||||
} else if (condition == false) {
|
||||
if (option.textContent === item) {
|
||||
option.removeAttribute('selected');
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Создание кнопки отчиски селекта, при единичном выборе.
|
||||
* @param {HTMLElement} select место в селекте которое будет переназначено на ''.
|
||||
* @param {HTMLElement} element экземпляр класса DropDown.
|
||||
* @param {object} dataSelectText текст который отрисовывается в селекте.
|
||||
*/
|
||||
export function clearSelect(select, element, dataSelectText) {
|
||||
const { selectedItems, indexes, darkTheme, multiselectTag } = dataSelectText;
|
||||
|
||||
const options = element.querySelectorAll('.list__item');
|
||||
const ulMultiSelect = element.querySelector('.multiselect-tag');
|
||||
const svgIcon = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
||||
const path1 = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
||||
const path2 = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
||||
const checkBox = element.querySelectorAll('li input');
|
||||
|
||||
svgIcon.setAttribute('viewBox', '0 0 10 10');
|
||||
path1.setAttribute('d', 'M2,8 L8,2');
|
||||
path2.setAttribute('d', 'M2,2 L8,8');
|
||||
svgIcon.appendChild(path1);
|
||||
svgIcon.appendChild(path2);
|
||||
|
||||
if (multiselectTag && multiselectTag == true) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (darkTheme === true || !darkTheme) {
|
||||
path1.classList.add('pathWhite');
|
||||
path2.classList.add('pathWhite');
|
||||
}
|
||||
|
||||
if (darkTheme === false) {
|
||||
path1.classList.add('pathBlack');
|
||||
path2.classList.add('pathBlack');
|
||||
}
|
||||
|
||||
svgIcon.classList.add('svg-icon');
|
||||
svgIcon.classList.add('svg-clear');
|
||||
|
||||
select.appendChild(svgIcon);
|
||||
|
||||
svgIcon.addEventListener('click', () => {
|
||||
select.innerText = '';
|
||||
|
||||
if (Array.isArray(selectedItems)) {
|
||||
selectedItems.splice(0);
|
||||
indexes.splice(0);
|
||||
}
|
||||
|
||||
checkBox.forEach((item) => {
|
||||
item.checked = false;
|
||||
});
|
||||
|
||||
getSelectText(dataSelectText, select);
|
||||
|
||||
options.forEach((option) => {
|
||||
option.classList.remove('active');
|
||||
});
|
||||
});
|
||||
}
|
@ -86,7 +86,8 @@ export function createSelected(element: Element, content?: string, styles?: ISty
|
||||
if (content) {
|
||||
const text = document.createTextNode(content);
|
||||
selected.appendChild(text);
|
||||
element?.appendChild(select);
|
||||
element.innerHTML = '';
|
||||
element?.insertAdjacentElement('afterbegin', select);
|
||||
} else if (styles) {
|
||||
customStyles(element!, styles);
|
||||
select.setAttribute('style', `${styles}`);
|
||||
@ -105,7 +106,7 @@ export function clearSelect(select: HTMLElement, element: Element, dataSelectTex
|
||||
const { selectedItems, indexes, darkTheme, multiselectTag } = dataSelectText;
|
||||
|
||||
const options = element.querySelectorAll('.list__item');
|
||||
const ulMultiSelect = element.querySelector('.multiselect-tag');
|
||||
const nativeOption = element!.querySelectorAll('.nativeSelect__nativeOption');
|
||||
const svgIcon = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
||||
const path1 = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
||||
const path2 = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
||||
@ -139,6 +140,10 @@ export function clearSelect(select: HTMLElement, element: Element, dataSelectTex
|
||||
svgIcon.addEventListener('click', () => {
|
||||
select!.innerText = '';
|
||||
|
||||
nativeOption.forEach((option) => {
|
||||
option.removeAttribute('selected');
|
||||
});
|
||||
|
||||
if (Array.isArray(selectedItems)) {
|
||||
selectedItems!.splice(0);
|
||||
indexes!.splice(0);
|
Reference in New Issue
Block a user