Skip to content

Commit 71197d2

Browse files
committed
The nodes positions are now persisted so the graph wont randomize each time to tab is changed.
1 parent b5daecc commit 71197d2

File tree

5 files changed

+48
-32
lines changed

5 files changed

+48
-32
lines changed

src/commands/generateDependencyInjectionGraph.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ShowHierarchyBase } from './showHierarchyBase';
22
import { ModuleManager } from '@src';
3-
import { ArrowType, Component, Edge, GraphState, Node, NodeType, Project } from '@model';
3+
import { ArrowType, Component, Edge, GraphState, Node, NodeType, Position, Project } from '@model';
44
import * as fs from 'fs';
55
import * as path from 'path';
66
import * as vscode from 'vscode';
@@ -22,9 +22,8 @@ export class GenerateDependencyInjectionGraph extends ShowHierarchyBase {
2222
this.saveAsDot(this.config.dependencyInjectionDotGraphFilename, message.text, 'dependencyInjectionGraph', `'The components hierarchy has been analyzed and a GraphViz (dot) file '${this.config.dependencyInjectionDotGraphFilename}' has been created'`);
2323
return;
2424
case 'setGraphState':
25-
const newGraphState: GraphState = JSON.parse(message.text);
26-
this.graphState.networkSeed = newGraphState.networkSeed;
27-
this.graphState.nodePositions = newGraphState.nodePositions;
25+
const newGraphState: GraphState = JSON.parse(message.text);
26+
this.graphState = newGraphState;
2827
this.setNewState(this.graphState);
2928
return;
3029
}
@@ -35,7 +34,6 @@ export class GenerateDependencyInjectionGraph extends ShowHierarchyBase {
3534
var workspaceFolder = this.fsUtils.getWorkspaceFolder();
3635
const errors: string[] = [];
3736
const project: Project = ModuleManager.scanProject(workspaceFolder, errors, this.isTypescriptFile);
38-
3937
this.nodes = [];
4038
this.edges = [];
4139
this.addNodesAndEdges(project, this.appendNodes, this.appendEdges);
@@ -101,7 +99,8 @@ export class GenerateDependencyInjectionGraph extends ShowHierarchyBase {
10199
project.components.forEach(component => {
102100
let componentFilename = component.filename.replace(this.workspaceDirectory, '.');
103101
componentFilename = componentFilename.split('\\').join('/');
104-
appendNodes([new Node(component.name, this.generatedComponentNode(component), componentFilename, false, NodeType.component)]);
102+
const position = this.graphState.nodePositions[component.name];
103+
appendNodes([new Node(component.name, this.generatedComponentNode(component), componentFilename, false, NodeType.component, position)]);
105104
component.dependencyInjections.forEach(injectable => {
106105
appendNodes([new Node(injectable.name, injectable.name, injectable.filename.replace(this.workspaceDirectory, ''), false, NodeType.injectable)]);
107106
appendEdges([new Edge((this.edges.length + 1).toString(), injectable.name, component.name, ArrowType.injectable)]);

src/commands/showHierarchyBase.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ export class ShowHierarchyBase extends CommandBase {
129129

130130
protected setGraphState(jsContent: string): string {
131131
if (this.graphState.networkSeed !== undefined) {
132-
jsContent = jsContent.replace('var seed = network.getSeed();', `var seed = ${this.graphState.networkSeed};`);
132+
// console.log(this.graphState.networkSeed);
133+
// const seedParts = this.graphState.networkSeed.indexOf(':') > 0 ? this.graphState.networkSeed.split(':') : [this.graphState.networkSeed, ''];
134+
// console.log(seedParts);
135+
// jsContent = jsContent.replace('var seed = network.getSeed();', `var seed = ${seedParts[0]};`);
133136
}
134137
return jsContent;
135138
}

src/model/GraphState.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export class GraphState {
66
}
77
public graphLayout: string | undefined;
88
public graphDirection: string | undefined;
9-
public networkSeed: number | undefined;
9+
public networkSeed: string | undefined;
1010
public nodePositions: { [id: string]: Position };
11+
public showHierarchicalOptions: boolean = false;
1112
}

src/model/Position.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
export interface Position {
3-
x: number;
4-
y: number;
2+
export class Position {
3+
public x: number | undefined;
4+
public y: number | undefined;
55
}

templates/showHierarchy_Template.js

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@
3434
var network = new vis.Network(container, data, options);
3535
var seed = network.getSeed();
3636
network.on("stabilizationIterationsDone", function () {
37-
network.setOptions( { physics: false } );
37+
network.setOptions({
38+
physics: false
39+
});
40+
nodes.forEach(function (node) {
41+
nodes.update({
42+
id: node.id,
43+
fixed: false
44+
});
45+
});
46+
postGraphState();
3847
});
3948
network.on('dragEnd', postGraphState);
4049

@@ -52,6 +61,7 @@
5261
let selectionCanvasContext;
5362

5463
const hierarchicalOptionsDirectionSelect = document.getElementById('direction');
64+
const showHierarchicalOptionsCheckbox = document.getElementById('showHierarchicalOptions');
5565

5666
// add button event listeners
5767
const saveAsPngButton = document.getElementById('saveAsPngButton');
@@ -262,7 +272,7 @@
262272
nodes.forEach(node => {
263273
nodeExport[node.id] = {
264274
id: node.id,
265-
label: command==='saveAsDgml' ? cleanLabelDgml(node.label) : cleanLabelDot(node.label),
275+
label: command === 'saveAsDgml' ? cleanLabelDgml(node.label) : cleanLabelDot(node.label),
266276
position: network.getPosition(node.id),
267277
boundingBox: network.getBoundingBox(node.id)
268278
};
@@ -292,10 +302,12 @@
292302
let cleanedLabel = label.replace(/(<([^>]+)>)/ig, '');
293303
return cleanedLabel;
294304
}
305+
295306
function removeNewlines(label) {
296307
let cleanedLabel = label.replace(/\s+/g, '');
297308
return cleanedLabel;
298309
}
310+
299311
function convertNewlinesToDotNewlines(label) {
300312
let cleanedLabel = label.replace(/\n/g, '<br align="left"/>');
301313
return cleanedLabel;
@@ -306,7 +318,7 @@
306318
}
307319

308320
function setRandomLayout() {
309-
options.layout ={
321+
options.layout = {
310322
hierarchical: {
311323
enabled: false
312324
}
@@ -322,7 +334,7 @@
322334

323335
function setHierarchicalLayout(direction, sortMethod) {
324336
options.layout = {
325-
hierarchical: {
337+
hierarchical: {
326338
enabled: true,
327339
levelSeparation: 200,
328340
nodeSpacing: 200,
@@ -342,7 +354,6 @@
342354
function setNetworkLayout() {
343355
const hierarchicalOptionsDirection = document.getElementById('hierarchicalOptions_direction');
344356
const hierarchicalOptionsSortMethod = document.getElementById('hierarchicalOptions_sortmethod');
345-
const showHierarchicalOptionsCheckbox = document.getElementById('showHierarchicalOptions');
346357
hierarchicalOptionsDirection.style['display'] = showHierarchicalOptionsCheckbox.checked ? 'block' : 'none';
347358
hierarchicalOptionsSortMethod.style['display'] = showHierarchicalOptionsCheckbox.checked ? 'block' : 'none';
348359
const hierarchicalOptionsSortMethodSelect = document.getElementById('sortMethod');
@@ -361,28 +372,29 @@
361372
options.layout.randomSeed = seed;
362373
network = new vis.Network(container, data, options);
363374
network.on("stabilizationIterationsDone", function () {
364-
network.setOptions( { physics: false } );
375+
network.setOptions({
376+
physics: false
377+
});
378+
nodes.forEach(function (node) {
379+
nodes.update({
380+
id: node.id,
381+
fixed: false
382+
});
383+
});
384+
postGraphState();
365385
});
366386
network.on('dragEnd', postGraphState);
367-
vscode.postMessage({
368-
command: 'setGraphState',
369-
text: JSON.stringify({
370-
networkSeed: seed,
371-
graphDirection: hierarchicalOptionsDirectionSelect.value,
372-
graphLayout: hierarchicalOptionsSortMethodSelect.value,
373-
nodePositions: getNodePositions()
374-
})
375-
});
387+
postGraphState();
376388
}
377389

378390
function postGraphState() {
379391
const message = JSON.stringify({
380392
networkSeed: seed,
381-
// graphDirection: hierarchicalOptionsDirectionSelect.value,
382-
// graphLayout: hierarchicalOptionsSortMethodSelect.value,
393+
graphDirection: showHierarchicalOptionsCheckbox.checked ? hierarchicalOptionsDirectionSelect.value : undefined,
394+
graphLayout: showHierarchicalOptionsCheckbox.checked ? hierarchicalOptionsSortMethodSelect.value : undefined,
395+
showHierarchicalOptions: showHierarchicalOptionsCheckbox.checked,
383396
nodePositions: getNodePositions()
384397
});
385-
console.log('postGraphState', message);
386398
vscode.postMessage({
387399
command: 'setGraphState',
388400
text: message
@@ -392,12 +404,13 @@
392404
function getNodePositions() {
393405
const nodePositions = {};
394406
nodes.forEach(node => {
407+
const position = network.getPosition(node.id);
395408
nodePositions[node.id] = {
396-
id: node.id,
397-
position: network.getPosition(node.id)
409+
x: position.x,
410+
y: position.y
398411
};
399412
});
400413
return nodePositions;
401414
}
402-
415+
403416
}());

0 commit comments

Comments
 (0)