-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathii.cytoscape.js
400 lines (370 loc) · 10.3 KB
/
ii.cytoscape.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
( function ( spqlib, $ ) {
var g = [];
spqlib.cytoscape = ( function () {
var defaultCytoscapeStyle = [ // the stylesheet for the graph
{
selector: 'node',
style: {
width: 40,
height: 40,
/* 'shape':'rectangle', */
'background-fit': 'contain',
'background-color': 'data(color)',
'background-image': null,
label: 'data(label)',
labelFontSize: 10
}
}, {
selector: 'edge',
style: {
width: 5,
// 'label': 'data(property)',
'line-color': 'data(color)',
'target-arrow-color': 'data(color)',
'target-arrow-shape': 'triangle'
}
}, {
selector: '.multiline-auto',
style: {
'text-wrap': 'wrap',
'text-max-width': 80
}
}, {
selector: '.background',
style: {
/*
* 'text-background-opacity' : 1, 'text-background-color' :
* '#fff',
*/
'text-background-shape': 'roundrectangle',
/*
* 'text-border-color' : '#ccc', 'text-border-width' : 1,
*/
'text-border-opacity': 1,
'text-wrap': 'wrap',
'text-max-width': 100
/*
* 'text-valign': 'center', 'text-halign': 'center'
*/
}
} ],
layouts = [];
layouts.dagre = {
name: 'dagre',
padding: 50
};
layouts.random = {
name: 'random',
padding: 50
};
layouts.preset = {
name: 'preset',
padding: 50
};
layouts.grid = {
name: 'grid',
padding: 50
};
layouts.circle = {
name: 'circle',
padding: 50
};
layouts.concentric = {
name: 'concentric',
padding: 50
};
layouts.breadthfirst = {
name: 'breadthfirst',
padding: 50
};
layouts.cose = {
name: 'cose',
padding: 50
};
function exportAsImage( divId ) {
var png64 = g[ divId ].png( {
full: true
} );
return png64;
//hiddenElement = document.createElement( 'a' );
//hiddenElement.href = png64;// 'data:attachment/text,' +
// encodeURI(textToSave);
//hiddenElement.target = '_blank';
// non funziona in IE
//hiddenElement.download = divId+'-export.png';
//hiddenElement.click();
}
/**
* abilita il tooltip sui nodi di un particolare grafo
* divId - identificativo del grafo
* nodes - se specificato il tooltip viene abilitato solo sulla lista di nodi in ingresso, altrimenti su tutti i nodi
**/
function enableTooltipOnNodes( divId, nodes ) {
var collection = g[ divId ].elements().nodes();
if ( nodes ) {
collection = g[ divId ].collection( nodes );
}
// just use the regular qtip api but on cy elements
collection.qtip(
{
content: function () {
return spqlib.graph.createNodeTooltip( this );
},
position: {
my: 'top center',
at: 'bottom center',
target: $("#"+divId+"-container"),
container: $("#"+divId+"-container"),
viewport: $("#"+divId+"-container")
},
style: {
classes: 'qtip-bootstrap',
tip: {
width: 16,
height: 8
}
}
} );
}
/**
* abilita il tooltip sugli archi di un particolare grafo
* divId - identificativo del grafo
* edges - se specificato il tooltip viene abilitato solo sulla lista di archi in ingresso, altrimenti su tutti gli archi
**/
function enableTooltipOnEdges( divId, edges ) {
var collection = g[ divId ].elements().edges();
if ( edges ) {
collection = g[ divId ].collection( edges );
}
// just use the regular qtip api but on cy elements
collection.qtip( {
content: function () {
return spqlib.graph.createEdgeTooltip( this );
},
position: {
my: 'top center',
at: 'bottom center',
target: $("#"+divId+"-container"),
container: $("#"+divId+"-container"),
viewport: $("#"+divId+"-container")
},
style: {
classes: 'qtip-bootstrap',
tip: {
width: 16,
height: 8
}
}
} );
}
/**
* copia l'oggetto stile
*/
function newDefaultStyleObject() {
var newStyle = [];
for ( var j = 0; j < defaultCytoscapeStyle.length; j++ ) {
newStyle[ j ] = spqlib.util.cloneObject( defaultCytoscapeStyle[ j ] );
}
return newStyle;
}
/**
* assegna l'immagine di sfondo in base all'attributo type presente sul nodo
* graphId - identificativo del grafo
* obj - configurazione per una particolare categoria di nodi
* nodes - se specificato lo sfondo viene applicato solo sulla lista di nodi in ingresso, altrimenti su tutti i nodi
**/
function assignBackgrounImageToNodesByCategory( graphId, obj, nodes ) {
if ( !nodes ) {
var eles = g[ graphId ].elements( 'node[mainType="' +
obj.category + '"]' );
} else {
var eles = nodes.nodes( 'node[mainType="' +
obj.category + '"]' );
}
for ( var j = 0; j < eles.length; j++ ) {
var ele = eles[ j ];
ele.style( 'background-image', obj.image );
ele.style( 'background-opacity', 0 );
}
}
return {
getGraph: function ( graphId ) {
if ( g[ graphId ] ) {
return g[ graphId ];
} else {
return null;
}
},
resize: function ( graphId ) {
g[ graphId ].resize();
},
getElementsFromSourceAndTarget: function ( graphId, source, target ) {
return g[ graphId ].elements( "edge[source='" + source +
"'][target='" + target + "']" );
},
addNodesToGraph: function ( graphId, nodes ) {
return g[ graphId ].add( nodes );
},
addEdgesToGraph: function ( graphId, edges ) {
return g[ graphId ].add( edges );
},
enableTooltipOnNodes: enableTooltipOnNodes,
enableTooltipOnEdges: enableTooltipOnEdges,
assignBackgrounImageToNodesByCategory: assignBackgrounImageToNodesByCategory,
centerGraphToNode: centerGraphToNode,
exportAsImage: exportAsImage,
getGraphElementData: getGraphElementData,
setGraphElementData: setGraphElementData,
newDefaultStyleObject: newDefaultStyleObject,
collapseIncomers: collapseIncomers,
collapseOutgoers: collapseOutgoers,
getSelectedLayout: function getSelectedLayout( config ) {
if ( !layouts[ config.layout ] ) {
alert( 'layout ' + config.layout + ' not supported' );
throw 'layout not supported';
}
return spqlib.util.cloneObject( layouts[ config.layout ] );
},
setLayoutToGraph: function ( graphId, layout ) {
g[ graphId ].layout( layout );
},
/**
* funzione che si occupa di inizializzare il grafo cytoscape
*/
drawGraph: function ( config, nodes, edges, style, layout ) {
var divId = config.divId;
g[ divId ] = cytoscape( {
container: document.getElementById( config.divId ), // container
// to render
// in
elements: {
nodes: nodes,
edges: edges
},
style: style,
layout: layout,
motionBlur: true,
selectionType: 'single',
boxSelectionEnabled: false,
/* autoungrabify: true, */
zoom: 0.9,
minZoom: config.minZoom,
maxZoom: config.maxZoom,
/* hideEdgesOnViewport: true,
hideLabelsOnViewport: true,
textureOnViewport: true,*/
wheelSensitivity: 0.1,
pan: {
x: 0,
y: 0
},
// interaction options:
zoomingEnabled: true,
userZoomingEnabled: true
} );
g[ divId ].config = config;
g[ divId ].on( 'select', 'node', function ( e ) {
} );
g[ divId ].on( 'ready',
function ( e ) {
var conf = e.cy.config,
divIdentifier = conf.divId;
spqlib.graph.assignBgImageToNodes( conf );
if ( conf.rootElement ) {
if ( conf.rootElementColor ) {
var el = e.cy
.getElementById( conf.rootElement );
el.style( 'background-color',
conf.rootElementColor );
}
if ( conf.rootElementImage ) {
var el = e.cy.getElementById( conf.rootElement );
if ( el.length > 0 ) {
el.style( 'background-color',
'white' );
el.style( 'background-image',
conf.rootElementImage );
}
}
}
centerGraphToNode( divIdentifier,
conf.rootElement );
$( '#headertabs ul li a' )
.on(
'click',
function ( event, ui ) {
var tabDivId = $( this )
.attr( 'href' ),
t = $( tabDivId )
.find(
'.ii-graph-container div.ii-graph' )
.attr( 'id' );
if ( t ) {
g[ t ].resize();
$(
'.ii-graph-container' )
.show();
centerGraphToNode(
t,
g[ t ].config.rootElement );
}
} );
$( '#' + conf.divId + '-loader' ).hide();
} );
return g[ divId ];
}
};
function collapseIncomers( graphId, nodeURI, property, direction ) {
var node = getGraphElementById( graphId, nodeURI );
node.incomers( "edge[propertyURI='" + property + "']" ).forEach(
function ( ele ) {
var source = ele.source();
if ( source.degree( true ) == 1 ) {
source.remove();
}
ele.remove();
} );
}
function collapseOutgoers( graphId, nodeURI, property, direction ) {
var node = getGraphElementById( graphId, nodeURI );
node.outgoers( "edge[propertyURI='" + property + "']" ).forEach(
function ( ele ) {
var target = ele.target();
if ( target.degree( true ) == 1 ) {
target.remove();
}
ele.remove();
} );
}
function getGraphElementById( graphId, elementId ) {
return g[ graphId ].getElementById( elementId );
}
function getGraphElementData( graphId, elementId, attribute ) {
return getGraphElementById( graphId, elementId ).data( attribute );
}
function setGraphElementData( graphId, elementId, attribute, value ) {
getGraphElementById( graphId, elementId ).data( attribute, value );
}
function existEdge( graphId, sourceNodeId, targetNodeId ) {
if ( g[ graphId ].elements( "edge[source='" + sourceNodeId +
"'][target='" + targetNodeId + "']" ).length == 0 ) { return false; } else { return true; }
}
/**
* centra il grafo sull'elemento root (se è stato definito nella configurazione oppure su tutto il grafo
*/
function centerGraphToNode( graphId, nodeId ) {
if ( nodeId && nodeId != 'null' ) {
g[ graphId ].center( g[ graphId ]
.getElementById( g[ graphId ].config.rootElement ) );
var pos = g[ graphId ].pan(),
newPos = {
x: pos.x,
y: 45
};
g[ graphId ].pan( newPos );
} else {
g[ graphId ].center();
}
}
} );
}( spqlib, jQuery ) );