2022-10-10 20:05:02 +03:00
|
|
|
export function createSelected(element, content, styles) {
|
|
|
|
if (content) {
|
|
|
|
element.innerHTML = `
|
2022-10-12 22:40:57 +03:00
|
|
|
<div class="cg-select">
|
2022-10-10 20:05:02 +03:00
|
|
|
<p class="selected">${content}</p>
|
|
|
|
<div class="caret"></div>
|
2022-10-12 22:40:57 +03:00
|
|
|
</div>
|
2022-10-10 20:05:02 +03:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (styles) {
|
|
|
|
customStyles(element, styles);
|
|
|
|
|
|
|
|
element.innerHTML = `
|
2022-10-12 22:40:57 +03:00
|
|
|
<div class="cg-select" style = "${styles}">
|
|
|
|
<span class="selected" style = "${styles}">${content}</span>
|
|
|
|
<div class="caret" style = "${styles}"></div>
|
|
|
|
</div>
|
2022-10-10 20:05:02 +03:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function customStyles(element, styles) {
|
|
|
|
if (!styles) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { head, caret, placeholder } = styles;
|
2022-10-12 14:36:49 +03:00
|
|
|
const cgSelect = element.querySelector('.cg-select');
|
2022-10-10 20:05:02 +03:00
|
|
|
const crt = element.querySelector('.caret');
|
|
|
|
|
|
|
|
const placeh = element.querySelector('.selected');
|
|
|
|
|
|
|
|
if (head) {
|
|
|
|
Object.entries(head).forEach(([key, value]) => {
|
2022-10-12 14:36:49 +03:00
|
|
|
cgSelect.style[key] = value;
|
2022-10-10 20:05:02 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (caret) {
|
|
|
|
Object.entries(caret).forEach(([key, value]) => {
|
|
|
|
crt.style[key] = value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (placeh) {
|
|
|
|
if (placeholder) {
|
|
|
|
Object.entries(placeholder).forEach(([key, value]) => {
|
|
|
|
placeh.style[key] = value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-12 22:40:57 +03:00
|
|
|
|
|
|
|
export function checkItemStruct(item) {
|
|
|
|
if (item && typeof item !== 'object') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item.hasOwnProperty('id') && item.hasOwnProperty('title') && item.hasOwnProperty('value');
|
|
|
|
}
|
2022-10-14 14:39:34 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|