blob: d1ff083f54de032213dd0338f615ff2be99977f0 [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
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
45 // Move camera to desired node
46 sigma.misc.animation.camera( s.camera,
47 { x: obj[n].x, y: obj[n].y, ratio: 0.5 },
48 { duration: s.settings('animationsTime') || 300 }
49 );
Huguet57ae6d9742018-07-23 01:07:26 +020050
Huguet57004b1552018-07-23 03:29:12 +020051 // Close the search box
52 var searchBox = document.getElementById('searchBox');
53 searchBox.style.display = "none";
54
55 // Empty the input bar
56 var searchInput = document.getElementById('searchInput');
57 searchInput.value = "";
58
59 /*close the list of autocompleted values,
60 (or any other open lists of autocompleted values:*/
61 closeAllLists();
62 });
Huguet57cd3564b2018-07-23 04:40:40 +020063
64 // Set "data-edges" attribute and compare with others
65 var nEdges = Object.keys(s.graph.neighbors(node)).length;
66 b.dataset.edges = nEdges;
67 var inserted = false;
68
69 // Sort nodes by degree
70 for (i in a.childNodes) {
71 var child = a.childNodes[i];
72 if (!child.dataset) break;
73 if (nEdges > child.dataset.edges) {
74 a.insertBefore(b, child);
75 inserted = true;
76 break;
77 }
78 }
79
80 if (!inserted) {
81 a.appendChild(b);
82 console.log("inserted");
83 }
Huguet57004b1552018-07-23 03:29:12 +020084 }
Huguet57ae6d9742018-07-23 01:07:26 +020085 }
Huguet57ae6d9742018-07-23 01:07:26 +020086 });
87 /*execute a function presses a key on the keyboard:*/
88 inp.addEventListener("keydown", function(e) {
89 var x = document.getElementById(this.id + "autocomplete-list");
90 if (x) x = x.getElementsByTagName("div");
91 if (e.keyCode == 40) {
92 /*If the objow DOWN key is pressed,
93 increase the currentFocus variable:*/
94 currentFocus++;
95 /*and and make the current item more visible:*/
96 addActive(x);
97 } else if (e.keyCode == 38) { //up
98 /*If the objow UP key is pressed,
99 decrease the currentFocus variable:*/
100 currentFocus--;
101 /*and and make the current item more visible:*/
102 addActive(x);
103 } else if (e.keyCode == 13) {
104 /*If the ENTER key is pressed, prevent the form from being submitted,*/
105 e.preventDefault();
106 if (currentFocus > -1) {
107 /*and simulate a click on the "active" item:*/
108 if (x) x[currentFocus].click();
109 }
110 }
111 });
112 function addActive(x) {
113 /*a function to classify an item as "active":*/
114 if (!x) return false;
115 /*start by removing the "active" class on all items:*/
116 removeActive(x);
117 if (currentFocus >= x.length) currentFocus = 0;
118 if (currentFocus < 0) currentFocus = (x.length - 1);
119 /*add class "autocomplete-active":*/
120 x[currentFocus].classList.add("autocomplete-active");
121 }
122 function removeActive(x) {
123 /*a function to remove the "active" class from all autocomplete items:*/
124 for (var i = 0; i < x.length; i++) {
125 x[i].classList.remove("autocomplete-active");
126 }
127 }
128 function closeAllLists(elmnt) {
129 /*close all autocomplete lists in the document,
130 except the one passed as an argument:*/
131 var x = document.getElementsByClassName("autocomplete-items");
132 for (var i = 0; i < x.length; i++) {
133 if (elmnt != x[i] && elmnt != inp) {
134 x[i].parentNode.removeChild(x[i]);
135 }
136 }
137 }
138 /*execute a function when someone clicks in the document:*/
Huguet57004b1552018-07-23 03:29:12 +0200139 document.addEventListener("click", function (e) {
Huguet57ae6d9742018-07-23 01:07:26 +0200140 closeAllLists(e.target);
141 });
Huguet575b885692018-07-23 01:18:56 +0200142}