blob: 7751bc1595516d196e070fcec5cad05f292e220c [file] [log] [blame]
Huguet57f02e8c02018-07-23 16:48:47 +02001function autocomplete(inp, obj, act) {
Javier López-Contreras052503f2018-12-26 12:34:42 +01002 /*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) {
7 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;
avm99963a00c43e2018-09-26 20:16:07 +020014
Javier López-Contreras052503f2018-12-26 12:34:42 +010015 /*for each item in the object...*/
16 for (node in obj) {
17 var nomNode = obj[node].name;
avm99963a00c43e2018-09-26 20:16:07 +020018
Javier López-Contreras052503f2018-12-26 12:34:42 +010019 if (nomNode.toUpperCase().includes(val.toUpperCase())) {
20 is_empty = false;
21 var parts = nomNode.toUpperCase().split(val.toUpperCase());
22
23 /*create a DIV element for each matching element:*/
24 b = document.createElement("div");
25 b.setAttribute("class", "autocomplete-item");
avm99963a00c43e2018-09-26 20:16:07 +020026
Javier López-Contreras052503f2018-12-26 12:34:42 +010027 /*make the matching letters bold:*/
28 if (parts[0].length == 0) b.innerHTML = "";
29 else b.innerHTML = "<span style='font-weight: bold; position:relative; z-index:120;'>" + nomNode.substr(0, parts[0].length) + "</span>";
30
31 b.innerHTML += nomNode.substr(parts[0].length, val.length);
32 b.innerHTML += "<span style='font-weight: bold; position:relative; z-index:120;'>" + nomNode.substr(parts[0].length + val.length) + "</span>";
33 b.innerHTML += " <span class='autocomplete-year'>(" + obj[node].year + ")</span>";
avm99963a00c43e2018-09-26 20:16:07 +020034
Javier López-Contreras052503f2018-12-26 12:34:42 +010035 /*include node id to keep track of which is it*/
36 b.dataset.id = node;
avm99963a00c43e2018-09-26 20:16:07 +020037
Javier López-Contreras052503f2018-12-26 12:34:42 +010038 /*execute a function when someone clicks on the item value (DIV element):*/
39 b.addEventListener("click", function(e) {
40 /*insert the value for the autocomplete text field:*/
41 var n = this.dataset.id;
42 inp.value = obj[n].name;
avm99963a00c43e2018-09-26 20:16:07 +020043
Javier López-Contreras052503f2018-12-26 12:34:42 +010044 switch (act) {
45 case "search":
46 // Move camera to desired node
47 cameraGoto(obj[n].x, obj[n].y);
48 break;
49 case "addEdge":
50 // @TODO: Add an edge between A and B
51 alert(obj[n].name);
52 break;
53 }
avm99963a00c43e2018-09-26 20:16:07 +020054
Javier López-Contreras052503f2018-12-26 12:34:42 +010055 /*close the list of autocompleted values,
56 (or any other open lists of autocompleted values:*/
57 clearLists();
58 });
avm99963a00c43e2018-09-26 20:16:07 +020059
Javier López-Contreras052503f2018-12-26 12:34:42 +010060 // Set "data-edges" attribute and compare with others
61 var nEdges = Object.keys(s.graph.neighbors(node)).length;
62 b.dataset.edges = nEdges;
63 var inserted = false;
avm99963a00c43e2018-09-26 20:16:07 +020064
Javier López-Contreras052503f2018-12-26 12:34:42 +010065 // Sort nodes by degree
66 for (i in document.querySelector("#autocomplete-list").childNodes) {
67 var child = document.querySelector("#autocomplete-list").childNodes[i];
68 if (!child.dataset) break;
69 if (nEdges > child.dataset.edges) {
70 document.querySelector("#autocomplete-list").insertBefore(b, child);
71 inserted = true;
72 break;
73 }
74 }
avm99963a00c43e2018-09-26 20:16:07 +020075
Javier López-Contreras052503f2018-12-26 12:34:42 +010076 if (!inserted) {
77 document.querySelector("#autocomplete-list").appendChild(b);
78 }
79 }
80 }
avm99963a00c43e2018-09-26 20:16:07 +020081
Javier López-Contreras052503f2018-12-26 12:34:42 +010082 document.querySelector(".autocomplete-container").style.display = (is_empty ? "none" : "block");
83 });
avm99963a00c43e2018-09-26 20:16:07 +020084
Javier López-Contreras052503f2018-12-26 12:34:42 +010085 document.querySelector(".md-google-search__empty-btn").addEventListener("click", function() {
86 document.querySelector("#search-input").value = "";
87 this.style.display = "none";
88 });
avm99963a00c43e2018-09-26 20:16:07 +020089
Javier López-Contreras052503f2018-12-26 12:34:42 +010090 /*execute a function presses a key on the keyboard:*/
91 inp.addEventListener("keydown", function(e) {
92 var x = document.getElementById("autocomplete-list");
93 if (x) x = x.getElementsByTagName("div");
94 if (x.length == 0) return;
95 if (e.keyCode == 40) {
96 /*If the objow DOWN key is pressed,
97 increase the currentFocus variable:*/
98 currentFocus++;
99 /*and and make the current item more visible:*/
100 addActive(x);
101 } else if (e.keyCode == 38) { //up
102 /*If the objow UP key is pressed,
103 decrease the currentFocus variable:*/
104 currentFocus--;
105 /*and and make the current item more visible:*/
106 addActive(x);
107 } else if (e.keyCode == 13) {
108 /*If the ENTER key is pressed, prevent the form from being submitted,*/
109 e.preventDefault();
110 if (currentFocus > -1) {
111 /*and simulate a click on the "active" item:*/
112 if (x) x[currentFocus].click();
113 }
114 }
115 });
116 function addActive(x) {
117 /*a function to classify an item as "active":*/
118 if (!x) return false;
119 /*start by removing the "active" class on all items:*/
120 removeActive(x);
121 if (currentFocus >= x.length) currentFocus = 0;
122 if (currentFocus < 0) currentFocus = (x.length - 1);
123 /*add class "autocomplete-active":*/
124 x[currentFocus].classList.add("autocomplete-active");
125 }
126 function removeActive(x) {
127 /*a function to remove the "active" class from all autocomplete items:*/
128 for (var i = 0; i < x.length; i++) {
129 x[i].classList.remove("autocomplete-active");
130 }
131 }
132 function clearLists() {
133 /*close all autocomplete lists in the document,
134 except the one passed as an argument:*/
135 var x = document.querySelector("#autocomplete-list");
136 x.innerHTML = "";
137 document.querySelector(".autocomplete-container").style.display = "none";
138 }
139 /*execute a function when someone clicks in the document:*/
140 document.addEventListener("click", function (e) {
141 clearLists();
142 });
Huguet575b885692018-07-23 01:18:56 +0200143}