blob: 1b4a36fd2e7c30bfffb2f2107210eb1f2ad79242 [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";
avm99963596c6aa2018-09-26 21:06:48 +020031 document.querySelector("#search i").innerText = "fullscreen";
avm99963a00c43e2018-09-26 20:16:07 +020032 } else {
33 document.querySelector(".md-google-search__metacontainer").style.display = "none";
34 document.querySelector(".autocomplete-container").style.display = "none";
avm99963596c6aa2018-09-26 21:06:48 +020035 document.querySelector("#search i").innerText = "search";
avm99963a00c43e2018-09-26 20:16:07 +020036 }
37}
38
39var seq = [38, 38, 40, 40, 37, 39, 37, 39, 65, 66, 13];
40var cur = 0;
41
42function justdoit() {
43 s.graph.nodes().forEach(function(n) {
44 switch(n.color) {
45 case "#d61c08":
46 n.color = "#0159aa";
47 break;
48
49 case "#0159aa":
50 n.color = "#0ca80a";
51 break;
52
53 case "#0ca80a":
54 n.color = "#d61c08";
55 break;
56 }
57 });
58
59 s.refresh();
60 setTimeout(justdoit, 333);
61}
62
avm99963f0d15212017-10-08 17:03:22 +020063var dialog = {
64 fill: function(data, text, html=false) {
65 var el = document.querySelectorAll("*[data-fill=\""+data+"\"]");
66 for (var i in el) {
67 if (html === true) {
68 el[i].innerHTML = text;
69 } else {
70 el[i].innerText = text;
71 }
72 }
73 },
74 show: function(id, neighbors) {
75 var neighbors = Object.values(neighbors);
76
77 this.fill("name", graf.nodes[id].name);
78 this.fill("year", graf.nodes[id].year);
79 this.fill("sex", graf.nodes[id].sex);
80 this.fill("id", "#"+id);
81 this.fill("n-edges", neighbors.length);
82
83 var list = "";
84 neighbors.forEach(function (a) {
85 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>";
86 });
87 this.fill("edges", list, true);
88
89 if (window.innerWidth > 700) {
90 document.querySelector("#dialog").style.display = "block";
91 document.querySelector("#backdrop-container").style.display = "block";
92 } else {
93 document.querySelector("#summary-dialog").style.display = "block";
94 }
95 },
96 close: function() {
97 document.querySelector("#dialog").style.display = "none";
98 document.querySelector("#summary-dialog").style.display = "none";
99 document.querySelector("#backdrop-container").style.display = "none";
100
101 s.graph.nodes().forEach(function(n) {
102 n.color = n.originalColor;
103 });
104
105 s.graph.edges().forEach(function(e) {
106 e.color = "#fff";
107 });
108
109 s.refresh();
110 },
111 max: function() {
112 document.querySelector("#summary-dialog").style.display = "none";
113 document.querySelector("#dialog").style.display = "block";
114 },
115 min: function() {
116 document.querySelector("#dialog").style.display = "none";
117 document.querySelector("#summary-dialog").style.display = "block";
118 }
119};
120
121function init() {
avm99963a00c43e2018-09-26 20:16:07 +0200122 sigma.classes.graph.addMethod("neighbors", function(nodeId) {
123 var k,
124 neighbors = {},
125 index = this.allNeighborsIndex[nodeId] || [];
avm99963f0d15212017-10-08 17:03:22 +0200126
avm99963a00c43e2018-09-26 20:16:07 +0200127 for (k in index) {
128 neighbors[k] = this.nodesIndex[k];
129 }
avm99963f0d15212017-10-08 17:03:22 +0200130
avm99963a00c43e2018-09-26 20:16:07 +0200131 return neighbors;
132 });
avm99963f0d15212017-10-08 17:03:22 +0200133
avm99963a00c43e2018-09-26 20:16:07 +0200134 s = new sigma({
135 renderers: [{
136 container: "graf",
137 type: "webgl"
138 }],
139 settings: {
140 defaultEdgeColor: "#fff",
141 edgeColor: "default",
142 defaultLabelColor: "#fff",
143 autoRescale: false,
144 zoomMax: 10,
145 //enableEdgeHovering: true,
146 font: "Roboto",
147 labelThreshold: 5
148 }
149 });
avm99963f0d15212017-10-08 17:03:22 +0200150
avm99963a00c43e2018-09-26 20:16:07 +0200151 xhr("GET", "api.php", "action=getgraf", function(responseText, status) {
152 graf = JSON.parse(responseText);
avm99963f0d15212017-10-08 17:03:22 +0200153
avm99963a00c43e2018-09-26 20:16:07 +0200154 console.log(graf);
avm99963f0d15212017-10-08 17:03:22 +0200155
avm99963a00c43e2018-09-26 20:16:07 +0200156 for (var i in graf.nodes) {
157 var ncolor = (graf.nodes[i].sex == "F" ? "#d61c08" : (graf.nodes[i].sex == "M" ? "#0159aa" : "#0ca80a"));
avm99963f0d15212017-10-08 17:03:22 +0200158
avm99963a00c43e2018-09-26 20:16:07 +0200159 s.graph.addNode({
160 id: graf.nodes[i].id,
161 label: graf.nodes[i].name,
162 x: graf.nodes[i].x,
163 y: graf.nodes[i].y,
164 size: 10,
165 color: ncolor,
166 originalColor: ncolor
167 });
168 }
avm99963f0d15212017-10-08 17:03:22 +0200169
avm99963a00c43e2018-09-26 20:16:07 +0200170 for (var i in graf.edges) {
171 s.graph.addEdge({
172 id: i,
173 source: graf.edges[i].a,
174 target: graf.edges[i].b,
175 size: Math.min(4, Math.max((7/(2*Math.pow(20, 2)))*Math.pow(graf.edges[i].votes, 2) + 1/2, 0.5))
176 });
177 }
avm99963f0d15212017-10-08 17:03:22 +0200178
avm99963a00c43e2018-09-26 20:16:07 +0200179 s.bind('clickNode', function(e) {
180 var nodeId = e.data.node.id,
181 toKeep = s.graph.neighbors(nodeId);
182 //toKeep[nodeId] = e.data.node;
avm999632d35c5a2018-09-26 17:05:20 +0200183
avm99963a00c43e2018-09-26 20:16:07 +0200184 s.graph.nodes().forEach(function(n) {
185 if (toKeep[n.id] || n.id == nodeId) {
186 n.color = n.originalColor;
187 } else {
188 n.color = '#333';
189 }
avm99963f0d15212017-10-08 17:03:22 +0200190 });
191
avm99963a00c43e2018-09-26 20:16:07 +0200192 s.graph.edges().forEach(function(e) {
193 if ((e.source == nodeId || e.target == nodeId) && (toKeep[e.source] || toKeep[e.target])) {
194 e.color = '#fff';
195 } else {
196 e.color = '#333';
197 }
avm99963f0d15212017-10-08 17:03:22 +0200198 });
199
200 s.refresh();
avm99963a00c43e2018-09-26 20:16:07 +0200201
202 dialog.show(nodeId, toKeep);
avm99963f0d15212017-10-08 17:03:22 +0200203 });
avm99963a00c43e2018-09-26 20:16:07 +0200204
205 document.querySelector("#quit-dialog").addEventListener("click", dialog.close);
206 document.querySelector("#quit2-dialog").addEventListener("click", dialog.close);
207 document.querySelector("#max-dialog").addEventListener("click", dialog.max);
208 document.querySelector("#min-dialog").addEventListener("click", dialog.min);
209
210 document.querySelector("#zoomin").addEventListener("click", function() {
211 s.camera.goTo({
212 ratio: Math.max(s.camera.settings("zoomMin"), s.camera.ratio / Math.sqrt(2))
213 });
214 });
215
216 document.querySelector("#zoomout").addEventListener("click", function() {
217 s.camera.goTo({
218 ratio: Math.min(s.camera.settings("zoomMax"), s.camera.ratio * Math.sqrt(2))
219 });
220 });
221
222 document.addEventListener("keydown", function() {
223 if (event.key == "f" && event.target.getAttribute("id") != "search-input") altSearchBar();
224 if (event.which == seq[cur]) {
225 if (cur < seq.length) {
226 ++cur;
227 if (cur == seq.length) {
228 justdoit();
229 }
230 }
231 } else cur = 0;
232 })
233 document.querySelector("#search").addEventListener("click", altSearchBar);
234
235 if (window.innerWidth > 700) altSearchBar();
236
237 s.refresh();
238 autocomplete(document.querySelector("#search-input"), graf.nodes, "search");
239 });
avm99963f0d15212017-10-08 17:03:22 +0200240}
241
Huguet57004b1552018-07-23 03:29:12 +0200242function cameraGoto(nodeX, nodeY) {
243 sigma.misc.animation.camera( s.camera,
244 { x: nodeX, y: nodeY, ratio: 1 },
245 { duration: s.settings('animationsTime') || 300 }
246 );
247}
248
avm99963f0d15212017-10-08 17:03:22 +0200249window.addEventListener("load", init);