2023-01-13 15:26:35 +03:00
|
|
|
|
/**
|
|
|
|
|
* Utils module
|
|
|
|
|
* @module Utils
|
|
|
|
|
*/
|
2023-01-16 19:30:44 +03:00
|
|
|
|
import { IStyle } from 'interfaces/cg-select.interface';
|
|
|
|
|
import { IItems } from 'interfaces/items.interface';
|
|
|
|
|
import { ISelectedItems } from './urils.interface';
|
2023-01-05 19:56:07 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Преобразование каждого елемента полученного из поля Items;
|
2023-01-13 15:26:35 +03:00
|
|
|
|
* @param {any} dataItem полученный елемент переданный при создании селекта может быть как object / string
|
2023-01-05 19:56:07 +03:00
|
|
|
|
* @param {number} index индекс этого элемента
|
2023-01-13 15:26:35 +03:00
|
|
|
|
* @returns {IItems} возвращает сформированный объект
|
2023-01-05 19:56:07 +03:00
|
|
|
|
*/
|
2023-01-06 21:14:16 +03:00
|
|
|
|
export function getFormatItem(dataItem: any, index: number): IItems {
|
2023-01-05 19:56:07 +03:00
|
|
|
|
const random = Math.random().toString(36).substring(2, 10);
|
|
|
|
|
let item: IItems;
|
|
|
|
|
|
2023-01-06 21:14:16 +03:00
|
|
|
|
if (checkItemStruct(dataItem)) {
|
|
|
|
|
return dataItem;
|
|
|
|
|
} else {
|
2023-01-05 19:56:07 +03:00
|
|
|
|
item = {
|
|
|
|
|
id: random,
|
|
|
|
|
title: dataItem,
|
2023-01-06 21:14:16 +03:00
|
|
|
|
value: index,
|
|
|
|
|
};
|
2023-01-06 13:40:18 +03:00
|
|
|
|
|
2023-01-05 19:56:07 +03:00
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 16:10:43 +03:00
|
|
|
|
/**
|
|
|
|
|
* Вставка изначального текста селекта(до выбора)
|
|
|
|
|
* @param {ITextSelect} data объект в котором находяться title селекта
|
|
|
|
|
* @param {HTMLElement | null | undefined} select елемент селекта, куда будет вставляться title
|
|
|
|
|
* @returns {HTMLElement} возвращает сформированный елемент селекта
|
|
|
|
|
*/
|
|
|
|
|
export function getSelectText(
|
2023-01-10 14:33:09 +03:00
|
|
|
|
data: ISelectedItems,
|
2023-01-09 16:10:43 +03:00
|
|
|
|
select: HTMLElement | null | undefined,
|
|
|
|
|
): HTMLElement {
|
|
|
|
|
const { placeholder, selected } = data;
|
|
|
|
|
|
|
|
|
|
if (placeholder) {
|
|
|
|
|
select!.innerText = placeholder;
|
|
|
|
|
} else if (selected) {
|
|
|
|
|
select!.innerText = selected;
|
|
|
|
|
} else {
|
|
|
|
|
select!.innerText = 'Select...';
|
|
|
|
|
}
|
|
|
|
|
return select!;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-05 19:56:07 +03:00
|
|
|
|
/**
|
|
|
|
|
* Проверка содержит ли item указанные свойства,
|
|
|
|
|
* @param {object} item проверяемый на определенную структуру элемент
|
|
|
|
|
* @returns {boolean} возвращает true/false если item содержит указанные свойства
|
|
|
|
|
*/
|
|
|
|
|
export function checkItemStruct(item: object): boolean {
|
|
|
|
|
if (item && typeof item !== 'object') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return item.hasOwnProperty('id') && item.hasOwnProperty('title') && item.hasOwnProperty('value');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Создание кнопки выбора элементов
|
|
|
|
|
* @param {HTMLElement} element созданный экземпляр класса DropDown
|
|
|
|
|
* @param {string} content placeholer передаваемый из настроек селекта
|
|
|
|
|
* @param {object} styles не обязательный параметр. Объект в котором находяться настройки кастомизации частей селекта
|
|
|
|
|
*/
|
2023-01-13 15:26:35 +03:00
|
|
|
|
export function createSelected(element: Element, content?: string, styles?: IStyle) {
|
2023-01-05 19:56:07 +03:00
|
|
|
|
const select = document.createElement('div');
|
|
|
|
|
const selected = document.createElement('p');
|
|
|
|
|
const caret = document.createElement('div');
|
|
|
|
|
|
|
|
|
|
select.classList.add('cg-select');
|
|
|
|
|
selected.classList.add('selected');
|
|
|
|
|
caret.classList.add('caret');
|
|
|
|
|
|
|
|
|
|
select.appendChild(selected);
|
|
|
|
|
select.appendChild(caret);
|
|
|
|
|
|
2023-01-06 21:14:16 +03:00
|
|
|
|
if (content) {
|
2023-01-05 19:56:07 +03:00
|
|
|
|
const text = document.createTextNode(content);
|
|
|
|
|
selected.appendChild(text);
|
2023-01-06 21:14:16 +03:00
|
|
|
|
element?.appendChild(select);
|
|
|
|
|
} else if (styles) {
|
2023-01-12 16:23:49 +03:00
|
|
|
|
customStyles(element!, styles);
|
2023-01-06 21:14:16 +03:00
|
|
|
|
select.setAttribute('style', `${styles}`);
|
|
|
|
|
selected.setAttribute('style', `${styles}`);
|
|
|
|
|
caret.setAttribute('style', `${styles}`);
|
2023-01-05 19:56:07 +03:00
|
|
|
|
}
|
2023-01-06 21:14:16 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 14:33:09 +03:00
|
|
|
|
/**
|
|
|
|
|
* Создание кнопки отчиски селекта, при единичном выборе.
|
|
|
|
|
* @param {HTMLElement} select место в селекте которое будет переназначено на ''.
|
|
|
|
|
* @param {Element} element экземпляр класса DropDown.
|
|
|
|
|
* @param {ISelectedItems} dataSelectText текст который отрисовывается в селекте.
|
|
|
|
|
*/
|
|
|
|
|
export function clearSelect(select: HTMLElement, element: Element, dataSelectText: ISelectedItems) {
|
|
|
|
|
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) {
|
|
|
|
|
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) => {
|
|
|
|
|
if (item instanceof HTMLInputElement) {
|
|
|
|
|
item.checked = false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
getSelectText(dataSelectText, select);
|
|
|
|
|
|
|
|
|
|
options.forEach((option) => {
|
|
|
|
|
option.classList.remove('active');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 21:14:16 +03:00
|
|
|
|
/**
|
|
|
|
|
* Поведение нативного(одинарного) селекта при выборе кастомного
|
|
|
|
|
* @param {NodeList} element NodeList нативного селекта
|
|
|
|
|
* @param {any} item выбранный элемент в кастомном селекте
|
|
|
|
|
*/
|
|
|
|
|
export function nativeOptionOrdinary(element: NodeListOf<Element> | undefined, item: any) {
|
|
|
|
|
element!.forEach((option) => {
|
|
|
|
|
option.removeAttribute('selected');
|
|
|
|
|
if (option.textContent === item) {
|
|
|
|
|
option.setAttribute('selected', 'selected');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-01-09 16:10:43 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Поведение нативного(Multiple) селекта при выборе в кастомном
|
|
|
|
|
* @param {NodeListOf<Element> | undefined} element NodeList нативного селекта
|
|
|
|
|
* @param {string} item выбранный элемент в кастомном селекте
|
|
|
|
|
* @param {boolean} condition специальный флаг при котором добавляются/убераются атрибуты у нативного селекта
|
|
|
|
|
*/
|
|
|
|
|
export function nativeOptionMultiple(
|
|
|
|
|
element: NodeListOf<Element> | undefined,
|
|
|
|
|
item: string,
|
|
|
|
|
condition: boolean,
|
|
|
|
|
) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-01-10 18:12:38 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Поиск и стилизация елементов полученных из styles экземпляра DropDown
|
|
|
|
|
* @param {Element} element созданный экземпляр класса DropDown
|
|
|
|
|
* @param {object} styles объект в котором находяться настройки кастомизации частей селекта
|
|
|
|
|
*/
|
2023-01-13 15:26:35 +03:00
|
|
|
|
export function customStyles(element: Element, styles: IStyle) {
|
2023-01-10 18:12:38 +03:00
|
|
|
|
const cgSelect = element.querySelector('.cg-select');
|
|
|
|
|
const caretSelect = element.querySelector('.caret');
|
|
|
|
|
const placeholderSelect = element.querySelector('.selected');
|
|
|
|
|
const lableItem = element.parentElement!.querySelector('h1.label');
|
|
|
|
|
|
2023-01-13 15:26:35 +03:00
|
|
|
|
customStylesFormat(styles.head!, cgSelect!);
|
|
|
|
|
customStylesFormat(styles.caret!, caretSelect!);
|
|
|
|
|
customStylesFormat(styles.lable!, lableItem!);
|
2023-01-10 18:12:38 +03:00
|
|
|
|
|
|
|
|
|
if (placeholderSelect) {
|
2023-01-13 15:26:35 +03:00
|
|
|
|
customStylesFormat(styles.placeholder!, placeholderSelect);
|
2023-01-10 18:12:38 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Универсальный метод для стилизации селекта
|
|
|
|
|
* @param {object} elemOption объект полученное из объекта styles у которого мы получаем ключ-значение стилей
|
|
|
|
|
* @param {HTMLElement} selector HTMLElement подвергающиеся кастомизации
|
|
|
|
|
*/
|
|
|
|
|
export function customStylesFormat(elemOption: object, selector: any) {
|
|
|
|
|
if (elemOption) {
|
|
|
|
|
Object.entries(elemOption).forEach(([key, value]) => {
|
|
|
|
|
selector.style[key] = value;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|