-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathautoCompleter.js
155 lines (147 loc) · 4.95 KB
/
autoCompleter.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
function autoCompleter(inputElement, suggestionsArray, suggestionUrl, selector, search) {
/*
let autoComplete = autocompleter(document.getElementById("searchInput"),null,"https://democert.org/cveClient/cwe-common.json","cwe-common","templet");
autoComplete.hoverColor = "#333";
autoComplete.bgColor = "#000"; // for dark mode
*/
"use strict";
if( !(this instanceof autoCompleter) ){
return new autoCompleter(...arguments);
}
this.hoverColor = "#eee";
this.bgColor = "#f9f9f9";
this._version = "1.0.12";
let main = this;
if(!inputElement || (!(inputElement.parentElement))) {
console.error("Error no inputelement found to work with");
return;
}
async function fetch_data(suggestionUrl, searchString) {
const url = new URL(suggestionUrl);
if(search) {
url.searchParams.delete(search);
url.searchParams.append(search, searchString)
}
suggestionUrl = url.href;
const fobj = await fetch(suggestionUrl);
const data = await fobj.json();
if(selector && data[selector])
return data[selector];
return data;
}
inputElement.parentElement.style.display = "inline-block";
inputElement.parentElement.style.position = "relative";
inputElement.addEventListener("click", function(e) {
closeAllLists(e.target);
});
function cleanHTML(content) {
const div = document.createElement("div");
div.textContent = content;
return div.innerHTML;
}
let currentFocus;
inputElement.addEventListener("input", async function() {
let suggestionBox, suggestionItem, val = this.value;
/* Dynamicly update the suggestions array or fetch it once for all*/
if(suggestionUrl) {
if(search) {
suggestionsArray = await fetch_data(suggestionUrl, val);
} else if (!("once" in main)) {
suggestionsArray = await fetch_data(suggestionUrl, "");
main.once = true;
}
}
if((!Array.isArray(suggestionsArray))) {
console.error("Suggestion Array cannot be empty or not array");
return;
}
if(suggestionsArray.length < 1) {
console.error("Input array cannot be empty with no suggestions");
return;
}
closeAllLists();
if (!val) return false;
currentFocus = -1;
suggestionBox = document.createElement("DIV");
const boxCSS = {"position": "absolute",
"border": "1px solid #ddd",
"border-bottom": "none","border-top": "none",
"z-index": "99","top": "100%","left": "0",
"right": "0", "background-color": main.bgColor};
for(const k in boxCSS) {
suggestionBox.style.setProperty(k,boxCSS[k])
}
this.parentNode.appendChild(suggestionBox);
suggestionsArray.forEach(function(suggestion) {
if (suggestion.toLowerCase().indexOf(val.toLowerCase()) > -1) {
suggestionItem = document.createElement("DIV");
const itemCSS = {"padding": "10px",
"cursor": "pointer",
"border-bottom": "1px solid #ddd"};
for(const k in itemCSS) {
suggestionItem.style.setProperty(k,itemCSS[k])
}
suggestionItem.onmouseover = function() {
this.style.backgroundColor = main.hoverColor;
}
suggestionItem.onmouseleave = function () {
this.style.backgroundColor = "";
}
const r = new RegExp(cleanHTML(val),"dgi");
let suggestionHTML = cleanHTML(suggestion);
new Set(suggestionHTML.match(r)).forEach(function(m) {
suggestionHTML = suggestionHTML.replaceAll(m,"<strong>" + m + "</strong>");
});
suggestionItem.innerHTML = suggestionHTML;
let input = document.createElement("input");
input.value = suggestion;
input.type = "hidden";
suggestionItem.appendChild(input);
suggestionItem.addEventListener("click", function() {
inputElement.value = this.getElementsByTagName("input")[0].value;
closeAllLists();
});
suggestionBox.appendChild(suggestionItem);
}
});
});
inputElement.addEventListener("keydown", function(e) {
const keyCodes = {arrowDown: 40, arrowUp: 38, Enter: 13}
let items = inputElement.parentElement.querySelectorAll("div > div");
if (items) items = Array.from(items);
if (e.keyCode == keyCodes.arrowDown) {
currentFocus++;
if(currentFocus > 0 )
addActive(items);
} else if (e.keyCode == keyCodes.arrowUp) {
currentFocus--;
if(currentFocus > 0 )
addActive(items);
} else if (e.keyCode == keyCodes.Enter) {
e.preventDefault();
if (currentFocus > -1 && items) {
items[currentFocus].click();
}
}
});
function addActive(items) {
if (!items) return false;
removeActive(items);
if (currentFocus >= items.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = items.length - 1;
if(currentFocus > 0)
items[currentFocus].style.backgroundColor = main.hoverColor;
}
function removeActive(items) {
items.forEach(function(item) { item.style.backgroundColor= ""});
}
function closeAllLists(elmnt) {
let items = inputElement.parentElement.querySelectorAll(":scope > div");
items.forEach(function(item) {
if (elmnt != item && elmnt != inputElement) {
item.parentNode.removeChild(item);
}
});
}
return main;
}