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) {
|
| 7 | var a, b, i, val = this.value;
|
| 8 | /*close any already open lists of autocompleted values*/
|
| 9 | closeAllLists();
|
Huguet57 | 004b155 | 2018-07-23 03:29:12 +0200 | [diff] [blame] | 10 | if (!val || val.length < 3) return false;
|
Huguet57 | ae6d974 | 2018-07-23 01:07:26 +0200 | [diff] [blame] | 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;
|
Huguet57 | 004b155 | 2018-07-23 03:29:12 +0200 | [diff] [blame] | 23 |
|
| 24 | if (nomNode.toUpperCase().includes(val.toUpperCase())) {
|
| 25 | var parts = nomNode.toUpperCase().split(val.toUpperCase());
|
Huguet57 | 004b155 | 2018-07-23 03:29:12 +0200 | [diff] [blame] | 26 |
|
| 27 | /*create a DIV element for each matching element:*/
|
| 28 | b = document.createElement("DIV");
|
Huguet57 | cd3564b | 2018-07-23 04:40:40 +0200 | [diff] [blame] | 29 |
|
Huguet57 | 004b155 | 2018-07-23 03:29:12 +0200 | [diff] [blame] | 30 | /*make the matching letters bold:*/
|
| 31 | if (parts[0].length == 0) b.innerHTML = "";
|
| 32 | else b.innerHTML = nomNode.substr(0, parts[0].length);
|
| 33 | b.innerHTML += "<strong>" + nomNode.substr(parts[0].length, val.length) + "</strong>";
|
| 34 | b.innerHTML += nomNode.substr(parts[0].length + val.length);
|
| 35 | b.innerHTML += " - <span class='autocomplete-year'>" + obj[node].year + "</span>";
|
| 36 | /*insert a input field that will hold the current object item's value:*/
|
| 37 | b.innerHTML += "<input type='hidden' value='" + node + "'>";
|
| 38 |
|
| 39 | /*execute a function when someone clicks on the item value (DIV element):*/
|
| 40 | b.addEventListener("click", function(e) {
|
| 41 | /*insert the value for the autocomplete text field:*/
|
| 42 | var n = this.getElementsByTagName("input")[0].value;
|
| 43 | inp.value = obj[n].name;
|
| 44 |
|
Huguet57 | f02e8c0 | 2018-07-23 16:48:47 +0200 | [diff] [blame] | 45 | switch (act) {
|
| 46 | case "search":
|
| 47 | // Move camera to desired node
|
| 48 | cameraGoto(obj[n].x, obj[n].y);
|
Huguet57 | ac6d0c5 | 2018-07-23 18:37:16 +0200 | [diff] [blame^] | 49 | // Close the search box
|
| 50 | var searchBox = document.getElementById('searchBox');
|
| 51 | searchBox.style.display = "none";
|
| 52 |
|
| 53 | // Empty the input bar
|
| 54 | var searchInput = document.getElementById('searchInput');
|
| 55 | searchInput.value = "";
|
Huguet57 | f02e8c0 | 2018-07-23 16:48:47 +0200 | [diff] [blame] | 56 | break;
|
Huguet57 | ac6d0c5 | 2018-07-23 18:37:16 +0200 | [diff] [blame^] | 57 | case "addEdge":
|
Huguet57 | f02e8c0 | 2018-07-23 16:48:47 +0200 | [diff] [blame] | 58 | // Add an edge between A and B
|
Huguet57 | ac6d0c5 | 2018-07-23 18:37:16 +0200 | [diff] [blame^] | 59 | var sourceID = document.getElementById("node-id").innerText.substr(1);
|
| 60 | var edgeName = Math.min(sourceID, n) + "_" + Math.max(sourceID, n);
|
| 61 | if (!graf.edges[edgeName]) {
|
| 62 | graf.edges[edgeName] = {
|
| 63 | a: Math.min(sourceID, n),
|
| 64 | b: Math.max(sourceID, n),
|
| 65 | votes: 1
|
| 66 | };
|
| 67 |
|
| 68 | // Temporary fix, just for testing
|
| 69 | s.graph.addEdge({
|
| 70 | id: edgeName,
|
| 71 | source: sourceID,
|
| 72 | target: n,
|
| 73 | size: 0.5
|
| 74 | });
|
| 75 |
|
| 76 | clickNode(s, sourceID, s.graph.neighbors(sourceID));
|
| 77 | dialog.max();
|
| 78 |
|
| 79 | // Give a message of validation
|
| 80 | var edgeMSG = document.querySelector("#addedge-msg");
|
| 81 | addedEdgeMSG(edgeMSG);
|
| 82 |
|
| 83 | } else {
|
| 84 | alert("Edge already exists");
|
| 85 | }
|
| 86 |
|
| 87 | // Empty the input bar
|
| 88 | var addEdgeInput = document.getElementById('addedge-input');
|
| 89 | addEdgeInput.value = "";
|
| 90 |
|
| 91 | // Return to default view
|
| 92 | document.querySelector("#addedge-input").style.display = "none";
|
| 93 | document.querySelector("#edge-list").style.display = "block";
|
Huguet57 | f02e8c0 | 2018-07-23 16:48:47 +0200 | [diff] [blame] | 94 | break;
|
| 95 | }
|
Huguet57 | 004b155 | 2018-07-23 03:29:12 +0200 | [diff] [blame] | 96 |
|
| 97 | /*close the list of autocompleted values,
|
| 98 | (or any other open lists of autocompleted values:*/
|
| 99 | closeAllLists();
|
| 100 | });
|
Huguet57 | cd3564b | 2018-07-23 04:40:40 +0200 | [diff] [blame] | 101 |
|
| 102 | // Set "data-edges" attribute and compare with others
|
| 103 | var nEdges = Object.keys(s.graph.neighbors(node)).length;
|
| 104 | b.dataset.edges = nEdges;
|
| 105 | var inserted = false;
|
| 106 |
|
| 107 | // Sort nodes by degree
|
| 108 | for (i in a.childNodes) {
|
| 109 | var child = a.childNodes[i];
|
| 110 | if (!child.dataset) break;
|
| 111 | if (nEdges > child.dataset.edges) {
|
| 112 | a.insertBefore(b, child);
|
| 113 | inserted = true;
|
| 114 | break;
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | if (!inserted) {
|
| 119 | a.appendChild(b);
|
| 120 | console.log("inserted");
|
| 121 | }
|
Huguet57 | 004b155 | 2018-07-23 03:29:12 +0200 | [diff] [blame] | 122 | }
|
Huguet57 | ae6d974 | 2018-07-23 01:07:26 +0200 | [diff] [blame] | 123 | }
|
Huguet57 | ae6d974 | 2018-07-23 01:07:26 +0200 | [diff] [blame] | 124 | });
|
| 125 | /*execute a function presses a key on the keyboard:*/
|
| 126 | inp.addEventListener("keydown", function(e) {
|
| 127 | var x = document.getElementById(this.id + "autocomplete-list");
|
| 128 | if (x) x = x.getElementsByTagName("div");
|
| 129 | if (e.keyCode == 40) {
|
| 130 | /*If the objow DOWN key is pressed,
|
| 131 | increase the currentFocus variable:*/
|
| 132 | currentFocus++;
|
| 133 | /*and and make the current item more visible:*/
|
| 134 | addActive(x);
|
| 135 | } else if (e.keyCode == 38) { //up
|
| 136 | /*If the objow UP key is pressed,
|
| 137 | decrease the currentFocus variable:*/
|
| 138 | currentFocus--;
|
| 139 | /*and and make the current item more visible:*/
|
| 140 | addActive(x);
|
| 141 | } else if (e.keyCode == 13) {
|
| 142 | /*If the ENTER key is pressed, prevent the form from being submitted,*/
|
| 143 | e.preventDefault();
|
| 144 | if (currentFocus > -1) {
|
| 145 | /*and simulate a click on the "active" item:*/
|
| 146 | if (x) x[currentFocus].click();
|
| 147 | }
|
| 148 | }
|
| 149 | });
|
| 150 | function addActive(x) {
|
| 151 | /*a function to classify an item as "active":*/
|
| 152 | if (!x) return false;
|
| 153 | /*start by removing the "active" class on all items:*/
|
| 154 | removeActive(x);
|
| 155 | if (currentFocus >= x.length) currentFocus = 0;
|
| 156 | if (currentFocus < 0) currentFocus = (x.length - 1);
|
| 157 | /*add class "autocomplete-active":*/
|
| 158 | x[currentFocus].classList.add("autocomplete-active");
|
| 159 | }
|
| 160 | function removeActive(x) {
|
| 161 | /*a function to remove the "active" class from all autocomplete items:*/
|
| 162 | for (var i = 0; i < x.length; i++) {
|
| 163 | x[i].classList.remove("autocomplete-active");
|
| 164 | }
|
| 165 | }
|
| 166 | function closeAllLists(elmnt) {
|
| 167 | /*close all autocomplete lists in the document,
|
| 168 | except the one passed as an argument:*/
|
| 169 | var x = document.getElementsByClassName("autocomplete-items");
|
| 170 | for (var i = 0; i < x.length; i++) {
|
| 171 | if (elmnt != x[i] && elmnt != inp) {
|
| 172 | x[i].parentNode.removeChild(x[i]);
|
| 173 | }
|
| 174 | }
|
| 175 | }
|
| 176 | /*execute a function when someone clicks in the document:*/
|
Huguet57 | 004b155 | 2018-07-23 03:29:12 +0200 | [diff] [blame] | 177 | document.addEventListener("click", function (e) {
|
Huguet57 | ae6d974 | 2018-07-23 01:07:26 +0200 | [diff] [blame] | 178 | closeAllLists(e.target);
|
| 179 | });
|
Huguet57 | 5b88569 | 2018-07-23 01:18:56 +0200 | [diff] [blame] | 180 | }
|
Huguet57 | ac6d0c5 | 2018-07-23 18:37:16 +0200 | [diff] [blame^] | 181 |
|
| 182 | function addedEdgeMSG(edgeMSG) {
|
| 183 | var opacity = 8;
|
| 184 | edgeMSG.style.display = "block";
|
| 185 |
|
| 186 | var opacityChange = window.setInterval(function() {
|
| 187 | edgeMSG.style.color = "rgba(0,0,0, "+ opacity/10 +")";
|
| 188 | var edgeList = document.querySelector("#edge-list ul");
|
| 189 | edgeList.style.backgroundColor = "rgba(0,200,0, "+ opacity/10 +")";
|
| 190 | --opacity;
|
| 191 | console.log(opacity);
|
| 192 | }, 100);
|
| 193 |
|
| 194 | var backToDef = window.setTimeout(function() {
|
| 195 | edgeMSG.style.display = "none";
|
| 196 | edgeMSG.style.color = "black";
|
| 197 | window.clearInterval(opacityChange);
|
| 198 | }, 1000);
|
| 199 | } |