-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathlayout.js
152 lines (120 loc) · 3.87 KB
/
layout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const isFunction = function(o){ return typeof o === 'function'; };
const defaults = require('./defaults');
const assign = require('./assign');
const dagre = require('dagre');
// constructor
// options : object containing layout options
function DagreLayout( options ){
this.options = assign( {}, defaults, options );
}
// runs the layout
DagreLayout.prototype.run = function(){
let options = this.options;
let layout = this;
let cy = options.cy; // cy is automatically populated for us in the constructor
let eles = options.eles;
let getVal = function( ele, val ){
return isFunction(val) ? val.apply( ele, [ ele ] ) : val;
};
let bb = options.boundingBox || { x1: 0, y1: 0, w: cy.width(), h: cy.height() };
if( bb.x2 === undefined ){ bb.x2 = bb.x1 + bb.w; }
if( bb.w === undefined ){ bb.w = bb.x2 - bb.x1; }
if( bb.y2 === undefined ){ bb.y2 = bb.y1 + bb.h; }
if( bb.h === undefined ){ bb.h = bb.y2 - bb.y1; }
let g = new dagre.graphlib.Graph({
multigraph: true,
compound: true
});
let gObj = {};
let setGObj = function( name, val ){
if( val != null ){
gObj[ name ] = val;
}
};
setGObj( 'nodesep', options.nodeSep );
setGObj( 'edgesep', options.edgeSep );
setGObj( 'ranksep', options.rankSep );
setGObj( 'rankdir', options.rankDir );
setGObj( 'align', options.align);
setGObj( 'ranker', options.ranker );
setGObj( 'acyclicer', options.acyclicer);
g.setGraph( gObj );
g.setDefaultEdgeLabel(function() { return {}; });
g.setDefaultNodeLabel(function() { return {}; });
// add nodes to dagre
let nodes = eles.nodes();
for( let i = 0; i < nodes.length; i++ ){
let node = nodes[i];
let nbb = node.layoutDimensions( options );
g.setNode( node.id(), {
width: nbb.w,
height: nbb.h,
name: node.id()
} );
// console.log( g.node(node.id()) );
}
// set compound parents
for( let i = 0; i < nodes.length; i++ ){
let node = nodes[i];
if( node.isChild() ){
g.setParent( node.id(), node.parent().id() );
}
}
// add edges to dagre
let edges = eles.edges().stdFilter(function( edge ){
return !edge.source().isParent() && !edge.target().isParent(); // dagre can't handle edges on compound nodes
});
for( let i = 0; i < edges.length; i++ ){
let edge = edges[i];
g.setEdge( edge.source().id(), edge.target().id(), {
minlen: getVal( edge, options.minLen ),
weight: getVal( edge, options.edgeWeight ),
name: edge.id()
}, edge.id() );
// console.log( g.edge(edge.source().id(), edge.target().id(), edge.id()) );
}
dagre.layout( g );
let gNodeIds = g.nodes();
for( let i = 0; i < gNodeIds.length; i++ ){
let id = gNodeIds[i];
let n = g.node( id );
cy.getElementById(id).scratch().dagre = n;
}
let dagreBB;
if( options.boundingBox ){
dagreBB = { x1: Infinity, x2: -Infinity, y1: Infinity, y2: -Infinity };
nodes.forEach(function( node ){
let dModel = node.scratch().dagre;
dagreBB.x1 = Math.min( dagreBB.x1, dModel.x );
dagreBB.x2 = Math.max( dagreBB.x2, dModel.x );
dagreBB.y1 = Math.min( dagreBB.y1, dModel.y );
dagreBB.y2 = Math.max( dagreBB.y2, dModel.y );
});
dagreBB.w = dagreBB.x2 - dagreBB.x1;
dagreBB.h = dagreBB.y2 - dagreBB.y1;
} else {
dagreBB = bb;
}
let constrainPos = function( p ){
if( options.boundingBox ){
let xPct = dagreBB.w === 0 ? 0 : (p.x - dagreBB.x1) / dagreBB.w;
let yPct = dagreBB.h === 0 ? 0 : (p.y - dagreBB.y1) / dagreBB.h;
return {
x: bb.x1 + xPct * bb.w,
y: bb.y1 + yPct * bb.h
};
} else {
return p;
}
};
nodes.layoutPositions(layout, options, function( ele ){
ele = typeof ele === "object" ? ele : this;
let dModel = ele.scratch().dagre;
return constrainPos({
x: dModel.x,
y: dModel.y
});
});
return this; // chaining
};
module.exports = DagreLayout;