blob: 8b40ad9685a4a3ddbeed0ffe43383f38813f7548 [file] [log] [blame]
Huguet57ae6d9742018-07-23 01:07:26 +02001function autocomplete(inp, obj) {
2 /*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 var nomDinamic = "";
24 var allWordsInName = nomNode.split(" ");
25
26 if (nomNode.toUpperCase().includes(val.toUpperCase())) {
27 var parts = nomNode.toUpperCase().split(val.toUpperCase());
28 console.log(parts);
29
30 /*create a DIV element for each matching element:*/
31 b = document.createElement("DIV");
32 /*make the matching letters bold:*/
33 if (parts[0].length == 0) b.innerHTML = "";
34 else b.innerHTML = nomNode.substr(0, parts[0].length);
35 b.innerHTML += "<strong>" + nomNode.substr(parts[0].length, val.length) + "</strong>";
36 b.innerHTML += nomNode.substr(parts[0].length + val.length);
37 b.innerHTML += " - <span class='autocomplete-year'>" + obj[node].year + "</span>";
38 /*insert a input field that will hold the current object item's value:*/
39 b.innerHTML += "<input type='hidden' value='" + node + "'>";
40
41 /*execute a function when someone clicks on the item value (DIV element):*/
42 b.addEventListener("click", function(e) {
43 /*insert the value for the autocomplete text field:*/
44 var n = this.getElementsByTagName("input")[0].value;
45 inp.value = obj[n].name;
46
47 // Move camera to desired node
48 sigma.misc.animation.camera( s.camera,
49 { x: obj[n].x, y: obj[n].y, ratio: 0.5 },
50 { duration: s.settings('animationsTime') || 300 }
51 );
Huguet57ae6d9742018-07-23 01:07:26 +020052
Huguet57004b1552018-07-23 03:29:12 +020053 // Close the search box
54 var searchBox = document.getElementById('searchBox');
55 searchBox.style.display = "none";
56
57 // Empty the input bar
58 var searchInput = document.getElementById('searchInput');
59 searchInput.value = "";
60
61 /*close the list of autocompleted values,
62 (or any other open lists of autocompleted values:*/
63 closeAllLists();
64 });
65 a.appendChild(b);
66 }
Huguet57ae6d9742018-07-23 01:07:26 +020067 }
Huguet57ae6d9742018-07-23 01:07:26 +020068 });
69 /*execute a function presses a key on the keyboard:*/
70 inp.addEventListener("keydown", function(e) {
71 var x = document.getElementById(this.id + "autocomplete-list");
72 if (x) x = x.getElementsByTagName("div");
73 if (e.keyCode == 40) {
74 /*If the objow DOWN key is pressed,
75 increase the currentFocus variable:*/
76 currentFocus++;
77 /*and and make the current item more visible:*/
78 addActive(x);
79 } else if (e.keyCode == 38) { //up
80 /*If the objow UP key is pressed,
81 decrease the currentFocus variable:*/
82 currentFocus--;
83 /*and and make the current item more visible:*/
84 addActive(x);
85 } else if (e.keyCode == 13) {
86 /*If the ENTER key is pressed, prevent the form from being submitted,*/
87 e.preventDefault();
88 if (currentFocus > -1) {
89 /*and simulate a click on the "active" item:*/
90 if (x) x[currentFocus].click();
91 }
92 }
93 });
94 function addActive(x) {
95 /*a function to classify an item as "active":*/
96 if (!x) return false;
97 /*start by removing the "active" class on all items:*/
98 removeActive(x);
99 if (currentFocus >= x.length) currentFocus = 0;
100 if (currentFocus < 0) currentFocus = (x.length - 1);
101 /*add class "autocomplete-active":*/
102 x[currentFocus].classList.add("autocomplete-active");
103 }
104 function removeActive(x) {
105 /*a function to remove the "active" class from all autocomplete items:*/
106 for (var i = 0; i < x.length; i++) {
107 x[i].classList.remove("autocomplete-active");
108 }
109 }
110 function closeAllLists(elmnt) {
111 /*close all autocomplete lists in the document,
112 except the one passed as an argument:*/
113 var x = document.getElementsByClassName("autocomplete-items");
114 for (var i = 0; i < x.length; i++) {
115 if (elmnt != x[i] && elmnt != inp) {
116 x[i].parentNode.removeChild(x[i]);
117 }
118 }
119 }
120 /*execute a function when someone clicks in the document:*/
Huguet57004b1552018-07-23 03:29:12 +0200121 document.addEventListener("click", function (e) {
Huguet57ae6d9742018-07-23 01:07:26 +0200122 closeAllLists(e.target);
123 });
Huguet575b885692018-07-23 01:18:56 +0200124}