From aa237c2380834caae50bb9b1b89a96d5d6ddee5c Mon Sep 17 00:00:00 2001 From: Nils Schneider Date: Wed, 1 Apr 2015 15:59:05 +0200 Subject: [PATCH] forcegraph: test for localStorage before using it --- helper.js | 11 +++++++++++ lib/forcegraph.js | 16 +++++++++++----- package.json | 1 + 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/helper.js b/helper.js index 844c122..51f07f6 100644 --- a/helper.js +++ b/helper.js @@ -62,6 +62,17 @@ function dictGet(dict, key) { return dictGet(dict[k], key) } +function localStorageTest() { + var test = 'test' + try { + localStorage.setItem(test, test) + localStorage.removeItem(test) + return true + } catch(e) { + return false + } +} + /* Helpers working with nodes */ function offline(d) { diff --git a/lib/forcegraph.js b/lib/forcegraph.js index 64870a3..a2b525e 100644 --- a/lib/forcegraph.js +++ b/lib/forcegraph.js @@ -12,6 +12,9 @@ define(["d3"], function (d3) { var LINK_DISTANCE = 70 function savePositions() { + if (!localStorageTest()) + return + var save = nodes.map( function (d) { return { id: d.id, x: d.x, y: d.y } }) @@ -140,13 +143,16 @@ define(["d3"], function (d3) { window.addEventListener("resize", resize) self.setData = function (data) { - var save = JSON.parse(localStorage.getItem("graph/nodeposition")) var nodePositions = {} - if (save) - save.forEach( function (d) { - nodePositions[d.id] = d - }) + if (localStorageTest()) { + var save = JSON.parse(localStorage.getItem("graph/nodeposition")) + + if (save) + save.forEach( function (d) { + nodePositions[d.id] = d + }) + } links = data.graph.links.filter( function (d) { return !d.vpn diff --git a/package.json b/package.json index 4fa0602..ef2558f 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "has_location": false, "limit": false, "linkId": false, + "localStorageTest": false, "offline": false, "one": false, "online": false,