@ts4nfdi/terminology-service-suite 5.1.8 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -4450,16 +4450,20 @@ function WrappedOntologyInfoWidget(props) {
4450
4450
  }
4451
4451
 
4452
4452
  function GraphViewWidget(props) {
4453
- const { api, iri, ontologyId, rootWalk, className } = props;
4453
+ const { api, iri, ontologyId, rootWalk, className, hierarchy, edgeLabel, onNodeClick, } = props;
4454
4454
  const [selectedIri, setSelectedIri] = useState(iri);
4455
4455
  const [firstLoad, setFirstLoad] = useState(true);
4456
4456
  const [dbclicked, setDbclicked] = useState(false);
4457
4457
  const [rootWalkIsSelected, setRootWalkIsSelected] = useState(rootWalk ? rootWalk : false);
4458
+ const [hierarchicalView, setHierarchicalView] = useState(hierarchy ? true : false);
4458
4459
  const [isPopoverOpen, setIsPopoverOpen] = useState(false);
4459
4460
  // needed for useQuery. without it the graph won't get updated on switching berween rootWalk=true and false.
4460
4461
  const [counter, setCounter] = useState(0);
4461
4462
  const olsApi = new OlsApi(api);
4462
4463
  const finalClassName = className || "ts4nfdi-graph-style";
4464
+ const subClassEdgeLabel = !edgeLabel || edgeLabel === "undefined" ? "is a" : edgeLabel;
4465
+ const onNodeClickCallbackIdProvided = typeof onNodeClick === "function" &&
4466
+ !onNodeClick.name.includes("mockConstructor");
4463
4467
  const { data, isLoading, isError, error } = useQuery([
4464
4468
  "termGraph",
4465
4469
  api,
@@ -4469,21 +4473,40 @@ function GraphViewWidget(props) {
4469
4473
  dbclicked,
4470
4474
  counter,
4471
4475
  ], async () => {
4472
- if (rootWalkIsSelected && firstLoad) {
4476
+ if (rootWalkIsSelected && firstLoad && !hierarchicalView) {
4473
4477
  // only use this call on load. Double ckicking on a node should call the normal getTermRelations function.
4474
- return olsApi.getTermTree({ ontologyId: ontologyId, termIri: iri }, { viewMode: "All", siblings: false });
4478
+ return {
4479
+ treeData: await olsApi.getTermTree({ ontologyId: ontologyId, termIri: iri }, { viewMode: "All", siblings: false }),
4480
+ };
4475
4481
  }
4476
- else if (firstLoad || dbclicked) {
4477
- return olsApi.getTermRelations({
4482
+ else if (rootWalkIsSelected && firstLoad && hierarchicalView) {
4483
+ let termTree = await olsApi.getTermTree({ ontologyId: ontologyId, termIri: iri }, { viewMode: "All", siblings: false });
4484
+ let termRelation = await olsApi.getTermRelations({
4478
4485
  ontologyId: ontologyId,
4479
4486
  termIri: selectedIri,
4480
4487
  });
4488
+ return { treeData: termTree, termRelations: termRelation };
4489
+ }
4490
+ else if (firstLoad || dbclicked) {
4491
+ return {
4492
+ termRelations: await olsApi.getTermRelations({
4493
+ ontologyId: ontologyId,
4494
+ termIri: selectedIri,
4495
+ }),
4496
+ };
4481
4497
  }
4482
4498
  });
4483
4499
  const nodes = useRef(new DataSet([]));
4484
4500
  const edges = useRef(new DataSet([]));
4485
4501
  const graphNetwork = useRef({});
4486
4502
  const container = useRef(null);
4503
+ const hierarchicalConfig = {
4504
+ enabled: true,
4505
+ //@ts-ignore
4506
+ direction: "DU",
4507
+ //@ts-ignore
4508
+ sortMethod: "directed",
4509
+ };
4487
4510
  const graphNetworkConfig = {
4488
4511
  /**
4489
4512
  * for more options have a look at: https://visjs.github.io/vis-network/docs/network/#options
@@ -4496,6 +4519,9 @@ function GraphViewWidget(props) {
4496
4519
  randomSeed: 1,
4497
4520
  improvedLayout: true,
4498
4521
  clusterThreshold: 150,
4522
+ hierarchical: {
4523
+ enabled: false,
4524
+ },
4499
4525
  },
4500
4526
  physics: {
4501
4527
  enabled: true,
@@ -4507,8 +4533,15 @@ function GraphViewWidget(props) {
4507
4533
  damping: 0.09,
4508
4534
  avoidOverlap: 0,
4509
4535
  },
4536
+ hierarchicalRepulsion: {
4537
+ damping: 0.09,
4538
+ avoidOverlap: 0.9,
4539
+ },
4510
4540
  },
4511
4541
  };
4542
+ if (hierarchicalView) {
4543
+ graphNetworkConfig["layout"]["hierarchical"] = hierarchicalConfig;
4544
+ }
4512
4545
  class GraphNode {
4513
4546
  id;
4514
4547
  label;
@@ -4565,35 +4598,17 @@ function GraphViewWidget(props) {
4565
4598
  }
4566
4599
  }
4567
4600
  }
4568
- function convertToOlsGraphFormat(listOfJsTreeNodes) {
4569
- // used for converting the list of ancestors to the ols api graph endpoints format. to be consumed by GraphNode and GraphEdge classes constructor.
4570
- // currently used in showing ancestors. Equivalent to is-a relation.
4571
- let data = { nodes: [], edges: [] };
4572
- listOfJsTreeNodes.map((treeNode) => {
4573
- if (!data.nodes.find((obj) => obj.iri === treeNode.iri)) {
4574
- let node = { iri: treeNode.iri, label: treeNode.text };
4575
- data.nodes.push(node);
4576
- }
4577
- let parentNode = listOfJsTreeNodes.find((obj) => obj.id === treeNode.parent);
4578
- if (parentNode) {
4579
- // parent does not exists --> '#' id that indicates a node is a root.
4580
- let edge = {
4581
- source: treeNode.iri,
4582
- target: parentNode.iri,
4583
- label: "subClassOf",
4584
- uri: "http://www.w3.org/2000/01/rdf-schema#subClassOf",
4585
- };
4586
- if (!data.edges.find((obj) => obj.source === edge.source && obj.target === edge.target)) {
4587
- data.edges.push(edge);
4588
- }
4589
- }
4590
- });
4591
- return data;
4592
- }
4593
4601
  if (data && (firstLoad || dbclicked)) {
4594
- let gData = data;
4595
- if (rootWalkIsSelected && firstLoad) {
4596
- gData = convertToOlsGraphFormat(data);
4602
+ let gData = data.termRelations;
4603
+ if (data.treeData && rootWalkIsSelected && firstLoad && !hierarchicalView) {
4604
+ gData = convertToOlsGraphFormat(data.treeData, undefined);
4605
+ }
4606
+ else if (data.termRelations &&
4607
+ data.treeData &&
4608
+ rootWalkIsSelected &&
4609
+ firstLoad &&
4610
+ hierarchicalView) {
4611
+ gData = convertToOlsGraphFormat(data.treeData, data.termRelations);
4597
4612
  }
4598
4613
  for (let node of gData["nodes"]) {
4599
4614
  let gNode = new GraphNode({ node: node });
@@ -4631,6 +4646,88 @@ function GraphViewWidget(props) {
4631
4646
  setDbclicked(false);
4632
4647
  }
4633
4648
  }
4649
+ function convertToOlsGraphFormat(listOfJsTreeNodes, nodeRelations) {
4650
+ // used for converting the list of ancestors to the ols api graph endpoints format. to be consumed by GraphNode and GraphEdge classes constructor.
4651
+ // currently used in showing ancestors. Equivalent to is-a relation.
4652
+ // first, the flat array of nodes should turn to a tree.
4653
+ let treeData = [];
4654
+ for (let node of listOfJsTreeNodes) {
4655
+ if (node.parent === "#") {
4656
+ treeData.push(node);
4657
+ continue;
4658
+ }
4659
+ for (let pn of listOfJsTreeNodes) {
4660
+ if (pn.id === node.parent) {
4661
+ if ("childrenList" in pn) {
4662
+ pn.childrenList.push(node);
4663
+ }
4664
+ else {
4665
+ pn.childrenList = [node];
4666
+ }
4667
+ if ("parentList" in node) {
4668
+ //@ts-ignore
4669
+ node.parentList.push(pn);
4670
+ }
4671
+ else {
4672
+ node.parentList = [pn];
4673
+ }
4674
+ }
4675
+ }
4676
+ }
4677
+ let graphData = { nodes: [], edges: [] };
4678
+ let q = [...treeData];
4679
+ let layerq = [];
4680
+ let height = 1;
4681
+ while (true) {
4682
+ let node = q[0];
4683
+ q = q.slice(1);
4684
+ if (!graphData.nodes.find((obj) => obj.iri === node.iri)) {
4685
+ let gnode = { iri: node.iri, label: node.text, level: height };
4686
+ graphData.nodes.push(gnode);
4687
+ }
4688
+ if (node.parentList && node.parentList.length !== 0) {
4689
+ node.parentList.forEach((pn) => {
4690
+ let edge = {
4691
+ source: node.iri,
4692
+ target: pn.iri,
4693
+ label: subClassEdgeLabel,
4694
+ uri: "http://www.w3.org/2000/01/rdf-schema#subClassOf",
4695
+ };
4696
+ if (!graphData.edges.find((obj) => obj.source === edge.source && obj.target === edge.target)) {
4697
+ graphData.edges.push(edge);
4698
+ }
4699
+ });
4700
+ }
4701
+ if (node.childrenList && node.childrenList.length !== 0) {
4702
+ layerq.push(...node.childrenList);
4703
+ }
4704
+ if (q.length === 0) {
4705
+ if (layerq.length === 0) {
4706
+ break;
4707
+ }
4708
+ height += 1;
4709
+ q.push(...layerq);
4710
+ layerq = [];
4711
+ }
4712
+ }
4713
+ if (nodeRelations) {
4714
+ // add the "has part" relation to the hierarchy
4715
+ let onlyHasPartRelations = {
4716
+ nodes: [],
4717
+ edges: [],
4718
+ };
4719
+ for (let edge of nodeRelations["edges"]) {
4720
+ if (edge["label"] === "has part") {
4721
+ onlyHasPartRelations.edges.push(edge);
4722
+ onlyHasPartRelations.nodes.push(nodeRelations.nodes.find((node) => node.iri === edge.source));
4723
+ onlyHasPartRelations.nodes.push(nodeRelations.nodes.find((node) => node.iri === edge.target));
4724
+ }
4725
+ }
4726
+ graphData["nodes"] = graphData["nodes"].concat(onlyHasPartRelations["nodes"]);
4727
+ graphData["edges"] = graphData["edges"].concat(onlyHasPartRelations["edges"]);
4728
+ }
4729
+ return graphData;
4730
+ }
4634
4731
  function reset() {
4635
4732
  nodes.current.clear();
4636
4733
  edges.current.clear();
@@ -4652,7 +4749,12 @@ function GraphViewWidget(props) {
4652
4749
  if (params.nodes.length > 0) {
4653
4750
  let nodeIri = params.nodes[0];
4654
4751
  setSelectedIri(nodeIri);
4655
- setDbclicked(true);
4752
+ if (onNodeClickCallbackIdProvided) {
4753
+ onNodeClick(nodeIri);
4754
+ }
4755
+ else {
4756
+ setDbclicked(true);
4757
+ }
4656
4758
  }
4657
4759
  });
4658
4760
  }
@@ -4665,6 +4767,36 @@ function GraphViewWidget(props) {
4665
4767
  // when user change the storybook rootWalk value
4666
4768
  setRootWalkIsSelected(rootWalk ? rootWalk : false);
4667
4769
  }, [rootWalk]);
4770
+ useEffect(() => {
4771
+ // when user change the storybook hierarchy value
4772
+ setHierarchicalView(hierarchy ? hierarchy : false);
4773
+ }, [hierarchy]);
4774
+ useEffect(() => {
4775
+ if (hierarchicalView) {
4776
+ graphNetworkConfig["layout"]["hierarchical"] = hierarchicalConfig;
4777
+ }
4778
+ else {
4779
+ //@ts-ignore
4780
+ delete graphNetworkConfig["layout"]["hierarchical"];
4781
+ }
4782
+ let graphData = { nodes: nodes.current, edges: edges.current };
4783
+ graphNetwork.current = new Network(
4784
+ //@ts-ignore
4785
+ container.current, graphData, graphNetworkConfig);
4786
+ //@ts-ignore
4787
+ graphNetwork.current.on("doubleClick", function (params) {
4788
+ if (params.nodes.length > 0) {
4789
+ let nodeIri = params.nodes[0];
4790
+ setSelectedIri(nodeIri);
4791
+ if (onNodeClickCallbackIdProvided) {
4792
+ onNodeClick(nodeIri);
4793
+ }
4794
+ else {
4795
+ setDbclicked(true);
4796
+ }
4797
+ }
4798
+ });
4799
+ }, [hierarchicalView]);
4668
4800
  const onButtonClick = () => setIsPopoverOpen((isPopoverOpen) => !isPopoverOpen);
4669
4801
  const closePopover = () => setIsPopoverOpen(false);
4670
4802
  const button = (React.createElement(EuiButtonEmpty, { iconType: "iInCircle", iconSide: "right", onClick: onButtonClick }, "Guide me"));
@@ -4682,6 +4814,9 @@ function GraphViewWidget(props) {
4682
4814
  React.createElement("div", { style: { display: "inline-block", float: "right", paddingTop: 10 } },
4683
4815
  React.createElement(EuiSwitch, { label: "root walk", checked: rootWalkIsSelected, onChange: () => {
4684
4816
  setRootWalkIsSelected(!rootWalkIsSelected);
4817
+ }, title: "Enable the root walk mode in the graph: You can see the path from roots to the target node" }),
4818
+ React.createElement(EuiSwitch, { label: "hierarchy", checked: hierarchicalView, onChange: () => {
4819
+ setHierarchicalView(!hierarchicalView);
4685
4820
  }, title: "Enable the root walk mode in the graph: You can see the path from roots to the target node" }))),
4686
4821
  React.createElement(EuiPanel, { style: { width: 900, height: 900 }, hasShadow: false, hasBorder: true, borderRadius: "none" },
4687
4822
  isLoading && React.createElement(EuiLoadingSpinner, { size: "m" }),
@@ -4694,7 +4829,7 @@ function WrappedGraphViewWidget(props) {
4694
4829
  const queryClient = new QueryClient();
4695
4830
  return (React.createElement(EuiProvider, { colorMode: "light", globalStyles: false },
4696
4831
  React.createElement(QueryClientProvider, { client: queryClient },
4697
- React.createElement(GraphViewWidget, { api: props.api, iri: props.iri, ontologyId: props.ontologyId, rootWalk: props.rootWalk }))));
4832
+ React.createElement(GraphViewWidget, { api: props.api, iri: props.iri, ontologyId: props.ontologyId, rootWalk: props.rootWalk, hierarchy: props.hierarchy, edgeLabel: props.edgeLabel, onNodeClick: props.onNodeClick }))));
4698
4833
  }
4699
4834
 
4700
4835
  function TermDepictionWidget(props) {