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