Skip to content

Commit

Permalink
highlighting newNode, fixed findClosestOverlappingNode
Browse files Browse the repository at this point in the history
  • Loading branch information
jexp committed Feb 23, 2013
1 parent 657820d commit f1be24a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 4 additions & 0 deletions graph-diagram.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ circle.node {
fill: white;
}

circle.node.new {
stroke: red;
}

circle.node.highlight {
stroke: blue;
}
Expand Down
4 changes: 3 additions & 1 deletion graph-diagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ gd = {};
var classes = [];
var properties = new Properties();

this.isNew = false;

this.class = function(classesString) {
if (arguments.length == 1) {
classes = classesString.split(" ").filter(function(className) {
return className.length > 0 && className != "node";
});
return this;
}
return ["node"].concat(classes);
return (this.isNew ? ["new","node"]:["node"]).concat(classes);
};

this.x = function(x) {
Expand Down
21 changes: 14 additions & 7 deletions graph-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
function findClosestOverlappingNode( node )
{
var closestNode = null;
var closestDistance = Number.MAX_VALUE;
var closestDistance = gd.parameters.radius * graphModel.internalScale();

var allNodes = graphModel.nodeList();

Expand All @@ -54,7 +54,7 @@
if ( candidateNode !== node )
{
var candidateDistance = node.distanceTo( candidateNode ) * graphModel.internalScale();
if ( candidateDistance < 50 && candidateDistance < closestDistance )
if ( candidateDistance < closestDistance )
{
closestNode = candidateNode;
closestDistance = candidateDistance;
Expand All @@ -71,18 +71,21 @@
var node = dragTarget[0][0].__data__;
if ( !newNode && shiftKey )
{
newNode = graphModel.createNode().x( node.x() ).y( node.y() );
newNode = graphModel.createNode().x( node.x() + d3.event.dx).y( node.y() + d3.event.dy);
newNode.isNew = true;
newRelationship = graphModel.createRelationship( node, newNode );
}
if ( newNode )
{
var connectionNode = findClosestOverlappingNode( newNode );
if ( connectionNode )
if ( connectionNode && connectionNode != node )
{
newRelationship.end = connectionNode
newRelationship.end = connectionNode;
newNode.isNew = false;
} else
{
newRelationship.end = newNode;
newNode.isNew = true;
}
node = newNode;
}
Expand All @@ -92,9 +95,13 @@

function dragEnd()
{
if ( newNode && newRelationship && newRelationship.end !== newNode )
if ( newNode )
{
graphModel.deleteNode( newNode );
if ( newRelationship && newRelationship.end !== newNode )
{
graphModel.deleteNode( newNode );
}
newNode.isNew = false;
}
newNode = null;
save( formatMarkup() );
Expand Down

0 comments on commit f1be24a

Please sign in to comment.