|
1 |
| -import { Component } from "@src"; |
| 1 | +import { BoundingBox, Category, Edge, NetworkNode, Node, Position } from "@model"; |
| 2 | + |
| 3 | +export class DgmlManager { |
2 | 4 |
|
3 |
| -export class DGMLManager { |
4 |
| - |
5 | 5 | public createNewDirectedGraph(domImpl: DOMImplementation, direction: string, layout: string, zoomLevel: string) {
|
6 | 6 | let xmlDoc: Document = domImpl.createDocument('', null, null);
|
7 | 7 | const root = xmlDoc.createElement("DirectedGraph");
|
@@ -70,53 +70,81 @@ export class DGMLManager {
|
70 | 70 | }
|
71 | 71 | }
|
72 | 72 |
|
73 |
| - public addNodesAndLinks(xmlDoc: Document, componentHash: { [selector: string]: Component; }) { |
| 73 | + public addNodesAndLinks(xmlDoc: Document, nodes: Node[], nodeInfos: NetworkNode[], edges: Edge[]) { |
| 74 | + const nodeInfoDictionary = Object.assign({}, ...nodeInfos.map((nodeInfo) => ({ [nodeInfo.id]: nodeInfo }))); |
74 | 75 | const nodesElement = this.addNodeToRoot(xmlDoc, "Nodes");
|
75 | 76 | const linksElement = this.addNodeToRoot(xmlDoc, "Links");
|
76 |
| - for (let selector in componentHash) { |
77 |
| - const component = componentHash[selector]; |
78 |
| - if (component.isRoot) { |
79 |
| - this.generateDirectedGraphNodesXml(xmlDoc, component.subComponents, component, true, nodesElement); |
80 |
| - this.generateDirectedGraphLinksXml(xmlDoc, component.subComponents, selector, "", linksElement); |
| 77 | + nodes.forEach(node => { |
| 78 | + if (node.id in nodeInfoDictionary) { |
| 79 | + this.enrichNode(node, nodeInfoDictionary[node.id]); |
81 | 80 | }
|
| 81 | + this.generateDirectedGraphNodesXml(xmlDoc, node, nodesElement); |
| 82 | + }); |
| 83 | + edges.forEach(edge => { |
| 84 | + this.generateDirectedGraphLinksXml(xmlDoc, edge, linksElement); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + private enrichNode(node: Node, networkNode: NetworkNode) { |
| 89 | + if (networkNode.label) { |
| 90 | + node.name = networkNode.label; |
| 91 | + } |
| 92 | + if (networkNode.position) { |
| 93 | + node.position = networkNode.position; |
| 94 | + } |
| 95 | + if (networkNode.boundingBox) { |
| 96 | + node.boundingBox = networkNode.boundingBox; |
82 | 97 | }
|
83 | 98 | }
|
84 | 99 |
|
85 |
| - private generateDirectedGraphNodesXml(xmlDoc: Document, components: Component[], component: Component, isRoot: boolean, nodesElement: Element | null) { |
| 100 | + private generateDirectedGraphNodesXml(xmlDoc: Document, node: Node, nodesElement: Element | null) { |
86 | 101 | const nodeElement = xmlDoc.createElement("Node");
|
87 |
| - nodeElement.setAttribute("ComponentFilename", component.tsFilename); |
88 |
| - nodeElement.setAttribute("Label", component.selector); |
89 |
| - nodeElement.setAttribute("Id", component.selector); |
90 |
| - if (isRoot) { |
91 |
| - nodeElement.setAttribute("Category", "RootComponent"); |
| 102 | + nodeElement.setAttribute("Label", node.name); |
| 103 | + nodeElement.setAttribute("Id", node.id); |
| 104 | + if (node.boundingBox !== undefined && node.position !== undefined) { |
| 105 | + nodeElement.setAttribute("Bounds", this.calculateBounds(node.position, node.boundingBox)); |
| 106 | + nodeElement.setAttribute("UseManualLocation", "True"); |
92 | 107 | }
|
93 |
| - this.addNode(nodesElement, nodeElement); |
94 |
| - if (components.length > 0) { |
95 |
| - components.forEach((subComponent) => { |
96 |
| - this.generateDirectedGraphNodesXml(xmlDoc, subComponent.subComponents, subComponent, subComponent.isRoot, nodesElement); |
| 108 | + if (node.attributes && node.attributes.length > 0) { |
| 109 | + node.attributes.forEach(attribute => { |
| 110 | + nodeElement.setAttribute(attribute.name, attribute.value); |
97 | 111 | });
|
98 | 112 | }
|
| 113 | + this.addNode(nodesElement, nodeElement); |
| 114 | + } |
| 115 | + |
| 116 | + private calculateBounds(position: Position, boundingBox: BoundingBox): string { |
| 117 | + const width = boundingBox.right - boundingBox.left; |
| 118 | + const height = boundingBox.bottom - boundingBox.top; |
| 119 | + return `${position.x},${position.y},${width},${height}`; |
99 | 120 | }
|
100 | 121 |
|
101 |
| - private generateDirectedGraphLinksXml(xmlDoc: Document, subComponents: Component[], displayName: string, parentDisplayName: string, linksElement: Element | null) { |
102 |
| - if (parentDisplayName.length > 0) { |
103 |
| - this.addLinkNode(xmlDoc, linksElement, parentDisplayName, displayName); |
104 |
| - } |
105 |
| - if (subComponents.length > 0) { |
106 |
| - subComponents.forEach((subComponent) => { |
107 |
| - this.generateDirectedGraphLinksXml(xmlDoc, subComponent.subComponents, subComponent.selector, displayName, linksElement); |
108 |
| - }); |
| 122 | + private generateDirectedGraphLinksXml(xmlDoc: Document, edge: Edge, linksElement: Element | null) { |
| 123 | + this.addLinkNode(xmlDoc, linksElement, edge.source, edge.target); |
| 124 | + } |
| 125 | + |
| 126 | + private addCategory(xmlDoc: Document, categoriesElement: Element | null, category: Category) { |
| 127 | + if (categoriesElement !== null) { |
| 128 | + const categoryElement = xmlDoc.createElement("Category"); |
| 129 | + categoryElement.setAttribute("Id", category.id); |
| 130 | + categoryElement.setAttribute("Label", category.label); |
| 131 | + categoryElement.setAttribute("Background", category.backgroundColor); |
| 132 | + categoryElement.setAttribute("IsTag", "True"); |
| 133 | + this.addNode(categoriesElement, categoryElement); |
109 | 134 | }
|
110 | 135 | }
|
111 | 136 |
|
112 |
| - public addCategory(xmlDoc: Document, id: string, label: string, backgroundColor: string) { |
| 137 | + public addCategories(xmlDoc: Document, categories: Category[]) { |
113 | 138 | const categoriesElement = this.addNodeToRoot(xmlDoc, "Categories");
|
114 |
| - const categoryElement = xmlDoc.createElement("Category"); |
115 |
| - categoryElement.setAttribute("Id", id); |
116 |
| - categoryElement.setAttribute("Label", label); |
117 |
| - categoryElement.setAttribute("Background", backgroundColor); |
118 |
| - categoryElement.setAttribute("IsTag", "True"); |
119 |
| - this.addNode(categoriesElement, categoryElement); |
| 139 | + // const categoryElement = xmlDoc.createElement("Category"); |
| 140 | + // categoryElement.setAttribute("Id", "RootComponent"); |
| 141 | + // categoryElement.setAttribute("Label", "Root component"); |
| 142 | + // // categoryElement.setAttribute("Background", this.config.rootNodeBackgroundColor); |
| 143 | + // categoryElement.setAttribute("IsTag", "True"); |
| 144 | + // this.addNode(categoriesElement, categoryElement); |
| 145 | + categories.forEach(category => { |
| 146 | + this.addCategory(xmlDoc, categoriesElement, category); |
| 147 | + }); |
120 | 148 | }
|
121 | 149 |
|
122 | 150 | public addProperties(xmlDoc: Document) {
|
|
0 commit comments