Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 1 | function nomcurs(curs) { |
| 2 | if (curs == 1) return "1er"; |
| 3 | if (curs == 2) return "2on"; |
| 4 | if (curs == 3) return "3er"; |
| 5 | if (curs == 4) return "4rt"; |
| 6 | } |
| 7 | |
| 8 | function nomgrau(grau) { |
| 9 | if (grau == 0) return "MAT"; |
| 10 | if (grau == 1) return "EST"; |
| 11 | if (grau == 2) return "MAMME"; |
| 12 | } |
| 13 | |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 14 | function autocomplete(inp, obj, act) { |
| 15 | /*the autocomplete function takes two arguments, |
| 16 | the text field element and an objay of possible autocompleted values:*/ |
| 17 | var currentFocus; |
| 18 | /*execute a function when someone writes in the text field:*/ |
| 19 | inp.addEventListener("input", function(e) { |
| 20 | var a, b, i, val = this.value; |
| 21 | /*close any already open lists of autocompleted values*/ |
| 22 | clearLists(); |
| 23 | document.querySelector(".md-google-search__empty-btn").style.display = (val ? "block" : "none"); |
| 24 | if (!val || val.length < 3) return false; |
| 25 | currentFocus = -1; |
| 26 | var is_empty = true; |
| 27 | |
| 28 | /*for each item in the object...*/ |
| 29 | for (node in obj) { |
| 30 | var nomNode = obj[node].nomcomplet; |
| 31 | |
| 32 | if (nomNode.toUpperCase().includes(val.toUpperCase())) { |
| 33 | is_empty = false; |
| 34 | var parts = nomNode.toUpperCase().split(val.toUpperCase()); |
| 35 | |
| 36 | /*create a DIV element for each matching element:*/ |
| 37 | b = document.createElement("div"); |
| 38 | b.setAttribute("class", "autocomplete-item"); |
| 39 | |
| 40 | /*make the matching letters bold:*/ |
| 41 | if (parts[0].length == 0) b.innerHTML = ""; |
| 42 | else b.innerHTML = "<span style='font-weight: bold; position:relative; z-index:120;'>" + nomNode.substr(0, parts[0].length) + "</span>"; |
| 43 | |
| 44 | b.innerHTML += nomNode.substr(parts[0].length, val.length); |
| 45 | b.innerHTML += "<span style='font-weight: bold; position:relative; z-index:120;'>" + nomNode.substr(parts[0].length + val.length) + "</span>"; |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 46 | b.innerHTML += " <span class='autocomplete-year'>(" + nomcurs(obj[node].curs) + " - " + nomgrau(obj[node].grau) + ")</span>"; |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 47 | |
| 48 | /*include node id to keep track of which is it*/ |
| 49 | b.dataset.id = node; |
| 50 | |
| 51 | /*execute a function when someone clicks on the item value (DIV element):*/ |
| 52 | b.addEventListener("click", function(e) { |
| 53 | /*insert the value for the autocomplete text field:*/ |
| 54 | var n = this.dataset.id; |
| 55 | inp.value = obj[n].nomcomplet; |
| 56 | |
| 57 | switch (act) { |
| 58 | case "search": |
| 59 | // Insert hidden input id and show password field if applicable |
| 60 | document.getElementById("user").value = obj[n].id; |
| 61 | $('#password').prop('disabled', (obj[n].nopassword == "nopassword")); |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | /*close the list of autocompleted values, |
| 66 | (or any other open lists of autocompleted values:*/ |
| 67 | clearLists(); |
| 68 | }); |
| 69 | |
| 70 | document.querySelector("#autocomplete-list").appendChild(b); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | document.querySelector(".autocomplete-container").style.display = (is_empty ? "none" : "block"); |
| 75 | }); |
| 76 | |
| 77 | document.querySelector(".md-google-search__empty-btn").addEventListener("click", function() { |
| 78 | document.querySelector("#search-input").value = ""; |
| 79 | this.style.display = "none"; |
| 80 | }); |
| 81 | |
| 82 | /*execute a function presses a key on the keyboard:*/ |
| 83 | inp.addEventListener("keydown", function(e) { |
| 84 | var x = document.getElementById("autocomplete-list"); |
| 85 | if (x) x = x.getElementsByTagName("div"); |
| 86 | if (x.length == 0) return; |
| 87 | if (e.keyCode == 40) { |
| 88 | /*If the objow DOWN key is pressed, |
| 89 | increase the currentFocus variable:*/ |
| 90 | currentFocus++; |
| 91 | /*and and make the current item more visible:*/ |
| 92 | addActive(x); |
| 93 | } else if (e.keyCode == 38) { //up |
| 94 | /*If the objow UP key is pressed, |
| 95 | decrease the currentFocus variable:*/ |
| 96 | currentFocus--; |
| 97 | /*and and make the current item more visible:*/ |
| 98 | addActive(x); |
| 99 | } else if (e.keyCode == 13) { |
| 100 | /*If the ENTER key is pressed, prevent the form from being submitted,*/ |
| 101 | e.preventDefault(); |
| 102 | if (currentFocus > -1) { |
| 103 | /*and simulate a click on the "active" item:*/ |
| 104 | if (x) x[currentFocus].click(); |
| 105 | } |
| 106 | } |
| 107 | }); |
| 108 | function addActive(x) { |
| 109 | /*a function to classify an item as "active":*/ |
| 110 | if (!x) return false; |
| 111 | /*start by removing the "active" class on all items:*/ |
| 112 | removeActive(x); |
| 113 | if (currentFocus >= x.length) currentFocus = 0; |
| 114 | if (currentFocus < 0) currentFocus = (x.length - 1); |
| 115 | /*add class "autocomplete-active":*/ |
| 116 | x[currentFocus].classList.add("autocomplete-active"); |
| 117 | } |
| 118 | function removeActive(x) { |
| 119 | /*a function to remove the "active" class from all autocomplete items:*/ |
| 120 | for (var i = 0; i < x.length; i++) { |
| 121 | x[i].classList.remove("autocomplete-active"); |
| 122 | } |
| 123 | } |
| 124 | function clearLists() { |
| 125 | /*close all autocomplete lists in the document, |
| 126 | except the one passed as an argument:*/ |
| 127 | var x = document.querySelector("#autocomplete-list"); |
| 128 | x.innerHTML = ""; |
| 129 | document.querySelector(".autocomplete-container").style.display = "none"; |
| 130 | } |
| 131 | /*execute a function when someone clicks in the document:*/ |
| 132 | document.addEventListener("click", function (e) { |
| 133 | clearLists(); |
| 134 | }); |
| 135 | } |