Huguet57 | ae6d974 | 2018-07-23 01:07:26 +0200 | [diff] [blame] | 1 | function 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 | );
|
Huguet57 | 5b88569 | 2018-07-23 01:18:56 +0200 | [diff] [blame] | 44 |
|
| 45 | // Close the search box
|
| 46 | var searchBox = document.getElementById('searchBox');
|
| 47 | searchBox.style.display = "none";
|
Huguet57 | ae6d974 | 2018-07-23 01:07:26 +0200 | [diff] [blame] | 48 |
|
| 49 | /*close the list of autocompleted values,
|
| 50 | (or any other open lists of autocompleted values:*/
|
| 51 | closeAllLists();
|
| 52 | });
|
| 53 | a.appendChild(b);
|
| 54 | }
|
| 55 | }
|
| 56 | });
|
| 57 | /*execute a function presses a key on the keyboard:*/
|
| 58 | inp.addEventListener("keydown", function(e) {
|
| 59 | var x = document.getElementById(this.id + "autocomplete-list");
|
| 60 | if (x) x = x.getElementsByTagName("div");
|
| 61 | if (e.keyCode == 40) {
|
| 62 | /*If the objow DOWN key is pressed,
|
| 63 | increase the currentFocus variable:*/
|
| 64 | currentFocus++;
|
| 65 | /*and and make the current item more visible:*/
|
| 66 | addActive(x);
|
| 67 | } else if (e.keyCode == 38) { //up
|
| 68 | /*If the objow UP key is pressed,
|
| 69 | decrease the currentFocus variable:*/
|
| 70 | currentFocus--;
|
| 71 | /*and and make the current item more visible:*/
|
| 72 | addActive(x);
|
| 73 | } else if (e.keyCode == 13) {
|
| 74 | /*If the ENTER key is pressed, prevent the form from being submitted,*/
|
| 75 | e.preventDefault();
|
| 76 | if (currentFocus > -1) {
|
| 77 | /*and simulate a click on the "active" item:*/
|
| 78 | if (x) x[currentFocus].click();
|
| 79 | }
|
| 80 | }
|
| 81 | });
|
| 82 | function addActive(x) {
|
| 83 | /*a function to classify an item as "active":*/
|
| 84 | if (!x) return false;
|
| 85 | /*start by removing the "active" class on all items:*/
|
| 86 | removeActive(x);
|
| 87 | if (currentFocus >= x.length) currentFocus = 0;
|
| 88 | if (currentFocus < 0) currentFocus = (x.length - 1);
|
| 89 | /*add class "autocomplete-active":*/
|
| 90 | x[currentFocus].classList.add("autocomplete-active");
|
| 91 | }
|
| 92 | function removeActive(x) {
|
| 93 | /*a function to remove the "active" class from all autocomplete items:*/
|
| 94 | for (var i = 0; i < x.length; i++) {
|
| 95 | x[i].classList.remove("autocomplete-active");
|
| 96 | }
|
| 97 | }
|
| 98 | function closeAllLists(elmnt) {
|
| 99 | /*close all autocomplete lists in the document,
|
| 100 | except the one passed as an argument:*/
|
| 101 | var x = document.getElementsByClassName("autocomplete-items");
|
| 102 | for (var i = 0; i < x.length; i++) {
|
| 103 | if (elmnt != x[i] && elmnt != inp) {
|
| 104 | x[i].parentNode.removeChild(x[i]);
|
| 105 | }
|
| 106 | }
|
| 107 | }
|
| 108 | /*execute a function when someone clicks in the document:*/
|
| 109 | document.addEventListener("click", function (e) {
|
| 110 | closeAllLists(e.target);
|
| 111 | });
|
Huguet57 | 5b88569 | 2018-07-23 01:18:56 +0200 | [diff] [blame] | 112 | }
|