Skip to content

Commit

Permalink
[#476] refactor: traverse function removed
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasmadei authored and blcham committed Jul 3, 2024
1 parent a56d679 commit 53bd829
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 38 deletions.
31 changes: 0 additions & 31 deletions src/components/editor/faultTree/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,36 +204,6 @@ const Editor = () => {
refreshTree();
};

const redirectToInstance = (navigateFrom: string) => {
if (faultTree?.manifestingEvent?.children) {
// Recursive function to traverse the tree and find the desired key-value pair
const findNodeValue = (children) => {
for (let node of children) {
const nodeKey = node.iri;
const nodeValue = node?.references?.isPartOf;
if (nodeKey === navigateFrom && nodeValue) {
return nodeValue;
}
if (node.children) {
const result = findNodeValue(node.children);
if (result) {
return result;
}
}
}
return null;
};

const nodeValue = findNodeValue(faultTree?.manifestingEvent?.children);
const redirectTo = nodeValue?.split("/").pop();
if (redirectTo) {
history(`/fta/${redirectTo}`);
} else {
console.error("No matching node found for navigation.");
}
}
};

return (
<div onContextMenu={handleOnContextMenu}>
{faultTree && (
Expand All @@ -255,7 +225,6 @@ const Editor = () => {
showPath={showPath}
showTable={showTable}
onScenarioSelect={(scenario: FaultEventScenario) => handleOnScenarioSelect(scenario)}
redirectToInstance={redirectToInstance}
/>
)}

Expand Down
16 changes: 9 additions & 7 deletions src/components/editor/faultTree/canvas/EditorCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { useCurrentFaultTree } from "@hooks/useCurrentFaultTree";
import { useFaultTrees } from "@hooks/useFaultTrees";
import { calculateCutSets } from "@services/faultTreeService";
import { SnackbarType, useSnackbar } from "@hooks/useSnackbar";
import { useNavigate } from "react-router-dom";

enum MOVE_NODE {
DRAGGING = 0,
Expand All @@ -49,7 +50,6 @@ interface Props {
showTable: boolean;
possibleFaultEventScenarios: FaultEventScenario[];
onScenarioSelect: (scenario: FaultEventScenario) => void;
redirectToInstance: (navigateFrom: string) => void;
}

const EditorCanvas = ({
Expand All @@ -70,10 +70,10 @@ const EditorCanvas = ({
showTable,
possibleFaultEventScenarios,
onScenarioSelect,
redirectToInstance,
}: Props) => {
const { classes } = useStyles();
const theme = useTheme();
const history = useNavigate();
const { t } = useTranslation();

const containerRef = useRef(null);
Expand Down Expand Up @@ -150,11 +150,13 @@ const EditorCanvas = ({
handleNodeMove(MOVE_NODE.RELEASING, elementView, evt, x, y);
},
"element:pointerdblclick": (elementView: joint.dia.ElementView) => {
const navigateFrom = elementView?.model.attributes["custom/faultEventIri"];
redirectToInstance(navigateFrom);
setTimeout(() => {
diagramZoom.reset();
}, 100);
if (elementView?.model.attributes[JOINTJS_NODE_MODEL.redirectTo]) {
const redirectTo = elementView?.model.attributes[JOINTJS_NODE_MODEL.redirectTo];
history(`/fta/${redirectTo}`);
setTimeout(() => {
diagramZoom.reset();
}, 100);
}
},
"blank:pointerclick": () => onBlankPointerClick(),
"blank:pointerdown": () => diagramZoom.enablePan(),
Expand Down
7 changes: 7 additions & 0 deletions src/components/editor/faultTree/shapes/RenderTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ const renderTree = async (container, node, parentShape = null, pathsToHighlight,
}
// @ts-ignore
nodeShape.set(JOINTJS_NODE_MODEL.faultEventIri, node.iri);

if (node?.references?.isPartOf) {
const iriOfReference = node.references.isPartOf.split("/").pop();
// @ts-ignore
nodeShape.set(JOINTJS_NODE_MODEL.redirectTo, iriOfReference);
}

const r = node.rectangle;
if (r && r.x && r.y && r.width && r.height) {
nodeShape.position(node.rectangle.x, node.rectangle.y);
Expand Down
1 change: 1 addition & 0 deletions src/components/editor/faultTree/shapes/constants.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const JOINTJS_NODE_MODEL = {
faultEventIri: "custom/faultEventIri",
hasPersistentPosition: "custom/has-persistent-position",
redirectTo: "custom/redirectTo",
};

export const ERROR_PATH_COLOR = "#ff7f50";
Expand Down

0 comments on commit 53bd829

Please sign in to comment.