forked from elastic/kibana-code-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
170 lines (141 loc) · 5.56 KB
/
main.js
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
//
// main.js
//
// A project template for using arbor.js
//
(function($){
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
console.log(node);
var w = 50
ctx.fillStyle = 'black';
ctx.font = "12px Arial";
ctx.fillText(node.name, pt.x, pt.y);
//ctx.fillStyle = (node.data.alone) ? "orange" : "black"
//ctx.fillRect(pt.x-w/2, pt.y-w/2, w,w)
})
},
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 (e.which == 3) {
sys.pruneNode(dragged.node);
e.preventDefault();
e.stopPropagation();
return false;
}
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)
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.addEventListener('contextmenu', e => { e.preventDefault(); return false });
},
}
return that
}
var sys;
$(document).ready(function(){
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("#viewport") // our newly created renderer will have its .init() method called shortly by sys...
let deps;
function addNodes(obj, pname) {
let fullname;
if (pname) {
if (!deps[pname]) console.log(`cant find ${pname}`);
fullname = `${pname} [${deps[pname]['Params']}]`;
console.log(fullname);
}
if (fullname) sys.addNode(fullname);
_.forEach(obj, function(node, name) {
const toName = `${name} [${deps[name]['Params']}]`;
if (fullname) sys.addEdge(fullname, toName);
addNodes(node, name);
});
}
fetch('deps.json').then(d => d.json()).then(d => {
deps = d;
fetch('output/privateDeps.json').then(d => d.json()).then(addNodes);
});
})
})(this.jQuery)