hopglass/lib/router.js

140 lines
2.5 KiB
JavaScript
Raw Normal View History

2015-03-25 20:25:41 +01:00
define(function () {
2015-03-29 16:14:10 +02:00
return function () {
2015-03-25 20:25:41 +01:00
var objects = { nodes: {}, links: {} }
var targets = []
var self = this
2015-03-29 16:14:10 +02:00
function saveState(d) {
var s = "#!"
if (d) {
if ("node" in d)
s += "n:" + encodeURIComponent(d.node.nodeinfo.node_id)
2015-03-29 16:14:10 +02:00
if ("link" in d)
s += "l:" + encodeURIComponent(linkId(d.link))
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
if (args[1] !== undefined) {
id = decodeURIComponent(args[1])
2015-03-25 20:25:41 +01: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
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 () {
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-03-25 20:25:41 +01:00
objects.links[linkId(d)] = d
})
}
return self
}
})