2015-03-31 15:22:36 +00:00
|
|
|
define(["d3"], function (d3) {
|
2015-04-06 13:54:26 +00:00
|
|
|
return function (config, linkScale, sidebar, router) {
|
2015-03-31 15:22:36 +00:00
|
|
|
var self = this
|
2015-04-01 01:57:10 +00:00
|
|
|
var svg, vis, link, node
|
2015-03-31 15:22:36 +00:00
|
|
|
var nodesDict, linksDict
|
2015-03-31 19:18:06 +00:00
|
|
|
var zoomBehavior
|
2015-03-31 15:22:36 +00:00
|
|
|
var force
|
2015-03-31 19:31:06 +00:00
|
|
|
var el
|
2015-03-31 21:45:32 +00:00
|
|
|
var doAnimation = false
|
2015-04-03 00:32:32 +00:00
|
|
|
var intNodes = []
|
2015-04-07 23:21:52 +00:00
|
|
|
var intLinks = []
|
2015-04-06 09:37:52 +00:00
|
|
|
var highlight
|
2015-03-31 15:22:36 +00:00
|
|
|
|
2015-03-31 22:06:17 +00:00
|
|
|
var LINK_DISTANCE = 70
|
|
|
|
|
2015-04-01 16:21:54 +00:00
|
|
|
function graphDiameter(nodes) {
|
|
|
|
return Math.sqrt(nodes.length / Math.PI) * LINK_DISTANCE
|
|
|
|
}
|
|
|
|
|
2015-03-31 23:06:53 +00:00
|
|
|
function savePositions() {
|
2015-04-01 13:59:05 +00:00
|
|
|
if (!localStorageTest())
|
|
|
|
return
|
|
|
|
|
2015-04-03 00:32:32 +00:00
|
|
|
var save = intNodes.map( function (d) {
|
|
|
|
return { id: d.o.id, x: d.x, y: d.y }
|
2015-03-31 23:06:53 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
localStorage.setItem("graph/nodeposition", JSON.stringify(save))
|
|
|
|
}
|
|
|
|
|
2015-03-31 15:22:36 +00:00
|
|
|
function nodeName(d) {
|
2015-04-03 00:32:32 +00:00
|
|
|
if (d.o.node && d.o.node.nodeinfo)
|
|
|
|
return d.o.node.nodeinfo.hostname
|
2015-03-31 15:22:36 +00:00
|
|
|
else
|
2015-04-03 00:32:32 +00:00
|
|
|
return d.o.id
|
2015-03-31 15:22:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function dragstart(d) {
|
|
|
|
d3.event.sourceEvent.stopPropagation()
|
|
|
|
d.fixed |= 2
|
|
|
|
}
|
|
|
|
|
|
|
|
function dragmove(d) {
|
|
|
|
d.px = d3.event.x
|
|
|
|
d.py = d3.event.y
|
|
|
|
force.resume()
|
|
|
|
}
|
|
|
|
|
|
|
|
function dragend(d) {
|
|
|
|
d3.event.sourceEvent.stopPropagation()
|
|
|
|
d.fixed &= 1
|
|
|
|
}
|
|
|
|
|
2015-03-31 19:31:06 +00:00
|
|
|
function animatePanzoom(translate, scale) {
|
|
|
|
zoomBehavior.scale(scale)
|
|
|
|
zoomBehavior.translate(translate)
|
|
|
|
|
2015-03-31 21:45:32 +00:00
|
|
|
var el = vis
|
|
|
|
|
|
|
|
if (doAnimation)
|
|
|
|
el = el.transition().duration(500)
|
|
|
|
|
|
|
|
el.attr("transform", "translate(" + translate + ") " +
|
|
|
|
"scale(" + scale + ")")
|
2015-03-31 19:31:06 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 15:22:36 +00:00
|
|
|
function panzoom() {
|
2015-03-31 19:18:06 +00:00
|
|
|
var translate = zoomBehavior.translate()
|
|
|
|
var scale = zoomBehavior.scale()
|
|
|
|
vis.attr("transform", "translate(" + translate + ") " +
|
|
|
|
"scale(" + scale + ")")
|
|
|
|
}
|
|
|
|
|
2015-04-01 16:21:54 +00:00
|
|
|
function getSize() {
|
|
|
|
var sidebarWidth = sidebar.getWidth()
|
|
|
|
var width = el.offsetWidth - sidebarWidth
|
|
|
|
var height = el.offsetHeight
|
|
|
|
|
|
|
|
return [width, height]
|
|
|
|
}
|
|
|
|
|
2015-03-31 19:18:06 +00:00
|
|
|
function panzoomTo(a, b) {
|
|
|
|
var sidebarWidth = sidebar.getWidth()
|
2015-04-01 16:21:54 +00:00
|
|
|
var size = getSize()
|
2015-03-31 19:18:06 +00: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 19:29:47 +00:00
|
|
|
animatePanzoom(translate, scale)
|
|
|
|
}
|
|
|
|
|
2015-04-06 09:37:52 +00:00
|
|
|
function updateHighlight(nopanzoom) {
|
|
|
|
if (highlight !== undefined)
|
|
|
|
if (highlight.type === "node") {
|
|
|
|
var n = nodesDict[highlight.o.nodeinfo.node_id]
|
|
|
|
|
|
|
|
if (n) {
|
|
|
|
link.classed("highlight", false)
|
|
|
|
node.classed("highlight", function (e) {
|
|
|
|
return e.o.node === n.o.node && n.o.node !== undefined
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!nopanzoom)
|
|
|
|
panzoomTo([n.x, n.y], [n.x, n.y])
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
} else if (highlight.type === "link") {
|
2015-04-06 21:14:24 +00:00
|
|
|
var l = linksDict[highlight.o.id]
|
2015-04-06 09:37:52 +00:00
|
|
|
|
|
|
|
if (l) {
|
|
|
|
node.classed("highlight", false)
|
|
|
|
link.classed("highlight", function (e) {
|
|
|
|
return e.o === l.o && l.o !== undefined
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
node.classed("highlight", false)
|
|
|
|
link.classed("highlight", false)
|
|
|
|
|
|
|
|
if (!nopanzoom)
|
|
|
|
panzoomTo([0, 0], force.size())
|
|
|
|
}
|
|
|
|
|
2015-03-31 15:22:36 +00:00
|
|
|
function tickEvent() {
|
|
|
|
link.selectAll("line")
|
|
|
|
.attr("x1", function(d) { return d.source.x })
|
|
|
|
.attr("y1", function(d) { return d.source.y })
|
|
|
|
.attr("x2", function(d) { return d.target.x })
|
|
|
|
.attr("y2", function(d) { return d.target.y })
|
|
|
|
|
|
|
|
node
|
|
|
|
.attr("cx", function(d) { return d.x })
|
|
|
|
.attr("cy", function(d) { return d.y })
|
|
|
|
}
|
|
|
|
|
2015-03-31 19:31:06 +00:00
|
|
|
el = document.createElement("div")
|
2015-03-31 15:22:36 +00:00
|
|
|
el.classList.add("graph")
|
|
|
|
self.div = el
|
|
|
|
|
2015-03-31 19:18:06 +00:00
|
|
|
zoomBehavior = d3.behavior.zoom()
|
2015-03-31 19:31:06 +00:00
|
|
|
.scaleExtent([1 / 3, 3])
|
2015-03-31 19:18:06 +00:00
|
|
|
.on("zoom", panzoom)
|
2015-03-31 21:24:24 +00:00
|
|
|
.translate([sidebar.getWidth(), 0])
|
|
|
|
|
|
|
|
svg = d3.select(el).append("svg")
|
|
|
|
.attr("pointer-events", "all")
|
|
|
|
.call(zoomBehavior)
|
2015-03-31 19:18:06 +00:00
|
|
|
|
2015-03-31 21:24:24 +00:00
|
|
|
vis = svg.append("g")
|
2015-03-31 15:22:36 +00:00
|
|
|
|
|
|
|
vis.append("g").attr("class", "links")
|
|
|
|
vis.append("g").attr("class", "nodes")
|
|
|
|
|
|
|
|
force = d3.layout.force()
|
2015-03-31 19:18:06 +00:00
|
|
|
.charge(-70)
|
2015-03-31 15:22:36 +00:00
|
|
|
.gravity(0.05)
|
2015-03-31 22:06:17 +00:00
|
|
|
.linkDistance(LINK_DISTANCE)
|
2015-03-31 23:07:40 +00:00
|
|
|
.linkStrength(function (d) {
|
2015-04-03 00:32:32 +00:00
|
|
|
return 1 / d.o.tq
|
2015-03-31 23:07:40 +00:00
|
|
|
})
|
2015-03-31 15:22:36 +00:00
|
|
|
.on("tick", tickEvent)
|
2015-03-31 23:06:53 +00:00
|
|
|
.on("end", savePositions)
|
2015-03-31 15:22:36 +00:00
|
|
|
|
2015-03-31 21:24:24 +00:00
|
|
|
panzoom()
|
|
|
|
|
2015-03-31 15:22:36 +00:00
|
|
|
var draggableNode = d3.behavior.drag()
|
|
|
|
.on("dragstart", dragstart)
|
|
|
|
.on("drag", dragmove)
|
|
|
|
.on("dragend", dragend)
|
|
|
|
|
|
|
|
self.setData = function (data) {
|
2015-04-03 00:32:32 +00:00
|
|
|
var oldNodes = {}
|
2015-03-31 23:06:53 +00:00
|
|
|
|
2015-04-03 00:32:32 +00:00
|
|
|
intNodes.forEach( function (d) {
|
|
|
|
oldNodes[d.o.id] = d
|
|
|
|
})
|
2015-04-01 13:59:05 +00:00
|
|
|
|
2015-04-03 00:32:32 +00: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-03-31 23:06:53 +00:00
|
|
|
|
2015-04-03 00:32:32 +00:00
|
|
|
return e
|
|
|
|
})
|
|
|
|
|
|
|
|
var newNodesDict = {}
|
|
|
|
|
|
|
|
intNodes.forEach( function (d) {
|
|
|
|
newNodesDict[d.o.id] = d
|
|
|
|
})
|
|
|
|
|
2015-04-07 23:21:52 +00:00
|
|
|
var oldLinks = {}
|
|
|
|
|
|
|
|
intLinks.forEach( function (d) {
|
|
|
|
oldLinks[d.o.id] = d
|
|
|
|
})
|
|
|
|
|
|
|
|
intLinks = data.graph.links.filter( function (d) {
|
2015-03-31 15:22:36 +00:00
|
|
|
return !d.vpn
|
2015-04-03 00:32:32 +00:00
|
|
|
}).map( function (d) {
|
2015-04-07 23:21:52 +00: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-03 00:32:32 +00:00
|
|
|
|
2015-04-07 23:21:52 +00:00
|
|
|
return e
|
2015-03-31 15:22:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
link = vis.select("g.links")
|
2015-03-31 20:18:09 +00:00
|
|
|
.selectAll("g.link")
|
2015-04-06 21:14:24 +00:00
|
|
|
.data(intLinks, function (d) { return d.o.id })
|
2015-03-31 15:22:36 +00:00
|
|
|
|
2015-04-03 00:32:32 +00:00
|
|
|
link.exit().remove()
|
|
|
|
|
2015-03-31 15:22:36 +00:00
|
|
|
var linkEnter = link.enter().append("g")
|
|
|
|
.attr("class", "link")
|
|
|
|
.on("click", function (d) {
|
|
|
|
if (!d3.event.defaultPrevented)
|
2015-04-07 23:12:30 +00:00
|
|
|
router.link(d.o)()
|
2015-03-31 15:22:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
linkEnter.append("line")
|
|
|
|
.append("title")
|
|
|
|
|
|
|
|
link.selectAll("line")
|
2015-04-03 00:32:32 +00:00
|
|
|
.style("stroke", function (d) { return linkScale(d.o.tq).hex() })
|
2015-03-31 15:22:36 +00:00
|
|
|
|
2015-04-03 00:32:32 +00:00
|
|
|
link.selectAll("title").text(function (d) { return showTq(d.o) })
|
2015-03-31 15:22:36 +00:00
|
|
|
|
|
|
|
linksDict = {}
|
|
|
|
|
|
|
|
link.each( function (d) {
|
2015-04-03 00:32:32 +00:00
|
|
|
if (d.o.source.node && d.o.target.node)
|
|
|
|
linksDict[d.o.id] = d
|
2015-03-31 15:22:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
node = vis.select("g.nodes")
|
|
|
|
.selectAll(".node")
|
2015-04-03 00:32:32 +00:00
|
|
|
.data(intNodes, function(d) { return d.o.id })
|
|
|
|
|
|
|
|
node.exit().remove()
|
2015-03-31 15:22:36 +00:00
|
|
|
|
|
|
|
var nodeEnter = node.enter().append("circle")
|
|
|
|
.attr("r", 8)
|
|
|
|
.on("click", function (d) {
|
|
|
|
if (!d3.event.defaultPrevented)
|
2015-04-03 00:32:32 +00:00
|
|
|
router.node(d.o.node)()
|
2015-03-31 15:22:36 +00:00
|
|
|
})
|
|
|
|
.call(draggableNode)
|
|
|
|
|
|
|
|
node.attr("class", function (d) {
|
|
|
|
var s = ["node"]
|
2015-04-03 00:32:32 +00:00
|
|
|
if (!d.o.node)
|
2015-03-31 15:22:36 +00:00
|
|
|
s.push("unknown")
|
|
|
|
|
|
|
|
return s.join(" ")
|
|
|
|
})
|
|
|
|
|
|
|
|
nodesDict = {}
|
|
|
|
|
|
|
|
node.each( function (d) {
|
2015-04-03 00:32:32 +00:00
|
|
|
if (d.o.node)
|
|
|
|
nodesDict[d.o.node.nodeinfo.node_id] = d
|
2015-03-31 15:22:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
nodeEnter.append("title")
|
2015-04-03 00:32:32 +00:00
|
|
|
|
|
|
|
if (localStorageTest()) {
|
|
|
|
var save = JSON.parse(localStorage.getItem("graph/nodeposition"))
|
|
|
|
|
|
|
|
if (save) {
|
|
|
|
var nodePositions = {}
|
|
|
|
save.forEach( function (d) {
|
|
|
|
nodePositions[d.id] = d
|
|
|
|
})
|
|
|
|
|
|
|
|
nodeEnter.each( function (d) {
|
|
|
|
if (nodePositions[d.o.id]) {
|
|
|
|
d.x = nodePositions[d.o.id].x
|
|
|
|
d.y = nodePositions[d.o.id].y
|
|
|
|
}
|
|
|
|
})
|
2015-03-31 23:06:53 +00:00
|
|
|
}
|
2015-04-03 00:32:32 +00:00
|
|
|
}
|
2015-03-31 15:22:36 +00:00
|
|
|
|
|
|
|
node.selectAll("title").text(nodeName)
|
|
|
|
|
2015-04-03 00:32:32 +00:00
|
|
|
var diameter = graphDiameter(intNodes)
|
2015-04-01 16:21:54 +00:00
|
|
|
|
2015-04-03 00:32:32 +00:00
|
|
|
force.nodes(intNodes)
|
|
|
|
.links(intLinks)
|
2015-04-01 16:21:54 +00:00
|
|
|
.size([diameter, diameter])
|
2015-04-03 00:32:32 +00:00
|
|
|
|
2015-04-06 09:37:52 +00:00
|
|
|
updateHighlight(true)
|
|
|
|
|
2015-04-03 00:32:32 +00:00
|
|
|
if (node.enter().size() + link.enter().size() > 0)
|
|
|
|
force.start()
|
2015-03-31 15:22:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.resetView = function () {
|
2015-04-06 09:37:52 +00:00
|
|
|
highlight = undefined
|
|
|
|
updateHighlight()
|
2015-03-31 21:45:32 +00:00
|
|
|
doAnimation = true
|
2015-03-31 15:22:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.gotoNode = function (d) {
|
2015-04-06 09:37:52 +00:00
|
|
|
highlight = {type: "node", o: d}
|
|
|
|
updateHighlight()
|
2015-03-31 21:45:32 +00:00
|
|
|
doAnimation = true
|
2015-03-31 15:22:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.gotoLink = function (d) {
|
2015-04-06 09:37:52 +00:00
|
|
|
highlight = {type: "link", o: d}
|
|
|
|
updateHighlight()
|
2015-03-31 21:45:32 +00:00
|
|
|
doAnimation = true
|
2015-03-31 15:22:36 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 21:24:24 +00:00
|
|
|
self.destroy = function () {
|
|
|
|
force.stop()
|
|
|
|
node.remove()
|
|
|
|
link.remove()
|
|
|
|
svg.remove()
|
|
|
|
force = null
|
|
|
|
svg = null
|
|
|
|
vis = null
|
|
|
|
link = null
|
|
|
|
node = null
|
|
|
|
}
|
|
|
|
|
2015-03-31 15:22:36 +00:00
|
|
|
return self
|
|
|
|
}
|
|
|
|
})
|