-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateSearchBox.js
59 lines (54 loc) · 1.55 KB
/
createSearchBox.js
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
function createSearchBox() {
this.words = [];
this.element = null;
this.ulElement = null;
this.inputRef = null;
this.init();
this.createListItem();
}
createSearchBox.prototype.init = function () {
this.element = document.createElement("div");
this.element.classList.add("search-box");
this.ulElement = document.createElement("ul");
this.element.appendChild(this.ulElement);
};
createSearchBox.prototype.createListItem = function (results, inputValue) {
this.clearList();
this.clearList();
if (inputValue === "" || inputValue === undefined || results.length <= 0) {
this.element.style.display = "none";
} else {
this.element.style.display = "block";
}
inputValue !== "" &&
results &&
results !== undefined &&
results.forEach((word) => {
const listElem = document.createElement("li");
listElem.setAttribute("id", word);
listElem.addEventListener("click", (e) => this.onItemClick(e));
listElem.innerHTML =
`<b id='${word}'>${word.substr(0, inputValue.length)}</b>` +
`<span id='${word}'>${word.substr(
inputValue.length,
word.length
)}</span>`;
this.element.appendChild(listElem);
});
};
createSearchBox.prototype.clearList = function () {
const childListNodes = this.element.childNodes;
childListNodes &&
childListNodes.length > 0 &&
childListNodes.forEach((i, j) => {
i.remove();
});
};
createSearchBox.prototype.setInputRef = function (that) {
this.inputRef = that;
};
createSearchBox.prototype.onItemClick = function (e) {
e.preventDefault();
this.inputRef.onSelect(e.target.id);
e.stopPropagation();
};