Created
June 6, 2014 20:19
-
-
Save DavidDurman/d4250dd3abe5efc9a1af to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function layout() { | |
try { | |
var adjacencyList = eval('adjacencyList = ' + $('#adjacency-list').val()); | |
} catch (e) { alert(e); } | |
var cells = buildGraphFromAdjacencyList(adjacencyList); | |
graph.resetCells(cells); | |
joint.layout.DirectedGraph.layout(graph, { setLinkVertices: false }); | |
_.each(cells, function(cell) { | |
if (!(cell instanceof joint.dia.Link)) return; | |
if (cell.get('source').id === cell.get('target').id) { | |
console.log('makeLoopLink'); | |
makeLoopLink(paper, cell); | |
} | |
}); | |
} | |
function makeLoopLink(paper, link) { | |
var linkWidth = 40; | |
var preferredSide = 'top'; | |
var graph = paper.model; | |
var paperRect = g.rect({x: 0, y: 0, width: paper.options.width, height: paper.options.height}); | |
var bbox = V(paper.findViewByModel(link.get('source').id).el).bbox(false, paper.viewport); | |
var p1, p2; | |
var sides = _.uniq([preferredSide, 'top', 'bottom', 'left', 'right']); | |
var sideFound = _.find(sides, function(side) { | |
var centre, dx = 0, dy = 0; | |
switch (side) { | |
case 'top': | |
centre = g.point(bbox.x + bbox.width / 2, bbox.y - linkWidth); | |
dx = linkWidth / 2; | |
break; | |
case 'bottom': | |
centre = g.point(bbox.x + bbox.width / 2, bbox.y + bbox.height + linkWidth); | |
dx = linkWidth / 2; | |
break; | |
case 'left': | |
centre = g.point(bbox.x - linkWidth, bbox.y + bbox.height / 2); | |
dy = linkWidth / 2; | |
break; | |
case 'right': | |
centre = g.point(bbox.x + bbox.width + linkWidth, bbox.y + bbox.height / 2); | |
dy = linkWidth / 2; | |
break; | |
}; | |
p1 = g.point(centre).offset(-dx, -dy); | |
p2 = g.point(centre).offset(dx, dy); | |
return paperRect.containsPoint(p1) && paperRect.containsPoint(p2); | |
}, this); | |
if (sideFound) link.set('vertices', [p1,p2]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment