-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-element.ts
124 lines (100 loc) · 3.9 KB
/
create-element.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { customStylesFormat, nativeOptionMultiple } from '../utils/utils';
import { ICreateBreadCrumb } from './create-element.interface';
/**
* The method that creates the native select.
* @returns {HTMLSelectElement} Returns the created native select.
*/
export function createNativeSelect(): HTMLSelectElement {
const nativeSelect = document.createElement('select');
nativeSelect.setAttribute('name', 'dataSelect');
nativeSelect.classList.add('nativeSelect');
return nativeSelect;
}
/**
* The method that creates Options for the native select.
* @returns {HTMLOptionElement} Returns the generated Options of the native select.
*/
export function createNativeSelectOption(): HTMLOptionElement {
const nativeOption = document.createElement('option');
nativeOption.classList.add('nativeSelect__nativeOption');
return nativeOption;
}
/**
* The method that creates and is responsible for the behavior of the chips.
* @param {ICreateBreadCrumb} data an object that contains settings and select elements.
* @param {string} title the name of the selected element to draw chips.
* @param {number} index index of the selected item to draw chips.
* @param {string} id unique id of the selected element.
* @returns {HTMLElement} returns the generated HTMLElement chips item.
*/
export function createBreadCrumb(
data: ICreateBreadCrumb,
title: string,
index: number,
id: string,
): HTMLLIElement {
const { element, option, indexes, selectedItems } = data;
const { placeholder, styles } = option;
const selected: HTMLElement | null | undefined = 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: any;
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;
}
/**
* The method that creates a search for elements in the select.
* @param {string} random unique value for input element.
* @param {string} lenguage text in specific language passed from language.ts file
* @returns {HTMLInputElement} Returns the rendered input element.
*/
export function createInputSearch(random: string, lenguage: string): HTMLInputElement {
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;
}