Repository navigation
Expand file tree
/
Copy pathviewer.html
More file actions
272 lines (254 loc) · 8.69 KB
/
Copy pathviewer.html
File metadata and controls
272 lines (254 loc) · 8.69 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Software Contract Dependency Visualizer</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.21.1/cytoscape.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-yaml/4.1.0/js-yaml.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f4f4f9;
}
#cy {
width: 100%;
height: 600px;
border: 1px solid #ccc;
background-color: #fff;
}
#input-section {
margin-bottom: 20px;
}
#yaml-input {
width: 100%;
height: 100px;
margin-top: 10px;
}
#controls {
margin-bottom: 10px;
}
button {
padding: 10px 20px;
margin: 5px;
background-color: #007bff;
color: white;
border: none;
cursor: pointer;
border-radius: 4px;
}
button:hover {
background-color: #0056b3;
}
#error {
color: red;
margin-top: 10px;
display: none;
}
</style>
</head>
<body>
<div id="input-section">
<h2>Software Contract Dependency Visualizer</h2>
<p>Upload a YAML file or paste YAML data representing Software Contracts to visualize the call-chain.</p>
<input type="file" id="file-input" accept=".yaml,.yml">
<textarea id="yaml-input" placeholder="Or paste YAML data here..."></textarea>
<button onclick="visualize()">Visualize</button>
<div id="error"></div>
</div>
<div id="controls">
<button onclick="zoomIn()">Zoom In</button>
<button onclick="zoomOut()">Zoom Out</button>
<button onclick="relayout()">Relayout</button>
</div>
<div id="cy"></div>
<script>
let cy;
function initializeCytoscape() {
cy = cytoscape({
container: document.getElementById('cy'),
style: [
{
selector: 'node[type="system"]',
style: {
'background-color': '#007bff',
'label': 'data(label)',
'color': '#fff',
'text-valign': 'center',
'text-halign': 'center',
'shape': 'rectangle',
'width': 'label',
'height': 'label',
'padding': '10px'
}
},
{
selector: 'node[type="contract"]',
style: {
'background-color': '#28a745',
'label': 'data(label)',
'color': '#fff',
'text-valign': 'center',
'text-halign': 'center',
'shape': 'ellipse',
'width': 'label',
'height': 'label',
'padding': '10px'
}
},
{
selector: 'node.selected',
style: {
'border-width': '3px',
'border-color': '#ff0',
'background-color': '#ff9900'
}
},
{
selector: 'edge',
style: {
'width': 2,
'line-color': '#666',
'target-arrow-color': '#666',
'target-arrow-shape': 'triangle',
'curve-style': 'bezier'
}
}
],
layout: {
name: 'cose',
idealEdgeLength: 100,
nodeOverlap: 20,
fit: true,
padding: 30
}
});
// Handle node selection with click or tap
cy.on('tap click', 'node', function(evt) {
const node = evt.target;
const label = node.data('label') || 'Unknown';
const type = node.data('type') || 'Unknown';
// Toggle selection state
if (node.hasClass('selected')) {
node.removeClass('selected');
} else {
node.addClass('selected');
}
// Log selection for debugging
console.log(`Selected node: ${label} (Type: ${type})`);
});
}
function zoomIn() {
const currentZoom = cy.zoom();
cy.zoom(currentZoom * 1.2); // Increase zoom by 20%
cy.center(); // Re-center the graph
}
function zoomOut() {
const currentZoom = cy.zoom();
cy.zoom(currentZoom / 1.2); // Decrease zoom by 20%
cy.center(); // Re-center the graph
}
function relayout() {
cy.layout({
name: 'cose',
idealEdgeLength: 100,
nodeOverlap: 20,
fit: true,
padding: 30
}).run();
}
function parseContracts(data) {
const elements = [];
const contracts = Array.isArray(data) ? data : [data];
contracts.forEach(contract => {
// Add contract node
elements.push({
data: {
id: contract.contract_id,
label: contract.name || 'Unnamed Contract',
type: 'contract'
}
});
// Add calling systems and edges
if (contract.calling_systems) {
contract.calling_systems.forEach(system => {
elements.push({
data: {
id: system.system_id,
label: system.name || 'Unnamed System',
type: 'system'
}
});
elements.push({
data: {
id: `edge-${system.system_id}-${contract.contract_id}`,
source: system.system_id,
target: contract.contract_id
}
});
});
}
// Add dependent contracts and edges
if (contract.dependent_contracts) {
contract.dependent_contracts.forEach(depContract => {
elements.push({
data: {
id: depContract.contract_id,
label: depContract.name || 'Unnamed Contract',
type: 'contract'
}
});
elements.push({
data: {
id: `edge-${contract.contract_id}-${depContract.contract_id}`,
source: contract.contract_id,
target: depContract.contract_id
}
});
});
}
});
return elements;
}
function visualize() {
const fileInput = document.getElementById('file-input');
const yamlInput = document.getElementById('yaml-input');
const errorDiv = document.getElementById('error');
errorDiv.style.display = 'none';
const processYaml = (yamlData) => {
try {
const data = jsyaml.load(yamlData);
const elements = parseContracts(data);
cy.elements().remove();
cy.add(elements);
cy.layout({
name: 'cose',
idealEdgeLength: 100,
nodeOverlap: 20,
fit: true,
padding: 30
}).run();
} catch (e) {
errorDiv.textContent = `Error parsing YAML: ${e.message}`;
errorDiv.style.display = 'block';
}
};
if (fileInput.files.length > 0) {
const file = fileInput.files[0];
const reader = new FileReader();
reader.onload = (e) => processYaml(e.target.result);
reader.readAsText(file);
} else if (yamlInput.value.trim()) {
processYaml(yamlInput.value);
} else {
errorDiv.textContent = 'Please upload a YAML file or paste YAML data.';
errorDiv.style.display = 'block';
}
}
// Initialize Cytoscape on page load
window.onload = initializeCytoscape;
</script>
</body>
</html>