From bc8943abc2b7d499e3a1bcabf5ca7331ce0593c8 Mon Sep 17 00:00:00 2001 From: Nils Schneider Date: Tue, 7 Jul 2015 00:06:38 +0200 Subject: [PATCH] router: save view in state --- lib/gui.js | 16 ++++++-- lib/router.js | 111 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 90 insertions(+), 37 deletions(-) diff --git a/lib/gui.js b/lib/gui.js index a1b6b2e..91e63c3 100644 --- a/lib/gui.js +++ b/lib/gui.js @@ -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 diff --git a/lib/router.js b/lib/router.js index b4abc73..2ac6575 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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 } })