2017-10-29 18:04:02 +00:00
|
|
|
define(['d3-selection', 'd3-force', 'd3-zoom', 'd3-drag', 'd3-timer', 'd3-ease', 'd3-interpolate', 'utils/math', 'forcegraph/draw'],
|
|
|
|
function (d3Selection, d3Force, d3Zoom, d3Drag, d3Timer, d3Ease, d3Interpolate, math, draw) {
|
2017-02-23 21:32:25 +00:00
|
|
|
'use strict';
|
2015-03-31 15:22:36 +00:00
|
|
|
|
2017-11-03 21:07:22 +00:00
|
|
|
return function (linkScale, sidebar) {
|
2017-02-23 21:32:25 +00:00
|
|
|
var self = this;
|
|
|
|
var el;
|
|
|
|
var canvas;
|
|
|
|
var ctx;
|
|
|
|
var force;
|
|
|
|
var forceLink;
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
var transform = d3Zoom.zoomIdentity;
|
|
|
|
var intNodes = [];
|
|
|
|
var dictNodes = {};
|
|
|
|
var intLinks = [];
|
2017-04-14 18:43:03 +00:00
|
|
|
var movetoTimer;
|
2017-10-30 02:14:08 +00:00
|
|
|
var initial = 1.8;
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
var NODE_RADIUS_DRAG = 10;
|
|
|
|
var NODE_RADIUS_SELECT = 15;
|
|
|
|
var LINK_RADIUS_SELECT = 12;
|
2017-04-14 18:43:03 +00:00
|
|
|
var ZOOM_ANIMATE_DURATION = 350;
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
var ZOOM_MIN = 1 / 8;
|
|
|
|
var ZOOM_MAX = 3;
|
2015-07-08 16:03:19 +00:00
|
|
|
|
2017-10-30 02:14:08 +00:00
|
|
|
var FORCE_ALPHA = 0.01;
|
2015-03-31 15:22:36 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
draw.setTransform(transform);
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
function resizeCanvas() {
|
|
|
|
canvas.width = el.offsetWidth;
|
|
|
|
canvas.height = el.offsetHeight;
|
|
|
|
draw.setMaxArea(canvas.width, canvas.height);
|
2015-04-15 23:29:17 +00:00
|
|
|
}
|
2015-03-31 21:45:32 +00:00
|
|
|
|
2017-04-14 18:43:03 +00:00
|
|
|
function transformPosition(p) {
|
|
|
|
transform.x = p.x;
|
|
|
|
transform.y = p.y;
|
|
|
|
transform.k = p.k;
|
|
|
|
}
|
|
|
|
|
|
|
|
function moveTo(callback, forceMove) {
|
|
|
|
clearTimeout(movetoTimer);
|
|
|
|
if (!forceMove && force.alpha() > 0.3) {
|
|
|
|
movetoTimer = setTimeout(function timerOfMoveTo() {
|
|
|
|
moveTo(callback);
|
|
|
|
}, 300);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var result = callback();
|
|
|
|
var x = result[0];
|
|
|
|
var y = result[1];
|
|
|
|
var k = result[2];
|
|
|
|
var end = { k: k };
|
|
|
|
|
2017-12-25 15:12:44 +00:00
|
|
|
end.x = (canvas.width + sidebar.getWidth()) / 2 - x * k;
|
2017-04-14 18:43:03 +00:00
|
|
|
end.y = canvas.height / 2 - y * k;
|
|
|
|
|
|
|
|
var start = { x: transform.x, y: transform.y, k: transform.k };
|
|
|
|
|
|
|
|
var interpolate = d3Interpolate.interpolateObject(start, end);
|
|
|
|
|
|
|
|
var timer = d3Timer.timer(function (t) {
|
|
|
|
if (t >= ZOOM_ANIMATE_DURATION) {
|
|
|
|
timer.stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var v = interpolate(d3Ease.easeQuadInOut(t / ZOOM_ANIMATE_DURATION));
|
|
|
|
transformPosition(v);
|
|
|
|
window.requestAnimationFrame(redraw);
|
|
|
|
});
|
2015-04-10 18:47:11 +00:00
|
|
|
}
|
2015-04-10 19:53:40 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
function onClick() {
|
|
|
|
if (d3Selection.event.defaultPrevented) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-31 19:18:06 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
var e = transform.invert([d3Selection.event.clientX, d3Selection.event.clientY]);
|
|
|
|
var n = force.find(e[0], e[1], NODE_RADIUS_SELECT);
|
2015-03-31 19:29:47 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
if (n !== undefined) {
|
2017-10-29 14:11:24 +00:00
|
|
|
router.fullUrl({ node: n.o.node_id });
|
2017-02-23 21:32:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-04-10 18:47:11 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
e = { x: e[0], y: e[1] };
|
2015-04-06 09:37:52 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
var closedLink;
|
|
|
|
var radius = LINK_RADIUS_SELECT;
|
|
|
|
intLinks
|
|
|
|
.forEach(function (d) {
|
|
|
|
var distance = math.distanceLink(e, d.source, d.target);
|
|
|
|
if (distance < radius) {
|
|
|
|
closedLink = d;
|
|
|
|
radius = distance;
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2017-02-23 21:32:25 +00:00
|
|
|
});
|
2015-04-06 09:37:52 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
if (closedLink !== undefined) {
|
2017-03-05 11:29:21 +00:00
|
|
|
router.fullUrl({ link: closedLink.o.id });
|
2015-04-06 09:37:52 +00:00
|
|
|
}
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-04-06 09:37:52 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
function redraw() {
|
|
|
|
ctx.save();
|
|
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
ctx.translate(transform.x, transform.y);
|
|
|
|
ctx.scale(transform.k, transform.k);
|
2015-04-10 11:50:39 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
intLinks.forEach(draw.drawLink);
|
|
|
|
intNodes.forEach(draw.drawNode);
|
2015-04-10 11:50:39 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
ctx.restore();
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-04-10 11:50:39 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
el = document.createElement('div');
|
|
|
|
el.classList.add('graph');
|
2015-04-19 00:16:06 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
forceLink = d3Force.forceLink()
|
|
|
|
.distance(function (d) {
|
2017-10-31 13:22:00 +00:00
|
|
|
if (d.o.type.indexOf('vpn') === 0) {
|
2017-02-23 21:32:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 75;
|
|
|
|
})
|
|
|
|
.strength(function (d) {
|
2017-10-31 13:22:00 +00:00
|
|
|
if (d.o.type.indexOf('vpn') === 0) {
|
2017-02-23 21:32:25 +00:00
|
|
|
return 0.02;
|
|
|
|
}
|
2017-10-29 14:11:24 +00:00
|
|
|
return Math.max(0.5, d.o.source_tq);
|
2017-02-07 23:14:48 +00:00
|
|
|
});
|
2016-05-22 11:23:43 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
var zoom = d3Zoom.zoom()
|
|
|
|
.scaleExtent([ZOOM_MIN, ZOOM_MAX])
|
|
|
|
.on('zoom', function () {
|
|
|
|
transform = d3Selection.event.transform;
|
|
|
|
draw.setTransform(transform);
|
|
|
|
redraw();
|
2016-05-22 11:23:43 +00:00
|
|
|
});
|
2015-07-30 21:47:58 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
force = d3Force.forceSimulation()
|
|
|
|
.force('link', forceLink)
|
|
|
|
.force('charge', d3Force.forceManyBody())
|
|
|
|
.force('x', d3Force.forceX().strength(0.02))
|
|
|
|
.force('y', d3Force.forceY().strength(0.02))
|
|
|
|
.force('collide', d3Force.forceCollide())
|
2017-04-14 18:43:03 +00:00
|
|
|
.on('tick', redraw)
|
2017-10-30 02:14:08 +00:00
|
|
|
.alphaDecay(0.025);
|
2017-02-23 21:32:25 +00:00
|
|
|
|
|
|
|
var drag = d3Drag.drag()
|
|
|
|
.subject(function () {
|
|
|
|
var e = transform.invert([d3Selection.event.x, d3Selection.event.y]);
|
|
|
|
var n = force.find(e[0], e[1], NODE_RADIUS_DRAG);
|
|
|
|
|
|
|
|
if (n !== undefined) {
|
|
|
|
n.x = d3Selection.event.x;
|
|
|
|
n.y = d3Selection.event.y;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
})
|
|
|
|
.on('start', function () {
|
|
|
|
if (!d3Selection.event.active) {
|
|
|
|
force.alphaTarget(FORCE_ALPHA).restart();
|
|
|
|
}
|
|
|
|
d3Selection.event.subject.fx = transform.invertX(d3Selection.event.subject.x);
|
|
|
|
d3Selection.event.subject.fy = transform.invertY(d3Selection.event.subject.y);
|
|
|
|
})
|
|
|
|
.on('drag', function () {
|
|
|
|
d3Selection.event.subject.fx = transform.invertX(d3Selection.event.x);
|
|
|
|
d3Selection.event.subject.fy = transform.invertY(d3Selection.event.y);
|
|
|
|
})
|
|
|
|
.on('end', function () {
|
|
|
|
if (!d3Selection.event.active) {
|
|
|
|
force.alphaTarget(0);
|
|
|
|
}
|
|
|
|
d3Selection.event.subject.fx = null;
|
|
|
|
d3Selection.event.subject.fy = null;
|
2016-05-22 11:23:43 +00:00
|
|
|
});
|
2015-04-10 18:47:11 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
canvas = d3Selection.select(el)
|
|
|
|
.append('canvas')
|
|
|
|
.on('click', onClick)
|
|
|
|
.call(drag)
|
|
|
|
.call(zoom)
|
|
|
|
.node();
|
|
|
|
|
|
|
|
ctx = canvas.getContext('2d');
|
|
|
|
draw.setCTX(ctx);
|
|
|
|
|
|
|
|
window.addEventListener('resize', function () {
|
|
|
|
resizeCanvas();
|
|
|
|
redraw();
|
|
|
|
});
|
|
|
|
|
|
|
|
self.setData = function setData(data) {
|
2017-10-31 12:32:39 +00:00
|
|
|
intNodes = data.nodes.online.map(function (d) {
|
2017-10-29 10:21:55 +00:00
|
|
|
var e = dictNodes[d.node_id];
|
|
|
|
if (!e) {
|
2017-02-23 21:32:25 +00:00
|
|
|
e = {};
|
2017-10-29 14:11:24 +00:00
|
|
|
dictNodes[d.node_id] = e;
|
2017-02-23 21:32:25 +00:00
|
|
|
}
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
e.o = d;
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
return e;
|
2017-10-30 02:14:08 +00:00
|
|
|
});
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2017-10-29 10:21:55 +00:00
|
|
|
intLinks = data.links.filter(function (d) {
|
2017-10-31 12:32:39 +00:00
|
|
|
return data.nodeDict[d.source.node_id].is_online && data.nodeDict[d.target.node_id].is_online;
|
2017-10-29 10:21:55 +00:00
|
|
|
}).map(function (d) {
|
|
|
|
return {
|
|
|
|
o: d,
|
|
|
|
source: dictNodes[d.source.node_id],
|
|
|
|
target: dictNodes[d.target.node_id],
|
|
|
|
color: linkScale(d.source_tq),
|
|
|
|
color_to: linkScale(d.target_tq)
|
|
|
|
};
|
2017-02-23 21:32:25 +00:00
|
|
|
});
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
force.nodes(intNodes);
|
|
|
|
forceLink.links(intLinks);
|
2015-04-15 23:29:17 +00:00
|
|
|
|
2017-10-30 02:14:08 +00:00
|
|
|
force.alpha(initial).velocityDecay(0.15).restart();
|
|
|
|
if (initial === 1.8) {
|
|
|
|
initial = 0.5;
|
|
|
|
}
|
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
resizeCanvas();
|
2016-05-22 12:51:30 +00:00
|
|
|
};
|
2015-07-07 08:19:38 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
self.resetView = function resetView() {
|
2017-04-14 18:43:03 +00:00
|
|
|
moveTo(function calcToReset() {
|
|
|
|
draw.setHighlight(null);
|
2017-12-30 19:01:00 +00:00
|
|
|
return [0, 0, (ZOOM_MIN + config.forceGraph.zoomModifier) / 2];
|
2017-04-14 18:43:03 +00:00
|
|
|
}, true);
|
2016-05-22 11:23:43 +00:00
|
|
|
};
|
2015-03-31 21:24:24 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
self.gotoNode = function gotoNode(d) {
|
2017-04-14 18:43:03 +00:00
|
|
|
moveTo(function calcToNode() {
|
2017-11-01 10:43:20 +00:00
|
|
|
draw.setHighlight({ type: 'node', id: d.node_id });
|
|
|
|
var n = dictNodes[d.node_id];
|
|
|
|
if (n) {
|
2017-04-14 18:43:03 +00:00
|
|
|
return [n.x, n.y, (ZOOM_MAX + 1) / 2];
|
2017-02-23 21:32:25 +00:00
|
|
|
}
|
2017-12-30 19:01:00 +00:00
|
|
|
return resetView();
|
2017-04-14 18:43:03 +00:00
|
|
|
});
|
2017-02-23 21:32:25 +00:00
|
|
|
};
|
2015-04-03 00:32:32 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
self.gotoLink = function gotoLink(d) {
|
2017-04-14 18:43:03 +00:00
|
|
|
moveTo(function calcToLink() {
|
2017-11-14 16:37:20 +00:00
|
|
|
draw.setHighlight({ type: 'link', id: d[0].id });
|
2017-11-01 10:43:20 +00:00
|
|
|
var l = intLinks.find(function (link) {
|
2017-11-14 16:37:20 +00:00
|
|
|
return link.o.id === d[0].id;
|
2017-11-01 10:43:20 +00:00
|
|
|
});
|
|
|
|
if (l) {
|
2017-04-14 18:43:03 +00:00
|
|
|
return [(l.source.x + l.target.x) / 2, (l.source.y + l.target.y) / 2, (ZOOM_MAX / 2) + ZOOM_MIN];
|
2017-02-23 21:32:25 +00:00
|
|
|
}
|
2017-12-30 19:01:00 +00:00
|
|
|
return resetView();
|
2017-04-14 18:43:03 +00:00
|
|
|
});
|
2017-02-23 21:32:25 +00:00
|
|
|
};
|
2015-04-07 23:21:52 +00:00
|
|
|
|
2017-03-05 11:29:21 +00:00
|
|
|
self.gotoLocation = function gotoLocation() {
|
|
|
|
// ignore
|
|
|
|
};
|
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
self.destroy = function destroy() {
|
|
|
|
force.stop();
|
2017-12-27 18:52:47 +00:00
|
|
|
canvas.parentNode.removeChild(canvas);
|
2017-02-23 21:32:25 +00:00
|
|
|
force = null;
|
2015-04-10 13:19:42 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
if (el.parentNode) {
|
|
|
|
el.parentNode.removeChild(el);
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2017-02-23 21:32:25 +00:00
|
|
|
};
|
2016-05-22 11:23:43 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
self.render = function render(d) {
|
|
|
|
d.appendChild(el);
|
|
|
|
resizeCanvas();
|
|
|
|
};
|
2015-07-11 22:11:18 +00:00
|
|
|
|
2017-02-23 21:32:25 +00:00
|
|
|
return self;
|
2016-05-22 11:23:43 +00:00
|
|
|
};
|
2017-02-23 21:32:25 +00:00
|
|
|
});
|