|
| 1 | +// import {MetaNodeModel} from "../react-diagrams/MetaNodeModel"; |
| 2 | +import {MetaLink, MetaNodeModel, MetaLinkModel} from "@metacell/meta-diagram" |
| 3 | + |
| 4 | +class Graph { |
| 5 | + private readonly node: MetaNodeModel; |
| 6 | + private readonly children: Map<string, Graph>; |
| 7 | + |
| 8 | + constructor(metaNodeModel: MetaNodeModel) { |
| 9 | + this.node = metaNodeModel; |
| 10 | + this.children = new Map<string, Graph>() |
| 11 | + } |
| 12 | + |
| 13 | + getID() : string{ |
| 14 | + return this.node.getID() |
| 15 | + } |
| 16 | + |
| 17 | + getNode() : MetaNodeModel{ |
| 18 | + return this.node |
| 19 | + } |
| 20 | + |
| 21 | + getChild(id:string) { |
| 22 | + return this.children.get(id) |
| 23 | + } |
| 24 | + |
| 25 | + addChild(graph: Graph) : void { |
| 26 | + this.children.set(graph.getID(), graph) |
| 27 | + } |
| 28 | + |
| 29 | + getChildren(): MetaNodeModel[] { |
| 30 | + return Array.from(this.children.values()).map(g => g.getNode()) |
| 31 | + } |
| 32 | + |
| 33 | + getDescendancy(): MetaNodeModel[] { |
| 34 | + const descendancy = this.getChildren() |
| 35 | + for(const graph of Array.from(this.children.values())){ |
| 36 | + descendancy.push(...graph.getDescendancy()) |
| 37 | + } |
| 38 | + return descendancy |
| 39 | + } |
| 40 | + |
| 41 | + dfs(id: string): MetaNodeModel | boolean { |
| 42 | + if(this.getID() === id){ |
| 43 | + return this.node |
| 44 | + } |
| 45 | + for (let node of Array.from(this.children.values())) { |
| 46 | + const found = node.dfs(id) |
| 47 | + if(found){ |
| 48 | + return found |
| 49 | + } |
| 50 | + } |
| 51 | + return false |
| 52 | + } |
| 53 | + |
| 54 | + getContainerBoundingBox() : any { |
| 55 | + return this.node.getNodeBoundingBox(); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +export class MetaGraph { |
| 61 | + private readonly roots: Map<string, Graph>; |
| 62 | + private readonly links: MetaLinkModel[]; |
| 63 | + |
| 64 | + constructor() { |
| 65 | + this.roots = new Map<string, Graph>() |
| 66 | + this.links = []; |
| 67 | + } |
| 68 | + |
| 69 | + addLinks(links: MetaLink[]) { |
| 70 | + links.forEach( (child: MetaLink) => { |
| 71 | + const link = child.toModel(); |
| 72 | + const source = this.getNodeDFS(child.getSourceId()); |
| 73 | + const target = this.getNodeDFS(child.getTargetId()); |
| 74 | + if (source && target) { |
| 75 | + link.setSourcePort(source.getPort(child.getSourcePortId())); |
| 76 | + link.setTargetPort(target.getPort(child.getTargetPortId())); |
| 77 | + this.links.push(link); |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + getLinks(): MetaLinkModel[] { |
| 83 | + return this.links; |
| 84 | + } |
| 85 | + |
| 86 | + addNode(metaNodeModel:MetaNodeModel): void { |
| 87 | + const path = metaNodeModel.getGraphPath() |
| 88 | + if(path.length === 1){ |
| 89 | + this.roots.set(metaNodeModel.getID(), new Graph(metaNodeModel)) |
| 90 | + }else{ |
| 91 | + path.pop() // Removes own id from path |
| 92 | + const parentGraph = this.findNodeGraph(path) |
| 93 | + parentGraph.addChild(new Graph(metaNodeModel)) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + getNodes() : MetaNodeModel[] { |
| 98 | + const nodes = [] |
| 99 | + for(const graph of Array.from(this.roots.values())){ |
| 100 | + nodes.push(graph.getNode()) |
| 101 | + nodes.push(...graph.getDescendancy()) |
| 102 | + } |
| 103 | + return nodes |
| 104 | + } |
| 105 | + |
| 106 | + getAncestors(node : MetaNodeModel): MetaNodeModel[] { |
| 107 | + const path = node.getGraphPath() |
| 108 | + const oldestAncestor = this.getRoot(path[0]) |
| 109 | + return [oldestAncestor.getNode(), ...oldestAncestor.getChildren()] |
| 110 | + } |
| 111 | + |
| 112 | + getRoot(rootId: string) : Graph{ |
| 113 | + const root = this.roots.get(rootId) |
| 114 | + if(root === undefined){ |
| 115 | + throw new Error('unknown parent ' + rootId); |
| 116 | + } |
| 117 | + return root |
| 118 | + } |
| 119 | + |
| 120 | + getChildren(parent : MetaNodeModel): MetaNodeModel[] { |
| 121 | + const path = parent.getGraphPath() |
| 122 | + if (path.length === 1) { |
| 123 | + const root = this.getRoot(parent.getID()) |
| 124 | + return root.getChildren() |
| 125 | + } else { |
| 126 | + const graph = this.findNodeGraph(path) |
| 127 | + return graph.getChildren() |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + getParent(node : MetaNodeModel): MetaNodeModel | undefined { |
| 132 | + const path = node.getGraphPath() |
| 133 | + if (path.length === 1) { |
| 134 | + return undefined |
| 135 | + } else { |
| 136 | + path.pop() // removes own id from path |
| 137 | + const parentGraph = this.findNodeGraph(path) |
| 138 | + return parentGraph.getNode() |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + getNodeDFS(nodeId: string): MetaNodeModel | undefined { |
| 143 | + for (let root of Array.from(this.roots.values())) { |
| 144 | + const found = root.dfs(nodeId) |
| 145 | + if(found){ |
| 146 | + // @ts-ignore |
| 147 | + return found |
| 148 | + } |
| 149 | + } |
| 150 | + return undefined |
| 151 | + } |
| 152 | + |
| 153 | + getNodeContainerBoundingBox(node: MetaNodeModel) : any { |
| 154 | + const graph = this.findNodeGraph(node.getGraphPath()) |
| 155 | + return graph.getContainerBoundingBox() |
| 156 | + } |
| 157 | + |
| 158 | + private findNodeGraph(path: string[]) : Graph { |
| 159 | + const rootId = path.shift() |
| 160 | + // @ts-ignore |
| 161 | + let parent = this.getRoot(rootId) |
| 162 | + while(path.length > 0){ |
| 163 | + const next = path.shift() |
| 164 | + // @ts-ignore |
| 165 | + parent = parent.getChild(next) |
| 166 | + if (parent === undefined){ |
| 167 | + throw new Error('unknown parent ' + rootId); |
| 168 | + } |
| 169 | + } |
| 170 | + return parent |
| 171 | + } |
| 172 | + |
| 173 | + handleNodePositionChanged(metaNodeModel: MetaNodeModel, cursorX: number, cursorY: number){ |
| 174 | + // TODO: Update node parent (add or remove parent) |
| 175 | + // update node graph path, |
| 176 | + // bounding boxes of parents |
| 177 | + |
| 178 | + // Update children position (children should move the same delta as node) |
| 179 | + this.updateChildrenPosition(metaNodeModel) |
| 180 | + // Update local position / relative position to the parent |
| 181 | + this.updateNodeLocalPosition(metaNodeModel) |
| 182 | + // update the graph for right parent children relationship |
| 183 | + this.updateGraph(metaNodeModel, cursorX, cursorY); |
| 184 | + } |
| 185 | + |
| 186 | + updateGraph(metaNodeModel: MetaNodeModel, cursorX: number, cursorY: number) { |
| 187 | + let parent = undefined; |
| 188 | + let search = true; |
| 189 | + this.roots.forEach((node, id) => { |
| 190 | + if (node.getContainerBoundingBox().containsPoint(cursorX, cursorY)) { |
| 191 | + parent = node; |
| 192 | + } |
| 193 | + }); |
| 194 | + // TODO add the new child to the graph and update graphPath for the metaNodeModel instance |
| 195 | + } |
| 196 | + |
| 197 | + private updateChildrenPosition(metaNodeModel: MetaNodeModel){ |
| 198 | + const children = this.getChildren(metaNodeModel); |
| 199 | + |
| 200 | + children.forEach(n => { |
| 201 | + /* |
| 202 | + No need to explicitly call updateChildrenPosition for n children because it will happen automatically in |
| 203 | + the event listener |
| 204 | + */ |
| 205 | + // @ts-ignore |
| 206 | + const localPosition = n.getLocalPosition() |
| 207 | + n.setPosition(metaNodeModel.getX() + localPosition.x, metaNodeModel.getY() + localPosition.y) |
| 208 | + |
| 209 | + }) |
| 210 | + } |
| 211 | + |
| 212 | + private updateNodeLocalPosition(metaNodeModel: MetaNodeModel){ |
| 213 | + const parent = this.getParent(metaNodeModel) |
| 214 | + metaNodeModel.updateLocalPosition(parent) |
| 215 | + } |
| 216 | + |
| 217 | + updateNodesContainerBoundingBoxes(nodes: MetaNodeModel[]): void { |
| 218 | + nodes.forEach(n => n.setContainerBoundingBox(this.getNodeContainerBoundingBox(n))) |
| 219 | + } |
| 220 | +} |
0 commit comments