Skip to content

Commit 256d0c8

Browse files
committed
#36 adding logic for the new path calculation
1 parent 1e1b144 commit 256d0c8

File tree

4 files changed

+104
-28
lines changed

4 files changed

+104
-28
lines changed

installation.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ if [ "$INSTALL" = true ]; then
5656
yarn run start
5757
elif [ "$UPDATE" = true ]; then
5858
echo " ### Updating meta-diagram"
59-
yarn remove @metacell/meta-diagram
59+
# yarn remove @metacell/meta-diagram
60+
rm -rf yalc.lock
6061
PSYVIEW=`pwd`
6162

6263
if [ -d '../meta-diagram' ]; then

src/components/Main.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,15 @@ class Main extends React.Component {
4848
}
4949

5050
handlePostUpdates(event) {
51+
const node = event.entity;
5152
switch(event.function) {
5253
case CallbackTypes.POSITION_CHANGED: {
53-
const node = event.entity;
54-
this.metaGraph.handleNodePositionChanged(node, this.mousePos.x, this.mousePos.y);
54+
this.metaGraph.updateGraph(node, this.mousePos.x, this.mousePos.y);
55+
this.interpreter.updateModel(node);
56+
return true;
57+
}
58+
case CallbackTypes.CHILD_POSITION_CHANGED: {
59+
this.metaGraph.handleNodePositionChanged(node);
5560
this.interpreter.updateModel(node);
5661
return true;
5762
}
@@ -63,7 +68,7 @@ class Main extends React.Component {
6368
}
6469

6570
handlePreUpdates(event) {
66-
console.log('preUpdates not yet implemented.');
71+
// console.log('preUpdates not yet implemented.');
6772
}
6873

6974
metaCallback(event) {

src/components/graph/MetaGraph.ts

+78-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// import {MetaNodeModel} from "../react-diagrams/MetaNodeModel";
22
import {MetaLink, MetaNodeModel, MetaLinkModel} from "@metacell/meta-diagram"
3+
import { PNLClasses } from "../../constants";
34

45
class Graph {
56
private readonly node: MetaNodeModel;
@@ -26,6 +27,10 @@ class Graph {
2627
this.children.set(graph.getID(), graph)
2728
}
2829

30+
deleteChild(id: string) : void {
31+
this.children.delete(id);
32+
}
33+
2934
getChildren(): MetaNodeModel[] {
3035
return Array.from(this.children.values()).map(g => g.getNode())
3136
}
@@ -60,10 +65,12 @@ class Graph {
6065
export class MetaGraph {
6166
private readonly roots: Map<string, Graph>;
6267
private readonly links: MetaLinkModel[];
68+
private parentUpdating: boolean;
6369

6470
constructor() {
6571
this.roots = new Map<string, Graph>()
6672
this.links = [];
73+
this.parentUpdating = false;
6774
}
6875

6976
addLinks(links: MetaLink[]) {
@@ -170,28 +177,82 @@ export class MetaGraph {
170177
return parent
171178
}
172179

173-
handleNodePositionChanged(metaNodeModel: MetaNodeModel, cursorX: number, cursorY: number){
180+
private findParentNodeGraph(path: string[]) : Graph {
181+
const newPath = [...path];
182+
newPath.pop();
183+
return this.findNodeGraph(newPath);
184+
}
185+
186+
updateGraph(metaNodeModel: MetaNodeModel, cursorX: number, cursorY: number) {
187+
this.updateNodeContainerBoundingBox(metaNodeModel);
188+
let parent: MetaNodeModel|undefined = this.rootContainsNode(metaNodeModel, cursorX, cursorY);
189+
let newPath = this.findNewPath(metaNodeModel, parent, cursorX, cursorY);
190+
if (metaNodeModel.getGraphPath().join().toString() !== newPath.join().toString()) {
191+
this.updateNodeInGraph(metaNodeModel, newPath);
192+
}
193+
this.handleNodePositionChanged(metaNodeModel);
194+
}
195+
196+
handleNodePositionChanged(metaNodeModel: MetaNodeModel){
174197
// TODO: Update node parent (add or remove parent)
175198
// update node graph path,
176199
// bounding boxes of parents
177200

201+
// update the graph for right parent children relationship
202+
this.updateNodeContainerBoundingBox(metaNodeModel);
178203
// Update children position (children should move the same delta as node)
179204
this.updateChildrenPosition(metaNodeModel)
180205
// Update local position / relative position to the parent
181206
this.updateNodeLocalPosition(metaNodeModel)
182-
// update the graph for right parent children relationship
183-
this.updateGraph(metaNodeModel, cursorX, cursorY);
184207
}
185208

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)) {
209+
rootContainsNode(metaNodeModel: MetaNodeModel, cursorX: number, cursorY: number): MetaNodeModel|undefined {
210+
let parent = undefined
211+
this.roots.forEach((graph, id) => {
212+
const node = graph.getNode();
213+
if (node.getID() !== metaNodeModel.getID()
214+
&& node.getOption('shape') === PNLClasses.COMPOSITION
215+
&& node.getNodeBoundingBox().containsPoint(cursorX, cursorY))
216+
{
191217
parent = node;
192218
}
193219
});
194-
// TODO add the new child to the graph and update graphPath for the metaNodeModel instance
220+
return parent;
221+
}
222+
223+
findNewPath(metaNodeModel: MetaNodeModel, parent: MetaNodeModel|undefined, cursorX: number, cursorY: number) {
224+
let search: boolean = true;
225+
let newPath: string[] = [];
226+
while (search && parent) {
227+
search = false;
228+
const children = this.getChildren(parent);
229+
// eslint-disable-next-line no-loop-func
230+
children.forEach((child: MetaNodeModel) => {
231+
if (!search
232+
&& child.getID() !== metaNodeModel.getID()
233+
&& child.getOption('shape') === PNLClasses.COMPOSITION
234+
&& child.getNodeBoundingBox().containsPoint(cursorX, cursorY))
235+
{
236+
search = true;
237+
parent = child;
238+
}
239+
});
240+
// @ts-ignore
241+
newPath = parent.getGraphPath();
242+
}
243+
return [...newPath, metaNodeModel.getID()];
244+
}
245+
246+
updateNodeInGraph(metaNodeModel: MetaNodeModel, newPath: string[]) {
247+
const oldPath = metaNodeModel.getGraphPath();
248+
if (oldPath.length === 1) {
249+
this.roots.delete(oldPath[0]);
250+
} else {
251+
let parentGraph = this.findParentNodeGraph(oldPath);
252+
parentGraph.deleteChild(metaNodeModel.getID());
253+
}
254+
metaNodeModel.setOption('graphPath', newPath);
255+
this.addNode(metaNodeModel);
195256
}
196257

197258
private updateChildrenPosition(metaNodeModel: MetaNodeModel){
@@ -204,7 +265,7 @@ export class MetaGraph {
204265
*/
205266
// @ts-ignore
206267
const localPosition = n.getLocalPosition()
207-
n.setPosition(metaNodeModel.getX() + localPosition.x, metaNodeModel.getY() + localPosition.y)
268+
n.setNodePosition(metaNodeModel.getX() + localPosition.x, metaNodeModel.getY() + localPosition.y)
208269

209270
})
210271
}
@@ -214,7 +275,12 @@ export class MetaGraph {
214275
metaNodeModel.updateLocalPosition(parent)
215276
}
216277

217-
updateNodesContainerBoundingBoxes(nodes: MetaNodeModel[]): void {
218-
nodes.forEach(n => n.setContainerBoundingBox(this.getNodeContainerBoundingBox(n)))
278+
updateNodeContainerBoundingBox(node: MetaNodeModel): void {
279+
node.setContainerBoundingBox({
280+
left: node.getX(),
281+
top: node.getY(),
282+
bottom: node.getY() + node.height,
283+
right: node.getX() + node.width
284+
});
219285
}
220286
}

src/components/views/compositions/Composition.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,9 @@ class Composition extends React.Component {
8585
height: props.model.options.height,
8686
}
8787
this.changeVisibility = this.changeVisibility.bind(this);
88-
}
89-
90-
changeVisibility() {
91-
this.setState({expanded: !this.state.expanded});
92-
}
93-
94-
render() {
95-
const { expanded } = this.state;
96-
const { classes } = this.props;
9788

98-
return (
99-
<Box className={`${classes.root} ${expanded ? classes.selected : ''}`}>
100-
<Rnd
89+
this.rndElement = (
90+
<Rnd
10191
size={{ width: this.state.width, height: this.state.height }}
10292
position={{ x: this.props.model.options.x, y: this.props.model.options.y }}
10393
onResizeStop={(e, direction, ref, delta, position) => {
@@ -111,6 +101,20 @@ class Composition extends React.Component {
111101
>
112102
<Chip icon={<img src={MORE_OPTION} alt="" />} label="New Comp" color="secondary" />
113103
</Rnd>
104+
);
105+
}
106+
107+
changeVisibility() {
108+
this.setState({expanded: !this.state.expanded});
109+
}
110+
111+
render() {
112+
const { expanded } = this.state;
113+
const { classes } = this.props;
114+
115+
return (
116+
<Box className={`${classes.root} ${expanded ? classes.selected : ''}`}>
117+
{this.rndElement}
114118
</Box>
115119
);
116120
}

0 commit comments

Comments
 (0)