-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCanvasGraph.js
More file actions
198 lines (188 loc) · 8.75 KB
/
Copy pathCanvasGraph.js
File metadata and controls
198 lines (188 loc) · 8.75 KB
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
(function(def) {
/*
EXAMPLE:
CanvasGraph([
{ nodeLabel:'FancyLabel', nodeId:'A', children: [ { property: ':b', propertyLabel:':bFancy', nodeId: 'B', children: [ { property: ':d', target: 'D' }, { property: ':e', nodeId: 'E' }, ] }, { property: ':c', nodeId: 'C' } ] },
{ nodeId:'Y2', children: [ { property: ':c', nodeId: 'C', children:[{property:':cz',target:'D1'}] } ] },
{ nodeId:'D', },
{ nodeId:'Z', children: [ { property: ':b', nodeId: 'B', children: [ { property: ':d', target: 'D' }, ] }, { property: ':z', target: 'A' }, ] },
{ nodeId:'Y1', children: [ { property: ':c', nodeId: 'C' } ] },
{ nodeId:'A1', children: [ { property: ':b', nodeId: 'B', children: [ { property: ':d', target: 'D' }, { property: ':e', nodeId: 'E' }, ] }, { property: ':c', nodeId: 'C' } ] },
{ nodeId:'D1'},
], document.body);
*/
def(function CanvasGraph(roots, container) {
var nodesEl = divEl('CanvasGraph-nodes-container');
var edgesEl = divEl('CanvasGraph-edges-container');
var canvasGraphEl = divEl('CanvasGraph', nodesEl, edgesEl);
container.appendChild(canvasGraphEl);
var targetLookup, sourceEls;
var renderNodes = (function() {
function div(spec /*, .. rest .. */) {
if (typeof spec === 'string') {
return '<div class="'+spec+'">' + rest(arguments).join('') + '</div>';
} else {
return '<div data-CanvasGraph-node-id="'+spec.nodeId+'" data-CanvasGraph-target="'+(spec.target||'')+'" class="'+spec.className+'">' + rest(arguments).join('') + '</div>';
}
}
function safe(s) {
if (!s) return '';
return s.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
}
function renderNode(node) {
if (node.property) {
var propertyValue = '';
if (node.nodeId) {
propertyValue = div('CanvasGraph-property-value',
div({className:'CanvasGraph-node', nodeId:node.nodeId},
div('CanvasGraph-node-header',
div('CanvasGraph-node-label', safe(node.nodeLabel || node.nodeId))),
node.children ? node.children.map(renderNode).join('') : ''));
}
return div('CanvasGraph-property',
div('CanvasGraph-property-header',
div({className:'CanvasGraph-property-label', nodeId:node.property, target:node.target}, safe(node.propertyLabel || node.property))),
propertyValue);
} else {
return div({className:'CanvasGraph-node',nodeId:node.nodeId},
div('CanvasGraph-node-header',
div('CanvasGraph-node-label', safe(node.nodeLabel || node.nodeId))),
node.children ? node.children.map(renderNode).join('') : '');
}
}
function indexTargets() {
var sources = nodesEl.querySelectorAll('[data-CanvasGraph-target]')
sourceEls = [];
targetLookup = {};
var target;
for (var i=0, n=sources.length; i < n; i++) {
target = sources[i].getAttribute('data-CanvasGraph-target')
if (target) {
sourceEls.push(sources[i]);
targetLookup[target] = nodesEl.querySelector('[data-CanvasGraph-node-id="'+target+'"]')
}
}
}
return function() {
var finalHtml = roots.map(renderNode).join('');
finalHtml += div('CanvasGraph-clear-float');
nodesEl.innerHTML = finalHtml;
indexTargets()
};
})();
var renderEdges = (function() {
function getPos(el) {
var x = 0, y = 0;
for (; el && el != nodesEl; el = el.offsetParent) {
x += el.offsetLeft
y += el.offsetTop
}
x -= canvasGraphEl.scrollLeft
y -= canvasGraphEl.scrollTop
return { x:x, y:y }
}
function sq(x) { return x*x }
function distance(pA, pB) { return Math.sqrt(sq(pB.x-pA.x) + sq(pB.y-pA.y)); }
function drawEdge(pA,pB, gfx) {
var x1 = pA.x - 30;
var y1 = (pA.y <= pB.y) ? pA.y + 100 : pA.y - 50;
var x2 = (pA.x <= pB.x) ? pB.x - 30 : pB.x + 30;
var y2 = (pA.y <= pB.y) ? pB.y - 100 : pB.y + 50;
var x3 = pB.x;
var y3 = pB.y - 40;
gfx.beginPath();
gfx.moveTo(pA.x-2, pA.y);
gfx.lineTo(x1, pA.y);
if (distance(pA,pB) > 400) {
gfx.bezierCurveTo(x1,y1, x2,y2, x2,y3);
} else {
gfx.lineTo(x2,y3);
}
gfx.lineTo(x3, y3);
gfx.lineTo(pB.x, pB.y-4);
gfx.strokeStyle = 'white';
gfx.lineWidth = 6;
gfx.lineJoin = 'round';
gfx.stroke();
gfx.strokeStyle = 'hsl(200,80%,50%)';
gfx.lineWidth = 2;
gfx.stroke();
gfx.closePath();
// Arrow
var r = 16;
gfx.beginPath();
gfx.moveTo(pB.x-r-4, pB.y-r-2);
gfx.lineTo(pB.x+r+4, pB.y-r-2);
gfx.lineTo(pB.x+4, pB.y);
gfx.lineTo(pB.x-4, pB.y);
gfx.fillStyle = 'white';
gfx.fill();
gfx.closePath();
gfx.beginPath();
gfx.moveTo(pB.x-r, pB.y-r);
gfx.lineTo(pB.x+r, pB.y-r);
gfx.lineTo(pB.x, pB.y-2);
gfx.fillStyle = 'hsl(170,90%,40%)';
gfx.fill();
gfx.closePath();
}
function renderNow() {
var canvasEl = document.createElement('canvas');
canvasEl.width = canvasGraphEl.clientWidth;
canvasEl.height = canvasGraphEl.clientHeight;
edgesEl.style.top = canvasGraphEl.scrollTop + 'px';
edgesEl.style.left = canvasGraphEl.scrollLeft + 'px';
var gfx = canvasEl.getContext('2d');
sourceEls.forEach(function(sourceEl) {
var target = sourceEl.getAttribute('data-CanvasGraph-target');
var targetEl = targetLookup[target];
var a = getPos(sourceEl);
var b = getPos(targetEl);
a.y += sourceEl.clientHeight/2;
b.x += 8;
drawEdge(a,b, gfx);
});
edgesEl.style.backgroundImage = 'url('+canvasEl.toDataURL('image/png')+')';
}
var renderTimer;
return function() {
clearTimeout(renderTimer);
renderTimer = setTimeout(renderNow, 300);
};
})();
renderNodes();
renderEdges();
canvasGraphEl.addEventListener('scroll', renderEdges);
window.addEventListener('resize', renderEdges);
return {
setRoots: function(newRoots) {
canvasGraphEl.scrollTop = 0;
canvasGraphEl.scrollLeft = 0;
roots = newRoots;
renderNodes();
renderEdges();
},
redrawEdges: function() {
renderEdges();
},
destroy: function() {
canvasGraphEl.removeEventListener('scroll', renderEdges);
window.removeEventListener('resize', renderEdges);
container.removeChild(canvasGraphEl);
}
};
});
function rest(list) {
return Array.prototype.slice.call(list, 1);
}
function divEl(className/*, .. rest .. */) {
var el = document.createElement('div');
el.className = className;
rest(arguments).forEach(function(childEl) {
el.appendChild(childEl);
});
return el;
}
})(typeof window.define === 'function' ? window.define : function(api) { window.CanvasGraph = api });