Browse Source

refactor router

Nils Schneider 9 years ago
parent
commit
f5dbd56a70
3 changed files with 21 additions and 14 deletions
  1. 1 1
      lib/main.js
  2. 0 2
      lib/map.js
  3. 20 11
      lib/router.js

+ 1 - 1
lib/main.js

@@ -45,7 +45,7 @@ function (Router, Map, Sidebar, Tabs, Container, Meshstats, Linklist, Nodelist,
     Promise.all(urls.map(getJSON))
       .then(function (d) { createGUI(); return d })
       .then(handle_data)
-      .then(function () { router.loadState(window.location.hash) })
+      .then(function () { router.start() })
 
     function handle_data(data) {
       var nodedict = data[0]

+ 0 - 2
lib/map.js

@@ -108,8 +108,6 @@ define(function () {
       groupOnline = L.featureGroup(markersOnline).addTo(map)
       groupNew = L.featureGroup(markersNew).addTo(map)
       groupLost = L.featureGroup(markersLost).addTo(map)
-
-      resetView()
     }
 
     function resetView() {

+ 20 - 11
lib/router.js

@@ -43,28 +43,41 @@ define(function () {
 
     function loadState(s) {
       if (!s)
-        return
+        return false
 
       if (!s.startsWith("#!"))
-        return
+        return false
 
       var args = s.slice(2).split(":")
 
-      if (args.length == 1 && args[0] == "")
-        resetView(false)
-
       if (args[0] === "n") {
         var id = args[1]
 
-        if (id in objects.nodes)
+        if (id in objects.nodes) {
           gotoNode(objects.nodes[id])
+          return true
+        }
       }
 
       if (args[0] === "l") {
         var id = args[1]
 
-        if (id in objects.links)
+        if (id in objects.links) {
           gotoLink(objects.links[id])
+          return true
+        }
+      }
+
+      return false
+    }
+
+    self.start = function () {
+      if (!loadState(window.location.hash))
+        resetView(false)
+
+      window.onpopstate = function (d) {
+        if (!loadState(d.state))
+          resetView(false)
       }
     }
 
@@ -94,8 +107,6 @@ define(function () {
                       }
     self.addTarget = function (d) { targets.push(d) }
 
-    self.loadState = loadState
-
     self.setData = function (nodes, links) {
       objects.nodes = {}
       objects.links = {}
@@ -109,8 +120,6 @@ define(function () {
       })
     }
 
-    window.onpopstate = function (d) { loadState(d.state) }
-
     return self
   }
 })