2017-01-29 23:51:08 +00:00
|
|
|
define(['d3', 'helper'], function (d3, helper) {
|
|
|
|
'use strict';
|
2016-05-27 21:59:01 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var margin = 200;
|
|
|
|
var NODE_RADIUS = 15;
|
|
|
|
var LINE_RADIUS = 12;
|
2015-04-10 19:53:40 +00:00
|
|
|
|
|
|
|
return function (config, linkScale, sidebar, router) {
|
2016-05-22 11:23:43 +00:00
|
|
|
var self = this;
|
2017-01-29 23:51:08 +00:00
|
|
|
var canvas;
|
|
|
|
var ctx;
|
|
|
|
var screenRect;
|
|
|
|
var nodesDict;
|
|
|
|
var linksDict;
|
2016-05-22 11:23:43 +00:00
|
|
|
var zoomBehavior;
|
|
|
|
var force;
|
|
|
|
var el;
|
2016-06-19 11:35:35 +00:00
|
|
|
var font;
|
2016-05-22 11:23:43 +00:00
|
|
|
var doAnimation = false;
|
|
|
|
var intNodes = [];
|
|
|
|
var intLinks = [];
|
|
|
|
var highlight;
|
|
|
|
var highlightedNodes = [];
|
|
|
|
var highlightedLinks = [];
|
|
|
|
var nodes = [];
|
|
|
|
var uplinkNodes = [];
|
|
|
|
var nonUplinkNodes = [];
|
|
|
|
var unseenNodes = [];
|
|
|
|
var unknownNodes = [];
|
|
|
|
var savedPanZoom;
|
|
|
|
|
|
|
|
var draggedNode;
|
|
|
|
|
|
|
|
var LINK_DISTANCE = 70;
|
2015-03-31 22:06:17 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
function graphDiameter(n) {
|
|
|
|
return Math.sqrt(n.length / Math.PI) * LINK_DISTANCE * 1.41;
|
2015-04-01 16:21:54 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 23:06:53 +00:00
|
|
|
function savePositions() {
|
2016-05-26 16:37:24 +00:00
|
|
|
if (!helper.localStorageTest()) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return;
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-04-01 13:59:05 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
var save = intNodes.map(function (d) {
|
2017-01-29 23:51:08 +00:00
|
|
|
return { id: d.o.id, x: d.x, y: d.y };
|
2016-05-22 11:23:43 +00:00
|
|
|
});
|
2015-03-31 23:06:53 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
localStorage.setItem('graph/nodeposition', JSON.stringify(save));
|
2015-03-31 23:06:53 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 15:22:36 +00:00
|
|
|
function nodeName(d) {
|
2016-05-22 12:51:30 +00:00
|
|
|
if (d.o.node && d.o.node.nodeinfo) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return d.o.node.nodeinfo.hostname;
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2017-01-29 23:51:08 +00:00
|
|
|
return d.o.id;
|
2015-03-31 15:22:36 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 23:29:17 +00:00
|
|
|
function dragstart() {
|
2016-05-22 11:23:43 +00:00
|
|
|
var e = translateXY(d3.mouse(el));
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
var n = intNodes.filter(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return distancePoint(e, d) < NODE_RADIUS;
|
|
|
|
});
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
if (n.length === 0) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return;
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
draggedNode = n[0];
|
2016-05-22 11:23:43 +00:00
|
|
|
d3.event.sourceEvent.stopPropagation();
|
|
|
|
d3.event.sourceEvent.preventDefault();
|
|
|
|
draggedNode.fixed |= 2;
|
2015-07-08 16:03:19 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
draggedNode.px = draggedNode.x;
|
|
|
|
draggedNode.py = draggedNode.y;
|
2015-03-31 15:22:36 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 23:29:17 +00:00
|
|
|
function dragmove() {
|
|
|
|
if (draggedNode) {
|
2016-05-22 11:23:43 +00:00
|
|
|
var e = translateXY(d3.mouse(el));
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
draggedNode.px = e.x;
|
|
|
|
draggedNode.py = e.y;
|
|
|
|
force.resume();
|
2015-04-15 23:29:17 +00:00
|
|
|
}
|
2015-03-31 15:22:36 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 23:29:17 +00:00
|
|
|
function dragend() {
|
|
|
|
if (draggedNode) {
|
2016-05-22 11:23:43 +00:00
|
|
|
d3.event.sourceEvent.stopPropagation();
|
|
|
|
d3.event.sourceEvent.preventDefault();
|
|
|
|
draggedNode.fixed &= ~2;
|
|
|
|
draggedNode = undefined;
|
2015-04-15 23:29:17 +00:00
|
|
|
}
|
2015-03-31 15:22:36 +00:00
|
|
|
}
|
|
|
|
|
2015-04-10 11:50:39 +00:00
|
|
|
var draggableNode = d3.behavior.drag()
|
2017-01-29 23:51:08 +00:00
|
|
|
.on('dragstart', dragstart)
|
|
|
|
.on('drag', dragmove)
|
|
|
|
.on('dragend', dragend);
|
2015-04-10 11:50:39 +00:00
|
|
|
|
2015-03-31 19:31:06 +00:00
|
|
|
function animatePanzoom(translate, scale) {
|
2016-05-22 11:23:43 +00:00
|
|
|
var translateP = zoomBehavior.translate();
|
|
|
|
var scaleP = zoomBehavior.scale();
|
2015-03-31 19:31:06 +00:00
|
|
|
|
2015-04-10 18:47:11 +00:00
|
|
|
if (!doAnimation) {
|
2016-05-22 11:23:43 +00:00
|
|
|
zoomBehavior.translate(translate);
|
|
|
|
zoomBehavior.scale(scale);
|
|
|
|
panzoom();
|
2015-04-10 18:47:11 +00:00
|
|
|
} else {
|
2017-01-29 23:51:08 +00:00
|
|
|
var start = { x: translateP[0], y: translateP[1], scale: scaleP };
|
|
|
|
var end = { x: translate[0], y: translate[1], scale: scale };
|
2015-03-31 21:45:32 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var interpolate = d3.interpolateObject(start, end);
|
|
|
|
var duration = 500;
|
2015-03-31 21:45:32 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
var ease = d3.ease('cubic-in-out');
|
2015-04-10 18:47:11 +00:00
|
|
|
|
|
|
|
d3.timer(function (t) {
|
2016-05-22 12:51:30 +00:00
|
|
|
if (t >= duration) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return true;
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-04-10 18:47:11 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var v = interpolate(ease(t / duration));
|
|
|
|
zoomBehavior.translate([v.x, v.y]);
|
|
|
|
zoomBehavior.scale(v.scale);
|
|
|
|
panzoom();
|
2015-04-10 18:47:11 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
return false;
|
|
|
|
});
|
2015-04-10 18:47:11 +00:00
|
|
|
}
|
2015-03-31 19:31:06 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 14:29:22 +00:00
|
|
|
function onPanZoom() {
|
2016-05-22 12:51:30 +00:00
|
|
|
savedPanZoom = {
|
|
|
|
translate: zoomBehavior.translate(),
|
|
|
|
scale: zoomBehavior.scale()
|
|
|
|
};
|
2016-05-22 11:23:43 +00:00
|
|
|
panzoom();
|
2015-04-19 14:29:22 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 15:22:36 +00:00
|
|
|
function panzoom() {
|
2016-05-22 11:23:43 +00:00
|
|
|
var translate = zoomBehavior.translate();
|
|
|
|
var scale = zoomBehavior.scale();
|
2015-04-10 18:47:11 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
panzoomReal(translate, scale);
|
2015-04-10 18:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function panzoomReal(translate, scale) {
|
2016-05-22 12:51:30 +00:00
|
|
|
screenRect = {
|
|
|
|
left: -translate[0] / scale, top: -translate[1] / scale,
|
|
|
|
right: (canvas.width - translate[0]) / scale,
|
|
|
|
bottom: (canvas.height - translate[1]) / scale
|
|
|
|
};
|
2015-04-10 19:53:40 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
requestAnimationFrame(redraw);
|
2015-03-31 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 16:21:54 +00:00
|
|
|
function getSize() {
|
2016-05-22 11:23:43 +00:00
|
|
|
var sidebarWidth = sidebar();
|
|
|
|
var width = el.offsetWidth - sidebarWidth;
|
|
|
|
var height = el.offsetHeight;
|
2015-04-01 16:21:54 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
return [width, height];
|
2015-04-01 16:21:54 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 19:18:06 +00:00
|
|
|
function panzoomTo(a, b) {
|
2016-05-22 11:23:43 +00:00
|
|
|
var sidebarWidth = sidebar();
|
|
|
|
var size = getSize();
|
2015-03-31 19:18:06 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var targetWidth = Math.max(1, b[0] - a[0]);
|
|
|
|
var targetHeight = Math.max(1, b[1] - a[1]);
|
2015-03-31 19:18:06 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var scaleX = size[0] / targetWidth;
|
|
|
|
var scaleY = size[1] / targetHeight;
|
|
|
|
var scaleMax = zoomBehavior.scaleExtent()[1];
|
|
|
|
var scale = 0.5 * Math.min(scaleMax, Math.min(scaleX, scaleY));
|
2015-03-31 19:18:06 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var centroid = [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2];
|
|
|
|
var x = -centroid[0] * scale + size[0] / 2;
|
|
|
|
var y = -centroid[1] * scale + size[1] / 2;
|
|
|
|
var translate = [x + sidebarWidth, y];
|
2015-03-31 19:18:06 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
animatePanzoom(translate, scale);
|
2015-03-31 19:29:47 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 09:37:52 +00:00
|
|
|
function updateHighlight(nopanzoom) {
|
2016-05-22 11:23:43 +00:00
|
|
|
highlightedNodes = [];
|
|
|
|
highlightedLinks = [];
|
2015-04-10 18:47:11 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
if (highlight !== undefined) {
|
2017-01-29 23:51:08 +00:00
|
|
|
if (highlight.type === 'node') {
|
2016-05-22 11:23:43 +00:00
|
|
|
var n = nodesDict[highlight.o.nodeinfo.node_id];
|
2015-04-06 09:37:52 +00:00
|
|
|
|
|
|
|
if (n) {
|
2016-05-22 11:23:43 +00:00
|
|
|
highlightedNodes = [n];
|
2015-04-06 09:37:52 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
if (!nopanzoom) {
|
2016-05-22 11:23:43 +00:00
|
|
|
panzoomTo([n.x, n.y], [n.x, n.y]);
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-04-06 09:37:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
return;
|
2017-01-29 23:51:08 +00:00
|
|
|
} else if (highlight.type === 'link') {
|
2016-05-22 11:23:43 +00:00
|
|
|
var l = linksDict[highlight.o.id];
|
2015-04-06 09:37:52 +00:00
|
|
|
|
|
|
|
if (l) {
|
2016-05-22 11:23:43 +00:00
|
|
|
highlightedLinks = [l];
|
2015-04-06 09:37:52 +00:00
|
|
|
|
|
|
|
if (!nopanzoom) {
|
2016-05-22 12:51:30 +00:00
|
|
|
var x = d3.extent([l.source, l.target], function (d) {
|
|
|
|
return d.x;
|
|
|
|
});
|
|
|
|
var y = d3.extent([l.source, l.target], function (d) {
|
|
|
|
return d.y;
|
|
|
|
});
|
2016-05-22 11:23:43 +00:00
|
|
|
panzoomTo([x[0], y[0]], [x[1], y[1]]);
|
2015-04-06 09:37:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
return;
|
2015-04-06 09:37:52 +00:00
|
|
|
}
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-04-06 09:37:52 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
if (!nopanzoom) {
|
|
|
|
if (!savedPanZoom) {
|
2016-05-22 11:23:43 +00:00
|
|
|
panzoomTo([0, 0], force.size());
|
2016-05-22 12:51:30 +00:00
|
|
|
} else {
|
2016-05-22 11:23:43 +00:00
|
|
|
animatePanzoom(savedPanZoom.translate, savedPanZoom.scale);
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-06 09:37:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-10 18:47:11 +00:00
|
|
|
function drawLabel(d) {
|
2017-01-29 23:51:08 +00:00
|
|
|
var neighbours = d.neighbours.filter(function (n) {
|
|
|
|
return n.link.o.type !== 'fastd' && n.link.o.type !== 'L2TP';
|
2016-05-22 11:23:43 +00:00
|
|
|
});
|
2015-04-19 00:16:06 +00:00
|
|
|
|
|
|
|
var sum = neighbours.reduce(function (a, b) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return [a[0] + b.node.x, a[1] + b.node.y];
|
|
|
|
}, [0, 0]);
|
2015-04-10 11:50:39 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var sumCos = sum[0] - d.x * neighbours.length;
|
|
|
|
var sumSin = sum[1] - d.y * neighbours.length;
|
2015-04-10 11:50:39 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var angle = Math.PI / 2;
|
2015-04-10 11:50:39 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
if (neighbours.length > 0) {
|
2016-05-22 11:23:43 +00:00
|
|
|
angle = Math.PI + Math.atan2(sumSin, sumCos);
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-04-10 11:50:39 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var cos = Math.cos(angle);
|
|
|
|
var sin = Math.sin(angle);
|
2015-04-10 11:50:39 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var width = d.labelWidth;
|
|
|
|
var height = d.labelHeight;
|
2015-04-10 11:50:39 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var x = d.x + d.labelA * Math.pow(Math.abs(cos), 2 / 5) * Math.sign(cos) - width / 2;
|
|
|
|
var y = d.y + d.labelB * Math.pow(Math.abs(sin), 2 / 5) * Math.sign(sin) - height / 2;
|
2015-04-10 22:30:36 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.drawImage(d.label, x, y, width, height);
|
2015-04-10 18:47:11 +00:00
|
|
|
}
|
2015-04-10 11:50:39 +00:00
|
|
|
|
2015-04-10 19:53:40 +00:00
|
|
|
function visibleLinks(d) {
|
|
|
|
return (d.source.x > screenRect.left && d.source.x < screenRect.right &&
|
2016-05-22 12:51:30 +00:00
|
|
|
d.source.y > screenRect.top && d.source.y < screenRect.bottom) ||
|
|
|
|
(d.target.x > screenRect.left && d.target.x < screenRect.right &&
|
|
|
|
d.target.y > screenRect.top && d.target.y < screenRect.bottom);
|
2015-04-10 19:53:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function visibleNodes(d) {
|
|
|
|
return d.x + margin > screenRect.left && d.x - margin < screenRect.right &&
|
2016-05-22 12:51:30 +00:00
|
|
|
d.y + margin > screenRect.top && d.y - margin < screenRect.bottom;
|
2015-04-10 19:53:40 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 23:33:39 +00:00
|
|
|
function drawNode(color, radius, scale, r) {
|
2017-01-29 23:51:08 +00:00
|
|
|
var node = document.createElement('canvas');
|
2016-05-22 21:32:05 +00:00
|
|
|
node.height = node.width = scale * radius * 8 * r;
|
2016-05-22 11:23:43 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
var nctx = node.getContext('2d');
|
2016-05-22 11:23:43 +00:00
|
|
|
nctx.scale(scale * r, scale * r);
|
|
|
|
nctx.save();
|
|
|
|
|
|
|
|
nctx.translate(-node.width / scale, -node.height / scale);
|
|
|
|
nctx.lineWidth = radius;
|
|
|
|
|
|
|
|
nctx.beginPath();
|
|
|
|
nctx.moveTo(radius, 0);
|
|
|
|
nctx.arc(0, 0, radius, 0, 2 * Math.PI);
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
nctx.strokeStyle = 'rgba(255, 0, 0, 1)';
|
2016-05-22 21:32:05 +00:00
|
|
|
nctx.shadowOffsetX = node.width * 1.5;
|
2016-05-22 11:23:43 +00:00
|
|
|
nctx.shadowOffsetY = node.height * 1.5 + 3;
|
|
|
|
nctx.shadowBlur = 12;
|
2017-01-29 23:51:08 +00:00
|
|
|
nctx.shadowColor = 'rgba(0, 0, 0, 0.16)';
|
2016-05-22 11:23:43 +00:00
|
|
|
nctx.stroke();
|
2016-05-22 21:32:05 +00:00
|
|
|
nctx.shadowOffsetX = node.width * 1.5;
|
2016-05-22 11:23:43 +00:00
|
|
|
nctx.shadowOffsetY = node.height * 1.5 + 3;
|
|
|
|
nctx.shadowBlur = 12;
|
2017-01-29 23:51:08 +00:00
|
|
|
nctx.shadowColor = 'rgba(0, 0, 0, 0.23)';
|
2016-05-22 11:23:43 +00:00
|
|
|
nctx.stroke();
|
|
|
|
|
|
|
|
nctx.restore();
|
|
|
|
nctx.translate(node.width / 2 / scale / r, node.height / 2 / scale / r);
|
|
|
|
|
|
|
|
nctx.beginPath();
|
|
|
|
nctx.moveTo(radius, 0);
|
|
|
|
nctx.arc(0, 0, radius, 0, 2 * Math.PI);
|
|
|
|
|
|
|
|
nctx.strokeStyle = color;
|
|
|
|
nctx.lineWidth = radius;
|
|
|
|
nctx.stroke();
|
|
|
|
|
|
|
|
return node;
|
2016-02-05 23:33:39 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 19:01:46 +00:00
|
|
|
function redraw() {
|
2016-05-22 11:23:43 +00:00
|
|
|
var r = window.devicePixelRatio;
|
|
|
|
var translate = zoomBehavior.translate();
|
|
|
|
var scale = zoomBehavior.scale();
|
|
|
|
var links = intLinks.filter(visibleLinks);
|
|
|
|
|
|
|
|
ctx.save();
|
|
|
|
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
ctx.restore();
|
|
|
|
|
|
|
|
ctx.save();
|
|
|
|
ctx.translate(translate[0], translate[1]);
|
|
|
|
ctx.scale(scale, scale);
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
var clientColor = 'rgba(230, 50, 75, 1.0)';
|
|
|
|
var unknownColor = '#D10E2A';
|
|
|
|
var nonUplinkColor = '#F2E3C6';
|
|
|
|
var uplinkColor = '#5BAAEB';
|
|
|
|
var unseenColor = '#FFA726';
|
|
|
|
var highlightColor = 'rgba(252, 227, 198, 0.15)';
|
2016-05-22 11:23:43 +00:00
|
|
|
var nodeRadius = 6;
|
2017-01-29 23:51:08 +00:00
|
|
|
var cableColor = '#50B0F0';
|
2015-07-07 10:42:57 +00:00
|
|
|
|
2015-07-31 09:42:25 +00:00
|
|
|
// -- draw links --
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.save();
|
2015-04-10 18:47:11 +00:00
|
|
|
links.forEach(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
var dx = d.target.x - d.source.x;
|
|
|
|
var dy = d.target.y - d.source.y;
|
|
|
|
var a = Math.sqrt(dx * dx + dy * dy);
|
|
|
|
dx /= a;
|
|
|
|
dy /= a;
|
|
|
|
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.moveTo(d.source.x + dx * nodeRadius, d.source.y + dy * nodeRadius);
|
|
|
|
ctx.lineTo(d.target.x - dx * nodeRadius, d.target.y - dy * nodeRadius);
|
2017-01-29 23:51:08 +00:00
|
|
|
ctx.strokeStyle = d.o.type === 'Kabel' ? cableColor : d.color;
|
|
|
|
ctx.globalAlpha = d.o.type === 'fastd' || d.o.type === 'L2TP' ? 0.2 : 0.8;
|
|
|
|
ctx.lineWidth = d.o.type === 'fastd' || d.o.type === 'L2TP' ? 1.5 : 2.5;
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.stroke();
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.restore();
|
2015-04-19 00:16:06 +00:00
|
|
|
|
2015-07-31 09:42:25 +00:00
|
|
|
// -- draw unknown nodes --
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.beginPath();
|
2015-04-10 19:53:40 +00:00
|
|
|
unknownNodes.filter(visibleNodes).forEach(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.moveTo(d.x + nodeRadius, d.y);
|
|
|
|
ctx.arc(d.x, d.y, nodeRadius, 0, 2 * Math.PI);
|
|
|
|
});
|
2015-04-10 11:50:39 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.strokeStyle = unknownColor;
|
|
|
|
ctx.lineWidth = nodeRadius;
|
2015-04-10 11:50:39 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.stroke();
|
2015-04-10 11:50:39 +00:00
|
|
|
|
2015-07-31 09:42:25 +00:00
|
|
|
// -- draw nodes --
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.save();
|
|
|
|
ctx.scale(1 / scale / r, 1 / scale / r);
|
2016-02-05 23:33:39 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var nonUplinkNode = drawNode(nonUplinkColor, nodeRadius, scale, r);
|
2016-02-05 23:33:39 +00:00
|
|
|
nonUplinkNodes.filter(visibleNodes).forEach(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.drawImage(nonUplinkNode, scale * r * d.x - nonUplinkNode.width / 2, scale * r * d.y - nonUplinkNode.height / 2);
|
|
|
|
});
|
2016-02-05 23:33:39 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var uplinkNode = drawNode(uplinkColor, nodeRadius, scale, r);
|
2016-02-05 23:33:39 +00:00
|
|
|
uplinkNodes.filter(visibleNodes).forEach(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.drawImage(uplinkNode, scale * r * d.x - uplinkNode.width / 2, scale * r * d.y - uplinkNode.height / 2);
|
|
|
|
});
|
2016-02-05 23:33:39 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var unseenNode = drawNode(unseenColor, nodeRadius, scale, r);
|
2016-03-02 20:55:44 +00:00
|
|
|
unseenNodes.filter(visibleNodes).forEach(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.drawImage(unseenNode, scale * r * d.x - unseenNode.width / 2, scale * r * d.y - unseenNode.height / 2);
|
|
|
|
});
|
2016-03-02 20:55:44 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.restore();
|
2015-04-10 18:47:11 +00:00
|
|
|
|
2015-07-31 09:42:25 +00:00
|
|
|
// -- draw clients --
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.save();
|
|
|
|
ctx.beginPath();
|
2017-02-07 23:14:48 +00:00
|
|
|
if (scale > 0.9) {
|
|
|
|
nodes.filter(visibleNodes).forEach(function (d) {
|
|
|
|
var clients = d.o.node.statistics.clients;
|
|
|
|
if (clients === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-31 09:42:25 +00:00
|
|
|
|
2017-02-07 23:14:48 +00:00
|
|
|
var startDistance = 16;
|
|
|
|
var radius = 3;
|
|
|
|
var a = 1.2;
|
|
|
|
var startAngle = Math.PI;
|
2015-07-31 09:42:25 +00:00
|
|
|
|
2017-02-07 23:14:48 +00:00
|
|
|
for (var orbit = 0, i = 0; i < clients; orbit++) {
|
|
|
|
var di = startDistance + orbit * 2 * radius * a;
|
|
|
|
var n = Math.floor((Math.PI * di) / (a * radius));
|
|
|
|
var delta = clients - i;
|
2015-07-31 09:42:25 +00:00
|
|
|
|
2017-02-07 23:14:48 +00:00
|
|
|
for (var j = 0; j < Math.min(delta, n); i++, j++) {
|
|
|
|
var angle = 2 * Math.PI / n * j;
|
|
|
|
var x = d.x + di * Math.cos(angle + startAngle);
|
|
|
|
var y = d.y + di * Math.sin(angle + startAngle);
|
2015-07-31 09:42:25 +00:00
|
|
|
|
2017-02-07 23:14:48 +00:00
|
|
|
ctx.moveTo(x, y);
|
|
|
|
ctx.arc(x, y, radius, 0, 2 * Math.PI);
|
|
|
|
}
|
2015-07-31 09:42:25 +00:00
|
|
|
}
|
2017-02-07 23:14:48 +00:00
|
|
|
});
|
|
|
|
}
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.fillStyle = clientColor;
|
|
|
|
ctx.fill();
|
|
|
|
ctx.restore();
|
2015-07-31 09:42:25 +00:00
|
|
|
|
|
|
|
// -- draw node highlights --
|
2015-04-10 18:47:11 +00:00
|
|
|
if (highlightedNodes.length) {
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.save();
|
2017-01-29 23:51:08 +00:00
|
|
|
ctx.shadowColor = 'rgba(255, 255, 255, 1.0)';
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.shadowBlur = 10 * nodeRadius;
|
|
|
|
ctx.shadowOffsetX = 0;
|
|
|
|
ctx.shadowOffsetY = 0;
|
2017-01-29 23:51:08 +00:00
|
|
|
ctx.globalCompositeOperation = 'lighten';
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.fillStyle = highlightColor;
|
|
|
|
|
|
|
|
ctx.beginPath();
|
2015-04-10 18:47:11 +00:00
|
|
|
highlightedNodes.forEach(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.moveTo(d.x + 5 * nodeRadius, d.y);
|
|
|
|
ctx.arc(d.x, d.y, 5 * nodeRadius, 0, 2 * Math.PI);
|
|
|
|
});
|
|
|
|
ctx.fill();
|
2015-07-30 21:47:58 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.restore();
|
2015-07-30 21:47:58 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 09:42:25 +00:00
|
|
|
// -- draw link highlights --
|
2015-07-30 21:47:58 +00:00
|
|
|
if (highlightedLinks.length) {
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.save();
|
|
|
|
ctx.lineWidth = 2 * 5 * nodeRadius;
|
2017-01-29 23:51:08 +00:00
|
|
|
ctx.shadowColor = 'rgba(255, 255, 255, 1.0)';
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.shadowBlur = 10 * nodeRadius;
|
|
|
|
ctx.shadowOffsetX = 0;
|
|
|
|
ctx.shadowOffsetY = 0;
|
2017-01-29 23:51:08 +00:00
|
|
|
ctx.globalCompositeOperation = 'lighten';
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.strokeStyle = highlightColor;
|
2017-01-29 23:51:08 +00:00
|
|
|
ctx.lineCap = 'round';
|
2016-05-22 11:23:43 +00:00
|
|
|
|
|
|
|
ctx.beginPath();
|
2015-07-30 21:47:58 +00:00
|
|
|
highlightedLinks.forEach(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.moveTo(d.source.x, d.source.y);
|
|
|
|
ctx.lineTo(d.target.x, d.target.y);
|
|
|
|
});
|
|
|
|
ctx.stroke();
|
2015-04-10 18:47:11 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.restore();
|
2015-04-10 18:47:11 +00:00
|
|
|
}
|
|
|
|
|
2015-08-01 22:34:20 +00:00
|
|
|
// -- draw labels --
|
2016-05-22 12:51:30 +00:00
|
|
|
if (scale > 0.9) {
|
2016-05-22 11:23:43 +00:00
|
|
|
intNodes.filter(visibleNodes).forEach(drawLabel, scale);
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-08-01 22:34:20 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.restore();
|
2015-04-10 11:50:39 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 15:22:36 +00:00
|
|
|
function tickEvent() {
|
2016-05-22 11:23:43 +00:00
|
|
|
redraw();
|
2015-04-10 18:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function resizeCanvas() {
|
2016-05-22 11:23:43 +00:00
|
|
|
var r = window.devicePixelRatio;
|
|
|
|
canvas.width = el.offsetWidth * r;
|
|
|
|
canvas.height = el.offsetHeight * r;
|
2017-01-29 23:51:08 +00:00
|
|
|
canvas.style.width = el.offsetWidth + 'px';
|
|
|
|
canvas.style.height = el.offsetHeight + 'px';
|
2016-05-22 11:23:43 +00:00
|
|
|
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
|
|
ctx.scale(r, r);
|
|
|
|
requestAnimationFrame(redraw);
|
2015-03-31 15:22:36 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 23:29:17 +00:00
|
|
|
function distance(a, b) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2);
|
2015-04-15 23:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function distancePoint(a, b) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return Math.sqrt(distance(a, b));
|
2015-04-15 23:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function distanceLink(p, a, b) {
|
|
|
|
/* http://stackoverflow.com/questions/849211 */
|
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var l2 = distance(a, b);
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
if (l2 === 0) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return distance(p, a);
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var t = ((p.x - a.x) * (b.x - a.x) + (p.y - a.y) * (b.y - a.y)) / l2;
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
if (t < 0) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return distance(p, a);
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
if (t > 1) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return distance(p, b);
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
return Math.sqrt(distance(p, {
|
|
|
|
x: a.x + t * (b.x - a.x),
|
|
|
|
y: a.y + t * (b.y - a.y)
|
|
|
|
}));
|
2015-04-15 23:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function translateXY(d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
var translate = zoomBehavior.translate();
|
|
|
|
var scale = zoomBehavior.scale();
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
return {
|
|
|
|
x: (d[0] - translate[0]) / scale,
|
|
|
|
y: (d[1] - translate[1]) / scale
|
|
|
|
};
|
2015-04-15 23:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onClick() {
|
2016-05-22 12:51:30 +00:00
|
|
|
if (d3.event.defaultPrevented) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return;
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var e = translateXY(d3.mouse(el));
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
var n = intNodes.filter(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return distancePoint(e, d) < NODE_RADIUS;
|
|
|
|
});
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
if (n.length > 0) {
|
|
|
|
router.node(n[0].o.node)();
|
2016-05-22 11:23:43 +00:00
|
|
|
return;
|
2015-04-15 23:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var links = intLinks.filter(function (d) {
|
2017-01-29 23:51:08 +00:00
|
|
|
return d.o.type !== 'fastd' && d.o.type !== 'L2TP';
|
2015-04-22 00:44:07 +00:00
|
|
|
}).filter(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return distanceLink(e, d.source, d.target) < LINE_RADIUS;
|
|
|
|
});
|
2015-04-15 23:29:17 +00:00
|
|
|
|
|
|
|
if (links.length > 0) {
|
2016-05-22 11:23:43 +00:00
|
|
|
router.link(links[0].o)();
|
2015-04-15 23:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-08 13:17:09 +00:00
|
|
|
function zoom(z, scale) {
|
2016-05-22 11:23:43 +00:00
|
|
|
var size = getSize();
|
|
|
|
var newSize = [size[0] / scale, size[1] / scale];
|
2015-07-08 13:17:09 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var sidebarWidth = sidebar();
|
|
|
|
var delta = [size[0] - newSize[0], size[1] - newSize[1]];
|
|
|
|
var translate = z.translate();
|
|
|
|
var translateNew = [sidebarWidth + (translate[0] - sidebarWidth - delta[0] / 2) * scale, (translate[1] - delta[1] / 2) * scale];
|
2015-07-08 13:17:09 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
animatePanzoom(translateNew, z.scale() * scale);
|
2015-07-08 13:17:09 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 08:19:38 +00:00
|
|
|
function keyboardZoom(z) {
|
|
|
|
return function () {
|
2016-05-22 11:23:43 +00:00
|
|
|
var e = d3.event;
|
2015-07-07 08:19:38 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
if (e.altKey || e.ctrlKey || e.metaKey) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return;
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-07-07 08:19:38 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
if (e.keyCode === 43) {
|
2016-05-22 11:23:43 +00:00
|
|
|
zoom(z, 1.41);
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-07-07 08:19:38 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
if (e.keyCode === 45) {
|
2016-05-22 11:23:43 +00:00
|
|
|
zoom(z, 1 / 1.41);
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2016-05-22 11:23:43 +00:00
|
|
|
};
|
2015-07-07 08:19:38 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
el = document.createElement('div');
|
|
|
|
el.classList.add('graph');
|
2015-03-31 15:22:36 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
font = window.getComputedStyle(el).fontSize + ' ' + window.getComputedStyle(el).fontFamily;
|
2016-06-19 11:35:35 +00:00
|
|
|
|
2015-03-31 19:18:06 +00:00
|
|
|
zoomBehavior = d3.behavior.zoom()
|
2016-05-22 12:51:30 +00:00
|
|
|
.scaleExtent([1 / 3, 3])
|
2017-01-29 23:51:08 +00:00
|
|
|
.on('zoom', onPanZoom)
|
2016-05-22 12:51:30 +00:00
|
|
|
.translate([sidebar(), 0]);
|
2015-03-31 21:24:24 +00:00
|
|
|
|
2015-04-15 23:29:17 +00:00
|
|
|
canvas = d3.select(el)
|
2017-01-29 23:51:08 +00:00
|
|
|
.attr('tabindex', 1)
|
|
|
|
.on('keypress', keyboardZoom(zoomBehavior))
|
2016-05-22 12:51:30 +00:00
|
|
|
.call(zoomBehavior)
|
2017-01-29 23:51:08 +00:00
|
|
|
.append('canvas')
|
|
|
|
.on('click', onClick)
|
2016-05-22 12:51:30 +00:00
|
|
|
.call(draggableNode)
|
|
|
|
.node();
|
2015-03-31 15:22:36 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
ctx = canvas.getContext('2d');
|
2015-03-31 15:22:36 +00:00
|
|
|
|
|
|
|
force = d3.layout.force()
|
2016-05-22 12:51:30 +00:00
|
|
|
.charge(-250)
|
|
|
|
.gravity(0.1)
|
|
|
|
.linkDistance(function (d) {
|
2017-01-29 23:51:08 +00:00
|
|
|
if (d.o.type === 'fastd' || d.o.type === 'L2TP') {
|
2016-05-22 12:51:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-01-29 23:51:08 +00:00
|
|
|
return LINK_DISTANCE;
|
2016-05-22 12:51:30 +00:00
|
|
|
})
|
|
|
|
.linkStrength(function (d) {
|
2017-01-29 23:51:08 +00:00
|
|
|
if (d.o.type === 'fastd' || d.o.type === 'L2TP') {
|
2016-06-08 22:20:10 +00:00
|
|
|
return 0.02;
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2017-01-29 23:51:08 +00:00
|
|
|
return Math.max(0.5, 1 / d.o.tq);
|
2016-05-22 12:51:30 +00:00
|
|
|
})
|
2017-01-29 23:51:08 +00:00
|
|
|
.on('tick', tickEvent)
|
|
|
|
.on('end', savePositions);
|
2015-03-31 15:22:36 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
window.addEventListener('resize', resizeCanvas);
|
2015-04-10 18:47:11 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
panzoom();
|
2015-03-31 21:24:24 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
self.setData = function setData(data) {
|
2016-05-22 11:23:43 +00:00
|
|
|
var oldNodes = {};
|
2015-03-31 23:06:53 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
intNodes.forEach(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
oldNodes[d.o.id] = d;
|
|
|
|
});
|
2015-04-01 13:59:05 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
intNodes = data.graph.nodes.map(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
var e;
|
2016-05-22 12:51:30 +00:00
|
|
|
if (d.id in oldNodes) {
|
2016-05-22 11:23:43 +00:00
|
|
|
e = oldNodes[d.id];
|
2016-05-22 12:51:30 +00:00
|
|
|
} else {
|
2016-05-22 11:23:43 +00:00
|
|
|
e = {};
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-04-03 00:32:32 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
e.o = d;
|
2015-03-31 23:06:53 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
return e;
|
|
|
|
});
|
2015-04-03 00:32:32 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var newNodesDict = {};
|
2015-04-03 00:32:32 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
intNodes.forEach(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
newNodesDict[d.o.id] = d;
|
|
|
|
});
|
2015-04-03 00:32:32 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var oldLinks = {};
|
2015-04-07 23:21:52 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
intLinks.forEach(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
oldLinks[d.o.id] = d;
|
|
|
|
});
|
2015-04-07 23:21:52 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
intLinks = data.graph.links.map(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
var e;
|
2016-05-22 12:51:30 +00:00
|
|
|
if (d.id in oldLinks) {
|
2016-05-22 11:23:43 +00:00
|
|
|
e = oldLinks[d.id];
|
2016-05-22 12:51:30 +00:00
|
|
|
} else {
|
2016-05-22 11:23:43 +00:00
|
|
|
e = {};
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-04-07 23:21:52 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
e.o = d;
|
|
|
|
e.source = newNodesDict[d.source.id];
|
|
|
|
e.target = newNodesDict[d.target.id];
|
2016-06-08 22:20:10 +00:00
|
|
|
e.color = linkScale(d.tq).hex();
|
2015-04-03 00:32:32 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
return e;
|
|
|
|
});
|
2015-03-31 15:22:36 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
linksDict = {};
|
|
|
|
nodesDict = {};
|
2015-04-10 13:19:42 +00:00
|
|
|
|
2015-04-10 11:50:39 +00:00
|
|
|
intNodes.forEach(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
d.neighbours = {};
|
2015-04-10 13:19:42 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
if (d.o.node) {
|
2016-05-22 11:23:43 +00:00
|
|
|
nodesDict[d.o.node.nodeinfo.node_id] = d;
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2016-05-22 11:23:43 +00:00
|
|
|
|
|
|
|
var name = nodeName(d);
|
|
|
|
|
|
|
|
var offset = 5;
|
|
|
|
var lineWidth = 3;
|
2017-01-29 23:51:08 +00:00
|
|
|
var buffer = document.createElement('canvas');
|
2016-05-22 11:23:43 +00:00
|
|
|
var r = window.devicePixelRatio;
|
2017-01-29 23:51:08 +00:00
|
|
|
var bctx = buffer.getContext('2d');
|
2016-06-19 11:35:35 +00:00
|
|
|
bctx.font = font;
|
2016-05-22 11:23:43 +00:00
|
|
|
var width = bctx.measureText(name).width;
|
|
|
|
var scale = zoomBehavior.scaleExtent()[1] * r;
|
|
|
|
buffer.width = (width + 2 * lineWidth) * scale;
|
|
|
|
buffer.height = (16 + 2 * lineWidth) * scale;
|
2016-06-19 11:35:35 +00:00
|
|
|
bctx.font = font;
|
2016-05-22 11:23:43 +00:00
|
|
|
bctx.scale(scale, scale);
|
2017-01-29 23:51:08 +00:00
|
|
|
bctx.textBaseline = 'middle';
|
|
|
|
bctx.textAlign = 'center';
|
|
|
|
bctx.fillStyle = 'rgba(242, 227, 198, 1.0)';
|
|
|
|
bctx.shadowColor = 'rgba(0, 0, 0, 1)';
|
2016-05-22 11:23:43 +00:00
|
|
|
bctx.shadowBlur = 5;
|
|
|
|
bctx.fillText(name, buffer.width / (2 * scale), buffer.height / (2 * scale));
|
|
|
|
|
|
|
|
d.label = buffer;
|
|
|
|
d.labelWidth = buffer.width / scale;
|
|
|
|
d.labelHeight = buffer.height / scale;
|
|
|
|
d.labelA = offset + buffer.width / (2 * scale);
|
|
|
|
d.labelB = offset + buffer.height / (2 * scale);
|
|
|
|
});
|
2015-03-31 15:22:36 +00:00
|
|
|
|
2015-04-10 11:50:39 +00:00
|
|
|
intNodes.forEach(function (d) {
|
|
|
|
d.neighbours = Object.keys(d.neighbours).map(function (k) {
|
2016-05-22 11:23:43 +00:00
|
|
|
return d.neighbours[k];
|
|
|
|
});
|
|
|
|
});
|
2015-03-31 15:22:36 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
nodes = intNodes.filter(function (d) {
|
|
|
|
return !d.o.unseen && d.o.node;
|
|
|
|
});
|
|
|
|
uplinkNodes = nodes.filter(function (d) {
|
|
|
|
return d.o.node.flags.uplink;
|
|
|
|
});
|
|
|
|
nonUplinkNodes = nodes.filter(function (d) {
|
|
|
|
return !d.o.node.flags.uplink;
|
|
|
|
});
|
|
|
|
unseenNodes = intNodes.filter(function (d) {
|
|
|
|
return d.o.unseen && d.o.node;
|
|
|
|
});
|
|
|
|
unknownNodes = intNodes.filter(function (d) {
|
|
|
|
return !d.o.node;
|
|
|
|
});
|
2015-04-10 19:53:40 +00:00
|
|
|
|
2016-05-26 16:37:24 +00:00
|
|
|
if (helper.localStorageTest()) {
|
2017-01-29 23:51:08 +00:00
|
|
|
var save = JSON.parse(localStorage.getItem('graph/nodeposition'));
|
2015-04-03 00:32:32 +00:00
|
|
|
|
|
|
|
if (save) {
|
2016-05-22 11:23:43 +00:00
|
|
|
var nodePositions = {};
|
2016-05-22 12:51:30 +00:00
|
|
|
save.forEach(function (d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
nodePositions[d.id] = d;
|
|
|
|
});
|
2015-04-03 00:32:32 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
intNodes.forEach(function (d) {
|
2015-04-10 11:50:39 +00:00
|
|
|
if (nodePositions[d.o.id] && (d.x === undefined || d.y === undefined)) {
|
2016-05-22 11:23:43 +00:00
|
|
|
d.x = nodePositions[d.o.id].x;
|
|
|
|
d.y = nodePositions[d.o.id].y;
|
2015-04-03 00:32:32 +00:00
|
|
|
}
|
2016-05-22 11:23:43 +00:00
|
|
|
});
|
2015-03-31 23:06:53 +00:00
|
|
|
}
|
2015-04-03 00:32:32 +00:00
|
|
|
}
|
2015-03-31 15:22:36 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var diameter = graphDiameter(intNodes);
|
2015-04-01 16:21:54 +00:00
|
|
|
|
2016-03-05 10:49:10 +00:00
|
|
|
force.nodes(intNodes)
|
2016-05-22 12:51:30 +00:00
|
|
|
.links(intLinks)
|
|
|
|
.size([diameter, diameter]);
|
2015-04-03 00:32:32 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
updateHighlight(true);
|
2015-04-06 09:37:52 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
force.start();
|
|
|
|
resizeCanvas();
|
|
|
|
};
|
2015-03-31 15:22:36 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
self.resetView = function resetView() {
|
2016-05-22 11:23:43 +00:00
|
|
|
highlight = undefined;
|
|
|
|
updateHighlight();
|
|
|
|
doAnimation = true;
|
|
|
|
};
|
2015-03-31 15:22:36 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
self.gotoNode = function gotoNode(d) {
|
|
|
|
highlight = { type: 'node', o: d };
|
2016-05-22 11:23:43 +00:00
|
|
|
updateHighlight();
|
|
|
|
doAnimation = true;
|
|
|
|
};
|
2015-03-31 15:22:36 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
self.gotoLink = function gotoLink(d) {
|
|
|
|
highlight = { type: 'link', o: d };
|
2016-05-22 11:23:43 +00:00
|
|
|
updateHighlight();
|
|
|
|
doAnimation = true;
|
|
|
|
};
|
2015-03-31 15:22:36 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
self.destroy = function destroy() {
|
2016-05-22 11:23:43 +00:00
|
|
|
force.stop();
|
|
|
|
canvas.remove();
|
|
|
|
force = null;
|
2015-07-11 22:11:18 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
if (el.parentNode) {
|
2016-05-22 11:23:43 +00:00
|
|
|
el.parentNode.removeChild(el);
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2016-05-22 11:23:43 +00:00
|
|
|
};
|
2015-07-11 22:11:18 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
self.render = function render(d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
d.appendChild(el);
|
|
|
|
resizeCanvas();
|
|
|
|
updateHighlight();
|
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
|
|
|
};
|
|
|
|
});
|