2015-03-25 20:25:41 +01:00
|
|
|
define(function () {
|
2015-03-29 16:14:10 +02:00
|
|
|
return function () {
|
2015-03-31 23:24:24 +02:00
|
|
|
var self = this
|
2015-03-25 20:25:41 +01:00
|
|
|
var objects = { nodes: {}, links: {} }
|
|
|
|
var targets = []
|
2015-03-31 23:24:24 +02:00
|
|
|
var running = false
|
2015-03-25 20:25:41 +01:00
|
|
|
|
2015-03-29 16:14:10 +02:00
|
|
|
function saveState(d) {
|
|
|
|
var s = "#!"
|
|
|
|
|
|
|
|
if (d) {
|
|
|
|
if ("node" in d)
|
2015-03-31 15:13:11 +02:00
|
|
|
s += "n:" + encodeURIComponent(d.node.nodeinfo.node_id)
|
2015-03-29 16:14:10 +02:00
|
|
|
|
|
|
|
if ("link" in d)
|
2015-04-06 23:10:37 +02:00
|
|
|
s += "l:" + encodeURIComponent(d.link.id)
|
2015-03-29 16:14:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
window.history.pushState(s, undefined, s)
|
|
|
|
}
|
|
|
|
|
2015-03-25 20:25:41 +01:00
|
|
|
function resetView(push) {
|
|
|
|
push = trueDefault(push)
|
|
|
|
|
|
|
|
targets.forEach( function (t) {
|
|
|
|
t.resetView()
|
|
|
|
})
|
|
|
|
|
|
|
|
if (push)
|
|
|
|
saveState()
|
|
|
|
}
|
|
|
|
|
2015-03-25 20:55:49 +01:00
|
|
|
function gotoNode(d) {
|
2015-03-31 16:30:16 +02:00
|
|
|
if (!d)
|
|
|
|
return false
|
|
|
|
|
2015-03-25 20:25:41 +01:00
|
|
|
targets.forEach( function (t) {
|
|
|
|
t.gotoNode(d)
|
|
|
|
})
|
2015-03-31 16:30:16 +02:00
|
|
|
|
|
|
|
return true
|
2015-03-25 20:25:41 +01:00
|
|
|
}
|
|
|
|
|
2015-03-25 20:55:49 +01:00
|
|
|
function gotoLink(d) {
|
2015-03-31 16:30:16 +02:00
|
|
|
if (!d)
|
|
|
|
return false
|
|
|
|
|
2015-03-25 20:25:41 +01:00
|
|
|
targets.forEach( function (t) {
|
|
|
|
t.gotoLink(d)
|
|
|
|
})
|
2015-03-31 16:30:16 +02:00
|
|
|
|
|
|
|
return true
|
2015-03-25 20:25:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function loadState(s) {
|
|
|
|
if (!s)
|
2015-03-27 22:25:28 +01:00
|
|
|
return false
|
2015-03-25 20:25:41 +01:00
|
|
|
|
|
|
|
if (!s.startsWith("#!"))
|
2015-03-27 22:25:28 +01:00
|
|
|
return false
|
2015-03-25 20:25:41 +01:00
|
|
|
|
|
|
|
var args = s.slice(2).split(":")
|
2015-03-29 16:14:10 +02:00
|
|
|
var id
|
2015-03-25 20:25:41 +01:00
|
|
|
|
2015-03-31 15:13:11 +02:00
|
|
|
if (args[1] !== undefined) {
|
|
|
|
id = decodeURIComponent(args[1])
|
2015-03-25 20:25:41 +01:00
|
|
|
|
2015-03-31 15:13:11 +02:00
|
|
|
if (args[0] === "n" && id in objects.nodes) {
|
2015-03-25 20:55:49 +01:00
|
|
|
gotoNode(objects.nodes[id])
|
2015-03-27 22:25:28 +01:00
|
|
|
return true
|
|
|
|
}
|
2015-03-25 20:25:41 +01:00
|
|
|
|
2015-03-31 15:13:11 +02:00
|
|
|
if (args[0] === "l" && id in objects.links) {
|
2015-03-25 20:55:49 +01:00
|
|
|
gotoLink(objects.links[id])
|
2015-03-27 22:25:28 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
self.start = function () {
|
2015-03-31 23:24:24 +02:00
|
|
|
running = true
|
|
|
|
|
2015-03-27 22:25:28 +01:00
|
|
|
if (!loadState(window.location.hash))
|
|
|
|
resetView(false)
|
|
|
|
|
|
|
|
window.onpopstate = function (d) {
|
|
|
|
if (!loadState(d.state))
|
|
|
|
resetView(false)
|
2015-03-25 20:25:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 20:55:49 +01:00
|
|
|
self.node = function (d) {
|
|
|
|
return function () {
|
2015-03-31 16:30:16 +02:00
|
|
|
if (gotoNode(d))
|
|
|
|
saveState({ node: d })
|
|
|
|
|
2015-03-25 20:55:49 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.link = function (d) {
|
|
|
|
return function () {
|
2015-03-31 16:30:16 +02:00
|
|
|
if (gotoLink(d))
|
|
|
|
saveState({ link: d })
|
|
|
|
|
2015-03-25 20:55:49 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.reset = function () {
|
|
|
|
resetView()
|
|
|
|
saveState()
|
|
|
|
}
|
|
|
|
|
2015-03-31 22:18:09 +02:00
|
|
|
self.addTarget = function (d) {
|
|
|
|
targets.push(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
self.removeTarget = function (d) {
|
|
|
|
targets = targets.filter( function (e) {
|
|
|
|
return d !== e
|
|
|
|
})
|
|
|
|
}
|
2015-03-25 20:25:41 +01:00
|
|
|
|
2015-03-29 17:48:25 +02:00
|
|
|
self.setData = function (data) {
|
2015-03-25 20:25:41 +01:00
|
|
|
objects.nodes = {}
|
|
|
|
objects.links = {}
|
|
|
|
|
2015-03-29 17:48:25 +02:00
|
|
|
data.nodes.all.forEach( function (d) {
|
2015-03-25 20:25:41 +01:00
|
|
|
objects.nodes[d.nodeinfo.node_id] = d
|
|
|
|
})
|
|
|
|
|
2015-03-31 17:21:58 +02:00
|
|
|
data.graph.links.forEach( function (d) {
|
2015-04-06 23:10:37 +02:00
|
|
|
objects.links[d.id] = d
|
2015-03-25 20:25:41 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-31 23:24:24 +02:00
|
|
|
self.reload = function () {
|
|
|
|
if (!running)
|
|
|
|
return
|
|
|
|
|
|
|
|
if (!loadState(window.history.state))
|
|
|
|
resetView(false)
|
|
|
|
}
|
|
|
|
|
2015-03-25 20:25:41 +01:00
|
|
|
return self
|
|
|
|
}
|
|
|
|
})
|