Skip to content

Commit

Permalink
#38 more comments, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
canbax committed Dec 22, 2020
1 parent cc94823 commit 41d3dd7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ Collapse all edges in the graph.
`api.expandAllEdges()`
Expand all edges in the graph.

`api.loadJson(jsonStr)`
Load elements from JSON string.

`api.saveJson(elems, filename)`
Saves elements in JSON format to a file. Default value for `elems` is all the elements. Default value for `filename` is 'expand-collapse-output.json'

## Events
Notice that following events are performed for *each* node that is collapsed/expanded. Also, notice that any post-processing layout is performed *after* the event.

Expand Down
33 changes: 31 additions & 2 deletions cytoscape-expand-collapse.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"main": "cytoscape-expand-collapse.js"
},
"scripts": {
"build": "echo 'node version must be 10!' && gulp build",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
Expand Down
31 changes: 30 additions & 1 deletion src/saveLoadUtilities.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
function saveLoadUtilities(cy, api) {
/** converts array of JSON to a cytoscape.js collection (bottom-up recursive)
* keeps information about parents, all nodes added to cytoscape, and nodes to be collapsed
* @param {} jsonArr an array of objects (a JSON array)
* @param {} allNodes a cytoscape.js collection
* @param {} nodes2collapse a cytoscape.js collection
* @param {} node2parent a JS object (simply key-value pairs)
*/
function json2cyCollection(jsonArr, allNodes, nodes2collapse, node2parent) {
// process edges last since they depend on nodes
jsonArr.sort((a) => {
Expand Down Expand Up @@ -49,6 +56,9 @@ function saveLoadUtilities(cy, api) {
return coll;
}

/** clears all the data related to collapsed node
* @param {} e a cytoscape element
*/
function clearCollapseMetaData(e) {
e.data('collapsedChildren', null);
e.removeClass('cy-expand-collapse-collapsed-node');
Expand All @@ -59,6 +69,9 @@ function saveLoadUtilities(cy, api) {
e.data('expandcollapseRenderedCueSize', null);
}

/** converts cytoscape collection to JSON array.(bottom-up recursive)
* @param {} elems
*/
function cyCollection2Json(elems) {
let r = [];
for (let i = 0; i < elems.length; i++) {
Expand All @@ -84,7 +97,10 @@ function saveLoadUtilities(cy, api) {
return r;
}

// { cy: any, collapsedEdges: any, collapsedChildren: any, originalEnds: any }[]
/** returns { cy: any, collapsedEdges: any, collapsedChildren: any, originalEnds: any }[]
* from cytoscape collection
* @param {} col
*/
function halfDeepCopyCollection(col) {
let arr = [];
for (let i = 0; i < col.length; i++) {
Expand All @@ -109,6 +125,13 @@ function saveLoadUtilities(cy, api) {
}

return {

/** Load elements from JSON formatted string representation.
* For collapsed compounds, first add all collapsed nodes as normal nodes then collapse them. Then reposition them.
* For collapsed edges, first add all of the edges then remove collapsed edges from cytoscape.
* For original ends, restore their reference to cytoscape elements
* @param {} txt string
*/
loadJson: function (txt) {
const fileJSON = JSON.parse(txt);
// original endpoints won't exist in cy. So keep a reference.
Expand Down Expand Up @@ -161,6 +184,12 @@ function saveLoadUtilities(cy, api) {
cy.fit();
},


/** saves cytoscape elements (collection) as JSON
* calls elements' json method (https://js.cytoscape.org/#ele.json) when we keep a cytoscape element in the data.
* @param {} elems cytoscape collection
* @param {} filename string
*/
saveJson: function (elems, filename) {
if (!elems) {
elems = cy.$();
Expand Down

0 comments on commit 41d3dd7

Please sign in to comment.