forcegraph: test for localStorage before using it
This commit is contained in:
parent
eb315ab60a
commit
aa237c2380
11
helper.js
11
helper.js
|
@ -62,6 +62,17 @@ function dictGet(dict, key) {
|
||||||
return dictGet(dict[k], 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 */
|
/* Helpers working with nodes */
|
||||||
|
|
||||||
function offline(d) {
|
function offline(d) {
|
||||||
|
|
|
@ -12,6 +12,9 @@ define(["d3"], function (d3) {
|
||||||
var LINK_DISTANCE = 70
|
var LINK_DISTANCE = 70
|
||||||
|
|
||||||
function savePositions() {
|
function savePositions() {
|
||||||
|
if (!localStorageTest())
|
||||||
|
return
|
||||||
|
|
||||||
var save = nodes.map( function (d) {
|
var save = nodes.map( function (d) {
|
||||||
return { id: d.id, x: d.x, y: d.y }
|
return { id: d.id, x: d.x, y: d.y }
|
||||||
})
|
})
|
||||||
|
@ -140,13 +143,16 @@ define(["d3"], function (d3) {
|
||||||
window.addEventListener("resize", resize)
|
window.addEventListener("resize", resize)
|
||||||
|
|
||||||
self.setData = function (data) {
|
self.setData = function (data) {
|
||||||
var save = JSON.parse(localStorage.getItem("graph/nodeposition"))
|
|
||||||
var nodePositions = {}
|
var nodePositions = {}
|
||||||
|
|
||||||
if (save)
|
if (localStorageTest()) {
|
||||||
save.forEach( function (d) {
|
var save = JSON.parse(localStorage.getItem("graph/nodeposition"))
|
||||||
nodePositions[d.id] = d
|
|
||||||
})
|
if (save)
|
||||||
|
save.forEach( function (d) {
|
||||||
|
nodePositions[d.id] = d
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
links = data.graph.links.filter( function (d) {
|
links = data.graph.links.filter( function (d) {
|
||||||
return !d.vpn
|
return !d.vpn
|
||||||
|
|
|
@ -30,6 +30,7 @@
|
||||||
"has_location": false,
|
"has_location": false,
|
||||||
"limit": false,
|
"limit": false,
|
||||||
"linkId": false,
|
"linkId": false,
|
||||||
|
"localStorageTest": false,
|
||||||
"offline": false,
|
"offline": false,
|
||||||
"one": false,
|
"one": false,
|
||||||
"online": false,
|
"online": false,
|
||||||
|
|
Loading…
Reference in a new issue