-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataCrawler.js
238 lines (234 loc) · 7.71 KB
/
DataCrawler.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
var labelType, useGradients, nativeTextSupport, animate;
(function() {
var ua = navigator.userAgent,
iStuff = ua.match(/iPhone/i) || ua.match(/iPad/i),
typeOfCanvas = typeof HTMLCanvasElement,
nativeCanvasSupport = (typeOfCanvas == 'object' || typeOfCanvas == 'function'),
textSupport = nativeCanvasSupport
&& (typeof document.createElement('canvas').getContext('2d').fillText == 'function');
//I'm setting this based on the fact that ExCanvas provides text support for IE
//and that as of today iPhone/iPad current text support is lame
labelType = (!nativeCanvasSupport || (textSupport && !iStuff))? 'Native' : 'HTML';
nativeTextSupport = labelType == 'Native';
useGradients = nativeCanvasSupport;
animate = !(iStuff || !nativeCanvasSupport);
})();
var Log = {
elem: false,
write: function(text){
if (!this.elem)
this.elem = document.getElementById('log');
this.elem.innerHTML = text;
this.elem.style.left = (500 - this.elem.offsetWidth / 2) + 'px';
}
};
function add(fd, node, r, f){
alert("Entrée");
$.ajax({
url: "crawler.php",
type: "POST",
dataType: "json",
//contentType: "json",
data: {"target":node.name, "mainrule":r, "field":f},
error: function(xhr, ajaxOptions, thrownError) {
alert("Error during data collect");
}
}).done(function( arg ) {
var j = arg;
alert(JSON.stringify(j));
for (var i = 0; i < j['noeuds'].length; i++) {
//alert("Noeud: " + j["noeuds"][i]["id"] + " Nom: " + j["noeuds"][i]["nom"]);
var newnode = {id: j["noeuds"][i]["id"], name: j["noeuds"][i]["nom"], data: {color: "#FF0000"}};
fd.graph.addNode(newnode);
fd.graph.addAdjacence(node, newnode);
};
fd.computeIncremental({
iter: 5,
property: 'end',
onStep: function(perc) {
Log.write("loading " + perc + "%");
},
onComplete: function() {
Log.write("done");
fd.animate();
}
});
});
};
function init(t, r, f){
var fd = new $jit.ForceDirected({
//id of the visualization container
injectInto: 'container',
//Enable zooming and panning
//with scrolling and DnD
Navigation: {
enable: true,
type: 'Native',
//Enable panning events only if we're dragging the empty
//canvas (and not a node).
panning: 'avoid nodes',
zooming: 10 //zoom speed. higher is more sensible
},
// Change node and edge styles such as
// color and width.
// These properties are also set per node
// with dollar prefixed data-properties in the
// JSON structure.
Node: {
overridable: true,
dim: 12
},
Edge: {
overridable: true,
color: '#23A4FF',
lineWidth: 0.4
},
// Add node events
Events: {
enable: true,
type: 'Native',
//Change cursor style when hovering a node
onMouseEnter: function() {
fd.canvas.getElement().style.cursor = 'move';
},
onMouseLeave: function() {
fd.canvas.getElement().style.cursor = '';
},
//Update node positions when dragged
onDragMove: function(node, eventInfo, e) {
var pos = eventInfo.getPos();
node.pos.setc(pos.x, pos.y);
fd.plot();
},
onClick: function(node, eventInfo, e) {
// if (node.color == '#FF0000'){
// add(fd, node, mainrule, field);
// node.color = "#0000FF";
// }
// else {
// if(!node) return;
// var html = "<h4>" + node.name + "</h4><b> connections:</b><ul><li>";
// var list = [];
// node.eachAdjacency(function(adj){
// list.push(adj.nodeTo.name);
// });
// $jit.id('inner-details').innerHTML = html + list.join("</li><li>") + "</li></ul>";
// }
if (node) {
alert("Tu a cliqué sur un noeud");
add(fd, node, r, f);
} else{
alert("Tu a cliqué sur le canevas");
};
},
//Implement the same handler for touchscreens
onTouchMove: function(node, eventInfo, e) {
$jit.util.event.stop(e); //stop default touchmove event
this.onDragMove(node, eventInfo, e);
},
},
//Number of iterations for the FD algorithm
iterations: 200,
//Edge length
levelDistance: 130,
// This method is only triggered
// on label creation and only for DOM labels (not native canvas ones).
onCreateLabel: function(domElement, node){
// Create a 'name' and 'close' buttons and add them
// to the main node label
var nameContainer = document.createElement('span'),
closeButton = document.createElement('span'),
style = nameContainer.style;
nameContainer.className = 'name';
nameContainer.innerHTML = node.name;
closeButton.className = 'close';
closeButton.innerHTML = 'x';
domElement.appendChild(nameContainer);
domElement.appendChild(closeButton);
style.fontSize = "0.8em";
style.color = "#ddd";
//Fade the node and its connections when
//clicking the close button
closeButton.onclick = function() {
node.setData('alpha', 0, 'end');
node.eachAdjacency(function(adj) {
adj.setData('alpha', 0, 'end');
});
fd.fx.animate({
modes: ['node-property:alpha',
'edge-property:alpha'],
duration: 500
});
};
//Toggle a node selection when clicking
//its name. This is done by animating some
//node styles like its dimension and the color
//and lineWidth of its adjacencies.
nameContainer.onclick = function() {
//set final styles
fd.graph.eachNode(function(n) {
if(n.id != node.id) delete n.selected;
n.setData('dim', 7, 'end');
n.eachAdjacency(function(adj) {
adj.setDataset('end', {
lineWidth: 0.4,
color: '#23a4ff'
});
});
});
if(!node.selected) {
node.selected = true;
node.setData('dim', 17, 'end');
node.eachAdjacency(function(adj) {
adj.setDataset('end', {
lineWidth: 3,
color: '#36acfb'
});
});
} else {
delete node.selected;
}
//trigger animation to final styles
fd.fx.animate({
modes: ['node-property:dim',
'edge-property:lineWidth:color'],
duration: 500
});
// Build the right column relations list.
// This is done by traversing the clicked node connections.
var html = "<h4>" + node.name + "</h4><b> connections:</b><ul><li>",
list = [];
node.eachAdjacency(function(adj){
if(adj.getData('alpha')) list.push(adj.nodeTo.name);
});
//append connections information
$jit.id('inner-details').innerHTML = html + list.join("</li><li>") + "</li></ul>";
};
},
// Change node styles when DOM labels are placed
// or moved.
onPlaceLabel: function(domElement, node){
var style = domElement.style;
var left = parseInt(style.left);
var top = parseInt(style.top);
var w = domElement.offsetWidth;
style.left = (left - w / 2) + 'px';
style.top = (top + 10) + 'px';
style.display = '';
}
});
var rootnode = {id: t, name: t, data: {color: "#FF0000"}};
fd.loadJSON(rootnode);
fd.refresh();
// fd.computeIncremental({
// iter: 20,
// property: 'end',
// onStep: function(perc) {
// Log.write("loading " + perc + "%");
// },
// onComplete: function() {
// Log.write("done");
// fd.animate();
// }
// });
}