blob: 2239a50790c45239299c700e144784a9c5079fb7 [file] [log] [blame]
avm99963a00c43e2018-09-26 20:16:07 +02001var s, graf;
2
avm99963f0d15212017-10-08 17:03:22 +02003function xhr(method, url, params, callback) {
4 var http = new XMLHttpRequest();
5 if (method == "POST") {
6 http.open(method, url, true);
7 } else {
8 if (params != "") {
9 http.open(method, url+"?"+params, true);
10 } else {
11 http.open(method, url, true);
12 }
13 }
14 http.onload = function() {
15 if(this.status != 200) {
16 console.warn("Attention, status code "+this.status+" when loading via xhr url "+url);
17 }
18 callback(this.responseText, this.status);
19 };
20 if (method == "POST") {
21 http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
22 http.send(params);
23 } else {
24 http.send();
25 }
26}
27
avm99963a00c43e2018-09-26 20:16:07 +020028function altSearchBar() {
29 if (document.querySelector(".md-google-search__metacontainer").style.display == "none") {
30 document.querySelector(".md-google-search__metacontainer").style.display = "block";
31 } else {
32 document.querySelector(".md-google-search__metacontainer").style.display = "none";
33 document.querySelector(".autocomplete-container").style.display = "none";
34 }
35}
36
37var seq = [38, 38, 40, 40, 37, 39, 37, 39, 65, 66, 13];
38var cur = 0;
39
40function justdoit() {
41 s.graph.nodes().forEach(function(n) {
42 switch(n.color) {
43 case "#d61c08":
44 n.color = "#0159aa";
45 break;
46
47 case "#0159aa":
48 n.color = "#0ca80a";
49 break;
50
51 case "#0ca80a":
52 n.color = "#d61c08";
53 break;
54 }
55 });
56
57 s.refresh();
58 setTimeout(justdoit, 333);
59}
60
avm99963f0d15212017-10-08 17:03:22 +020061var dialog = {
62 fill: function(data, text, html=false) {
63 var el = document.querySelectorAll("*[data-fill=\""+data+"\"]");
64 for (var i in el) {
65 if (html === true) {
66 el[i].innerHTML = text;
67 } else {
68 el[i].innerText = text;
69 }
70 }
71 },
72 show: function(id, neighbors) {
73 var neighbors = Object.values(neighbors);
74
75 this.fill("name", graf.nodes[id].name);
76 this.fill("year", graf.nodes[id].year);
77 this.fill("sex", graf.nodes[id].sex);
78 this.fill("id", "#"+id);
79 this.fill("n-edges", neighbors.length);
80
81 var list = "";
82 neighbors.forEach(function (a) {
83 list += "<li><b>"+graf.nodes[id].name+" - "+a.label+":</b> "+(graf.edges[id+"_"+a.id] ? graf.edges[id+"_"+a.id].votes : graf.edges[a.id+"_"+id].votes)+" vots</li>";
84 });
85 this.fill("edges", list, true);
86
87 if (window.innerWidth > 700) {
88 document.querySelector("#dialog").style.display = "block";
89 document.querySelector("#backdrop-container").style.display = "block";
90 } else {
91 document.querySelector("#summary-dialog").style.display = "block";
92 }
93 },
94 close: function() {
95 document.querySelector("#dialog").style.display = "none";
96 document.querySelector("#summary-dialog").style.display = "none";
97 document.querySelector("#backdrop-container").style.display = "none";
98
99 s.graph.nodes().forEach(function(n) {
100 n.color = n.originalColor;
101 });
102
103 s.graph.edges().forEach(function(e) {
104 e.color = "#fff";
105 });
106
107 s.refresh();
108 },
109 max: function() {
110 document.querySelector("#summary-dialog").style.display = "none";
111 document.querySelector("#dialog").style.display = "block";
112 },
113 min: function() {
114 document.querySelector("#dialog").style.display = "none";
115 document.querySelector("#summary-dialog").style.display = "block";
116 }
117};
118
119function init() {
avm99963a00c43e2018-09-26 20:16:07 +0200120 sigma.classes.graph.addMethod("neighbors", function(nodeId) {
121 var k,
122 neighbors = {},
123 index = this.allNeighborsIndex[nodeId] || [];
avm99963f0d15212017-10-08 17:03:22 +0200124
avm99963a00c43e2018-09-26 20:16:07 +0200125 for (k in index) {
126 neighbors[k] = this.nodesIndex[k];
127 }
avm99963f0d15212017-10-08 17:03:22 +0200128
avm99963a00c43e2018-09-26 20:16:07 +0200129 return neighbors;
130 });
avm99963f0d15212017-10-08 17:03:22 +0200131
avm99963a00c43e2018-09-26 20:16:07 +0200132 s = new sigma({
133 renderers: [{
134 container: "graf",
135 type: "webgl"
136 }],
137 settings: {
138 defaultEdgeColor: "#fff",
139 edgeColor: "default",
140 defaultLabelColor: "#fff",
141 autoRescale: false,
142 zoomMax: 10,
143 //enableEdgeHovering: true,
144 font: "Roboto",
145 labelThreshold: 5
146 }
147 });
avm99963f0d15212017-10-08 17:03:22 +0200148
avm99963a00c43e2018-09-26 20:16:07 +0200149 xhr("GET", "api.php", "action=getgraf", function(responseText, status) {
150 graf = JSON.parse(responseText);
avm99963f0d15212017-10-08 17:03:22 +0200151
avm99963a00c43e2018-09-26 20:16:07 +0200152 console.log(graf);
avm99963f0d15212017-10-08 17:03:22 +0200153
avm99963a00c43e2018-09-26 20:16:07 +0200154 for (var i in graf.nodes) {
155 var ncolor = (graf.nodes[i].sex == "F" ? "#d61c08" : (graf.nodes[i].sex == "M" ? "#0159aa" : "#0ca80a"));
avm99963f0d15212017-10-08 17:03:22 +0200156
avm99963a00c43e2018-09-26 20:16:07 +0200157 s.graph.addNode({
158 id: graf.nodes[i].id,
159 label: graf.nodes[i].name,
160 x: graf.nodes[i].x,
161 y: graf.nodes[i].y,
162 size: 10,
163 color: ncolor,
164 originalColor: ncolor
165 });
166 }
avm99963f0d15212017-10-08 17:03:22 +0200167
avm99963a00c43e2018-09-26 20:16:07 +0200168 for (var i in graf.edges) {
169 s.graph.addEdge({
170 id: i,
171 source: graf.edges[i].a,
172 target: graf.edges[i].b,
173 size: Math.min(4, Math.max((7/(2*Math.pow(20, 2)))*Math.pow(graf.edges[i].votes, 2) + 1/2, 0.5))
174 });
175 }
avm99963f0d15212017-10-08 17:03:22 +0200176
avm99963a00c43e2018-09-26 20:16:07 +0200177 s.bind('clickNode', function(e) {
178 var nodeId = e.data.node.id,
179 toKeep = s.graph.neighbors(nodeId);
180 //toKeep[nodeId] = e.data.node;
avm999632d35c5a2018-09-26 17:05:20 +0200181
avm99963a00c43e2018-09-26 20:16:07 +0200182 s.graph.nodes().forEach(function(n) {
183 if (toKeep[n.id] || n.id == nodeId) {
184 n.color = n.originalColor;
185 } else {
186 n.color = '#333';
187 }
avm99963f0d15212017-10-08 17:03:22 +0200188 });
189
avm99963a00c43e2018-09-26 20:16:07 +0200190 s.graph.edges().forEach(function(e) {
191 if ((e.source == nodeId || e.target == nodeId) && (toKeep[e.source] || toKeep[e.target])) {
192 e.color = '#fff';
193 } else {
194 e.color = '#333';
195 }
avm99963f0d15212017-10-08 17:03:22 +0200196 });
197
198 s.refresh();
avm99963a00c43e2018-09-26 20:16:07 +0200199
200 dialog.show(nodeId, toKeep);
avm99963f0d15212017-10-08 17:03:22 +0200201 });
avm99963a00c43e2018-09-26 20:16:07 +0200202
203 document.querySelector("#quit-dialog").addEventListener("click", dialog.close);
204 document.querySelector("#quit2-dialog").addEventListener("click", dialog.close);
205 document.querySelector("#max-dialog").addEventListener("click", dialog.max);
206 document.querySelector("#min-dialog").addEventListener("click", dialog.min);
207
208 document.querySelector("#zoomin").addEventListener("click", function() {
209 s.camera.goTo({
210 ratio: Math.max(s.camera.settings("zoomMin"), s.camera.ratio / Math.sqrt(2))
211 });
212 });
213
214 document.querySelector("#zoomout").addEventListener("click", function() {
215 s.camera.goTo({
216 ratio: Math.min(s.camera.settings("zoomMax"), s.camera.ratio * Math.sqrt(2))
217 });
218 });
219
220 document.addEventListener("keydown", function() {
221 if (event.key == "f" && event.target.getAttribute("id") != "search-input") altSearchBar();
222 if (event.which == seq[cur]) {
223 if (cur < seq.length) {
224 ++cur;
225 if (cur == seq.length) {
226 justdoit();
227 }
228 }
229 } else cur = 0;
230 })
231 document.querySelector("#search").addEventListener("click", altSearchBar);
232
233 if (window.innerWidth > 700) altSearchBar();
234
235 s.refresh();
236 autocomplete(document.querySelector("#search-input"), graf.nodes, "search");
237 });
avm99963f0d15212017-10-08 17:03:22 +0200238}
239
Huguet57004b1552018-07-23 03:29:12 +0200240function cameraGoto(nodeX, nodeY) {
241 sigma.misc.animation.camera( s.camera,
242 { x: nodeX, y: nodeY, ratio: 1 },
243 { duration: s.settings('animationsTime') || 300 }
244 );
245}
246
avm99963f0d15212017-10-08 17:03:22 +0200247window.addEventListener("load", init);