-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcytoscape-cola.js
516 lines (412 loc) · 15.5 KB
/
cytoscape-cola.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
;(function(){ 'use strict';
// registers the extension on a cytoscape lib ref
var register = function( cytoscape, cola ){
if( !cytoscape || !cola ){ return; } // can't register if cytoscape unspecified
var raf = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame;
var isString = function(o){ return typeof o === typeof ''; };
var isNumber = function(o){ return typeof o === typeof 0; };
var isObject = function(o){ return o != null && typeof o === typeof {}; };
// default layout options
var defaults = {
animate: true, // whether to show the layout as it's running
refresh: 1, // number of ticks per frame; higher is faster but more jerky
maxSimulationTime: 4000, // max length in ms to run the layout
ungrabifyWhileSimulating: false, // so you can't drag nodes during layout
fit: true, // on every layout reposition of nodes, fit the viewport
padding: 30, // padding around the simulation
boundingBox: undefined, // constrain layout bounds; { x1, y1, x2, y2 } or { x1, y1, w, h }
// layout event callbacks
ready: function(){}, // on layoutready
stop: function(){}, // on layoutstop
// positioning options
randomize: false, // use random node positions at beginning of layout
avoidOverlap: true, // if true, prevents overlap of node bounding boxes
handleDisconnected: true, // if true, avoids disconnected components from overlapping
nodeSpacing: function( node ){ return 10; }, // extra spacing around nodes
flow: undefined, // use DAG/tree flow layout if specified, e.g. { axis: 'y', minSeparation: 30 }
alignment: undefined, // relative alignment constraints on nodes, e.g. function( node ){ return { x: 0, y: 1 } }
// different methods of specifying edge length
// each can be a constant numerical value or a function like `function( edge ){ return 2; }`
edgeLength: undefined, // sets edge length directly in simulation
edgeSymDiffLength: undefined, // symmetric diff edge length in simulation
edgeJaccardLength: undefined, // jaccard edge length in simulation
// iterations of cola algorithm; uses default values on undefined
unconstrIter: undefined, // unconstrained initial layout iterations
userConstIter: undefined, // initial layout iterations with user-specified constraints
allConstIter: undefined, // initial layout iterations with all constraints including non-overlap
// infinite layout options
infinite: false // overrides all other options for a forces-all-the-time mode
};
// constructor
// options : object containing layout options
function ColaLayout( options ){
var opts = this.options = {};
for( var i in defaults ){ opts[i] = defaults[i]; }
for( var i in options ){ opts[i] = options[i]; }
}
// runs the layout
ColaLayout.prototype.run = function(){
var layout = this;
var options = this.options;
layout.manuallyStopped = false;
var cy = options.cy; // cy is automatically populated for us in the constructor
var eles = options.eles;
var nodes = eles.nodes();
var edges = eles.edges();
var ready = false;
var 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; }
var typeoffn = typeof function(){};
var getOptVal = function( val, ele ){
if( typeof val === typeoffn ){
var fn = val;
return fn.apply( ele, [ ele ] );
} else {
return val;
}
};
var updateNodePositions = function(){
var x = { min: Infinity, max: -Infinity };
var y = { min: Infinity, max: -Infinity };
for( var i = 0; i < nodes.length; i++ ){
var node = nodes[i];
var scratch = node.scratch('cola');
x.min = Math.min( x.min, scratch.x || 0 );
x.max = Math.max( x.max, scratch.x || 0 );
y.min = Math.min( y.min, scratch.y || 0 );
y.max = Math.max( y.max, scratch.y || 0 );
// update node dims
if( !scratch.updatedDims ){
var nbb = node.boundingBox();
var padding = getOptVal( options.nodeSpacing, node );
scratch.width = nbb.w + 2*padding;
scratch.height = nbb.h + 2*padding;
}
}
nodes.positions(function(i, node){
var scratch = node.scratch().cola;
var retPos;
if( !node.grabbed() && !node.isParent() ){
retPos = {
x: bb.x1 + scratch.x - x.min,
y: bb.y1 + scratch.y - y.min
};
if( !isNumber(retPos.x) || !isNumber(retPos.y) ){
retPos = undefined;
}
}
return retPos;
});
nodes.updateCompoundBounds(); // because the way this layout sets positions is buggy for some reason; ref #878
if( !ready ){
onReady();
ready = true;
}
if( options.fit ){
cy.fit( options.padding );
}
};
var onDone = function(){
if( options.ungrabifyWhileSimulating ){
grabbableNodes.grabify();
}
nodes.off('grab free position', grabHandler);
nodes.off('lock unlock', lockHandler);
// trigger layoutstop when the layout stops (e.g. finishes)
layout.one('layoutstop', options.stop);
layout.trigger({ type: 'layoutstop', layout: layout });
};
var onReady = function(){
// trigger layoutready when each node has had its position set at least once
layout.one('layoutready', options.ready);
layout.trigger({ type: 'layoutready', layout: layout });
};
var ticksPerFrame = options.refresh;
var tickSkip = 1; // frames until a tick; used to slow down sim for debugging
if( options.refresh < 0 ){
tickSkip = Math.abs( options.refresh );
ticksPerFrame = 1;
} else {
ticksPerFrame = Math.max( 1, ticksPerFrame ); // at least 1
}
var adaptor = layout.adaptor = cola.adaptor({
trigger: function( e ){ // on sim event
var TICK = cola.EventType ? cola.EventType.tick : null;
var END = cola.EventType ? cola.EventType.end : null;
switch( e.type ){
case 'tick':
case TICK:
if( options.animate ){
updateNodePositions();
}
break;
case 'end':
case END:
updateNodePositions();
if( !options.infinite ){ onDone(); }
break;
}
},
kick: function(){ // kick off the simulation
//var skip = 0;
var inftick = function(){
if( layout.manuallyStopped ){
onDone();
return true;
}
var ret = adaptor.tick();
if( ret && options.infinite ){ // resume layout if done
adaptor.resume(); // resume => new kick
}
return ret; // allow regular finish b/c of new kick
};
var multitick = function(){ // multiple ticks in a row
var ret;
// skip ticks to slow down layout for debugging
// var thisSkip = skip;
// skip = (skip + 1) % tickSkip;
// if( thisSkip !== 0 ){
// return false;
// }
for( var i = 0; i < ticksPerFrame && !ret; i++ ){
ret = ret || inftick(); // pick up true ret vals => sim done
}
return ret;
};
if( options.animate ){
var frame = function(){
if( multitick() ){ return; }
raf( frame );
};
raf( frame );
} else {
while( !inftick() ){}
}
},
on: function( type, listener ){}, // dummy; not needed
drag: function(){} // not needed for our case
});
layout.adaptor = adaptor;
// if set no grabbing during layout
var grabbableNodes = nodes.filter(':grabbable');
if( options.ungrabifyWhileSimulating ){
grabbableNodes.ungrabify();
}
// handle node dragging
var grabHandler;
nodes.on('grab free position', grabHandler = function(e){
var node = this;
var scrCola = node.scratch().cola;
var pos = node.position();
// update cola pos obj
scrCola.x = pos.x - bb.x1;
scrCola.y = pos.y - bb.y1;
switch( e.type ){
case 'grab':
adaptor.dragstart( scrCola );
adaptor.resume();
break;
case 'free':
adaptor.dragend( scrCola );
break;
}
});
var lockHandler;
nodes.on('lock unlock', lockHandler = function(e){
var node = this;
var scrCola = node.scratch().cola;
if( node.locked() ){
adaptor.dragstart( scrCola );
} else {
adaptor.dragend( scrCola );
}
});
var nonparentNodes = nodes.stdFilter(function( node ){
return !node.isParent();
});
// add nodes to cola
adaptor.nodes( nonparentNodes.map(function( node, i ){
var padding = getOptVal( options.nodeSpacing, node );
var pos = node.position();
var nbb = node.boundingBox();
var struct = node.scratch().cola = {
x: options.randomize || pos.x === undefined ? Math.round( Math.random() * bb.w ) : pos.x,
y: options.randomize || pos.y === undefined ? Math.round( Math.random() * bb.h ) : pos.y,
width: nbb.w + 2*padding,
height: nbb.h + 2*padding,
index: i
};
return struct;
}) );
if( options.alignment ){ // then set alignment constraints
var offsetsX = [];
var offsetsY = [];
nonparentNodes.forEach(function( node ){
var align = getOptVal( options.alignment, node );
var scrCola = node.scratch().cola;
var index = scrCola.index;
if( !align ){ return; }
if( align.x != null ){
offsetsX.push({
node: index,
offset: align.x
});
}
if( align.y != null ){
offsetsY.push({
node: index,
offset: align.y
});
}
});
// add alignment constraints on nodes
var constraints = [];
if( offsetsX.length > 0 ){
constraints.push({
type: 'alignment',
axis: 'x',
offsets: offsetsX
});
}
if( offsetsY.length > 0 ){
constraints.push({
type: 'alignment',
axis: 'y',
offsets: offsetsY
});
}
adaptor.constraints( constraints );
}
// add compound nodes to cola
adaptor.groups( nodes.stdFilter(function( node ){
return node.isParent();
}).map(function( node, i ){ // add basic group incl leaf nodes
var optPadding = getOptVal( options.nodeSpacing, node );
var getPadding = function(d){
return parseFloat( node.style('padding-'+d) );
};
var pleft = getPadding('left') + optPadding;
var pright = getPadding('right') + optPadding;
var ptop = getPadding('top') + optPadding;
var pbottom = getPadding('bottom') + optPadding;
node.scratch().cola = {
index: i,
padding: Math.max( pleft, pright, ptop, pbottom ),
leaves: node.descendants().stdFilter(function( child ){
return !child.isParent();
}).map(function( child ){
return child[0].scratch().cola.index;
})
};
return node;
}).map(function( node ){ // add subgroups
node.scratch().cola.groups = node.descendants().stdFilter(function( child ){
return child.isParent();
}).map(function( child ){
return child.scratch().cola.index;
});
return node.scratch().cola;
}) );
// get the edge length setting mechanism
var length;
var lengthFnName;
if( options.edgeLength != null ){
length = options.edgeLength;
lengthFnName = 'linkDistance';
} else if( options.edgeSymDiffLength != null ){
length = options.edgeSymDiffLength;
lengthFnName = 'symmetricDiffLinkLengths';
} else if( options.edgeJaccardLength != null ){
length = options.edgeJaccardLength;
lengthFnName = 'jaccardLinkLengths';
} else {
length = 100;
lengthFnName = 'linkDistance';
}
var lengthGetter = function( link ){
return link.calcLength;
};
// add the edges to cola
adaptor.links( edges.stdFilter(function( edge ){
return !edge.source().isParent() && !edge.target().isParent();
}).map(function( edge, i ){
var c = edge.scratch().cola = {
source: edge.source()[0].scratch().cola.index,
target: edge.target()[0].scratch().cola.index
};
if( length != null ){
c.calcLength = getOptVal( length, edge );
}
return c;
}) );
adaptor.size([ bb.w, bb.h ]);
if( length != null ){
adaptor[ lengthFnName ]( lengthGetter );
}
// set the flow of cola
if( options.flow ){
var flow;
var defAxis = 'y';
var defMinSep = 50;
if( isString(options.flow) ){
flow = {
axis: options.flow,
minSeparation: defMinSep
};
} else if( isNumber(options.flow) ){
flow = {
axis: defAxis,
minSeparation: options.flow
};
} else if( isObject(options.flow) ){
flow = options.flow;
flow.axis = flow.axis || defAxis;
flow.minSeparation = flow.minSeparation != null ? flow.minSeparation : defMinSep;
} else { // e.g. options.flow: true
flow = {
axis: defAxis,
minSeparation: defMinSep
};
}
adaptor.flowLayout( flow.axis , flow.minSeparation );
}
layout.trigger({ type: 'layoutstart', layout: layout });
adaptor
.avoidOverlaps( options.avoidOverlap )
.handleDisconnected( options.handleDisconnected )
.start( options.unconstrIter, options.userConstIter, options.allConstIter)
;
if( !options.infinite ){
setTimeout(function(){
if( !layout.manuallyStopped ){
adaptor.stop();
}
}, options.maxSimulationTime);
}
return this; // chaining
};
// called on continuous layouts to stop them before they finish
ColaLayout.prototype.stop = function(){
if( this.adaptor ){
this.manuallyStopped = true;
this.adaptor.stop();
}
return this; // chaining
};
cytoscape('layout', 'cola', ColaLayout);
};
if( typeof module !== 'undefined' && module.exports ){ // expose as a commonjs module
module.exports = register;
}
if( typeof define !== 'undefined' && define.amd ){ // expose as an amd/requirejs module
define('cytoscape-cola', function(){
return register;
});
}
if( typeof cytoscape !== 'undefined' ){ // expose to global cytoscape (i.e. window.cytoscape)
register( cytoscape, cola );
}
})();