Skip to content

Commit

Permalink
Add method for serializing network
Browse files Browse the repository at this point in the history
I still have to write one to undo it, but this is a step towards #18
  • Loading branch information
controversial committed May 8, 2016
1 parent 7de291b commit 69aea7a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ <h1>Wikipedia Map</h1>
<script type="text/javascript" src="./js/main_functions.js"> </script>
<!-- Load the main script -->
<script type="text/javascript" src="./js/main.js"> </script>
<!-- Load serialization functions -->
<script type="text/javascript" src="./js/network_serialize.js"> </script>
<!-- Build help popup -->
<script type="text/javascript" src="./js/help.js"> </script>
<!-- Load bindings -->
Expand Down
57 changes: 57 additions & 0 deletions js/network_serialize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Functions for the serialization of a vis.js network. This allows for storing
// a network as JSON and then loading it back later.

function howConcise() {
// Debugging functions to see the number of characters saved by only including
// select values in the JSON output.
var unAbbreviatedLength = JSON.stringify(nodes._data).length +
JSON.stringify(edges._data).length +
JSON.stringify(startpages).length;
var abbreviatedLength = networkToJson().length;
var bytesSaved = unAbbreviatedLength - abbreviatedLength;
return "Abbreviation takes it from " + unAbbreviatedLength + " bytes unabbreviated" +
" to " + abbreviatedLength + " bytes abbreviated, for a total of " + bytesSaved +
" bytes saved!";
}

function abbreviate(node) {
/* Remove all properties from a node dictionary which can easily be reconstructed
Omits the following properties:
- node.id, which is inferred from `label` through `getNeutralId`
- node.color, which is inferred from `level` through `getColor`
- node.value, which is inferred from `startpages` (included separately)
- node.x, which doesn't matter at all for reconstruction
- node.y, which also doesn't matter at all
This leaves us with:
- node.label, which is used to reconstruct node.id
- node.level, which is used to reconstruct node.color
- node.parent, which is used to reconstruct the network's edges */

var newnode = {label: node.label,
level: node.level,
parent: node.parent,};
return node;
}

function networkToJson() {
// Concisely JSON-ize the data needed to quickly reconstruct the network

var out = {};

// Store nodes
var data = nodes._data; // Retreive an object representing nodes data
var vals = Object.keys(data).map(function(k){return data[k];});
var abbv = vals.map(function(v){return abbreviate(v);}); // Process it
out.nodes = abbv; // Store it

// Store startpages
out.startpages = startpages;

return JSON.stringify(out);
}

function networkFromJson() {
return;
}

0 comments on commit 69aea7a

Please sign in to comment.