blob: c756789953c069710efa02e962407ad87c678fb1 [file] [log] [blame]
Andreudd6cfb42019-09-22 19:52:39 +02001function autocomplete(inp, obj, act) {
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 clearLists();
10 document.querySelector(".md-google-search__empty-btn").style.display = (val ? "block" : "none");
Adrià Vilanova Martínezdc7fbb62022-12-11 16:28:44 +010011 if (!val) val = "";
Andreudd6cfb42019-09-22 19:52:39 +020012 currentFocus = -1;
13 var is_empty = true;
14
15 /*for each item in the object...*/
16 for (node in obj) {
17 var nomNode = obj[node].nomcomplet;
Adrià Vilanova Martínezdc7fbb62022-12-11 16:28:44 +010018 var nomNodeNormalized = nomNode.normalize('NFD').replace(/\p{Diacritic}/gu, '').toUpperCase();
19 var valNormalized = val.normalize('NFD').replace(/\p{Diacritic}/gu, '').toUpperCase();
Andreudd6cfb42019-09-22 19:52:39 +020020
Adrià Vilanova Martínezdc7fbb62022-12-11 16:28:44 +010021 if (nomNodeNormalized.includes(valNormalized)) {
Andreudd6cfb42019-09-22 19:52:39 +020022 is_empty = false;
Adrià Vilanova Martínezdc7fbb62022-12-11 16:28:44 +010023 var parts = nomNodeNormalized.split(valNormalized);
Andreudd6cfb42019-09-22 19:52:39 +020024
25 /*create a DIV element for each matching element:*/
26 b = document.createElement("div");
27 b.setAttribute("class", "autocomplete-item");
28
29 /*make the matching letters bold:*/
30 if (parts[0].length == 0) b.innerHTML = "";
31 else b.innerHTML = "<span style='font-weight: bold; position:relative; z-index:120;'>" + nomNode.substr(0, parts[0].length) + "</span>";
32
33 b.innerHTML += nomNode.substr(parts[0].length, val.length);
34 b.innerHTML += "<span style='font-weight: bold; position:relative; z-index:120;'>" + nomNode.substr(parts[0].length + val.length) + "</span>";
Adrià Vilanova Martínez4861da62022-12-10 21:58:42 +010035 b.innerHTML += " <span class='autocomplete-year'>(" + obj[node].pista + ")</span>";
Andreudd6cfb42019-09-22 19:52:39 +020036
37 /*include node id to keep track of which is it*/
38 b.dataset.id = node;
39
40 /*execute a function when someone clicks on the item value (DIV element):*/
41 b.addEventListener("click", function(e) {
42 /*insert the value for the autocomplete text field:*/
43 var n = this.dataset.id;
44 inp.value = obj[n].nomcomplet;
45
46 switch (act) {
47 case "search":
48 // Insert hidden input id and show password field if applicable
49 document.getElementById("user").value = obj[n].id;
50 $('#password').prop('disabled', (obj[n].nopassword == "nopassword"));
51 break;
52 }
53
54 /*close the list of autocompleted values,
55 (or any other open lists of autocompleted values:*/
56 clearLists();
57 });
58
59 document.querySelector("#autocomplete-list").appendChild(b);
60 }
61 }
62
63 document.querySelector(".autocomplete-container").style.display = (is_empty ? "none" : "block");
64 });
65
66 document.querySelector(".md-google-search__empty-btn").addEventListener("click", function() {
67 document.querySelector("#search-input").value = "";
68 this.style.display = "none";
69 });
70
71 /*execute a function presses a key on the keyboard:*/
72 inp.addEventListener("keydown", function(e) {
73 var x = document.getElementById("autocomplete-list");
74 if (x) x = x.getElementsByTagName("div");
75 if (x.length == 0) return;
76 if (e.keyCode == 40) {
77 /*If the objow DOWN key is pressed,
78 increase the currentFocus variable:*/
79 currentFocus++;
80 /*and and make the current item more visible:*/
81 addActive(x);
82 } else if (e.keyCode == 38) { //up
83 /*If the objow UP key is pressed,
84 decrease the currentFocus variable:*/
85 currentFocus--;
86 /*and and make the current item more visible:*/
87 addActive(x);
88 } else if (e.keyCode == 13) {
89 /*If the ENTER key is pressed, prevent the form from being submitted,*/
90 e.preventDefault();
91 if (currentFocus > -1) {
92 /*and simulate a click on the "active" item:*/
93 if (x) x[currentFocus].click();
94 }
95 }
96 });
97 function addActive(x) {
98 /*a function to classify an item as "active":*/
99 if (!x) return false;
100 /*start by removing the "active" class on all items:*/
101 removeActive(x);
102 if (currentFocus >= x.length) currentFocus = 0;
103 if (currentFocus < 0) currentFocus = (x.length - 1);
104 /*add class "autocomplete-active":*/
105 x[currentFocus].classList.add("autocomplete-active");
106 }
107 function removeActive(x) {
108 /*a function to remove the "active" class from all autocomplete items:*/
109 for (var i = 0; i < x.length; i++) {
110 x[i].classList.remove("autocomplete-active");
111 }
112 }
113 function clearLists() {
114 /*close all autocomplete lists in the document,
115 except the one passed as an argument:*/
116 var x = document.querySelector("#autocomplete-list");
117 x.innerHTML = "";
118 document.querySelector(".autocomplete-container").style.display = "none";
119 }
120 /*execute a function when someone clicks in the document:*/
121 document.addEventListener("click", function (e) {
122 clearLists();
123 });
124}