summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorAlon Levy <alon@pobox.com>2014-08-17 18:31:30 +0300
committerAlon Levy <alon@pobox.com>2014-08-17 18:31:31 +0300
commit49c6e53c2b677abfa8aa2adabd77f92605ee3744 (patch)
tree147004e1ab2b4140d8b3497862cfbdaa2ed9cfe7 /scripts
parenta4206a91a314226bbff69094069e118bbd54bb65 (diff)
drag&drop load of json, plus save (kinda), some refactoring
save & load json moved from buttons to rhizicore.js added a save as button (need to switch to normal ui for that menu, it's fixed right now) save not correct - instead of a save dialog since it is a json file the browser just displays it (even though it's base64 encoded).
Diffstat (limited to 'scripts')
-rw-r--r--scripts/buttons.js54
-rw-r--r--scripts/rhizicore.js49
2 files changed, 59 insertions, 44 deletions
diff --git a/scripts/buttons.js b/scripts/buttons.js
index 79a961ff..dd449722 100644
--- a/scripts/buttons.js
+++ b/scripts/buttons.js
@@ -6,30 +6,15 @@ var key="#47989379";
$('.save').click(function(){
+ var json = graph.save_to_json();
+ console.log(json);
+ localStorage.setItem(key, json);
+});
- var d = {"nodes":[], "links":[]};
- console.log(nodes);
- for(var i = 0 ; i < nodes.length ; i++){
- var node = nodes[i];
- d['nodes'].push({
- "id":node.id,
- "type":node.type,
- "state":"perm",
- "start":node.start,
- "end":node.end,
- "status": node.status
- });
- }
- for(var j=0 ; j < links.length ; j++){
- var link = links[j];
- d['links'].push({
- "source":link.source.id,
- "target":link.target.id,
- "name":link.name
- });
- }
- console.log(JSON.stringify(d));
- localStorage.setItem(key, JSON.stringify(d));
+$('.saveToFile').click(function() {
+ var json = graph.save_to_json();
+ console.log(json);
+ location.href = 'data:text/json;base64,' + window.btoa(json);
});
$('.load').click(function(){
@@ -40,25 +25,10 @@ $('.load').click(function(){
}
if(acceptLoad){
- links=[];
- nodes=[];
- var data = JSON.parse(localStorage.getItem(key));
- if (data == null) {
- console.log('load callback: no data to load');
- return;
- }
- console.log(data);
- for(var i=0; i<data["nodes"].length; i++){
- var node=data.nodes[i];
- graph.addNodeComplete(node.id,node.type,"perm",new Date(node.start),new Date(node.end),node.status);
- }
-
- for(var j=0; j<data["links"].length; j++){
- var link=data.links[j];
- graph.addLink(link.source,link.target,link.name,"perm");
- }
- graph.recenterZoom();
- graph.update();
+ links=[]; // TODO - not global
+ nodes=[]; // TODO - not global
+ var json_blob = localStorage.getItem(key)
+ graph.load_from_json(json_blob);
}
});
diff --git a/scripts/rhizicore.js b/scripts/rhizicore.js
index f72d5176..ac3988a8 100644
--- a/scripts/rhizicore.js
+++ b/scripts/rhizicore.js
@@ -379,6 +379,53 @@ function myGraph(el) {
force.resume();
}
+ function load_from_json(json) {
+ var data = JSON.parse(json);
+ if (data == null) {
+ console.log('load callback: no data to load');
+ return;
+ }
+ for(var i=0; i<data["nodes"].length; i++){
+ var node=data.nodes[i];
+ graph.addNodeComplete(node.id,node.type,"perm",new Date(node.start),new Date(node.end),node.status);
+ }
+
+ for(var j=0; j<data["links"].length; j++){
+ var link=data.links[j];
+ graph.addLink(link.source,link.target,link.name,"perm");
+ }
+ graph.recenterZoom();
+ graph.update();
+ }
+ this.load_from_json = load_from_json;
+
+ function save_to_json() {
+ var d = {"nodes":[], "links":[]};
+ console.log(nodes);
+ for(var i = 0 ; i < nodes.length ; i++){
+ var node = nodes[i];
+ d['nodes'].push({
+ "id":node.id,
+ "type":node.type,
+ "state":"perm",
+ "start":node.start,
+ "end":node.end,
+ "status": node.status
+ });
+ }
+ for(var j=0 ; j < links.length ; j++){
+ var link = links[j];
+ d['links'].push({
+ "source":link.source.id,
+ "target":link.target.id,
+ "name":link.name
+ });
+ }
+ return JSON.stringify(d);
+ }
+ this.save_to_json = save_to_json;
+
+
force = d3.layout.force()
.distance(120)
.gravity(0.12)
@@ -389,8 +436,6 @@ function myGraph(el) {
nodes = force.nodes();
links = force.links();
-
-
var update = function() {
vis.selectAll(".graph").remove();