Huguet57 | f02e8c0 | 2018-07-23 16:48:47 +0200 | [diff] [blame] | 1 | function autocomplete(inp, obj, act) {
|
Huguet57 | ae6d974 | 2018-07-23 01:07:26 +0200 | [diff] [blame] | 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) {
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 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");
|
| 11 | if (!val || val.length < 3) return false;
|
| 12 | 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].name;
|
| 18 |
|
| 19 | if (nomNode.toUpperCase().includes(val.toUpperCase())) {
|
| 20 | is_empty = false;
|
| 21 | var parts = nomNode.toUpperCase().split(val.toUpperCase());
|
| 22 |
|
Huguet57 | 004b155 | 2018-07-23 03:29:12 +0200 | [diff] [blame] | 23 | /*create a DIV element for each matching element:*/
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 24 | b = document.createElement("div");
|
| 25 | b.setAttribute("class", "autocomplete-item");
|
| 26 |
|
Huguet57 | 004b155 | 2018-07-23 03:29:12 +0200 | [diff] [blame] | 27 | /*make the matching letters bold:*/
|
| 28 | if (parts[0].length == 0) b.innerHTML = "";
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 29 | else b.innerHTML = "<span style='font-weight: bold;'>" + nomNode.substr(0, parts[0].length) + "</span>";
|
| 30 | b.innerHTML += nomNode.substr(parts[0].length, val.length);
|
| 31 | b.innerHTML += "<span style='font-weight: bold;'>" + nomNode.substr(parts[0].length + val.length) + "</span>";
|
| 32 | b.innerHTML += " <span class='autocomplete-year'>(" + obj[node].year + ")</span>";
|
| 33 |
|
| 34 | /*include node id to keep track of which is it*/
|
| 35 | b.dataset.id = node;
|
| 36 |
|
| 37 | /*execute a function when someone clicks on the item value (DIV element):*/
|
Huguet57 | 004b155 | 2018-07-23 03:29:12 +0200 | [diff] [blame] | 38 | b.addEventListener("click", function(e) {
|
| 39 | /*insert the value for the autocomplete text field:*/
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 40 | var n = this.dataset.id;
|
Huguet57 | 004b155 | 2018-07-23 03:29:12 +0200 | [diff] [blame] | 41 | inp.value = obj[n].name;
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 42 |
|
Huguet57 | f02e8c0 | 2018-07-23 16:48:47 +0200 | [diff] [blame] | 43 | switch (act) {
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 44 | case "search":
|
| 45 | // Move camera to desired node
|
| 46 | cameraGoto(obj[n].x, obj[n].y);
|
| 47 | break;
|
| 48 | case "addEdge":
|
| 49 | // @TODO: Add an edge between A and B
|
| 50 | alert(obj[n].name);
|
| 51 | break;
|
Huguet57 | f02e8c0 | 2018-07-23 16:48:47 +0200 | [diff] [blame] | 52 | }
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 53 |
|
Huguet57 | 004b155 | 2018-07-23 03:29:12 +0200 | [diff] [blame] | 54 | /*close the list of autocompleted values,
|
| 55 | (or any other open lists of autocompleted values:*/
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 56 | clearLists();
|
Huguet57 | 004b155 | 2018-07-23 03:29:12 +0200 | [diff] [blame] | 57 | });
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 58 |
|
Huguet57 | cd3564b | 2018-07-23 04:40:40 +0200 | [diff] [blame] | 59 | // Set "data-edges" attribute and compare with others
|
| 60 | var nEdges = Object.keys(s.graph.neighbors(node)).length;
|
| 61 | b.dataset.edges = nEdges;
|
| 62 | var inserted = false;
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 63 |
|
Huguet57 | cd3564b | 2018-07-23 04:40:40 +0200 | [diff] [blame] | 64 | // Sort nodes by degree
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 65 | for (i in document.querySelector("#autocomplete-list").childNodes) {
|
| 66 | var child = document.querySelector("#autocomplete-list").childNodes[i];
|
Huguet57 | cd3564b | 2018-07-23 04:40:40 +0200 | [diff] [blame] | 67 | if (!child.dataset) break;
|
| 68 | if (nEdges > child.dataset.edges) {
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 69 | document.querySelector("#autocomplete-list").insertBefore(b, child);
|
Huguet57 | cd3564b | 2018-07-23 04:40:40 +0200 | [diff] [blame] | 70 | inserted = true;
|
| 71 | break;
|
| 72 | }
|
| 73 | }
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 74 |
|
Huguet57 | cd3564b | 2018-07-23 04:40:40 +0200 | [diff] [blame] | 75 | if (!inserted) {
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 76 | document.querySelector("#autocomplete-list").appendChild(b);
|
Huguet57 | cd3564b | 2018-07-23 04:40:40 +0200 | [diff] [blame] | 77 | }
|
Huguet57 | 004b155 | 2018-07-23 03:29:12 +0200 | [diff] [blame] | 78 | }
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 79 | }
|
| 80 |
|
| 81 | document.querySelector(".autocomplete-container").style.display = (is_empty ? "none" : "block");
|
Huguet57 | ae6d974 | 2018-07-23 01:07:26 +0200 | [diff] [blame] | 82 | });
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 83 |
|
| 84 | document.querySelector(".md-google-search__empty-btn").addEventListener("click", function() {
|
| 85 | document.querySelector("#search-input").value = "";
|
| 86 | this.style.display = "none";
|
| 87 | });
|
| 88 |
|
Huguet57 | ae6d974 | 2018-07-23 01:07:26 +0200 | [diff] [blame] | 89 | /*execute a function presses a key on the keyboard:*/
|
| 90 | inp.addEventListener("keydown", function(e) {
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 91 | var x = document.getElementById("autocomplete-list");
|
Huguet57 | ae6d974 | 2018-07-23 01:07:26 +0200 | [diff] [blame] | 92 | if (x) x = x.getElementsByTagName("div");
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 93 | if (x.length == 0) return;
|
Huguet57 | ae6d974 | 2018-07-23 01:07:26 +0200 | [diff] [blame] | 94 | if (e.keyCode == 40) {
|
| 95 | /*If the objow DOWN key is pressed,
|
| 96 | increase the currentFocus variable:*/
|
| 97 | currentFocus++;
|
| 98 | /*and and make the current item more visible:*/
|
| 99 | addActive(x);
|
| 100 | } else if (e.keyCode == 38) { //up
|
| 101 | /*If the objow UP key is pressed,
|
| 102 | decrease the currentFocus variable:*/
|
| 103 | currentFocus--;
|
| 104 | /*and and make the current item more visible:*/
|
| 105 | addActive(x);
|
| 106 | } else if (e.keyCode == 13) {
|
| 107 | /*If the ENTER key is pressed, prevent the form from being submitted,*/
|
| 108 | e.preventDefault();
|
| 109 | if (currentFocus > -1) {
|
| 110 | /*and simulate a click on the "active" item:*/
|
| 111 | if (x) x[currentFocus].click();
|
| 112 | }
|
| 113 | }
|
| 114 | });
|
| 115 | function addActive(x) {
|
| 116 | /*a function to classify an item as "active":*/
|
| 117 | if (!x) return false;
|
| 118 | /*start by removing the "active" class on all items:*/
|
| 119 | removeActive(x);
|
| 120 | if (currentFocus >= x.length) currentFocus = 0;
|
| 121 | if (currentFocus < 0) currentFocus = (x.length - 1);
|
| 122 | /*add class "autocomplete-active":*/
|
| 123 | x[currentFocus].classList.add("autocomplete-active");
|
| 124 | }
|
| 125 | function removeActive(x) {
|
| 126 | /*a function to remove the "active" class from all autocomplete items:*/
|
| 127 | for (var i = 0; i < x.length; i++) {
|
| 128 | x[i].classList.remove("autocomplete-active");
|
| 129 | }
|
| 130 | }
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 131 | function clearLists() {
|
Huguet57 | ae6d974 | 2018-07-23 01:07:26 +0200 | [diff] [blame] | 132 | /*close all autocomplete lists in the document,
|
| 133 | except the one passed as an argument:*/
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 134 | var x = document.querySelector("#autocomplete-list");
|
| 135 | x.innerHTML = "";
|
| 136 | document.querySelector(".autocomplete-container").style.display = "none";
|
Huguet57 | ae6d974 | 2018-07-23 01:07:26 +0200 | [diff] [blame] | 137 | }
|
| 138 | /*execute a function when someone clicks in the document:*/
|
avm99963 | a00c43e | 2018-09-26 20:16:07 +0200 | [diff] [blame] | 139 | document.addEventListener("click", function (e) {
|
| 140 | clearLists();
|
Huguet57 | ae6d974 | 2018-07-23 01:07:26 +0200 | [diff] [blame] | 141 | });
|
Huguet57 | 5b88569 | 2018-07-23 01:18:56 +0200 | [diff] [blame] | 142 | }
|