router: save view in state

This commit is contained in:
Nils Schneider 2015-07-07 00:06:38 +02:00
parent 2d1fbba40f
commit bc8943abc2
2 changed files with 90 additions and 37 deletions

View File

@ -40,7 +40,12 @@ function (chroma, Map, Sidebar, Tabs, Container, Meshstats, Linklist,
dataTargets.push(content)
router.addTarget(content)
router.reload()
}
function mkView(K) {
return function () {
addContent(K)
}
}
contentDiv = document.createElement("div")
@ -56,11 +61,11 @@ function (chroma, Map, Sidebar, Tabs, Container, Meshstats, Linklist,
if (content.constructor === Map) {
buttonToggle.classList.remove("next-graph")
buttonToggle.classList.add("next-map")
addContent(ForceGraph)
router.view("g")
} else {
buttonToggle.classList.remove("next-map")
buttonToggle.classList.add("next-graph")
addContent(Map)
router.view("m")
}
}
contentDiv.appendChild(buttonToggle)
@ -98,7 +103,10 @@ function (chroma, Map, Sidebar, Tabs, Container, Meshstats, Linklist,
router.addTarget(title)
router.addTarget(infobox)
addContent(Map)
router.addView("m", mkView(Map))
router.addView("g", mkView(ForceGraph))
router.view("m")
self.setData = function (data) {
latestData = data

View File

@ -3,19 +3,27 @@ define(function () {
var self = this
var objects = { nodes: {}, links: {} }
var targets = []
var views = {}
var currentView
var currentObject
var running = false
function saveState(d) {
var s = "#!"
function saveState() {
var e = []
if (d) {
if ("node" in d)
s += "n:" + encodeURIComponent(d.node.nodeinfo.node_id)
if (currentView)
e.push("v:" + currentView)
if ("link" in d)
s += "l:" + encodeURIComponent(d.link.id)
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))
}
var s = "#!" + e.join(";")
window.history.pushState(s, undefined, s)
}
@ -26,8 +34,10 @@ define(function () {
t.resetView()
})
if (push)
if (push) {
currentObject = undefined
saveState()
}
}
function gotoNode(d) {
@ -59,24 +69,38 @@ define(function () {
if (!s.startsWith("#!"))
return false
var args = s.slice(2).split(":")
var id
var targetSet = false
if (args[1] !== undefined) {
id = decodeURIComponent(args[1])
s.slice(2).split(";").forEach(function (d) {
var args = d.split(":")
if (args[0] === "n" && id in objects.nodes) {
gotoNode(objects.nodes[id])
return true
if (args[0] === "v" && args[1] in views) {
currentView = args[1]
views[args[1]]()
}
if (args[0] === "l" && id in objects.links) {
gotoLink(objects.links[id])
return true
}
}
var id
return false
if (args[0] === "n") {
id = decodeURIComponent(args[1])
if (id in objects.nodes) {
currentObject = { node: objects.nodes[id] }
gotoNode(objects.nodes[id])
targetSet = true
}
}
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
}
self.start = function () {
@ -91,10 +115,33 @@ define(function () {
}
}
self.view = function (d) {
if (d in views) {
views[d]()
if (running) {
currentView = d
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()
}
}
}
self.node = function (d) {
return function () {
if (gotoNode(d))
saveState({ node: d })
if (gotoNode(d)) {
currentObject = { node: d }
saveState()
}
return false
}
@ -102,8 +149,10 @@ define(function () {
self.link = function (d) {
return function () {
if (gotoLink(d))
saveState({ link: d })
if (gotoLink(d)) {
currentObject = { link: d }
saveState()
}
return false
}
@ -123,6 +172,10 @@ define(function () {
})
}
self.addView = function (k, d) {
views[k] = d
}
self.setData = function (data) {
objects.nodes = {}
objects.links = {}
@ -136,14 +189,6 @@ define(function () {
})
}
self.reload = function () {
if (!running)
return
if (!loadState(window.history.state))
resetView(false)
}
return self
}
})