Node doesn't have location when using Robot

@walter

Hi Walter,

I’ve two questions to be clarified?

  1. What does a GraphObject represent - Diagram? Node?

  2. When I’m using the following function to programmatically click a node, I’m not getting correct location for node:

function clickLambda() {
var lambda = myDiagram.findNodeForKey(“Lambda”); // I’m able to get the node
if (lambda === null) return;
var loc = lambda.location;

robot.mouseDown(loc.x + 10, loc.y + 10, 0, { });
robot.mouseUp(loc.x + 10, loc.y + 10, 100, { });

}

when I debug this console, I get loc = {x: NaN, y: NaN} bcz of which my click event is failing.

Could you pls suggest or correct if I’m doing something wrong?

Thanks,
Saravana

  1. A GraphObject is the smallest unit of something that can be drawn.
  2. That’s probably because you are calling that code before that node has gotten its location yet. Can you see that node in the viewport before you call your clickLambda function?

Thanks for your reply, @walter

Yes, I can see the node in the view port before clickLambda function is called. But, I’m seeing the issue still. Please find below the code sample:

<script src="go.js"></script>
<script src="Robot.js"></script>
<script>
	function init() {
		var $ = go.GraphObject.make;
		myDiag = $(go.Diagram, "myDiagramDiv",
			{ "clickCreatingTool.archetypeNodeData": { text: "Node", color: "white" }},
		);
		var noedDataArray = [
			{ key: "Lambda", colour: "red" },
			{ key: "Gamma", colour: "black" }
		];
		var linkDataArray = [
			{ to: "Gamma", from: "Lambda" }
		];
		myDiag.model = new go.GraphLinksModel(noedDataArray, linkDataArray);
		modifyNode();
		clickLambda();
	}		
	function modifyNode() {
		var $ = go.GraphObject.make;
		myDiag.nodeTemplate = 
			$(go.Node, "Auto",
				$(go.Shape, "RoundedRectangle",	
					{
						fill: "white"
					},
					new go.Binding("fill", "color")),
				$(go.TextBlock, "text",
					{
						editable: true
					},
					new go.Binding("text", "key"))
			);			
	}
	function clickLambda() {
		robot = new Robot(myDiag);
		var lambda = myDiag.findNodeForKey("Lambda");
		if (lambda === null) return;
		var loc = lambda.location;
		robot.mouseDown(loc.x + 10, loc.y + 10, 0, { });
		robot.mouseUp(loc.x + 10, loc.y + 10, 100, { });
	}		
</script>

Am I doing something wrong?

Thanks,
Saravana

No, you are calling modifyNode and clickLambda before you can actually see the diagram that was loaded from the new model.

You can call those functions either after the Diagram is completely visible in the viewport, or if you want to call it programmatically, in or after an “InitialLayoutCompleted” DiagramEvent listener.

Thanks @walter, this solved the issue