-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
374 lines (307 loc) · 10.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<!--
<script type="text/javascript" src="js/springy/springy.js"></script>
<script type="text/javascript" src="js/springy/springyui.js"></script>
-->
<script type="text/javascript" src="js/arbor-v0.92/lib/arbor.js"></script>
<script type="text/javascript" src="js/arbor-v0.92/lib/arbor-tween.js"></script>
<script type="text/javascript">
var jQuery = $;
var maxTitleLen = 20;
$( document ).ready(function() {
var removeIslands = false;
var selectedNode = false;
var Renderer = function(canvas){
var canvas = $(canvas).get(0)
var ctx = canvas.getContext("2d");
var particleSystem
var that = {
init:function(system){
//
// the particle system will call the init function once, right before the
// first frame is to be drawn. it's a good place to set up the canvas and
// to pass the canvas size to the particle system
//
// save a reference to the particle system for use in the .redraw() loop
particleSystem = system
// inform the system of the screen dimensions so it can map coords for us.
// if the canvas is ever resized, screenSize should be called again with
// the new dimensions
particleSystem.screenSize(canvas.width, canvas.height)
particleSystem.screenPadding(80) // leave an extra 80px of whitespace per side
// set up some event handlers to allow for node-dragging
that.initMouseHandling()
},
redraw:function(){
//
// redraw will be called repeatedly during the run whenever the node positions
// change. the new positions for the nodes can be accessed by looking at the
// .p attribute of a given node. however the p.x & p.y values are in the coordinates
// of the particle system rather than the screen. you can either map them to
// the screen yourself, or use the convenience iterators .eachNode (and .eachEdge)
// which allow you to step through the actual node objects but also pass an
// x,y point in the screen's coordinate system
//
ctx.fillStyle = "white"
ctx.fillRect(0,0, canvas.width, canvas.height)
particleSystem.eachEdge(function(edge, pt1, pt2){
// edge: {source:Node, target:Node, length:#, data:{}}
// pt1: {x:#, y:#} source position in screen coords
// pt2: {x:#, y:#} target position in screen coords
// draw a line from pt1 to pt2
ctx.strokeStyle = "rgba(0,0,0, .333)"
ctx.lineWidth = 1
ctx.beginPath()
ctx.moveTo(pt1.x, pt1.y)
ctx.lineTo(pt2.x, pt2.y)
ctx.stroke()
})
particleSystem.eachNode(function(node, pt){
// node: {mass:#, p:{x,y}, name:"", data:{}}
// pt: {x:#, y:#} node position in screen coords
// draw a rectangle centered at pt
if(removeIslands
&& ( node.data.pad.numLinks == 0)
&& ( node.data.pad.numInLinks == 0)){
removeNode(node);
}
var w = 10
// ctx.fillStyle = (node.data.alone) ? "orange" : "black"
// ctx.fillRect(pt.x-w/2, pt.y-w/2, w,w)
var fillStyle = node.data.fillStyle || "black";
if(node == selectedNode){
fillStyle = "green";
}
if(node.fixed && node != selectedNode){
fillStyle = "blue";
}
fontSize = 12;
if(node.data.pad.links){
fontSize += node.data.pad.numLinks;
}
ctx.font = fontSize+"px Helvetica"
ctx.textAlign = "center"
ctx.fillStyle = fillStyle;
if (node.data.color=='none') ctx.fillStyle = '#333333'
ctx.fillText(node.data.label||"", pt.x, pt.y+4)
// console.log(node.data);
})
removeIslands = false;
},
initMouseHandling:function(){
// no-nonsense drag and drop (thanks springy.js)
var dragged = null;
// set up a handler object that will initially listen for mousedowns then
// for moves and mouseups while dragging
var handler = {
clicked:function(e){
var pos = $(canvas).offset();
_mouseP = arbor.Point(e.pageX-pos.left, e.pageY-pos.top)
dragged = particleSystem.nearest(_mouseP);
if (dragged && dragged.node !== null){
// while we're dragging, don't let physics move the node
dragged.node.fixed = true
}
$(canvas).bind('mousemove', handler.dragged)
$(window).bind('mouseup', handler.dropped)
nodeClicked(dragged.node, dragged.node.data.pad);
return false
},
dragged:function(e){
var pos = $(canvas).offset();
var s = arbor.Point(e.pageX-pos.left, e.pageY-pos.top)
if (dragged && dragged.node !== null){
var p = particleSystem.fromScreen(s)
dragged.node.p = p
}
return false
},
dropped:function(e){
if (dragged===null || dragged.node===undefined) return
if (dragged.node !== null) dragged.node.fixed = false
dragged.node.tempMass = 1000
dragged = null
$(canvas).unbind('mousemove', handler.dragged)
$(window).unbind('mouseup', handler.dropped)
_mouseP = null
return false
}
}
// start listening
$(canvas).mousedown(handler.clicked);
$(window).keypress(function(evt){
console.log("keypressed");
console.log(evt);
if(evt.charCode == 100 && selectedNode){ // 'd' is pressed
removeNode(selectedNode);
selectedNode == false;
}
if(evt.charCode == 105){
removeIslands = true;
}
if(evt.charCode == 103 && selectedNode){
window.open(spaceurl + "/" + selectedNode.data.pad.padid);
removeIslands = true;
}
if(evt.charCode == 117 ){
$.ajax({
url: "/?action=updatelist",
success : function(data, status){
console.log("server updated");
console.log(data);
console.log(status);
},
error : function(jqXHR, status, message){
console.log("error ");
console.log(status);
console.log(message);
}
});
}
if(evt.charCode == 102 && selectedNode){
console.log("freezing node" + selectedNode.fixed);
selectedNode.fixed = !selectedNode.fixed;
console.log(" node froze" + selectedNode.fixed);
}
return false;
});
},
}
return that;
} ;
var sys = arbor.ParticleSystem(1000, 600, 0.5) // create the system with sensible repulsion/stiffness/friction
sys.parameters({gravity:true}) // use center-gravity to make the graph settle nicely (ymmv)
sys.renderer = Renderer("#my_canvas") // our newly created renderer will have its .init() method called shortly by sys...
/*
Edge options:
weight
directional : t/f
color
label
font
graph options:
- nodeFont
- edgeFont
node options:
- font
- ondoubleclick : callback
- label
*/
var spaceurl = "http://metmedia lab.hackpad.com";
var nodes = {};
// make a new graph
// call server to get node list;
var textlessnodes = {};
$.ajax({
url: "?action=listpads",
success : function(data, status){
$.each(data, function(index, pad){
var node = createNode(pad);
nodes[pad.padid] = node;
});
$.each(data, function(index, item){
var padid = item.padid;
$.each(item.links, function(index2, link){
var node1 = nodes[padid];
var node2 = nodes[link.padid];
if(node1 == null){
console.log("no node found for source " + padid);
}
if(node2 == null){
console.log("no node found for dest " + link.padid);
console.log(link);
if(textlessnodes[link.padid]){
linkpad = textlessnodes[link.padid];
}else{
linkpad = {title: link.title, padid : link.padid, inLinks : {}};
textlessnodes[link.padid] = linkpad;
}
linkpad.inLinks[padid] = padid;
node2 = createNode(linkpad);
}
sys.addEdge(node1, node2, {color: 'red', weight: .5});
});
});
},
error : function(jqXHR, status, message){
console.log("error ");
console.log(status);
console.log(message);
}
});
var removedNodes = {};
function removeNode(node){
removedNodes[node.data.pad.padid] = node;
$.each(node.data.pad.links, function(index, item){
var target = liveNodes[item.padid].data.pad;
if(target.inLinks[node.data.pad.padid]){
target.numInLinks--;
delete target.inLinks[node.data.pad.padid];
}
});
$.each(node.data.pad.inLinks, function(index, item){
var target = liveNodes[index].data.pad;
if(target.links[node.data.pad.padid]){
target.numlinks--;
delete target.links[node.data.pad.padid];
}
});
sys.pruneNode(node);
delete liveNodes[node.data.pad.padid];
}
function nodeClicked(node, pad){
selectedNode = node;
// window.open(spaceurl + "/" + pad.padid);
}
var liveNodes = {};
function createNode(pad){
var title = pad.title;
if(title.length > maxTitleLen){
title = title.substring(0,maxTitleLen) + "...";
}
var mass= 2 + pad.numLinks;
var node = sys.addNode(pad.padid,
{pad: pad,
label: title,
color: "green",
fillStyle : "red",
ondoubleclick : function(evt){ nodeDblClicked(this, pad);},
onclick : function(evt){nodeClicked(this, pad);}
});
liveNodes[pad.padid] = node;
return node;
}
// make some nodes
/* var spruce = graph.newNode({label: 'Norway Spruce'});
var fir = graph.newNode({label: 'Sicilian Fir'});
// connect them with an edge
graph.newEdge(spruce, fir);
*/
});
</script>
</head>
<body>
<div id="container">
<div id="canvasdiv">
<canvas id="my_canvas" width="1200" height="800" />
</div>
<div id="infodiv">
<div id="infotitle">
</div>
<div id="infocontent">
Hotkeys:<BR/>
g : Go to selected node's Pad<BR>
d : delete selected node from graph<BR>
f : (un)freeze selected node <BR>
i : remove any isolated nodes<BR>
u : tell server to update graph<BR>
</div>
</div>
</div>
<script>
</script>
</body>
</html>