-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
sigma.parsers.gexf.js
101 lines (86 loc) · 2.97 KB
/
sigma.parsers.gexf.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
;(function(undefined) {
'use strict';
if (typeof sigma === 'undefined')
throw 'sigma is not declared';
// Initialize package:
sigma.utils.pkg('sigma.parsers');
// Just a basic ID generator:
var _id = 0;
function edgeId() {
return 'e' + (_id++);
}
/**
* If the first arguments is a valid URL, this function loads a GEXF file and
* creates a new sigma instance or updates the graph of a given instance. It
* is possible to give a callback that will be executed at the end of the
* process. And if the first argument is a DOM element, it will skip the
* loading step and parse the given XML tree to fill the graph.
*
* @param {string|DOMElement} target The URL of the GEXF file or a valid
* GEXF tree.
* @param {object|sigma} sig A sigma configuration object or a
* sigma instance.
* @param {?function} callback Eventually a callback to execute
* after having parsed the file. It will
* be called with the related sigma
* instance as parameter.
*/
sigma.parsers.gexf = function(target, sig, callback) {
var i,
l,
arr,
obj;
function parse(graph) {
// Adapt the graph:
arr = graph.nodes;
for (i = 0, l = arr.length; i < l; i++) {
obj = arr[i];
obj.id = obj.id;
if (obj.viz && typeof obj.viz === 'object') {
if (obj.viz.position && typeof obj.viz.position === 'object') {
obj.x = obj.viz.position.x;
obj.y = obj.viz.position.y;
}
obj.size = obj.viz.size;
obj.color = obj.viz.color;
}
}
arr = graph.edges;
for (i = 0, l = arr.length; i < l; i++) {
obj = arr[i];
obj.id = typeof obj.id === 'string' ? obj.id : edgeId();
obj.size = obj.weight;
obj.source = '' + obj.source;
obj.target = '' + obj.target;
}
// Update the instance's graph:
if (sig instanceof sigma) {
sig.graph.clear();
arr = graph.nodes;
for (i = 0, l = arr.length; i < l; i++)
sig.graph.addNode(arr[i]);
arr = graph.edges;
for (i = 0, l = arr.length; i < l; i++)
sig.graph.addEdge(arr[i]);
// ...or instanciate sigma if needed:
} else if (typeof sig === 'object') {
sig.graph = graph;
sig = new sigma(sig);
// ...or it's finally the callback:
} else if (typeof sig === 'function') {
sig = null;
callback = sig;
}
// Call the callback if specified:
if (callback) {
callback(sig || graph);
return;
} else
return graph;
}
if (typeof target === 'string')
GexfParser.fetch(target, parse);
else if (typeof target === 'object')
return parse(GexfParser.parse(target));
};
}).call(this);