meshviewer/lib/router.js

197 lines
3.8 KiB
JavaScript
Raw Normal View History

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