diff options
| author | Alon Levy <alon@pobox.com> | 2014-08-18 21:24:08 +0300 |
|---|---|---|
| committer | Alon Levy <alon@pobox.com> | 2014-08-18 21:27:01 +0300 |
| commit | 59370f16a20cc56aaa61ca8a9a2aa62d418af22b (patch) | |
| tree | f4c024f8c179a9ae2c73409ba3ec572530b59fc3 /scripts | |
| parent | bfe3c2b399eec1a0fa11dbdedab9ed9b4173835d (diff) | |
adding history, links & nodes only
no atomicity - nodes/links recorded as separate additions
We record both temp (state == 'temp') and not temp nodes,
can be filtered during analysis.
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/buttons.js | 7 | ||||
| -rw-r--r-- | scripts/history.js | 79 | ||||
| -rw-r--r-- | scripts/rhizicore.js | 35 |
3 files changed, 113 insertions, 8 deletions
diff --git a/scripts/buttons.js b/scripts/buttons.js index 8af525c2..824505d3 100644 --- a/scripts/buttons.js +++ b/scripts/buttons.js @@ -49,3 +49,10 @@ $('.set-user').click(function() { $('.save-history').show(); }) }); + +$('.save-history').click(function() { + if (graph.history === undefined) { + throw "History is undefined"; + } + graph.history.save_to_file(); +}); diff --git a/scripts/history.js b/scripts/history.js new file mode 100644 index 00000000..396b3f3c --- /dev/null +++ b/scripts/history.js @@ -0,0 +1,79 @@ +// Once upon a time we shall have a versioned property graph from which history +// will be one extractable aspect, much like a git for graphs. Now we just have +// a plain list of events for a specific user. + +// Enums and chrome don't play along well. Object.freeze I guess? actually rhizi code cuases +// exceptions but that shouldn't break the console, as evidenced by the '__commandLineAPI is not defined' +// error below. +// +// Uncaught TypeError: Can't add property addednodes, object is not extensible rhizicore.js:4 +// Uncaught TypeError: Can't add property text, object is not extensible textanalysis.js:3 +// Uncaught TypeError: Can't add property key, object is not extensible buttons.js:5 +// Uncaught ReferenceError: sentence is not defined robot.js:11 +// Resource interpreted as Font but transferred with MIME type application/font-sfnt: "http://localhost:8000/external/Lato300.ttf". jquery.js:2 +// > document +// ReferenceError: __commandLineAPI is not defined +//var ActionEnum = Enum(); + +function History(user) { + this.records = []; + this.user = user; +} + +var ACTION_TYPE = 'ACTION_TYPE'; +var ACTION_MOUSE = 'ACTION_MOUSE'; +var ACTION_GRAPH_ADD = 'ACTION_GRAPH_ADD'; +var ACTION_GRAPH_DELETE = 'ACTION_GRAPH_DELETE'; + +History.prototype.record = function(d) +{ + if (d === undefined || d.action === undefined) { + throw "Invalid arguments"; + } + d['user'] = this.user; + d['timestamp'] = new Date(); + this.records.push(d); +}; + +History.prototype.save_to_file = function() +{ + // TODO: filename and chrome support (grep json;base64 for other locations) + // perhaps has to wait for FileWriter api? + location.href = 'data:text/json;base64,' + window.btoa(JSON.stringify(this.records)); +}; + +History.prototype.clear_history = function() +{ + this.records = []; +} + +History.prototype.record_nodes_removal = function(ids) +{ + this.record({ + 'action': ACTION_GRAPH_DELETE, + 'node_ids': ids + }); +} + +History.prototype.record_links = function(links) +{ + if (links === undefined || links.length === undefined || links.length <= 0) { + throw "Invalid arguments" + } + this.record({ + 'action': ACTION_GRAPH_ADD, + 'nodes': [], + 'links': links + }); +} + +History.prototype.record_nodes = function(nodes) +{ + if (nodes === undefined || nodes.length === undefined || nodes.length <= 0) { + throw "Invalid arguments" + } + this.record({'action': ACTION_GRAPH_ADD, + 'nodes': nodes, + 'linkes': [] + }); +} diff --git a/scripts/rhizicore.js b/scripts/rhizicore.js index f17830c6..f4e4f3c0 100644 --- a/scripts/rhizicore.js +++ b/scripts/rhizicore.js @@ -37,9 +37,10 @@ function myGraph(el) { id = id.toLowerCase(); var node = findNode(id, null); if (node !== undefined) { + console.log('addNode: node of same id exists: ' + id); //graph.editState(id, null, "temp"); } else { - nodes.push({ + var new_node = { "id": id, "text": text, "type": type, @@ -47,13 +48,16 @@ function myGraph(el) { "start": start, "end": end, "status": status - }); - + }; + nodes.push(new_node); + if (this.history !== undefined) { + this.history.record_nodes([new_node]); + } } - } this.addNodeComplete = function(id, type, state, start, end, status) { + // No history recorded - this is a helper for loading from files / constant graphs var text = id; id = id.toLowerCase(); var node = findNode(id, null); @@ -86,6 +90,9 @@ function myGraph(el) { if (index !== undefined) { nodes.splice(index, 1); } + if (this.history != undefined) { + this.history.record_nodes_removal([id]); + } } this.removeNodes = function(state) { @@ -101,9 +108,11 @@ function myGraph(el) { var index = findNodeIndex(id, state); if (index !== undefined) { nodes.splice(index, 1); - } } + if (ns.length > 0 && this.history !== undefined) { + this.history.record_nodes_removal(ns.map(function(n) { return n.id; })); + } } @@ -173,12 +182,16 @@ function myGraph(el) { if(name)if(name.replace(/ /g,"")==="and")state="temp"; if(!found && ((sourceNode !== undefined) && (targetNode !== undefined))) { - links.push({ + var link = { "source": sourceNode, "target": targetNode, "name": name, "state": state - }); + }; + links.push(link); + if (this.history !== undefined) { + this.history.record_links([link]); + } } } @@ -396,6 +409,7 @@ function myGraph(el) { } graph.recenterZoom(); graph.update(); + graph.clear_history(); } this.load_from_json = load_from_json; @@ -427,10 +441,16 @@ function myGraph(el) { function set_user(user) { this.user = user; + this.history = new History(this.user); console.log('new user: ' + user); } this.set_user = set_user; + function clear_history() { + this.history.clear(); + } + this.clear_history = clear_history; + force = d3.layout.force() .distance(120) .gravity(0.12) @@ -902,7 +922,6 @@ function showInfo(d, i) { $('.info').fadeOut(300); } graph.update(); - } function mousedown() { |
