blob: 4e742313cb42565b028e5f71c11b4feefec0573a [file] [log] [blame]
avm99963f0d15212017-10-08 17:03:22 +02001var s, graf;
2
3function 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
28var dialog = {
29 fill: function(data, text, html=false) {
30 var el = document.querySelectorAll("*[data-fill=\""+data+"\"]");
31 for (var i in el) {
32 if (html === true) {
33 el[i].innerHTML = text;
34 } else {
35 el[i].innerText = text;
36 }
37 }
38 },
39 show: function(id, neighbors) {
40 var neighbors = Object.values(neighbors);
41
42 this.fill("name", graf.nodes[id].name);
43 this.fill("year", graf.nodes[id].year);
44 this.fill("sex", graf.nodes[id].sex);
45 this.fill("id", "#"+id);
46 this.fill("n-edges", neighbors.length);
47
48 var list = "";
49 neighbors.forEach(function (a) {
50 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>";
51 });
52 this.fill("edges", list, true);
53
54 if (window.innerWidth > 700) {
55 document.querySelector("#dialog").style.display = "block";
56 document.querySelector("#backdrop-container").style.display = "block";
57 } else {
58 document.querySelector("#summary-dialog").style.display = "block";
59 }
60 },
61 close: function() {
62 document.querySelector("#dialog").style.display = "none";
63 document.querySelector("#summary-dialog").style.display = "none";
64 document.querySelector("#backdrop-container").style.display = "none";
65
66 s.graph.nodes().forEach(function(n) {
67 n.color = n.originalColor;
68 });
69
70 s.graph.edges().forEach(function(e) {
71 e.color = "#fff";
72 });
73
74 s.refresh();
75 },
76 max: function() {
77 document.querySelector("#summary-dialog").style.display = "none";
78 document.querySelector("#dialog").style.display = "block";
79 },
80 min: function() {
81 document.querySelector("#dialog").style.display = "none";
82 document.querySelector("#summary-dialog").style.display = "block";
83 }
84};
85
86function init() {
87 sigma.classes.graph.addMethod("neighbors", function(nodeId) {
88 var k,
89 neighbors = {},
90 index = this.allNeighborsIndex[nodeId] || [];
91
92 for (k in index) {
93 neighbors[k] = this.nodesIndex[k];
94 }
95
96 return neighbors;
97 });
98
99 s = new sigma({
100 renderers: [{
101 container: "graf",
102 type: "webgl"
103 }],
104 settings: {
105 defaultEdgeColor: "#fff",
106 edgeColor: "default",
107 defaultLabelColor: "#fff",
108 autoRescale: false,
109 zoomMax: 10,
110 //enableEdgeHovering: true,
111 font: "Roboto",
112 labelThreshold: 5
113 }
114 });
115
116 xhr("GET", "api.php", "action=getgraf", function(responseText, status) {
117 graf = JSON.parse(responseText);
118
119 console.log(graf);
120
121 for (var i in graf.nodes) {
122 var ncolor = (graf.nodes[i].sex == "F" ? "#d61c08" : (graf.nodes[i].sex == "M" ? "#0159aa" : "#0ca80a"));
123
124 s.graph.addNode({
125 id: graf.nodes[i].id,
126 label: graf.nodes[i].name,
127 x: graf.nodes[i].x,
128 y: graf.nodes[i].y,
129 size: 10,
130 color: ncolor,
131 originalColor: ncolor
132 });
133 }
134
135 for (var i in graf.edges) {
136 s.graph.addEdge({
137 id: i,
138 source: graf.edges[i].a,
139 target: graf.edges[i].b,
140 size: Math.min(4, Math.max((7/(2*Math.pow(20, 2)))*Math.pow(graf.edges[i].votes, 2) + 1/2, 0.5))
141 });
142 }
143
144 s.bind('clickNode', function(e) {
145 var nodeId = e.data.node.id,
146 toKeep = s.graph.neighbors(nodeId);
147 //toKeep[nodeId] = e.data.node;
148
149 s.graph.nodes().forEach(function(n) {
150 if (toKeep[n.id] || n.id == nodeId) {
151 n.color = n.originalColor;
152 } else {
153 n.color = '#333';
154 }
155 });
156
157 s.graph.edges().forEach(function(e) {
158 if ((e.source == nodeId || e.target == nodeId) && (toKeep[e.source] || toKeep[e.target])) {
159 e.color = '#fff';
160 } else {
161 e.color = '#333';
162 }
163 });
164
165 s.refresh();
166
167 dialog.show(nodeId, toKeep);
168 });
169
170 document.querySelector("#quit-dialog").addEventListener("click", dialog.close);
171 document.querySelector("#quit2-dialog").addEventListener("click", dialog.close);
172 document.querySelector("#max-dialog").addEventListener("click", dialog.max);
173 document.querySelector("#min-dialog").addEventListener("click", dialog.min);
174
175 document.querySelector("#zoomin").addEventListener("click", function() {
176 s.camera.goTo({
177 ratio: Math.max(s.camera.settings("zoomMin"), s.camera.ratio / Math.sqrt(2))
178 });
179 });
180
181 document.querySelector("#zoomout").addEventListener("click", function() {
182 s.camera.goTo({
183 ratio: Math.min(s.camera.settings("zoomMax"), s.camera.ratio * Math.sqrt(2))
184 });
185 });
186
187 s.refresh();
188 });
189}
190
191window.addEventListener("load", init);