Skip to content

Commit

Permalink
Merge pull request #18 from iVis-at-Bilkent/unstable
Browse files Browse the repository at this point in the history
Merge unstable to master
  • Loading branch information
hasanbalci authored Oct 3, 2019
2 parents bc9b290 + e0770c4 commit 0342d88
Show file tree
Hide file tree
Showing 6 changed files with 561 additions and 562 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 iVis-at-Bilkent

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cytoscape.js-undo-redo
cytoscape-undo-redo
================================================================================

## Description
Expand Down Expand Up @@ -185,15 +185,15 @@ res: The value returned when the function is executed. This value is to be passe

## Dependencies

* Cytoscape.js ^1.5.0
* Cytoscape.js ^3.3.0



## Usage instructions

Download the library:
* via npm: `npm install cytoscape.js-undo-redo`,
* via bower: `bower install cytoscape.js-undo-redo`, or
* via npm: `npm install cytoscape-undo-redo`,
* via bower: `bower install cytoscape-undo-redo`, or
* via direct download in the repository (probably from a tag).

`require()` the library as appropriate for your project:
Expand All @@ -208,7 +208,7 @@ undoRedo( cytoscape ); // register extension

AMD:
```js
require(['cytoscape', 'cytoscape.js-undo-redo'], function( cytoscape, undoRedo ){
require(['cytoscape', 'cytoscape-undo-redo'], function( cytoscape, undoRedo ){
undoRedo( cytoscape ); // register extension
});
```
Expand All @@ -223,7 +223,7 @@ This project is set up to automatically be published to npm and bower. To publi

1. Set the version number environment variable: `export VERSION=1.2.3`
1. Publish: `gulp publish`
1. If publishing to bower for the first time, you'll need to run `bower register cytoscape.js-undo-redo https://github.com/iVis-at-Bilkent/cytoscape.js-undo-redo.git`
1. If publishing to bower for the first time, you'll need to run `bower register cytoscape-undo-redo https://github.com/iVis-at-Bilkent/cytoscape.js-undo-redo.git`

## Team

Expand Down
83 changes: 77 additions & 6 deletions cytoscape-undo-redo.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
instance.reset = function(undos, redos)
{
this.undoStack = undos || [];
this.redoStack = undos || [];
this.redoStack = redos || [];
}

// Undo last action
Expand Down Expand Up @@ -334,7 +334,7 @@

function returnToPositions(positions) {
var currentPositions = {};
cy.nodes().positions(function (ele, i) {
cy.nodes().not(":parent").positions(function (ele, i) {
if(typeof ele === "number") {
ele = i;
}
Expand Down Expand Up @@ -366,7 +366,7 @@
return positions;
}

function changeParent(param) {
function changeParentOld(param) {
var result = {
};
// If this is first time we should move the node to its new parent and relocate it by given posDiff params
Expand Down Expand Up @@ -399,6 +399,77 @@

return result;
}

function changeParentNew(param) {
var result = {
};
// If this is first time we should move the node to its new parent and relocate it by given posDiff params
// else we should remove the moved eles and restore the eles to restore
if (param.firstTime) {
var newParentId = param.parentData == undefined ? null : param.parentData;
// These eles includes the nodes and their connected edges and will be removed in nodes.move().
// They should be restored in undo
var withDescendant = param.nodes.union(param.nodes.descendants());
var parentData = {};
withDescendant.forEach(function(ele){
if(ele.parent().id())
parentData[ele.id()] = ele.parent();
else
parentData[ele.id()] = null;
});
var positionData = {};
withDescendant.forEach(function(ele){
positionData[ele.id()] = {};
positionData[ele.id()].x = ele.position('x');
positionData[ele.id()].y = ele.position('y');
});
result.oldParent = parentData;
result.oldPosition = positionData;
result.newParent = newParentId;
result.movedEles = withDescendant;
param.nodes.move({"parent": newParentId}).nodes();
var posDiff = {
x: param.posDiffX,
y: param.posDiffY
};

moveNodes(posDiff, result.movedEles);
}
else {
result.oldParent = {};
param.movedEles.forEach(function(ele){
if(ele.parent().id())
result.oldParent[ele.id()] = ele.parent();
else
result.oldParent[ele.id()] = null;
});
result.oldPosition = {};
param.movedEles.forEach(function(ele){
result.oldPosition[ele.id()] = {};
result.oldPosition[ele.id()].x = ele.position("x");
result.oldPosition[ele.id()].y = ele.position("y");
});
result.newParent = param.oldParent;
result.movedEles = param.movedEles;
result.movedEles.forEach(function(ele){
if(typeof result.newParent !== 'object')
ele.move({'parent': result.newParent});
else if(result.newParent[ele.id()] == null)
ele.move({'parent': null});
else
ele.move({'parent': result.newParent[ele.id()].id()});

ele.position(param.oldPosition[ele.id()]);
});
}

if (param.callback) {
result.callback = param.callback; // keep the provided callback so it can be reused after undo/redo
param.callback(result.movedEles); // apply the callback on newly created elements
}

return result;
}

// function registered in the defaultActions below
// to be used like .do('batch', actionList)
Expand Down Expand Up @@ -560,11 +631,11 @@
}
},
"changeParent": {
_do: function (args) {
return changeParent(args);
_do: function (args) {
return (cy.nodes()[0].component ? changeParentNew(args) : changeParentOld(args));
},
_undo: function (args) {
return changeParent(args);
return (cy.nodes()[0].component ? changeParentNew(args) : changeParentOld(args));
}
},
"batch": {
Expand Down
96 changes: 0 additions & 96 deletions gulpfile.js~

This file was deleted.

Loading

0 comments on commit 0342d88

Please sign in to comment.