Skip to content

Commit

Permalink
Merge pull request #19 from rdtrimble/patch-2
Browse files Browse the repository at this point in the history
Breaks layout on Save Image, but at least nodes are preserved.
  • Loading branch information
csaladenes authored Apr 15, 2021
2 parents 0297f3b + ca4c265 commit 4c817f5
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions js/food.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function removelink() {
linksform[0][0].children[linksform[0][0].children.length-1].remove("div")
}
function draw() {

saveLS()
data={"nodes": [], "links": []}

for (i = 0; i < nodesform[0][0].children.length; i++) {
Expand All @@ -47,7 +47,19 @@ function draw() {
}
function save(){
d3.select('#save').style('z-index',100).transition().style('opacity',0.9);
st='{"sankey":{"nodes":['
st=generateJSON();
d3.select('#savetext').text(st);
}
function saveLS(){
if (typeof(Storage) !== "undefined") {
// Code for localStorage
window.localStorage.setItem('rawtext',generateJSON());
} else {
// No web storage Support.
}
}
function generateJSON(){
var st='{"sankey":{"nodes":['
for (i = 0; i < nodesform[0][0].children.length; i++) {
st=st+nodesform[0][0].children[i].children[0].value+',';
}
Expand All @@ -64,7 +76,7 @@ function save(){
st=st+',"fixedlayout":'+JSON.stringify(coords);
}
st=st+'}';
d3.select('#savetext').text(st);
return st;
}
function load(){
d3.select('#load').style('z-index',100).transition().style('opacity',0.9);
Expand All @@ -73,7 +85,23 @@ function loadsubmit(){
d3.select('#load').transition().style('opacity',0).style('z-index',-1);
var rawtext=d3.select('#load')[0][0].children[1].value;
if (rawtext!="") {
//parse data
loadraw(rawtext);
}
}
function loadLS(){
if (typeof(Storage) !== "undefined") {
// Code for localStorage
if (window.localStorage.getItem("rawtext") === null) {
//Nothing stored
} else {
loadraw(window.localStorage.getItem('rawtext'));
}
} else {
// No web storage Support.
}
}
function loadraw(rawtext){
//parse data
var rawdata=JSON.parse(rawtext);
if ("sankey" in rawdata) {
var newdata=rawdata.sankey;
Expand Down Expand Up @@ -124,7 +152,6 @@ function loadsubmit(){
else {
change(newdata);
}
}
}

//<!--SANKEY DIAGRAM-->
Expand Down Expand Up @@ -305,6 +332,8 @@ change = function(d) {
e.attr("d", path(2))
};
};
// check if there is any info stored in LocalStorage before first draw
loadLS();
draw();

//<!-- SAVE FUNCTION-->
Expand Down

0 comments on commit 4c817f5

Please sign in to comment.