Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursive Expand Parents #127

Open
sashokbg opened this issue Nov 2, 2021 · 2 comments
Open

Recursive Expand Parents #127

sashokbg opened this issue Nov 2, 2021 · 2 comments

Comments

@sashokbg
Copy link

sashokbg commented Nov 2, 2021

This is a feature proposal:

Context
Currently there is a very useful function that allows to recursively expand a node and its descendants (children) - expandRecursively()

Proposal
Create a function similar to expandRecursively() called expandParentsRecursively() and that works in the other direction i.e. taking a child and expanding its parents until no more parents are available.

This function can be useful when trying to locate a "hidden" element that is a child of a collapsed combo.

Please let me know what you think and I will try to create a proposal Pull Request

@hasanbalci
Copy link
Contributor

Hi @sashokbg, I think that this feature (even though not exactly) may be achieved by using the current API. Expand-collapse has the following API function:
api.getParent(nodeId) Get the parent of a node given its node id. Useful to reach parent of a node removed because of collapse operation.
By using this function, you can trace the ancestors of a removed node until you can find an ancestor that exists in the graph (maybe with a while loop). Then you can apply expandRecursively() function on that ancestor. Only downside of this approach is that it may result expanding some extra nodes that are not ancestor of the removed node. If it's not a problem for you, I think you can give it a try. Otherwise, we are open to pull requests, you can create one for unstable branch as it's ahead of the master.

@sashokbg
Copy link
Author

sashokbg commented Nov 7, 2021

Hello @hasanbalci, thank you for your answer.
I ended up writing the function as you suggested.

(Written in typescript)

export const expandParentsRecursively = (cy: Core, api: any, node: any, includeSelf = true) => {
  let parentId = node.data('parent');

  if (parentId) {
    let parentElement = cy.getElementById(parentId);
    if (!parentElement || parentElement.empty()) {
      // @ts-ignore
      parentElement = api.getAllCollapsedChildrenRecursively().filter(`node#${parentId}"]`)[0];
    }
    expandParentsRecursively(cy, api, parentElement);
  }

  if (includeSelf) {
    api.expand(node, { animate: false });
  }
};

If you think such a function can be beneficial for the project, I can create a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants