Skip to content

Commit 392a3bc

Browse files
committed
Node positions are now persisted corectly when changing to another tab and back again.
1 parent 70417b1 commit 392a3bc

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
lines changed

src/commands/showComponentHierarchy.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export class ShowComponentHierarchy extends ShowHierarchyBase {
2828
const newGraphState: GraphState = JSON.parse(message.text);
2929
this.graphState = newGraphState;
3030
this.setNewState(this.graphState);
31+
this.nodes.forEach(node => {
32+
node.position = this.graphState.nodePositions[node.id];
33+
});
34+
this.addNodesAndEdges(components, this.appendNodes, this.appendEdges);
35+
this.generateAndSaveJavascriptContent(() => { });
3136
return;
3237
}
3338
},
@@ -40,7 +45,12 @@ export class ShowComponentHierarchy extends ShowHierarchyBase {
4045
this.nodes = [];
4146
this.edges = [];
4247
this.addNodesAndEdges(components, this.appendNodes, this.appendEdges);
48+
const htmlContent = this.generateHtmlContent(webview, this.showComponentHierarchyJsFilename);
49+
//this.fsUtils.writeFile(this.extensionContext?.asAbsolutePath(path.join('out', ShowComponentHierarchy.Name + '.html')), htmlContent, () => { }); // For debugging
50+
this.generateAndSaveJavascriptContent(() => { webview.html = htmlContent; });
51+
}
4352

53+
private generateAndSaveJavascriptContent(callback: () => any) {
4454
const nodesJson = this.nodes
4555
.map((node, index, arr) => { return node.toJsonString(); })
4656
.join(',\n');
@@ -54,16 +64,10 @@ export class ShowComponentHierarchy extends ShowHierarchyBase {
5464

5565
try {
5666
const jsContent = this.generateJavascriptContent(nodesJson, rootNodesJson, edgesJson);
57-
const outputJsFilename = this.showComponentHierarchyJsFilename;
58-
let htmlContent = this.generateHtmlContent(webview, outputJsFilename);
59-
60-
//this.fsUtils.writeFile(this.extensionContext?.asAbsolutePath(path.join('out', ShowComponentHierarchy.Name + '.html')), htmlContent, () => { }); // For debugging
6167
this.fsUtils.writeFile(
62-
this.extensionContext?.asAbsolutePath(path.join('.', outputJsFilename)),
68+
this.extensionContext?.asAbsolutePath(path.join('.', this.showComponentHierarchyJsFilename)),
6369
jsContent,
64-
() => {
65-
webview.html = htmlContent;
66-
}
70+
callback
6771
);
6872
} catch (ex) {
6973
console.log('Angular Tools Exception:' + ex);

templates/showHierarchy_Template.js

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
const container = document.getElementById('network');
3434
let network = new vis.Network(container, data, options);
3535
let seed = network.getSeed();
36-
let nodePositions = [];
3736

3837
network.on("stabilizationIterationsDone", function () {
3938
network.setOptions({
@@ -58,7 +57,6 @@
5857
let selectionCanvasContext;
5958

6059
const hierarchicalOptionsDirectionSelect = document.getElementById('direction');
61-
const showHierarchicalOptionsCheckbox = document.getElementById('showHierarchicalOptions');
6260

6361
// add button event listeners
6462
const saveAsPngButton = document.getElementById('saveAsPngButton');
@@ -71,13 +69,15 @@
7169
regenerateGraphButton.addEventListener('click', regenerateGraph);
7270
const saveSelectionAsPngButton = document.getElementById('saveSelectionAsPngButton');
7371
saveSelectionAsPngButton.addEventListener('click', saveSelectionAsPng);
74-
const showHierarchicalOptionsButton = document.getElementById('showHierarchicalOptions');
75-
showHierarchicalOptionsButton.addEventListener('click', setNetworkLayout);
72+
const showHierarchicalOptionsCheckbox = document.getElementById('showHierarchicalOptions');
73+
showHierarchicalOptionsCheckbox.addEventListener('click', showHierarchicalOptions);
7674
const hierarchicalDirectionSelect = document.getElementById('direction');
7775
hierarchicalDirectionSelect.addEventListener('change', setNetworkLayout);
7876
const hierarchicalSortMethodSelect = document.getElementById('sortMethod');
7977
hierarchicalSortMethodSelect.addEventListener('change', setNetworkLayout);
8078
const hierarchicalOptionsSortMethodSelect = document.getElementById('sortMethod');
79+
const hierarchicalOptionsDirection = document.getElementById('hierarchicalOptions_direction');
80+
const hierarchicalOptionsSortMethod = document.getElementById('hierarchicalOptions_sortmethod');
8181

8282
function mouseUpEventListener(event) {
8383
// Convert the canvas to image data that can be saved
@@ -305,14 +305,7 @@
305305

306306
function regenerateGraph() {
307307
seed = Math.random();
308-
nodes.forEach(function (node) {
309-
nodes.update({
310-
id: node.id,
311-
fixed: false,
312-
x: undefined,
313-
y: undefined
314-
});
315-
});
308+
removeNodePositions();
316309
setNetworkLayout();
317310
}
318311

@@ -350,21 +343,38 @@
350343
};
351344
}
352345

346+
function showHierarchicalOptions(){
347+
if (showHierarchicalOptionsCheckbox.checked) {
348+
hierarchicalOptionsDirection.style['display'] = 'block';
349+
hierarchicalOptionsSortMethod.style['display'] = 'block';
350+
if (hierarchicalOptionsDirectionSelect.value && hierarchicalOptionsDirectionSelect.value === 'Random') {
351+
regenerateGraphButton.style['display'] = 'block';
352+
} else {
353+
regenerateGraphButton.style['display'] = 'none';
354+
}
355+
} else {
356+
hierarchicalOptionsDirection.style['display'] = 'none';
357+
hierarchicalOptionsSortMethod.style['display'] = 'none';
358+
regenerateGraphButton.style['display'] = 'block';
359+
}
360+
}
361+
353362
function setNetworkLayout() {
354-
const hierarchicalOptionsDirection = document.getElementById('hierarchicalOptions_direction');
355-
const hierarchicalOptionsSortMethod = document.getElementById('hierarchicalOptions_sortmethod');
356-
hierarchicalOptionsDirection.style['display'] = showHierarchicalOptionsCheckbox.checked ? 'block' : 'none';
357-
hierarchicalOptionsSortMethod.style['display'] = showHierarchicalOptionsCheckbox.checked ? 'block' : 'none';
358363
if (showHierarchicalOptionsCheckbox.checked) {
359364
if (hierarchicalOptionsDirectionSelect.value && hierarchicalOptionsDirectionSelect.value === 'Random') {
360365
setRandomLayout();
366+
seed = Math.random();
367+
removeNodePositions();
368+
regenerateGraphButton.style['display'] = 'block';
361369
} else {
362370
const direction = hierarchicalOptionsDirectionSelect.value ? hierarchicalOptionsDirectionSelect.value : 'UD';
363371
const sortMethod = hierarchicalOptionsSortMethodSelect.value ? hierarchicalOptionsSortMethodSelect.value : 'hubsize';
364372
setHierarchicalLayout(direction, sortMethod);
373+
regenerateGraphButton.style['display'] = 'none';
365374
}
366375
} else {
367376
options.layout = {};
377+
regenerateGraphButton.style['display'] = 'block';
368378
}
369379
options.layout.randomSeed = seed;
370380
network = new vis.Network(container, data, options);
@@ -414,4 +424,15 @@
414424
});
415425
}
416426

427+
function removeNodePositions() {
428+
nodes.forEach(function (node) {
429+
nodes.update({
430+
id: node.id,
431+
fixed: false,
432+
x: undefined,
433+
y: undefined
434+
});
435+
});
436+
}
437+
417438
}());

0 commit comments

Comments
 (0)