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

Html label inside the node / node size as a function of html label / add hook for DOM update #12

Open
cversari opened this issue Jul 25, 2018 · 4 comments

Comments

@cversari
Copy link

Hello,
first of all, thank you for this extension which I find extremely useful.

I am currently using the html label to display html content inside a node, by placing the label in the center of the node and resizing dynamically the node as a function of the size of the label (which may change over time during the lifetime of the node).

However I cannot do it in a clean way, because when the content of the label is changed, the addition of the html label to the DOM takes time and there is no triggered event that tells me when the document update is complete. Therefore I must use setTimeout() and wait for some milliseconds before calling the function that updates the sizes of the nodes. This is quite risky, because for some platforms the update time is long (mobile phones for example). On the other hand, having a too long timeout makes the UI quite impractical to use.
I also tried to add a script tag to the html label, to trigger a function as soon as the element is added to the DOM, but then the parsing of the label fails for some reason.

So here is my feature request: would it be possible to automatically update the size of the node as a function of the size of its html label?
Alternatively: would it be possible to specify a hook function in nodeHtmlLabel() parameters, that is triggered when the update of the DOM is complete?

Thank you

Cristian

@morficus
Copy link

morficus commented Oct 8, 2018

@cversari can you share how are you calculating the node size based on the HTML label?

@cversari
Copy link
Author

cversari commented Oct 8, 2018

@morficus just wrap the label content in a <div>, then use getBoundingClientRect to compute the size of the node.

@klysak
Copy link

klysak commented Mar 28, 2019

Hello,
first of all, thank you for this extension which I find extremely useful.

I am currently using the html label to display html content inside a node, by placing the label in the center of the node and resizing dynamically the node as a function of the size of the label (which may change over time during the lifetime of the node).

However I cannot do it in a clean way, because when the content of the label is changed, the addition of the html label to the DOM takes time and there is no triggered event that tells me when the document update is complete. Therefore I must use setTimeout() and wait for some milliseconds before calling the function that updates the sizes of the nodes. This is quite risky, because for some platforms the update time is long (mobile phones for example). On the other hand, having a too long timeout makes the UI quite impractical to use.
I also tried to add a script tag to the html label, to trigger a function as soon as the element is added to the DOM, but then the parsing of the label fails for some reason.

So here is my feature request: would it be possible to automatically update the size of the node as a function of the size of its html label?
Alternatively: would it be possible to specify a hook function in nodeHtmlLabel() parameters, that is triggered when the update of the DOM is complete?

Thank you

Cristian

Hello Cristian!

Did you manage to solve your problem?

@Mote-Z
Copy link

Mote-Z commented Oct 17, 2020

I can now update the node size by simply adding a return in addOrUpdateElem function

   addOrUpdateElem(id: string, param: CytoscapeNodeHtmlParams, payload: { data?: any, position?: ICytoscapeNodeHtmlPosition } = {}) {
      let cur = this._elements[id];
      console.log(cur)
      if (cur) {
        cur.updateParams(param);
        cur.updateData(payload.data);
        cur.updatePosition(payload.position);
        return null
      } else {
        let nodeElem = document.createElement("div");
        this._node.appendChild(nodeElem);

        this._elements[id] = new LabelElement({
          node: nodeElem,
          data: payload.data,
          position: payload.position
        }, param);
        return nodeElem
      }
    }

then update the node when creating the html label

    function createNodesCyHandler({cy}: ICyEventObject) {
      _params.forEach(x => {
        cy.elements(x.query).forEach((d: any) => {
          if (d.isNode()) {
            let htmlEle =_lc.addOrUpdateElem(d.id(), x, {
              position: getNodePosition(d),
              data: d.data()
            });
            console.log(htmlEle);
            console.log(htmlEle.clientHeight);
            d.style('height',htmlEle.clientHeight);
            d.style('width',htmlEle.clientWidth);
          }
        });
      });
    }

however , I encountered a new problem , when I click the link on the label , It actually click the node , I haven't found a solution yet

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

No branches or pull requests

5 participants