2015-03-31 17:22:36 +02:00
|
|
|
define(["d3"], function (d3) {
|
2015-04-10 21:53:40 +02:00
|
|
|
var margin = 200
|
2015-04-16 01:29:17 +02:00
|
|
|
var NODE_RADIUS = 15
|
|
|
|
var LINE_RADIUS = 12
|
2015-04-10 21:53:40 +02:00
|
|
|
|
|
|
|
return function (config, linkScale, sidebar, router) {
|
2015-03-31 17:22:36 +02:00
|
|
|
var self = this
|
2015-04-16 01:29:17 +02:00
|
|
|
var canvas, ctx, screenRect
|
2015-03-31 17:22:36 +02:00
|
|
|
var nodesDict, linksDict
|
2015-03-31 21:18:06 +02:00
|
|
|
var zoomBehavior
|
2015-03-31 17:22:36 +02:00
|
|
|
var force
|
2015-03-31 21:31:06 +02:00
|
|
|
var el
|
2015-03-31 23:45:32 +02:00
|
|
|
var doAnimation = false
|
2015-04-03 02:32:32 +02:00
|
|
|
var intNodes = []
|
2015-04-08 01:21:52 +02:00
|
|
|
var intLinks = []
|
2015-04-06 11:37:52 +02:00
|
|
|
var highlight
|
2015-04-10 20:47:11 +02:00
|
|
|
var highlightedNodes = []
|
|
|
|
var highlightedLinks = []
|
2015-04-10 21:53:40 +02:00
|
|
|
var nodes = []
|
|
|
|
var unknownNodes = []
|
2015-04-19 16:29:22 +02:00
|
|
|
var savedPanZoom
|
2015-03-31 17:22:36 +02:00
|
|
|
|
2015-04-16 01:29:17 +02:00
|
|
|
var draggedNode
|
|
|
|
|
2015-04-01 00:06:17 +02:00
|
|
|
var LINK_DISTANCE = 70
|
|
|
|
|
2015-04-01 18:21:54 +02:00
|
|
|
function graphDiameter(nodes) {
|
2015-04-10 13:50:39 +02:00
|
|
|
return Math.sqrt(nodes.length / Math.PI) * LINK_DISTANCE * 1.41
|
2015-04-01 18:21:54 +02:00
|
|
|
}
|
|
|
|
|
2015-04-01 01:06:53 +02:00
|
|
|
function savePositions() {
|
2015-04-01 15:59:05 +02:00
|
|
|
if (!localStorageTest())
|
|
|
|
return
|
|
|
|
|
2015-04-03 02:32:32 +02:00
|
|
|
var save = intNodes.map( function (d) {
|
|
|
|
return { id: d.o.id, x: d.x, y: d.y }
|
2015-04-01 01:06:53 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
localStorage.setItem("graph/nodeposition", JSON.stringify(save))
|
|
|
|
}
|
|
|
|
|
2015-03-31 17:22:36 +02:00
|
|
|
function nodeName(d) {
|
2015-04-03 02:32:32 +02:00
|
|
|
if (d.o.node && d.o.node.nodeinfo)
|
|
|
|
return d.o.node.nodeinfo.hostname
|
2015-03-31 17:22:36 +02:00
|
|
|
else
|
2015-04-03 02:32:32 +02:00
|
|
|
return d.o.id
|
2015-03-31 17:22:36 +02:00
|
|
|
}
|
|
|
|
|
2015-04-16 01:29:17 +02:00
|
|
|
function dragstart() {
|
|
|
|
var e = translateXY(d3.mouse(el))
|
|
|
|
|
|
|
|
var nodes = intNodes.filter(function (d) {
|
|
|
|
return distancePoint(e, d) < NODE_RADIUS
|
|
|
|
})
|
|
|
|
|
|
|
|
if (nodes.length === 0)
|
|
|
|
return
|
|
|
|
|
|
|
|
draggedNode = nodes[0]
|
2015-03-31 17:22:36 +02:00
|
|
|
d3.event.sourceEvent.stopPropagation()
|
2015-04-16 01:29:17 +02:00
|
|
|
d3.event.sourceEvent.preventDefault()
|
|
|
|
draggedNode.fixed |= 2
|
2015-03-31 17:22:36 +02:00
|
|
|
}
|
|
|
|
|
2015-04-16 01:29:17 +02:00
|
|
|
function dragmove() {
|
|
|
|
if (draggedNode) {
|
|
|
|
var e = translateXY(d3.mouse(el))
|
|
|
|
|
|
|
|
draggedNode.px = e.x
|
|
|
|
draggedNode.py = e.y
|
|
|
|
force.resume()
|
|
|
|
}
|
2015-03-31 17:22:36 +02:00
|
|
|
}
|
|
|
|
|
2015-04-16 01:29:17 +02:00
|
|
|
function dragend() {
|
|
|
|
if (draggedNode) {
|
|
|
|
d3.event.sourceEvent.stopPropagation()
|
|
|
|
d3.event.sourceEvent.preventDefault()
|
|
|
|
draggedNode.fixed &= 1
|
|
|
|
draggedNode = undefined
|
|
|
|
}
|
2015-03-31 17:22:36 +02:00
|
|
|
}
|
|
|
|
|
2015-04-10 13:50:39 +02:00
|
|
|
var draggableNode = d3.behavior.drag()
|
|
|
|
.on("dragstart", dragstart)
|
|
|
|
.on("drag", dragmove)
|
|
|
|
.on("dragend", dragend)
|
|
|
|
|
2015-03-31 21:31:06 +02:00
|
|
|
function animatePanzoom(translate, scale) {
|
2015-04-10 20:47:11 +02:00
|
|
|
var translateP = zoomBehavior.translate()
|
|
|
|
var scaleP = zoomBehavior.scale()
|
2015-03-31 21:31:06 +02:00
|
|
|
|
2015-04-10 20:47:11 +02:00
|
|
|
if (!doAnimation) {
|
|
|
|
zoomBehavior.translate(translate)
|
|
|
|
zoomBehavior.scale(scale)
|
|
|
|
panzoom()
|
|
|
|
} else {
|
|
|
|
var start = {x: translateP[0], y: translateP[1], scale: scaleP}
|
|
|
|
var end = {x: translate[0], y: translate[1], scale: scale}
|
2015-03-31 23:45:32 +02:00
|
|
|
|
2015-04-10 20:47:11 +02:00
|
|
|
var interpolate = d3.interpolateObject(start, end)
|
|
|
|
var duration = 500
|
2015-03-31 23:45:32 +02:00
|
|
|
|
2015-04-10 20:47:11 +02:00
|
|
|
var ease = d3.ease("cubic-in-out")
|
|
|
|
|
|
|
|
d3.timer(function (t) {
|
|
|
|
if (t >= duration)
|
|
|
|
return true
|
|
|
|
|
|
|
|
var v = interpolate(ease(t / duration))
|
|
|
|
zoomBehavior.translate([v.x, v.y])
|
|
|
|
zoomBehavior.scale(v.scale)
|
|
|
|
panzoom()
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
2015-03-31 21:31:06 +02:00
|
|
|
}
|
|
|
|
|
2015-04-10 21:53:40 +02:00
|
|
|
var translateP, scaleP
|
|
|
|
|
2015-04-19 16:29:22 +02:00
|
|
|
function onPanZoom() {
|
|
|
|
savedPanZoom = {translate: zoomBehavior.translate(),
|
|
|
|
scale: zoomBehavior.scale()}
|
|
|
|
panzoom()
|
|
|
|
}
|
|
|
|
|
2015-03-31 17:22:36 +02:00
|
|
|
function panzoom() {
|
2015-03-31 21:18:06 +02:00
|
|
|
var translate = zoomBehavior.translate()
|
|
|
|
var scale = zoomBehavior.scale()
|
2015-04-10 20:47:11 +02:00
|
|
|
|
2015-04-10 21:53:40 +02:00
|
|
|
|
2015-04-10 20:47:11 +02:00
|
|
|
panzoomReal(translate, scale)
|
|
|
|
}
|
|
|
|
|
|
|
|
function panzoomReal(translate, scale) {
|
2015-04-10 21:53:40 +02:00
|
|
|
screenRect = {left: -translate[0] / scale, top: -translate[1] / scale,
|
|
|
|
right: (canvas.width - translate[0]) / scale,
|
|
|
|
bottom: (canvas.height - translate[1]) / scale}
|
|
|
|
|
2015-04-16 20:19:31 +02:00
|
|
|
requestAnimationFrame(redraw)
|
2015-03-31 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
2015-04-01 18:21:54 +02:00
|
|
|
function getSize() {
|
|
|
|
var sidebarWidth = sidebar.getWidth()
|
|
|
|
var width = el.offsetWidth - sidebarWidth
|
|
|
|
var height = el.offsetHeight
|
|
|
|
|
|
|
|
return [width, height]
|
|
|
|
}
|
|
|
|
|
2015-03-31 21:18:06 +02:00
|
|
|
function panzoomTo(a, b) {
|
|
|
|
var sidebarWidth = sidebar.getWidth()
|
2015-04-01 18:21:54 +02:00
|
|
|
var size = getSize()
|
2015-03-31 21:18:06 +02:00
|
|
|
|
|
|
|
var targetWidth = Math.max(1, b[0] - a[0])
|
|
|
|
var targetHeight = Math.max(1, b[1] - a[1])
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
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 21:29:47 +02:00
|
|
|
animatePanzoom(translate, scale)
|
|
|
|
}
|
|
|
|
|
2015-04-06 11:37:52 +02:00
|
|
|
function updateHighlight(nopanzoom) {
|
2015-04-10 20:47:11 +02:00
|
|
|
highlightedNodes = []
|
|
|
|
highlightedLinks = []
|
|
|
|
|
2015-04-06 11:37:52 +02:00
|
|
|
if (highlight !== undefined)
|
|
|
|
if (highlight.type === "node") {
|
|
|
|
var n = nodesDict[highlight.o.nodeinfo.node_id]
|
|
|
|
|
|
|
|
if (n) {
|
2015-04-10 20:47:11 +02:00
|
|
|
highlightedNodes = [n]
|
2015-04-06 11:37:52 +02:00
|
|
|
|
|
|
|
if (!nopanzoom)
|
|
|
|
panzoomTo([n.x, n.y], [n.x, n.y])
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
} else if (highlight.type === "link") {
|
2015-04-06 23:14:24 +02:00
|
|
|
var l = linksDict[highlight.o.id]
|
2015-04-06 11:37:52 +02:00
|
|
|
|
|
|
|
if (l) {
|
2015-04-10 20:47:11 +02:00
|
|
|
highlightedLinks = [l]
|
2015-04-11 01:27:20 +02:00
|
|
|
highlightedNodes = [l.source, l.target]
|
2015-04-06 11:37:52 +02:00
|
|
|
|
|
|
|
if (!nopanzoom) {
|
|
|
|
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 })
|
|
|
|
panzoomTo([x[0], y[0]], [x[1], y[1]])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nopanzoom)
|
2015-04-19 16:29:22 +02:00
|
|
|
if (!savedPanZoom)
|
|
|
|
panzoomTo([0, 0], force.size())
|
|
|
|
else
|
|
|
|
animatePanzoom(savedPanZoom.translate, savedPanZoom.scale)
|
2015-04-06 11:37:52 +02:00
|
|
|
}
|
|
|
|
|
2015-04-10 20:47:11 +02:00
|
|
|
function drawLabel(d) {
|
2015-04-19 02:16:06 +02:00
|
|
|
var neighbours = d.neighbours.filter(function (d) {
|
|
|
|
return !d.link.o.vpn
|
|
|
|
})
|
|
|
|
|
|
|
|
var sum = neighbours.reduce(function (a, b) {
|
|
|
|
return [a[0] + b.node.x, a[1] + b.node.y]
|
2015-04-10 20:47:11 +02:00
|
|
|
}, [0, 0])
|
2015-04-10 13:50:39 +02:00
|
|
|
|
2015-04-19 02:16:06 +02:00
|
|
|
var sumCos = sum[0] - d.x * neighbours.length
|
|
|
|
var sumSin = sum[1] - d.y * neighbours.length
|
2015-04-10 13:50:39 +02:00
|
|
|
|
2015-04-10 20:47:11 +02:00
|
|
|
var angle = Math.PI / 2
|
2015-04-10 13:50:39 +02:00
|
|
|
|
2015-04-19 02:16:06 +02:00
|
|
|
if (neighbours.length > 0)
|
2015-04-10 20:47:11 +02:00
|
|
|
angle = Math.PI + Math.atan2(sumSin, sumCos)
|
2015-04-10 13:50:39 +02:00
|
|
|
|
2015-04-10 20:47:11 +02:00
|
|
|
var cos = Math.cos(angle)
|
|
|
|
var sin = Math.sin(angle)
|
2015-04-10 13:50:39 +02:00
|
|
|
|
2015-04-11 00:30:36 +02:00
|
|
|
var width = d.labelWidth
|
|
|
|
var height = d.labelHeight
|
2015-04-10 13:50:39 +02:00
|
|
|
|
2015-04-11 00:30:36 +02: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
|
|
|
|
|
|
|
|
ctx.drawImage(d.label, x, y, width, height)
|
2015-04-10 20:47:11 +02:00
|
|
|
}
|
2015-04-10 13:50:39 +02:00
|
|
|
|
2015-04-10 21:53:40 +02:00
|
|
|
function visibleLinks(d) {
|
|
|
|
return (d.source.x > screenRect.left && d.source.x < screenRect.right &&
|
|
|
|
d.source.y > screenRect.top && d.source.y < screenRect.bottom) ||
|
|
|
|
(d.target.x > screenRect.left && d.target.x < screenRect.right &&
|
2015-04-22 23:18:10 +02:00
|
|
|
d.target.y > screenRect.top && d.target.y < screenRect.bottom) ||
|
|
|
|
d.o.vpn
|
2015-04-10 21:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function visibleNodes(d) {
|
|
|
|
return d.x + margin > screenRect.left && d.x - margin < screenRect.right &&
|
|
|
|
d.y + margin > screenRect.top && d.y - margin < screenRect.bottom
|
|
|
|
}
|
|
|
|
|
2015-04-16 21:01:46 +02:00
|
|
|
function redraw() {
|
2015-04-10 20:47:11 +02:00
|
|
|
var translate = zoomBehavior.translate()
|
|
|
|
var scale = zoomBehavior.scale()
|
2015-04-10 21:53:40 +02:00
|
|
|
var links = intLinks.filter(visibleLinks)
|
|
|
|
|
|
|
|
var xExtent = d3.extent(intNodes, function (d) { return d.px })
|
|
|
|
var yExtent = d3.extent(intNodes, function (d) { return d.py })
|
|
|
|
|
|
|
|
if (translateP) {
|
|
|
|
ctx.save()
|
|
|
|
ctx.translate(translateP[0], translateP[1])
|
|
|
|
ctx.scale(scaleP, scaleP)
|
|
|
|
ctx.clearRect(xExtent[0] - margin, yExtent[0] - margin,
|
|
|
|
xExtent[1] - xExtent[0] + 2 * margin,
|
|
|
|
yExtent[1] - yExtent[0] + 2 * margin)
|
|
|
|
ctx.restore()
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.save()
|
2015-04-10 20:47:11 +02:00
|
|
|
ctx.translate(translate[0], translate[1])
|
|
|
|
ctx.scale(scale, scale)
|
|
|
|
|
2015-04-10 21:53:40 +02:00
|
|
|
if (!translateP)
|
|
|
|
ctx.clearRect(xExtent[0] - margin, yExtent[0] - margin,
|
|
|
|
xExtent[1] - xExtent[0] + 2 * margin,
|
|
|
|
yExtent[1] - yExtent[0] + 2 * margin)
|
|
|
|
|
2015-04-16 20:19:31 +02:00
|
|
|
// Remeber last translate/scale state
|
|
|
|
translateP = translate
|
|
|
|
scaleP = scale
|
|
|
|
|
2015-04-16 20:16:14 +02:00
|
|
|
ctx.beginPath()
|
|
|
|
nodes.filter(visibleNodes).forEach(function (d) {
|
|
|
|
var clients = d.o.node.statistics.clients
|
2015-06-26 11:33:39 +02:00
|
|
|
if (clients === 0)
|
2015-04-16 20:16:14 +02:00
|
|
|
return
|
|
|
|
|
2015-06-27 01:37:36 +02:00
|
|
|
var startDistance = 16
|
2015-04-16 20:16:14 +02:00
|
|
|
var radius = 3
|
|
|
|
var a = 1.2
|
|
|
|
var startAngle = Math.PI
|
|
|
|
|
2015-06-27 01:37:36 +02:00
|
|
|
for (var orbit = 0, i = 0; i < clients; orbit++) {
|
|
|
|
var distance = startDistance + orbit * 2 * radius * a
|
|
|
|
var n = Math.floor((Math.PI * distance) / (a * radius))
|
|
|
|
var delta = clients - i
|
2015-04-16 20:16:14 +02:00
|
|
|
|
2015-06-27 01:37:36 +02:00
|
|
|
for (var j = 0; j < Math.min(delta, n); i++, j++) {
|
|
|
|
var angle = 2 * Math.PI / n * j
|
|
|
|
var x = d.x + distance * Math.cos(angle + startAngle)
|
|
|
|
var y = d.y + distance * Math.sin(angle + startAngle)
|
2015-04-16 20:16:14 +02:00
|
|
|
|
2015-06-27 01:37:36 +02:00
|
|
|
ctx.moveTo(x, y)
|
|
|
|
ctx.arc(x, y, radius, 0, 2 * Math.PI)
|
|
|
|
}
|
2015-04-16 20:16:14 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2015-07-07 12:42:57 +02:00
|
|
|
var clientColor = "#E6324B"
|
|
|
|
var hilightColor = "#FA940F"
|
|
|
|
var unknownColor = "#353634"
|
|
|
|
var nodeColor = "#F2E3C6"
|
|
|
|
|
|
|
|
ctx.fillStyle = clientColor
|
2015-04-16 20:16:14 +02:00
|
|
|
ctx.fill()
|
|
|
|
|
2015-04-10 20:47:11 +02:00
|
|
|
if (highlightedLinks.length) {
|
|
|
|
ctx.save()
|
2015-04-11 01:27:20 +02:00
|
|
|
ctx.lineWidth = 10
|
2015-07-07 12:42:57 +02:00
|
|
|
ctx.strokeStyle = hilightColor
|
2015-04-10 20:47:11 +02:00
|
|
|
|
|
|
|
highlightedLinks.forEach(function (d) {
|
|
|
|
ctx.beginPath()
|
|
|
|
ctx.moveTo(d.source.x, d.source.y)
|
|
|
|
ctx.lineTo(d.target.x, d.target.y)
|
|
|
|
ctx.stroke()
|
2015-04-10 13:50:39 +02:00
|
|
|
})
|
|
|
|
|
2015-04-10 20:47:11 +02:00
|
|
|
ctx.restore()
|
|
|
|
}
|
2015-04-10 13:50:39 +02:00
|
|
|
|
2015-04-19 02:16:06 +02:00
|
|
|
ctx.save()
|
2015-04-10 13:50:39 +02:00
|
|
|
|
2015-04-10 20:47:11 +02:00
|
|
|
links.forEach(function (d) {
|
|
|
|
ctx.beginPath()
|
|
|
|
ctx.moveTo(d.source.x, d.source.y)
|
|
|
|
ctx.lineTo(d.target.x, d.target.y)
|
|
|
|
ctx.strokeStyle = d.color
|
2015-04-19 02:16:06 +02:00
|
|
|
ctx.globalAlpha = d.o.vpn ? 0.1 : 1
|
|
|
|
ctx.lineWidth = d.o.vpn ? 1.5 : 2.5
|
2015-04-10 20:47:11 +02:00
|
|
|
ctx.stroke()
|
|
|
|
})
|
2015-04-10 13:50:39 +02:00
|
|
|
|
2015-04-19 02:16:06 +02:00
|
|
|
ctx.restore()
|
|
|
|
|
2015-04-11 00:30:36 +02:00
|
|
|
if (scale > 0.9)
|
|
|
|
intNodes.filter(visibleNodes).forEach(drawLabel, scale)
|
2015-04-10 13:50:39 +02:00
|
|
|
|
2015-04-10 20:47:11 +02:00
|
|
|
ctx.beginPath()
|
2015-04-10 21:53:40 +02:00
|
|
|
|
|
|
|
unknownNodes.filter(visibleNodes).forEach(function (d) {
|
2015-04-10 20:47:11 +02:00
|
|
|
ctx.moveTo(d.x + 8, d.y)
|
|
|
|
ctx.arc(d.x, d.y, 8, 0, 2 * Math.PI)
|
|
|
|
})
|
2015-04-10 13:50:39 +02:00
|
|
|
|
2015-07-07 12:42:57 +02:00
|
|
|
ctx.fillStyle = unknownColor
|
2015-04-10 13:50:39 +02:00
|
|
|
|
2015-04-10 20:47:11 +02:00
|
|
|
ctx.fill()
|
2015-04-10 13:50:39 +02:00
|
|
|
|
2015-04-10 20:47:11 +02:00
|
|
|
ctx.beginPath()
|
2015-04-10 21:53:40 +02:00
|
|
|
nodes.filter(visibleNodes).forEach(function (d) {
|
2015-04-10 20:47:11 +02:00
|
|
|
ctx.moveTo(d.x + 8, d.y)
|
|
|
|
ctx.arc(d.x, d.y, 8, 0, 2 * Math.PI)
|
2015-04-10 13:50:39 +02:00
|
|
|
})
|
2015-04-10 20:47:11 +02:00
|
|
|
|
2015-07-07 12:42:57 +02:00
|
|
|
ctx.fillStyle = nodeColor
|
2015-04-10 20:47:11 +02:00
|
|
|
|
|
|
|
ctx.fill()
|
|
|
|
|
|
|
|
if (highlightedNodes.length) {
|
|
|
|
ctx.save()
|
2015-07-07 12:42:57 +02:00
|
|
|
ctx.strokeStyle = hilightColor
|
|
|
|
ctx.fillStyle = nodeColor
|
2015-04-10 20:47:11 +02:00
|
|
|
ctx.lineWidth = 6
|
|
|
|
|
|
|
|
highlightedNodes.forEach(function (d) {
|
|
|
|
ctx.beginPath()
|
|
|
|
ctx.moveTo(d.x + 8, d.y)
|
|
|
|
ctx.arc(d.x, d.y, 8, 0, 2 * Math.PI)
|
|
|
|
ctx.fill()
|
|
|
|
ctx.stroke()
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx.restore()
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.restore()
|
2015-04-10 13:50:39 +02:00
|
|
|
}
|
|
|
|
|
2015-03-31 17:22:36 +02:00
|
|
|
function tickEvent() {
|
2015-04-19 02:50:15 +02:00
|
|
|
redraw()
|
2015-04-10 20:47:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function resizeCanvas() {
|
2015-04-11 00:53:35 +02:00
|
|
|
var r = window.devicePixelRatio
|
|
|
|
canvas.width = el.offsetWidth * r
|
|
|
|
canvas.height = el.offsetHeight * r
|
|
|
|
canvas.style.width = el.offsetWidth + "px"
|
|
|
|
canvas.style.height = el.offsetHeight + "px"
|
|
|
|
ctx.resetTransform()
|
|
|
|
ctx.scale(r, r)
|
2015-04-16 20:19:31 +02:00
|
|
|
requestAnimationFrame(redraw)
|
2015-03-31 17:22:36 +02:00
|
|
|
}
|
|
|
|
|
2015-04-16 01:29:17 +02:00
|
|
|
function distance(a, b) {
|
|
|
|
return Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
function distancePoint(a, b) {
|
|
|
|
return Math.sqrt(distance(a, b))
|
|
|
|
}
|
|
|
|
|
|
|
|
function distanceLink(p, a, b) {
|
|
|
|
/* http://stackoverflow.com/questions/849211 */
|
|
|
|
|
|
|
|
var l2 = distance(a, b)
|
|
|
|
|
|
|
|
if (l2 === 0)
|
|
|
|
return distance(p, a)
|
|
|
|
|
|
|
|
var t = ((p.x - a.x) * (b.x - a.x) + (p.y - a.y) * (b.y - a.y)) / l2
|
|
|
|
|
|
|
|
if (t < 0)
|
|
|
|
return distance(p, a)
|
|
|
|
|
|
|
|
if (t > 1)
|
|
|
|
return distance(p, b)
|
|
|
|
|
|
|
|
return Math.sqrt(distance(p, { x: a.x + t * (b.x - a.x),
|
|
|
|
y: a.y + t * (b.y - a.y) }))
|
|
|
|
}
|
|
|
|
|
|
|
|
function translateXY(d) {
|
|
|
|
var translate = zoomBehavior.translate()
|
|
|
|
var scale = zoomBehavior.scale()
|
|
|
|
|
|
|
|
return {x: (d[0] - translate[0]) / scale,
|
|
|
|
y: (d[1] - translate[1]) / scale
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onClick() {
|
|
|
|
if (d3.event.defaultPrevented)
|
|
|
|
return
|
|
|
|
|
|
|
|
var e = translateXY(d3.mouse(el))
|
|
|
|
|
|
|
|
var nodes = intNodes.filter(function (d) {
|
|
|
|
return distancePoint(e, d) < NODE_RADIUS
|
|
|
|
})
|
|
|
|
|
|
|
|
if (nodes.length > 0) {
|
|
|
|
router.node(nodes[0].o.node)()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var links = intLinks.filter(function (d) {
|
2015-04-22 02:44:07 +02:00
|
|
|
return !d.o.vpn
|
|
|
|
}).filter(function (d) {
|
2015-04-16 01:29:17 +02:00
|
|
|
return distanceLink(e, d.source, d.target) < LINE_RADIUS
|
|
|
|
})
|
|
|
|
|
|
|
|
if (links.length > 0) {
|
|
|
|
router.link(links[0].o)()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 10:19:38 +02:00
|
|
|
function keyboardZoom(z) {
|
|
|
|
return function () {
|
|
|
|
var e = d3.event
|
|
|
|
|
|
|
|
if (e.altKey || e.ctrlKey || e.metaKey)
|
|
|
|
return
|
|
|
|
|
|
|
|
if (e.keyCode === 43)
|
|
|
|
animatePanzoom(z.translate(), z.scale() * 1.41)
|
|
|
|
|
|
|
|
if (e.keyCode === 45)
|
|
|
|
animatePanzoom(z.translate(), z.scale() / 1.41)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 21:31:06 +02:00
|
|
|
el = document.createElement("div")
|
2015-03-31 17:22:36 +02:00
|
|
|
el.classList.add("graph")
|
|
|
|
self.div = el
|
|
|
|
|
2015-03-31 21:18:06 +02:00
|
|
|
zoomBehavior = d3.behavior.zoom()
|
2015-03-31 21:31:06 +02:00
|
|
|
.scaleExtent([1 / 3, 3])
|
2015-04-19 16:29:22 +02:00
|
|
|
.on("zoom", onPanZoom)
|
2015-03-31 23:24:24 +02:00
|
|
|
.translate([sidebar.getWidth(), 0])
|
|
|
|
|
2015-04-16 01:29:17 +02:00
|
|
|
canvas = d3.select(el)
|
2015-07-07 10:19:38 +02:00
|
|
|
.attr("tabindex", 1)
|
|
|
|
.on("keypress", keyboardZoom(zoomBehavior))
|
|
|
|
.call(zoomBehavior)
|
2015-07-07 11:27:19 +02:00
|
|
|
.append("canvas")
|
|
|
|
.on("click", onClick)
|
2015-04-16 01:29:17 +02:00
|
|
|
.call(draggableNode)
|
|
|
|
.node()
|
2015-03-31 17:22:36 +02:00
|
|
|
|
2015-04-10 20:47:11 +02:00
|
|
|
ctx = canvas.getContext("2d")
|
2015-03-31 17:22:36 +02:00
|
|
|
|
|
|
|
force = d3.layout.force()
|
2015-04-19 02:16:06 +02:00
|
|
|
.charge(-250)
|
|
|
|
.gravity(0.1)
|
|
|
|
.linkDistance(function (d) {
|
|
|
|
if (d.o.vpn)
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return LINK_DISTANCE
|
|
|
|
})
|
2015-04-01 01:07:40 +02:00
|
|
|
.linkStrength(function (d) {
|
2015-04-19 02:16:06 +02:00
|
|
|
if (d.o.vpn)
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return Math.max(0.5, 1 / d.o.tq)
|
2015-04-01 01:07:40 +02:00
|
|
|
})
|
2015-03-31 17:22:36 +02:00
|
|
|
.on("tick", tickEvent)
|
2015-04-01 01:06:53 +02:00
|
|
|
.on("end", savePositions)
|
2015-03-31 17:22:36 +02:00
|
|
|
|
2015-04-10 20:47:11 +02:00
|
|
|
window.addEventListener("resize", resizeCanvas)
|
|
|
|
|
2015-03-31 23:24:24 +02:00
|
|
|
panzoom()
|
|
|
|
|
2015-03-31 17:22:36 +02:00
|
|
|
self.setData = function (data) {
|
2015-04-03 02:32:32 +02:00
|
|
|
var oldNodes = {}
|
2015-04-01 01:06:53 +02:00
|
|
|
|
2015-04-03 02:32:32 +02:00
|
|
|
intNodes.forEach( function (d) {
|
|
|
|
oldNodes[d.o.id] = d
|
|
|
|
})
|
2015-04-01 15:59:05 +02:00
|
|
|
|
2015-04-03 02:32:32 +02:00
|
|
|
intNodes = data.graph.nodes.map( function (d) {
|
|
|
|
var e
|
|
|
|
if (d.id in oldNodes)
|
|
|
|
e = oldNodes[d.id]
|
|
|
|
else
|
|
|
|
e = {}
|
|
|
|
|
|
|
|
e.o = d
|
2015-04-01 01:06:53 +02:00
|
|
|
|
2015-04-03 02:32:32 +02:00
|
|
|
return e
|
|
|
|
})
|
|
|
|
|
|
|
|
var newNodesDict = {}
|
|
|
|
|
|
|
|
intNodes.forEach( function (d) {
|
|
|
|
newNodesDict[d.o.id] = d
|
|
|
|
})
|
|
|
|
|
2015-04-08 01:21:52 +02:00
|
|
|
var oldLinks = {}
|
|
|
|
|
|
|
|
intLinks.forEach( function (d) {
|
|
|
|
oldLinks[d.o.id] = d
|
|
|
|
})
|
|
|
|
|
2015-04-19 02:16:06 +02:00
|
|
|
intLinks = data.graph.links.map( function (d) {
|
2015-04-08 01:21:52 +02:00
|
|
|
var e
|
|
|
|
if (d.id in oldLinks)
|
|
|
|
e = oldLinks[d.id]
|
|
|
|
else
|
|
|
|
e = {}
|
|
|
|
|
|
|
|
e.o = d
|
|
|
|
e.source = newNodesDict[d.source.id]
|
|
|
|
e.target = newNodesDict[d.target.id]
|
2015-04-22 12:37:18 +02:00
|
|
|
|
|
|
|
if (d.vpn)
|
2015-07-07 12:42:57 +02:00
|
|
|
e.color = "rgba(255, 255, 255, " + (0.6 / d.tq) + ")"
|
2015-04-22 12:37:18 +02:00
|
|
|
else
|
|
|
|
e.color = linkScale(d.tq).hex()
|
2015-04-03 02:32:32 +02:00
|
|
|
|
2015-04-08 01:21:52 +02:00
|
|
|
return e
|
2015-03-31 17:22:36 +02:00
|
|
|
})
|
|
|
|
|
2015-04-10 15:19:42 +02:00
|
|
|
linksDict = {}
|
|
|
|
nodesDict = {}
|
|
|
|
|
2015-04-10 13:50:39 +02:00
|
|
|
intNodes.forEach(function (d) {
|
|
|
|
d.neighbours = {}
|
2015-04-10 15:19:42 +02:00
|
|
|
|
|
|
|
if (d.o.node)
|
|
|
|
nodesDict[d.o.node.nodeinfo.node_id] = d
|
2015-04-10 20:47:11 +02:00
|
|
|
|
2015-04-11 00:30:36 +02:00
|
|
|
var name = nodeName(d)
|
2015-04-10 20:47:11 +02:00
|
|
|
|
2015-04-11 01:16:26 +02:00
|
|
|
var offset = 8
|
|
|
|
var lineWidth = 3
|
2015-04-11 00:30:36 +02:00
|
|
|
var buffer = document.createElement("canvas")
|
2015-04-11 01:16:26 +02:00
|
|
|
var r = window.devicePixelRatio
|
2015-04-11 00:30:36 +02:00
|
|
|
var bctx = buffer.getContext("2d")
|
2015-04-19 03:46:09 +02:00
|
|
|
bctx.font = "11px Roboto"
|
|
|
|
var width = bctx.measureText(name).width
|
2015-04-11 01:16:26 +02:00
|
|
|
var scale = zoomBehavior.scaleExtent()[1] * r
|
|
|
|
buffer.width = (width + 2 * lineWidth) * scale
|
|
|
|
buffer.height = (16 + 2 * lineWidth) * scale
|
|
|
|
bctx.scale(scale, scale)
|
|
|
|
bctx.textBaseline = "middle"
|
|
|
|
bctx.textAlign = "center"
|
|
|
|
bctx.lineWidth = lineWidth
|
|
|
|
bctx.lineCap = "round"
|
2015-07-07 12:42:57 +02:00
|
|
|
bctx.strokeStyle = "rgba(53, 54, 52, 0.1)"
|
|
|
|
bctx.fillStyle = "rgba(242, 227, 198, 0.8)"
|
2015-04-20 23:31:55 +02:00
|
|
|
bctx.miterLimit = 2
|
2015-04-11 01:16:26 +02:00
|
|
|
bctx.strokeText(name, buffer.width / (2 * scale), buffer.height / (2 * scale))
|
|
|
|
bctx.fillText(name, buffer.width / (2 * scale), buffer.height / (2 * scale))
|
2015-04-11 00:30:36 +02:00
|
|
|
|
|
|
|
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-04-10 13:50:39 +02:00
|
|
|
})
|
2015-03-31 17:22:36 +02:00
|
|
|
|
2015-04-10 13:50:39 +02:00
|
|
|
intLinks.forEach(function (d) {
|
2015-04-19 02:16:06 +02:00
|
|
|
d.source.neighbours[d.target.o.id] = {node: d.target, link: d}
|
|
|
|
d.target.neighbours[d.source.o.id] = {node: d.source, link: d}
|
2015-04-10 15:19:42 +02:00
|
|
|
|
|
|
|
if (d.o.source.node && d.o.target.node)
|
|
|
|
linksDict[d.o.id] = d
|
2015-04-10 13:50:39 +02:00
|
|
|
})
|
2015-03-31 17:22:36 +02:00
|
|
|
|
2015-04-10 13:50:39 +02:00
|
|
|
intNodes.forEach(function (d) {
|
|
|
|
d.neighbours = Object.keys(d.neighbours).map(function (k) {
|
|
|
|
return d.neighbours[k]
|
|
|
|
})
|
|
|
|
})
|
2015-03-31 17:22:36 +02:00
|
|
|
|
2015-04-10 21:53:40 +02:00
|
|
|
nodes = intNodes.filter(function (d) { return d.o.node })
|
|
|
|
unknownNodes = intNodes.filter(function (d) { return !d.o.node })
|
|
|
|
|
2015-04-03 02:32:32 +02:00
|
|
|
if (localStorageTest()) {
|
|
|
|
var save = JSON.parse(localStorage.getItem("graph/nodeposition"))
|
|
|
|
|
|
|
|
if (save) {
|
|
|
|
var nodePositions = {}
|
|
|
|
save.forEach( function (d) {
|
|
|
|
nodePositions[d.id] = d
|
|
|
|
})
|
|
|
|
|
2015-04-10 20:47:11 +02:00
|
|
|
intNodes.forEach( function (d) {
|
2015-04-10 13:50:39 +02:00
|
|
|
if (nodePositions[d.o.id] && (d.x === undefined || d.y === undefined)) {
|
2015-04-03 02:32:32 +02:00
|
|
|
d.x = nodePositions[d.o.id].x
|
|
|
|
d.y = nodePositions[d.o.id].y
|
|
|
|
}
|
|
|
|
})
|
2015-04-01 01:06:53 +02:00
|
|
|
}
|
2015-04-03 02:32:32 +02:00
|
|
|
}
|
2015-03-31 17:22:36 +02:00
|
|
|
|
2015-04-03 02:32:32 +02:00
|
|
|
var diameter = graphDiameter(intNodes)
|
2015-04-01 18:21:54 +02:00
|
|
|
|
2015-04-03 02:32:32 +02:00
|
|
|
force.nodes(intNodes)
|
|
|
|
.links(intLinks)
|
2015-04-01 18:21:54 +02:00
|
|
|
.size([diameter, diameter])
|
2015-04-03 02:32:32 +02:00
|
|
|
|
2015-04-06 11:37:52 +02:00
|
|
|
updateHighlight(true)
|
|
|
|
|
2015-04-10 20:47:11 +02:00
|
|
|
force.start()
|
|
|
|
resizeCanvas()
|
2015-03-31 17:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self.resetView = function () {
|
2015-04-06 11:37:52 +02:00
|
|
|
highlight = undefined
|
|
|
|
updateHighlight()
|
2015-03-31 23:45:32 +02:00
|
|
|
doAnimation = true
|
2015-03-31 17:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self.gotoNode = function (d) {
|
2015-04-06 11:37:52 +02:00
|
|
|
highlight = {type: "node", o: d}
|
|
|
|
updateHighlight()
|
2015-03-31 23:45:32 +02:00
|
|
|
doAnimation = true
|
2015-03-31 17:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self.gotoLink = function (d) {
|
2015-04-06 11:37:52 +02:00
|
|
|
highlight = {type: "link", o: d}
|
|
|
|
updateHighlight()
|
2015-03-31 23:45:32 +02:00
|
|
|
doAnimation = true
|
2015-03-31 17:22:36 +02:00
|
|
|
}
|
|
|
|
|
2015-03-31 23:24:24 +02:00
|
|
|
self.destroy = function () {
|
|
|
|
force.stop()
|
2015-04-10 20:47:11 +02:00
|
|
|
canvas.remove()
|
2015-03-31 23:24:24 +02:00
|
|
|
force = null
|
|
|
|
}
|
|
|
|
|
2015-03-31 17:22:36 +02:00
|
|
|
return self
|
|
|
|
}
|
|
|
|
})
|