Skip to content

Commit 70417b1

Browse files
committed
Added Regenerate button
1 parent 76cc36e commit 70417b1

21 files changed

+88
-60
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ dist
55
showComponentHierarchy.js
66
showModuleHierarchy.js
77
*.vsix
8+
stylesheet/all.vscode.min.css

src/commands/generateDependencyInjectionGraph.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class GenerateDependencyInjectionGraph extends ShowHierarchyBase {
4848
const jsContent = this.generateJavascriptContent(nodesJson, edgesJson);
4949
const outputJsFilename = this.showModuleHierarchyJsFilename;
5050
let htmlContent = this.generateHtmlContent(webview, this.showModuleHierarchyJsFilename);
51-
//this.fsUtils.writeFile(this.extensionContext?.asAbsolutePath(path.join('out', ShowComponentHierarchy.Name + '.html')), htmlContent, () => { }); // For debugging
51+
this.fsUtils.writeFile(this.extensionContext?.asAbsolutePath(path.join('out', GenerateDependencyInjectionGraph.commandName + '.html')), htmlContent, () => { }); // For debugging
5252
this.fsUtils.writeFile(
5353
this.extensionContext?.asAbsolutePath(path.join('.', outputJsFilename)),
5454
jsContent,
@@ -111,8 +111,8 @@ export class GenerateDependencyInjectionGraph extends ShowHierarchyBase {
111111

112112
generateJavascriptContent(nodesJson: string, edgesJson: string) {
113113
let template = fs.readFileSync(this.extensionContext?.asAbsolutePath(path.join('templates', this.templateJsFilename)), 'utf8');
114-
let jsContent = template.replace('var nodes = new vis.DataSet([]);', `var nodes = new vis.DataSet([${nodesJson}]);`);
115-
jsContent = jsContent.replace('var edges = new vis.DataSet([]);', `var edges = new vis.DataSet([${edgesJson}]);`);
114+
let jsContent = template.replace('const nodes = new vis.DataSet([]);', `var nodes = new vis.DataSet([${nodesJson}]);`);
115+
jsContent = jsContent.replace('const edges = new vis.DataSet([]);', `var edges = new vis.DataSet([${edgesJson}]);`);
116116
jsContent = jsContent.replace('type: "triangle" // edge arrow to type', `type: "${this.config.dependencyInjectionEdgeArrowToType}" // edge arrow to type}`);
117117
jsContent = jsContent.replace('ctx.strokeStyle = \'blue\'; // graph selection guideline color', `ctx.strokeStyle = '${this.config.graphSelectionGuidelineColor}'; // graph selection guideline color`);
118118
jsContent = jsContent.replace('ctx.lineWidth = 1; // graph selection guideline width', `ctx.lineWidth = ${this.config.graphSelectionGuidelineWidth}; // graph selection guideline width`);

src/commands/showComponentHierarchy.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ export class ShowComponentHierarchy extends ShowHierarchyBase {
108108

109109
private generateJavascriptContent(nodesJson: string, rootNodesJson: string, edgesJson: string): string {
110110
let template = fs.readFileSync(this.extensionContext?.asAbsolutePath(path.join('templates', this.templateJsFilename)), 'utf8');
111-
let jsContent = template.replace('var nodes = new vis.DataSet([]);', `var nodes = new vis.DataSet([${nodesJson}]);`);
112-
jsContent = jsContent.replace('var rootNodes = [];', `var rootNodes = [${rootNodesJson}];`);
113-
jsContent = jsContent.replace('var edges = new vis.DataSet([]);', `var edges = new vis.DataSet([${edgesJson}]);`);
111+
let jsContent = template.replace('const nodes = new vis.DataSet([]);', `var nodes = new vis.DataSet([${nodesJson}]);`);
112+
jsContent = jsContent.replace('const rootNodes = [];', `var rootNodes = [${rootNodesJson}];`);
113+
jsContent = jsContent.replace('const edges = new vis.DataSet([]);', `var edges = new vis.DataSet([${edgesJson}]);`);
114114
jsContent = jsContent.replace('background: "#00FF00" // rootNode background color', `background: "${this.config.rootNodeBackgroundColor}" // rootNode background color`);
115115
jsContent = jsContent.replace('shape: \'box\' // The shape of the nodes.', `shape: '${this.config.rootNodeShape}'// The shape of the nodes.`);
116116
jsContent = jsContent.replace('type: "triangle" // edge arrow to type', `type: "${this.config.componentHierarchyEdgeArrowToType}" // edge arrow to type}`);

src/commands/showHierarchyBase.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export class ShowHierarchyBase extends CommandBase {
2323
protected showComponentHierarchyJsFilename: string = 'showComponentHierarchy.js';
2424
protected showModuleHierarchyJsFilename: string = 'showModuleHierarchy.js';
2525
protected showHierarchyCssFilename: string = 'showHierarchy.css';
26+
protected fontAwesomeCssFilename: string = 'all.min.css';
27+
protected fontAwesomeFontFilename: string = '../webfonts/fa-';
28+
2629
protected workspaceDirectory = this.fsUtils.getWorkspaceFolder();
2730

2831
constructor(context: vscode.ExtensionContext, graphState: GraphState, setNewState: (newGraphState: GraphState) => any) {
@@ -141,21 +144,37 @@ export class ShowHierarchyBase extends CommandBase {
141144
const visUri = webview.asWebviewUri(visPath);
142145
htmlContent = htmlContent.replace('vis-network.min.js', visUri.toString());
143146

144-
const cssPath = vscode.Uri.joinPath(this.extensionContext.extensionUri, 'stylesheet', this.showHierarchyCssFilename);
145-
const cssUri = webview.asWebviewUri(cssPath);
147+
let cssPath = vscode.Uri.joinPath(this.extensionContext.extensionUri, 'stylesheet', this.showHierarchyCssFilename);
148+
let cssUri = webview.asWebviewUri(cssPath);
146149
htmlContent = htmlContent.replace(this.showHierarchyCssFilename, cssUri.toString());
147150

151+
const vscodyfiedFontAwesomeCssFilename = this.fixFontAwesomeFontUri(webview);
152+
cssPath = vscode.Uri.joinPath(this.extensionContext.extensionUri, 'stylesheet', vscodyfiedFontAwesomeCssFilename);
153+
cssUri = webview.asWebviewUri(cssPath);
154+
htmlContent = htmlContent.replace(this.fontAwesomeCssFilename, cssUri.toString());
155+
148156
const nonce = this.getNonce();
149157
htmlContent = htmlContent.replace('nonce-nonce', `nonce-${nonce}`);
150158
htmlContent = htmlContent.replace(/<script /g, `<script nonce="${nonce}" `);
151-
htmlContent = htmlContent.replace('cspSource', webview.cspSource);
159+
htmlContent = htmlContent.replace(/cspSource/g, webview.cspSource);
152160

153161
const jsPath = vscode.Uri.joinPath(this.extensionContext.extensionUri, outputJsFilename);
154162
const jsUri = webview.asWebviewUri(jsPath);
155163
htmlContent = htmlContent.replace('showHierarchy.js', jsUri.toString());
156164
return htmlContent;
157165
}
158166

167+
private fixFontAwesomeFontUri(webview: vscode.Webview): string {
168+
const fontPath = vscode.Uri.joinPath(this.extensionContext.extensionUri, this.fontAwesomeFontFilename.replace('../', ''));
169+
const fontPathUri = webview.asWebviewUri(fontPath);
170+
let cssFileContent = fs.readFileSync(this.extensionContext?.asAbsolutePath(path.join('stylesheet', this.fontAwesomeCssFilename)), 'utf8');
171+
var regex = new RegExp(this.fontAwesomeFontFilename, "g");
172+
cssFileContent = cssFileContent.replace(regex, fontPathUri.toString());
173+
const newCssFilename = 'all.vscode.min.css';
174+
this.fsUtils.writeFile(this.extensionContext?.asAbsolutePath(path.join('stylesheet', newCssFilename)), cssFileContent, () => {});
175+
return newCssFilename;
176+
}
177+
159178
protected showErrors(errors: string[], errorMessage: string) {
160179
const angularToolsOutput = vscode.window.createOutputChannel(this.config.angularToolsOutputChannel);
161180
angularToolsOutput.clear();

src/commands/showModuleHierarchy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ export class ShowModuleHierarchy extends ShowHierarchyBase {
105105

106106
private generateJavascriptContent(nodesJson: string, edgesJson: string) {
107107
let template = fs.readFileSync(this.extensionContext?.asAbsolutePath(path.join('templates', this.templateJsFilename)), 'utf8');
108-
let jsContent = template.replace('var nodes = new vis.DataSet([]);', `var nodes = new vis.DataSet([${nodesJson}]);`);
109-
jsContent = jsContent.replace('var edges = new vis.DataSet([]);', `var edges = new vis.DataSet([${edgesJson}]);`);
108+
let jsContent = template.replace('const nodes = new vis.DataSet([]);', `var nodes = new vis.DataSet([${nodesJson}]);`);
109+
jsContent = jsContent.replace('const edges = new vis.DataSet([]);', `var edges = new vis.DataSet([${edgesJson}]);`);
110110
jsContent = jsContent.replace('type: "triangle" // edge arrow to type', `type: "${this.config.moduleHierarchyEdgeArrowToType}" // edge arrow to type}`);
111111
jsContent = jsContent.replace('ctx.strokeStyle = \'blue\'; // graph selection guideline color', `ctx.strokeStyle = '${this.config.graphSelectionGuidelineColor}'; // graph selection guideline color`);
112112
jsContent = jsContent.replace('ctx.lineWidth = 1; // graph selection guideline width', `ctx.lineWidth = ${this.config.graphSelectionGuidelineWidth}; // graph selection guideline width`);

stylesheet/all.min.css

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stylesheet/showHierarchy.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
z-index: 300;
2828
}
2929

30-
input[type=button] {
30+
button {
3131
margin-right: 10px;
3232
}
3333

templates/showHierarchy_Template.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
<head>
44
<meta charset="UTF-8">
5-
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src cspSource; script-src 'nonce-nonce';">
5+
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; font-src cspSource; style-src cspSource; script-src 'nonce-nonce';">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
77

88
<link href="showHierarchy.css" rel="stylesheet">
9+
<link href="all.min.css" rel="stylesheet">
910

1011
<script type="text/javascript" src="vis-network.min.js"></script>
1112

@@ -14,15 +15,15 @@
1415
<body>
1516
<div id="buttons">
1617
<div class="dropdown">
17-
<input type="button" id="saveAsButton" value="Save as ...">
18+
<button id="saveAsButton" ><i class="fas fa-file-download"></i> Save as ...</button>
1819
<div class="dropdown-content">
1920
<a href="#" id="saveAsPngButton">Save as Png</a>
2021
<a href="#" Id="saveAsDgmlButton">Save as Dgml</a>
2122
<a href="#" Id="saveAsDotButton">Save as Dot</a>
2223
</div>
2324
</div>
24-
<input type="button" id="saveSelectionAsPngButton" value="Save selection as png">
25-
<input type="button" id="copyToClipboardButton" value="Copy to clipboard">
25+
<button id="regenerateGraphButton" title="Regenerate the graph"><i class="fas fa-sync"></i></button>
26+
<button id="saveSelectionAsPngButton"><i class="fas fa-file-download"></i> Save selection as png</button>
2627
<div id="hierarchical">Change layout:<input type="checkbox" id="showHierarchicalOptions"></div>
2728
<div id="hierarchicalOptions_direction">
2829
Direction: <select id="direction">

templates/showHierarchy_Template.js

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
(function () {
2-
var nodes = new vis.DataSet([]);
2+
const nodes = new vis.DataSet([]);
33

4-
var rootNodes = [];
4+
const rootNodes = [];
55
rootNodes.forEach(nodeId => {
66
nodes.get(nodeId).color = {
77
background: "#00FF00" // rootNode background color
88
};
99
});
1010

11-
var arrowAttr = {
11+
const arrowAttr = {
1212
to: {
1313
enabled: true,
1414
type: "triangle" // edge arrow to type
1515
}
1616
};
17-
var edges = new vis.DataSet([]);
17+
const edges = new vis.DataSet([]);
1818

19-
var data = {
19+
const data = {
2020
nodes: nodes,
2121
edges: edges
2222
};
23-
var options = {
23+
let options = {
2424
edges: {
2525
smooth: false // Make edges straight lines.
2626
},
@@ -30,9 +30,11 @@
3030
}
3131
};
3232
setRandomLayout();
33-
var container = document.getElementById('network');
34-
var network = new vis.Network(container, data, options);
35-
var seed = network.getSeed();
33+
const container = document.getElementById('network');
34+
let network = new vis.Network(container, data, options);
35+
let seed = network.getSeed();
36+
let nodePositions = [];
37+
3638
network.on("stabilizationIterationsDone", function () {
3739
network.setOptions({
3840
physics: false
@@ -48,9 +50,9 @@
4850
let mouseX = mouseY = 0;
4951
let selection;
5052
// get the vis.js canvas
51-
var graphDiv = document.getElementById('network');
52-
var visDiv = graphDiv.firstElementChild;
53-
var graphCanvas = visDiv.firstElementChild;
53+
const graphDiv = document.getElementById('network');
54+
const visDiv = graphDiv.firstElementChild;
55+
const graphCanvas = visDiv.firstElementChild;
5456
const selectionLayer = document.getElementById('selectionLayer');
5557
const selectionCanvas = selectionLayer.firstElementChild;
5658
let selectionCanvasContext;
@@ -65,17 +67,17 @@
6567
saveAsDgmlButton.addEventListener('click', saveAsDgml);
6668
const saveAsDotButton = document.getElementById('saveAsDotButton');
6769
saveAsDotButton.addEventListener('click', saveAsDot);
70+
const regenerateGraphButton = document.getElementById('regenerateGraphButton');
71+
regenerateGraphButton.addEventListener('click', regenerateGraph);
6872
const saveSelectionAsPngButton = document.getElementById('saveSelectionAsPngButton');
6973
saveSelectionAsPngButton.addEventListener('click', saveSelectionAsPng);
70-
const copyToClipboardButton = document.getElementById('copyToClipboardButton');
71-
copyToClipboardButton.addEventListener('click', copyToClipboard);
72-
copyToClipboardButton.style['display'] = 'none'; // TODO: Remove when copyToClipboard is implemented
7374
const showHierarchicalOptionsButton = document.getElementById('showHierarchicalOptions');
7475
showHierarchicalOptionsButton.addEventListener('click', setNetworkLayout);
7576
const hierarchicalDirectionSelect = document.getElementById('direction');
7677
hierarchicalDirectionSelect.addEventListener('change', setNetworkLayout);
7778
const hierarchicalSortMethodSelect = document.getElementById('sortMethod');
7879
hierarchicalSortMethodSelect.addEventListener('change', setNetworkLayout);
80+
const hierarchicalOptionsSortMethodSelect = document.getElementById('sortMethod');
7981

8082
function mouseUpEventListener(event) {
8183
// Convert the canvas to image data that can be saved
@@ -128,7 +130,7 @@
128130
}
129131

130132
function showGuideLines() {
131-
var tmpSelectionCanvasContext = selectionCanvas.getContext("2d");
133+
const tmpSelectionCanvasContext = selectionCanvas.getContext("2d");
132134
tmpSelectionCanvasContext.clearRect(0, 0, selectionCanvas.width, selectionCanvas.height);
133135
drawGuideLine(tmpSelectionCanvasContext, mouseX, -1);
134136
drawGuideLine(tmpSelectionCanvasContext, -1, mouseY);
@@ -157,10 +159,6 @@
157159
}
158160

159161
function saveSelectionAsPng() {
160-
graphDiv = document.getElementById('network');
161-
visDiv = graphDiv.firstElementChild;
162-
graphCanvas = visDiv.firstElementChild;
163-
164162
// show the help text
165163
helpTextDiv.style['display'] = 'block';
166164

@@ -180,9 +178,6 @@
180178
}
181179

182180
function saveAsPng() {
183-
graphDiv = document.getElementById('network');
184-
visDiv = graphDiv.firstElementChild;
185-
graphCanvas = visDiv.firstElementChild;
186181
// Calculate the bounding box of all the elements on the canvas
187182
const boundingBox = getBoundingBox();
188183

@@ -204,42 +199,42 @@
204199
}
205200

206201
function getBoundingBox() {
207-
var ctx = graphCanvas.getContext('2d');
202+
const ctx = graphCanvas.getContext('2d');
208203
const imgData = ctx.getImageData(0, 0, graphCanvas.width, graphCanvas.height);
209-
var bytesPerPixels = 4;
210-
var cWidth = graphCanvas.width * bytesPerPixels;
211-
var cHeight = graphCanvas.height;
212-
var minY = minX = maxY = maxX = -1;
213-
for (var y = cHeight; y > 0 && maxY === -1; y--) {
214-
for (var x = 0; x < cWidth; x += bytesPerPixels) {
215-
var arrayPos = x + y * cWidth;
204+
const bytesPerPixels = 4;
205+
const cWidth = graphCanvas.width * bytesPerPixels;
206+
const cHeight = graphCanvas.height;
207+
let minY = minX = maxY = maxX = -1;
208+
for (let y = cHeight; y > 0 && maxY === -1; y--) {
209+
for (let x = 0; x < cWidth; x += bytesPerPixels) {
210+
const arrayPos = x + y * cWidth;
216211
if (imgData.data[arrayPos + 3] > 0 && maxY === -1) {
217212
maxY = y;
218213
break;
219214
}
220215
}
221216
}
222-
for (var x = cWidth; x >= 0 && maxX === -1; x -= bytesPerPixels) {
223-
for (var y = 0; y < maxY; y++) {
224-
var arrayPos = x + y * cWidth;
217+
for (let x = cWidth; x >= 0 && maxX === -1; x -= bytesPerPixels) {
218+
for (let y = 0; y < maxY; y++) {
219+
const arrayPos = x + y * cWidth;
225220
if (imgData.data[arrayPos + 3] > 0 && maxX === -1) {
226221
maxX = x / bytesPerPixels;
227222
break;
228223
}
229224
}
230225
}
231-
for (var x = 0; x < maxX * bytesPerPixels && minX === -1; x += bytesPerPixels) {
232-
for (var y = 0; y < maxY; y++) {
233-
var arrayPos = x + y * cWidth;
226+
for (let x = 0; x < maxX * bytesPerPixels && minX === -1; x += bytesPerPixels) {
227+
for (let y = 0; y < maxY; y++) {
228+
const arrayPos = x + y * cWidth;
234229
if (imgData.data[arrayPos + 3] > 0 && minX === -1) {
235230
minX = x / bytesPerPixels;
236231
break;
237232
}
238233
}
239234
}
240-
for (var y = 0; y < maxY && minY === -1; y++) {
241-
for (var x = minX * bytesPerPixels; x < maxX * bytesPerPixels; x += bytesPerPixels) {
242-
var arrayPos = x + y * cWidth;
235+
for (let y = 0; y < maxY && minY === -1; y++) {
236+
for (let x = minX * bytesPerPixels; x < maxX * bytesPerPixels; x += bytesPerPixels) {
237+
const arrayPos = x + y * cWidth;
243238
if (imgData.data[arrayPos + 3] > 0 && minY === -1) {
244239
minY = y;
245240
break;
@@ -308,8 +303,17 @@
308303
return cleanedLabel;
309304
}
310305

311-
function copyToClipboard() {
312-
console.log('Not implemented yet...');
306+
function regenerateGraph() {
307+
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+
});
316+
setNetworkLayout();
313317
}
314318

315319
function setRandomLayout() {
@@ -351,11 +355,9 @@
351355
const hierarchicalOptionsSortMethod = document.getElementById('hierarchicalOptions_sortmethod');
352356
hierarchicalOptionsDirection.style['display'] = showHierarchicalOptionsCheckbox.checked ? 'block' : 'none';
353357
hierarchicalOptionsSortMethod.style['display'] = showHierarchicalOptionsCheckbox.checked ? 'block' : 'none';
354-
const hierarchicalOptionsSortMethodSelect = document.getElementById('sortMethod');
355358
if (showHierarchicalOptionsCheckbox.checked) {
356359
if (hierarchicalOptionsDirectionSelect.value && hierarchicalOptionsDirectionSelect.value === 'Random') {
357360
setRandomLayout();
358-
seed = Math.random();
359361
} else {
360362
const direction = hierarchicalOptionsDirectionSelect.value ? hierarchicalOptionsDirectionSelect.value : 'UD';
361363
const sortMethod = hierarchicalOptionsSortMethodSelect.value ? hierarchicalOptionsSortMethodSelect.value : 'hubsize';

webfonts/fa-brands-400.eot

131 KB
Binary file not shown.

0 commit comments

Comments
 (0)