-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigma.parsers.json.js
88 lines (74 loc) · 2.33 KB
/
sigma.parsers.json.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
;(function(undefined) {
'use strict';
if (typeof sigma === 'undefined')
throw 'sigma is not declared';
// Initialize package:
sigma.utils.pkg('sigma.parsers');
sigma.utils.pkg('sigma.utils');
/**
* Just an XmlHttpRequest polyfill for different IE versions.
*
* @return {*} The XHR like object.
*/
sigma.utils.xhr = function() {
if (window.XMLHttpRequest)
return new XMLHttpRequest();
var names,
i;
if (window.ActiveXObject) {
names = [
'Msxml2.XMLHTTP.6.0',
'Msxml2.XMLHTTP.3.0',
'Msxml2.XMLHTTP',
'Microsoft.XMLHTTP'
];
for (i in names)
try {
return new ActiveXObject(names[i]);
} catch (e) {}
}
return null;
};
/**
* This function loads a JSON 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.
*
* @param {string} url The URL of the JSON file.
* @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.json = function(url, sig, callback) {
var graph,
xhr = sigma.utils.xhr();
if (!xhr)
throw 'XMLHttpRequest not supported, cannot load the file.';
xhr.open('GET', url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
graph = JSON.parse(xhr.responseText);
// Update the instance's graph:
if (sig instanceof sigma) {
sig.graph.clear();
sig.graph.read(graph);
// ...or instantiate 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') {
callback = sig;
sig = null;
}
// Call the callback if specified:
if (callback)
callback(sig || graph);
}
};
xhr.send();
};
}).call(this);