blob: 351c786095c5a87dec4f69d76160b561461be7b5 [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) {
7 var a, b, i, val = this.value;
8 /*close any already open lists of autocompleted values*/
9 closeAllLists();
Huguet57004b1552018-07-23 03:29:12 +020010 if (!val || val.length < 3) return false;
Huguet57ae6d9742018-07-23 01:07:26 +020011 currentFocus = -1;
12
13 /*create a DIV element that will contain the items (values):*/
14 a = document.createElement("DIV");
15 a.setAttribute("id", this.id + "autocomplete-list");
16 a.setAttribute("class", "autocomplete-items");
17 /*append the DIV element as a child of the autocomplete container:*/
18 this.parentNode.appendChild(a);
19
20 /*for each item in the object...*/
21 for (node in obj) {
22 var nomNode = obj[node].name;
Huguet57004b1552018-07-23 03:29:12 +020023
24 if (nomNode.toUpperCase().includes(val.toUpperCase())) {
25 var parts = nomNode.toUpperCase().split(val.toUpperCase());
Huguet57004b1552018-07-23 03:29:12 +020026
27 /*create a DIV element for each matching element:*/
28 b = document.createElement("DIV");
Huguet57cd3564b2018-07-23 04:40:40 +020029
Huguet57004b1552018-07-23 03:29:12 +020030 /*make the matching letters bold:*/
31 if (parts[0].length == 0) b.innerHTML = "";
32 else b.innerHTML = nomNode.substr(0, parts[0].length);
33 b.innerHTML += "<strong>" + nomNode.substr(parts[0].length, val.length) + "</strong>";
34 b.innerHTML += nomNode.substr(parts[0].length + val.length);
35 b.innerHTML += " - <span class='autocomplete-year'>" + obj[node].year + "</span>";
36 /*insert a input field that will hold the current object item's value:*/
37 b.innerHTML += "<input type='hidden' value='" + node + "'>";
38
39 /*execute a function when someone clicks on the item value (DIV element):*/
40 b.addEventListener("click", function(e) {
41 /*insert the value for the autocomplete text field:*/
42 var n = this.getElementsByTagName("input")[0].value;
43 inp.value = obj[n].name;
44
Huguet57f02e8c02018-07-23 16:48:47 +020045 switch (act) {
46 case "search":
47 // Move camera to desired node
48 cameraGoto(obj[n].x, obj[n].y);
49 break;
avm999632d35c5a2018-09-26 17:05:20 +020050 case "addEdge":
Huguet57f02e8c02018-07-23 16:48:47 +020051 // Add an edge between A and B
avm999632d35c5a2018-09-26 17:05:20 +020052 alert(obj[n].name);
Huguet57f02e8c02018-07-23 16:48:47 +020053 break;
54 }
avm999632d35c5a2018-09-26 17:05:20 +020055
56 // Close the search box
57 var searchBox = document.getElementById('searchBox');
58 searchBox.style.display = "none";
59
60 // Empty the input bar
61 var searchInput = document.getElementById('searchInput');
62 searchInput.value = "";
Huguet57004b1552018-07-23 03:29:12 +020063
64 /*close the list of autocompleted values,
65 (or any other open lists of autocompleted values:*/
66 closeAllLists();
67 });
Huguet57cd3564b2018-07-23 04:40:40 +020068
69 // Set "data-edges" attribute and compare with others
70 var nEdges = Object.keys(s.graph.neighbors(node)).length;
71 b.dataset.edges = nEdges;
72 var inserted = false;
73
74 // Sort nodes by degree
75 for (i in a.childNodes) {
76 var child = a.childNodes[i];
77 if (!child.dataset) break;
78 if (nEdges > child.dataset.edges) {
79 a.insertBefore(b, child);
80 inserted = true;
81 break;
82 }
83 }
84
85 if (!inserted) {
86 a.appendChild(b);
87 console.log("inserted");
88 }
Huguet57004b1552018-07-23 03:29:12 +020089 }
Huguet57ae6d9742018-07-23 01:07:26 +020090 }
Huguet57ae6d9742018-07-23 01:07:26 +020091 });
92 /*execute a function presses a key on the keyboard:*/
93 inp.addEventListener("keydown", function(e) {
94 var x = document.getElementById(this.id + "autocomplete-list");
95 if (x) x = x.getElementsByTagName("div");
96 if (e.keyCode == 40) {
97 /*If the objow DOWN key is pressed,
98 increase the currentFocus variable:*/
99 currentFocus++;
100 /*and and make the current item more visible:*/
101 addActive(x);
102 } else if (e.keyCode == 38) { //up
103 /*If the objow UP key is pressed,
104 decrease the currentFocus variable:*/
105 currentFocus--;
106 /*and and make the current item more visible:*/
107 addActive(x);
108 } else if (e.keyCode == 13) {
109 /*If the ENTER key is pressed, prevent the form from being submitted,*/
110 e.preventDefault();
111 if (currentFocus > -1) {
112 /*and simulate a click on the "active" item:*/
113 if (x) x[currentFocus].click();
114 }
115 }
116 });
117 function addActive(x) {
118 /*a function to classify an item as "active":*/
119 if (!x) return false;
120 /*start by removing the "active" class on all items:*/
121 removeActive(x);
122 if (currentFocus >= x.length) currentFocus = 0;
123 if (currentFocus < 0) currentFocus = (x.length - 1);
124 /*add class "autocomplete-active":*/
125 x[currentFocus].classList.add("autocomplete-active");
126 }
127 function removeActive(x) {
128 /*a function to remove the "active" class from all autocomplete items:*/
129 for (var i = 0; i < x.length; i++) {
130 x[i].classList.remove("autocomplete-active");
131 }
132 }
133 function closeAllLists(elmnt) {
134 /*close all autocomplete lists in the document,
135 except the one passed as an argument:*/
136 var x = document.getElementsByClassName("autocomplete-items");
137 for (var i = 0; i < x.length; i++) {
138 if (elmnt != x[i] && elmnt != inp) {
139 x[i].parentNode.removeChild(x[i]);
140 }
141 }
142 }
143 /*execute a function when someone clicks in the document:*/
Huguet57004b1552018-07-23 03:29:12 +0200144 document.addEventListener("click", function (e) {
Huguet57ae6d9742018-07-23 01:07:26 +0200145 closeAllLists(e.target);
146 });
Huguet575b885692018-07-23 01:18:56 +0200147}