-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.html
197 lines (177 loc) · 5.68 KB
/
graph.html
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
<html>
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.8.0/vis.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.8.0/vis.min.css" rel="stylesheet" type="text/css" />
<style type="text/css">
#mynetwork {
width: 100%;
height: 100%;
border: 1px solid lightgray;
}
</style>
<script src="graph.js"></script>
</head>
<body>
<input type="text" id="searchbox" />
<div id="mynetwork"></div>
<script type="text/javascript">
var allNodes;
var network;
var nodesDataSet;
var edgesDataSet;
var highlightActive = false;
var elem = document.getElementById("searchbox");
elem.addEventListener("change", findNode);
function findNode(){
var text = document.getElementById('searchbox').value;
var re = new RegExp(".*" + text + ".*");
greyOutAll();
hideAllLabels();
for(var nodeId in allNodes){
if(nodeId.match(re)){
allNodes[nodeId].color = undefined;
displayLabels(Array(nodeId));
}
}
updateDataset();
}
function httpGet(theUrl)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
xmlHttp.send( null );
return xmlHttp.responseText;
}
function redrawAll(){
var options = {
nodes: {
shape: 'dot',
scaling: {
min: 10,
max: 30,
label: {
min: 8,
max: 30,
drawThreshold: 12,
maxVisible: 20
}
},
font: {
size: 12,
face: 'Tahoma'
}
},
edges: {
width: 0.15,
color: {inherit: 'from'},
smooth: {
type: 'continuous'
}
},
physics: true,
interaction: {
tooltipDelay: 200,
hideEdgesOnDrag: true
}
};
// get a JSON object
edgesDataset = new vis.DataSet(edges);
nodesDataset = new vis.DataSet(nodes);
allNodes = nodesDataset.get({returnType:"Object"});
// create a network
var container = document.getElementById('mynetwork');
var data = {nodes: nodesDataset, edges: edgesDataset};
network = new vis.Network(container, data, options);
network.on("click",neighbourhoodHighlight);
}
function neighbourhoodHighlight(params) {
// if something is selected:
if (params.nodes.length > 0) {
highlightActive = true;
var i,j;
var selectedNode = params.nodes[0];
var degrees;
if(allNodes[selectedNode].group == 'roles' || allNodes[selectedNode].group == 'resources' ){
degrees = 2;
} else if(allNodes[selectedNode].group == 'policies' || allNodes[selectedNode].group == 'role_policies'){
degrees = 1;
} else {
degrees = 1;
}
greyOutAll();
hideAllLabels();
var connectedNodes = network.getConnectedNodes(selectedNode);
var allConnectedNodes = [];
// get the second degree nodes
for (i = 1; i < degrees; i++) {
for (j = 0; j < connectedNodes.length; j++) {
allConnectedNodes = allConnectedNodes.concat(network.getConnectedNodes(connectedNodes[j]).filter(function(node){
return allNodes[node].group != allNodes[selectedNode].group;
}));
}
}
// all second degree nodes get their own color and their label back
displayLabels(allConnectedNodes);
for (i = 0; i < allConnectedNodes.length; i++) {
allNodes[allConnectedNodes[i]].color = undefined;
}
// all first degree nodes get their own color and their label back
displayLabels(connectedNodes);
for (i = 0; i < connectedNodes.length; i++) {
allNodes[connectedNodes[i]].color = undefined;
}
// the main node gets its own color and its label back.
allNodes[selectedNode].color = undefined;
displayLabels(Array(selectedNode));
}
else if (highlightActive === true) {
// reset all nodes
for (var nodeId in allNodes) {
allNodes[nodeId].color = undefined;
if (allNodes[nodeId].hiddenLabel !== undefined) {
allNodes[nodeId].label = allNodes[nodeId].hiddenLabel;
allNodes[nodeId].hiddenLabel = undefined;
}
}
highlightActive = false
}
updateDataset();
}
function updateDataset(){
// transform the object into an array
var updateArray = [];
for (nodeId in allNodes) {
if (allNodes.hasOwnProperty(nodeId)) {
updateArray.push(allNodes[nodeId]);
}
}
nodesDataset.update(updateArray);
}
function greyOutAll(){
// mark all nodes as hard to read.
for (var nodeId in allNodes) {
allNodes[nodeId].color = 'rgba(200,200,200,0.5)';
}
}
function hideAllLabels(){
// mark all nodes as hard to read.
for (var nodeId in allNodes) {
if (allNodes[nodeId].hiddenLabel === undefined) {
allNodes[nodeId].hiddenLabel = allNodes[nodeId].label;
allNodes[nodeId].label = undefined;
}
}
}
function displayLabels(nodes){
// all first degree nodes get their own color and their label back
for (i = 0; i < nodes.length; i++) {
if (allNodes[nodes[i]].hiddenLabel !== undefined) {
allNodes[nodes[i]].label = allNodes[nodes[i]].hiddenLabel;
allNodes[nodes[i]].hiddenLabel = undefined;
}
}
}
redrawAll();
</script>
</body>
</html>