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

View File

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