blob: d7648dae66dca68ce0a160211cf9e7b07dce6abb [file] [log] [blame]
Huguet57f02e8c02018-07-23 16:48:47 +02001function autocomplete(inp, obj, act) {
Huguet57ae6d9742018-07-23 01:07:26 +02002 /*the autocomplete function takes two arguments,
3 the text field element and an objay of possible autocompleted values:*/
4 var currentFocus;
5 /*execute a function when someone writes in the text field:*/
6 inp.addEventListener("input", function(e) {
avm99963a00c43e2018-09-26 20:16:07 +02007 var a, b, i, val = this.value;
8 /*close any already open lists of autocompleted values*/
9 clearLists();
10 document.querySelector(".md-google-search__empty-btn").style.display = (val ? "block" : "none");
11 if (!val || val.length < 3) return false;
12 currentFocus = -1;
13 var is_empty = true;
14
15 /*for each item in the object...*/
16 for (node in obj) {
17 var nomNode = obj[node].name;
18
19 if (nomNode.toUpperCase().includes(val.toUpperCase())) {
20 is_empty = false;
21 var parts = nomNode.toUpperCase().split(val.toUpperCase());
22
Huguet57004b1552018-07-23 03:29:12 +020023 /*create a DIV element for each matching element:*/
avm99963a00c43e2018-09-26 20:16:07 +020024 b = document.createElement("div");
25 b.setAttribute("class", "autocomplete-item");
26
Huguet57004b1552018-07-23 03:29:12 +020027 /*make the matching letters bold:*/
28 if (parts[0].length == 0) b.innerHTML = "";
avm99963a00c43e2018-09-26 20:16:07 +020029 else b.innerHTML = "<span style='font-weight: bold;'>" + nomNode.substr(0, parts[0].length) + "</span>";
30 b.innerHTML += nomNode.substr(parts[0].length, val.length);
31 b.innerHTML += "<span style='font-weight: bold;'>" + nomNode.substr(parts[0].length + val.length) + "</span>";
32 b.innerHTML += " <span class='autocomplete-year'>(" + obj[node].year + ")</span>";
33
34 /*include node id to keep track of which is it*/
35 b.dataset.id = node;
36
37 /*execute a function when someone clicks on the item value (DIV element):*/
Huguet57004b1552018-07-23 03:29:12 +020038 b.addEventListener("click", function(e) {
39 /*insert the value for the autocomplete text field:*/
avm99963a00c43e2018-09-26 20:16:07 +020040 var n = this.dataset.id;
Huguet57004b1552018-07-23 03:29:12 +020041 inp.value = obj[n].name;
avm99963a00c43e2018-09-26 20:16:07 +020042
Huguet57f02e8c02018-07-23 16:48:47 +020043 switch (act) {
avm99963a00c43e2018-09-26 20:16:07 +020044 case "search":
45 // Move camera to desired node
46 cameraGoto(obj[n].x, obj[n].y);
47 break;
48 case "addEdge":
49 // @TODO: Add an edge between A and B
50 alert(obj[n].name);
51 break;
Huguet57f02e8c02018-07-23 16:48:47 +020052 }
avm99963a00c43e2018-09-26 20:16:07 +020053
Huguet57004b1552018-07-23 03:29:12 +020054 /*close the list of autocompleted values,
55 (or any other open lists of autocompleted values:*/
avm99963a00c43e2018-09-26 20:16:07 +020056 clearLists();
Huguet57004b1552018-07-23 03:29:12 +020057 });
avm99963a00c43e2018-09-26 20:16:07 +020058
Huguet57cd3564b2018-07-23 04:40:40 +020059 // Set "data-edges" attribute and compare with others
60 var nEdges = Object.keys(s.graph.neighbors(node)).length;
61 b.dataset.edges = nEdges;
62 var inserted = false;
avm99963a00c43e2018-09-26 20:16:07 +020063
Huguet57cd3564b2018-07-23 04:40:40 +020064 // Sort nodes by degree
avm99963a00c43e2018-09-26 20:16:07 +020065 for (i in document.querySelector("#autocomplete-list").childNodes) {
66 var child = document.querySelector("#autocomplete-list").childNodes[i];
Huguet57cd3564b2018-07-23 04:40:40 +020067 if (!child.dataset) break;
68 if (nEdges > child.dataset.edges) {
avm99963a00c43e2018-09-26 20:16:07 +020069 document.querySelector("#autocomplete-list").insertBefore(b, child);
Huguet57cd3564b2018-07-23 04:40:40 +020070 inserted = true;
71 break;
72 }
73 }
avm99963a00c43e2018-09-26 20:16:07 +020074
Huguet57cd3564b2018-07-23 04:40:40 +020075 if (!inserted) {
avm99963a00c43e2018-09-26 20:16:07 +020076 document.querySelector("#autocomplete-list").appendChild(b);
Huguet57cd3564b2018-07-23 04:40:40 +020077 }
Huguet57004b1552018-07-23 03:29:12 +020078 }
avm99963a00c43e2018-09-26 20:16:07 +020079 }
80
81 document.querySelector(".autocomplete-container").style.display = (is_empty ? "none" : "block");
Huguet57ae6d9742018-07-23 01:07:26 +020082 });
avm99963a00c43e2018-09-26 20:16:07 +020083
84 document.querySelector(".md-google-search__empty-btn").addEventListener("click", function() {
85 document.querySelector("#search-input").value = "";
86 this.style.display = "none";
87 });
88
Huguet57ae6d9742018-07-23 01:07:26 +020089 /*execute a function presses a key on the keyboard:*/
90 inp.addEventListener("keydown", function(e) {
avm99963a00c43e2018-09-26 20:16:07 +020091 var x = document.getElementById("autocomplete-list");
Huguet57ae6d9742018-07-23 01:07:26 +020092 if (x) x = x.getElementsByTagName("div");
avm99963a00c43e2018-09-26 20:16:07 +020093 if (x.length == 0) return;
Huguet57ae6d9742018-07-23 01:07:26 +020094 if (e.keyCode == 40) {
95 /*If the objow DOWN key is pressed,
96 increase the currentFocus variable:*/
97 currentFocus++;
98 /*and and make the current item more visible:*/
99 addActive(x);
100 } else if (e.keyCode == 38) { //up
101 /*If the objow UP key is pressed,
102 decrease the currentFocus variable:*/
103 currentFocus--;
104 /*and and make the current item more visible:*/
105 addActive(x);
106 } else if (e.keyCode == 13) {
107 /*If the ENTER key is pressed, prevent the form from being submitted,*/
108 e.preventDefault();
109 if (currentFocus > -1) {
110 /*and simulate a click on the "active" item:*/
111 if (x) x[currentFocus].click();
112 }
113 }
114 });
115 function addActive(x) {
116 /*a function to classify an item as "active":*/
117 if (!x) return false;
118 /*start by removing the "active" class on all items:*/
119 removeActive(x);
120 if (currentFocus >= x.length) currentFocus = 0;
121 if (currentFocus < 0) currentFocus = (x.length - 1);
122 /*add class "autocomplete-active":*/
123 x[currentFocus].classList.add("autocomplete-active");
124 }
125 function removeActive(x) {
126 /*a function to remove the "active" class from all autocomplete items:*/
127 for (var i = 0; i < x.length; i++) {
128 x[i].classList.remove("autocomplete-active");
129 }
130 }
avm99963a00c43e2018-09-26 20:16:07 +0200131 function clearLists() {
Huguet57ae6d9742018-07-23 01:07:26 +0200132 /*close all autocomplete lists in the document,
133 except the one passed as an argument:*/
avm99963a00c43e2018-09-26 20:16:07 +0200134 var x = document.querySelector("#autocomplete-list");
135 x.innerHTML = "";
136 document.querySelector(".autocomplete-container").style.display = "none";
Huguet57ae6d9742018-07-23 01:07:26 +0200137 }
138 /*execute a function when someone clicks in the document:*/
avm99963a00c43e2018-09-26 20:16:07 +0200139 document.addEventListener("click", function (e) {
140 clearLists();
Huguet57ae6d9742018-07-23 01:07:26 +0200141 });
Huguet575b885692018-07-23 01:18:56 +0200142}