|
16 | 16 | };
|
17 | 17 | var edges = new vis.DataSet([]);
|
18 | 18 |
|
19 |
| - var data = { |
| 19 | + let data = { |
20 | 20 | nodes: nodes,
|
21 | 21 | edges: edges
|
22 | 22 | };
|
23 |
| - var options = { |
24 |
| - physics: { |
25 |
| - enabled: true, |
26 |
| - solver: 'repulsion' |
27 |
| - }, |
| 23 | + const options = { |
28 | 24 | edges: {
|
29 | 25 | smooth: false // Make edges straight lines.
|
30 | 26 | },
|
|
33 | 29 | },
|
34 | 30 | layout: {} // The layout of the directed graph
|
35 | 31 | };
|
36 |
| - var defaultGraphDirection = ''; // The graph direction from the dgml file itself |
37 |
| - setDefaultGraphDirection(); |
38 |
| - var container = document.getElementById('network'); |
39 |
| - var seed = Math.random(); |
40 |
| - options.layout.randomSeed = seed; |
41 |
| - var network = new vis.Network(container, data, options); |
42 |
| - network.on("stabilizationIterationsDone", function () { |
43 |
| - network.setOptions( { physics: false } ); |
44 |
| - }); |
45 |
| - |
46 |
| - const vscode = acquireVsCodeApi(); |
| 32 | + let defaultGraphDirection = ''; // The graph direction from the dgml file itself |
| 33 | + const container = document.getElementById('network'); |
| 34 | + const hierarchicalOptionsDirection = document.getElementById('hierarchicalOptions_direction'); |
| 35 | + const hierarchicalOptionsSortMethod = document.getElementById('hierarchicalOptions_sortmethod'); |
| 36 | + const showHierarchicalOptionsCheckbox = document.getElementById('showHierarchicalOptions'); |
| 37 | + const hierarchicalOptionsDirectionSelect = document.getElementById('direction'); |
| 38 | + const hierarchicalOptionsSortMethodSelect = document.getElementById('sortMethod'); |
| 39 | + const saveAsPngButton = document.getElementById('saveAsPngButton'); |
| 40 | + const saveSelectionAsPngButton = document.getElementById('saveSelectionAsPngButton'); |
| 41 | + const copyToClipboardButton = document.getElementById('copyToClipboardButton'); |
| 42 | + const graphDiv = document.getElementById('network'); |
| 43 | + const selectionLayer = document.getElementById('selectionLayer'); |
47 | 44 | const helpTextDiv = document.getElementById('helpText');
|
| 45 | + showHierarchicalOptions(); |
| 46 | + |
| 47 | + const vscode = acquireVsCodeApi(); |
48 | 48 | let lastMouseX = lastMouseY = 0;
|
49 | 49 | let mouseX = mouseY = 0;
|
50 | 50 | let selection;
|
51 | 51 | // get the vis.js canvas
|
52 |
| - var graphDiv = document.getElementById('network'); |
53 | 52 | var visDiv = graphDiv.firstElementChild;
|
54 | 53 | var graphCanvas = visDiv.firstElementChild;
|
55 |
| - const selectionLayer = document.getElementById('selectionLayer'); |
56 | 54 | const selectionCanvas = selectionLayer.firstElementChild;
|
57 | 55 | let selectionCanvasContext;
|
58 | 56 |
|
59 | 57 | // add button event listeners
|
60 |
| - const saveAsPngButton = document.getElementById('saveAsPngButton'); |
61 | 58 | saveAsPngButton.addEventListener('click', saveAsPng);
|
62 |
| - const saveSelectionAsPngButton = document.getElementById('saveSelectionAsPngButton'); |
63 | 59 | saveSelectionAsPngButton.addEventListener('click', saveSelectionAsPng);
|
64 |
| - const copyToClipboardButton = document.getElementById('copyToClipboardButton'); |
65 | 60 | copyToClipboardButton.addEventListener('click', copyToClipboard);
|
66 | 61 | copyToClipboardButton.style['display'] = 'none'; // TODO: Remove when copyToClipboard is implemented
|
67 |
| - const showHierarchicalOptionsButton = document.getElementById('showHierarchicalOptions'); |
68 |
| - showHierarchicalOptionsButton.addEventListener('click', showHierarchicalOptions); |
69 |
| - const hierarchicalDirectionSelect = document.getElementById('direction'); |
70 |
| - hierarchicalDirectionSelect.addEventListener('change', setNetworkLayout); |
71 |
| - const hierarchicalSortMethodSelect = document.getElementById('sortMethod'); |
72 |
| - hierarchicalSortMethodSelect.addEventListener('change', setNetworkLayout); |
| 62 | + showHierarchicalOptionsCheckbox.addEventListener('click', showHierarchicalOptions); |
| 63 | + hierarchicalOptionsDirectionSelect.addEventListener('change', setNetworkLayout); |
| 64 | + hierarchicalOptionsSortMethodSelect.addEventListener('change', setNetworkLayout); |
73 | 65 |
|
74 | 66 | function mouseUpEventListener(event) {
|
75 | 67 | // Convert the canvas to image data that can be saved
|
|
151 | 143 | }
|
152 | 144 |
|
153 | 145 | function saveSelectionAsPng() {
|
154 |
| - graphDiv = document.getElementById('network'); |
155 | 146 | visDiv = graphDiv.firstElementChild;
|
156 | 147 | graphCanvas = visDiv.firstElementChild;
|
157 | 148 |
|
|
174 | 165 | }
|
175 | 166 |
|
176 | 167 | function saveAsPng() {
|
177 |
| - graphDiv = document.getElementById('network'); |
178 | 168 | visDiv = graphDiv.firstElementChild;
|
179 | 169 | graphCanvas = visDiv.firstElementChild;
|
180 | 170 |
|
|
259 | 249 | }
|
260 | 250 |
|
261 | 251 | function setDefaultGraphDirection() {
|
262 |
| - const hierarchicalOptionsDirection = document.getElementById('direction'); |
263 | 252 | let selectedOption = '';
|
264 | 253 | selectedOption = defaultGraphDirection === '' ? 'Fixed' : defaultGraphDirection;
|
265 |
| - for (var i, j = 0; i = hierarchicalOptionsDirection.options[j]; j++) { |
| 254 | + for (var i, j = 0; i = hierarchicalOptionsDirectionSelect.options[j]; j++) { |
266 | 255 | if (i.value === selectedOption) {
|
267 |
| - hierarchicalOptionsDirection.selectedIndex = j; |
| 256 | + hierarchicalOptionsDirectionSelect.selectedIndex = j; |
268 | 257 | break;
|
269 | 258 | }
|
270 | 259 | }
|
|
291 | 280 | }
|
292 | 281 |
|
293 | 282 | function setNetworkLayout() {
|
294 |
| - const hierarchicalOptionsDirection = document.getElementById('hierarchicalOptions_direction'); |
295 |
| - const hierarchicalOptionsSortMethod = document.getElementById('hierarchicalOptions_sortmethod'); |
296 |
| - const showHierarchicalOptionsCheckbox = document.getElementById('showHierarchicalOptions'); |
297 | 283 | hierarchicalOptionsDirection.style['display'] = showHierarchicalOptionsCheckbox.checked ? 'block' : 'none';
|
298 | 284 | hierarchicalOptionsSortMethod.style['display'] = showHierarchicalOptionsCheckbox.checked ? 'block' : 'none';
|
299 | 285 |
|
300 |
| - options.layout.hierarchical = { |
301 |
| - enabled: false |
| 286 | + options.layout ={ |
| 287 | + hierarchical: { |
| 288 | + enabled: false |
| 289 | + } |
302 | 290 | };
|
303 | 291 | options.physics = {
|
304 | 292 | enabled: true,
|
305 |
| - solver: 'repulsion' |
| 293 | + barnesHut: { |
| 294 | + springConstant: 0, |
| 295 | + avoidOverlap: 0.8 |
| 296 | + } |
306 | 297 | };
|
307 | 298 | if (showHierarchicalOptionsCheckbox.checked) {
|
308 |
| - const hierarchicalOptionsDirectionSelect = document.getElementById('direction'); |
309 | 299 | if (hierarchicalOptionsDirectionSelect.value && hierarchicalOptionsDirectionSelect.value === 'Random') {
|
310 | 300 | nodes.getIds().forEach(id => storeCoordinates(nodes.get(id)));
|
311 | 301 | seed = Math.random();
|
312 | 302 | options.layout.randomSeed = seed;
|
313 | 303 | } else if (hierarchicalOptionsDirectionSelect.value && hierarchicalOptionsDirectionSelect.value === 'Fixed') {
|
314 | 304 | nodes.getIds().forEach(id => restoreCoordinates(nodes.get(id)));
|
315 | 305 | } else {
|
316 |
| - const hierarchicalOptionsSortMethodSelect = document.getElementById('sortMethod'); |
317 |
| - options.layout.hierarchical = { |
| 306 | + nodes.getIds().forEach(id => storeCoordinates(nodes.get(id))); |
| 307 | + options.layout = { |
| 308 | + hierarchical: { |
| 309 | + enabled: true, |
| 310 | + levelSeparation: 200, |
| 311 | + nodeSpacing: 200, |
| 312 | + direction: hierarchicalOptionsDirectionSelect.value ? hierarchicalOptionsDirectionSelect.value : defaultGraphDirection, |
| 313 | + sortMethod: hierarchicalOptionsSortMethodSelect.value ? hierarchicalOptionsSortMethodSelect.value : 'hubsize' |
| 314 | + } |
| 315 | + }; |
| 316 | + options.physics = { |
318 | 317 | enabled: true,
|
319 |
| - direction: hierarchicalOptionsDirectionSelect.value ? hierarchicalOptionsDirectionSelect.value : defaultGraphDirection, |
320 |
| - sortMethod: hierarchicalOptionsSortMethodSelect.value ? hierarchicalOptionsSortMethodSelect.value : 'hubsize' |
| 318 | + hierarchicalRepulsion: { |
| 319 | + springConstant: 0, |
| 320 | + avoidOverlap: 0.2 |
| 321 | + } |
321 | 322 | };
|
322 | 323 | }
|
323 | 324 | } else {
|
324 |
| - nodes.getIds().forEach(id => restoreCoordinates(nodes.get(id))); |
| 325 | + if(defaultGraphDirection !== '') { |
| 326 | + options.layout = { |
| 327 | + hierarchical: { |
| 328 | + enabled: true, |
| 329 | + levelSeparation: 200, |
| 330 | + nodeSpacing: 200, |
| 331 | + direction: defaultGraphDirection, |
| 332 | + } |
| 333 | + }; |
| 334 | + options.physics = { |
| 335 | + enabled: true, |
| 336 | + hierarchicalRepulsion: { |
| 337 | + springConstant: 0, |
| 338 | + avoidOverlap: 0.2 |
| 339 | + } |
| 340 | + }; |
| 341 | + nodes.getIds().forEach(id => storeCoordinates(nodes.get(id))); |
| 342 | + } else { |
| 343 | + nodes.getIds().forEach(id => restoreCoordinates(nodes.get(id))); |
| 344 | + } |
325 | 345 | }
|
326 |
| - network = new vis.Network(container, data, options); |
| 346 | + var network = new vis.Network(container, data, options); |
327 | 347 | network.on("stabilizationIterationsDone", function () {
|
328 | 348 | network.setOptions({
|
329 |
| - physics: false |
| 349 | + physics: { enabled: false } |
330 | 350 | });
|
331 | 351 | });
|
332 | 352 | }
|
|
0 commit comments