Cannot read properties of undefined draggingTool

Dear experts,
I want to get the dragged objects by the event ‘mouseDragEvent’ in myDiagram to drag them onto another object (‘backgroundObject’).
I get the following error : “Cannot read properties of undefined draggingTool”.

myDiagram.nodeTemplate =
	$(go.Node, "Auto",
		{  

		mouseDragEnter: (e, obj ) => {
			 var shape = obj.findObject("SHAPE");
			  console.log("backgroundObject: " + shape.part.data.key); // ok

			var aPart= null, theKey = '';
			var it = this.toolManager.draggingTool.draggedParts.iterator; // the error occurs in this line
			while (it.next()) {
					aPart = it.value;
					console.log("draggedPart: " + aPart.data.key );
			}
			  
			  
        },

My second question: How can I prevent the ‘mouseDragEnter’-event fire onto the palette. I should do only on the diagram and not on the palette.

Thanks in advance!

This holds the collection of Parts being moved: DraggingTool | GoJS API
But this will be null during a copy.

Use DraggingTool | GoJS API to get what has been temporarily copied.

In your event handler, what is this? Are you using the same node template in the Palette? Check whether e.diagram instanceof go.Palette.

My second question is solved - thank’s for your tip!

My first problem isn’t solved yet. I use …draggingTool.copiedParts

myWorkspace.nodeTemplate =
	$(go.Node, "Auto",
		{  
		locationSpot: go.Spot.Top,
		isShadowed: true, shadowBlur: 1,
		shadowOffset: new go.Point(0, 1),
		shadowColor: "rgba(0, 0, 0, .14)",


		mouseDragEnter: (e, obj, prev) => {
			  obj.isSelected = true;

			  var shape = obj.findObject("SHAPE");
			  console.log("backgroundObject: " + shape.part.data.key); // ok 
			  
			  
			var aPart= null, theKey = '';
			var diagram = obj.diagram;
			var tool = diagram.toolManager.draggingTool;
			var it = tool.copiedParts.iterator;
			while (it.next()) {
					aPart = it.value;
					console.log("draggedPart: " + aPart.data.key ); // error see below
			}
			  
			  
        },

image

Thanks for your support!

It’s hard for me to debug your app. What is it that you are really trying to accomplish?

image

Dear support,
my use case should be simple:
Drag a node (‘dragNode’) onto another node ('target’Node) to read some ‘dragNode’.properties and set them in the 'target’Node.properties. This should do only in the same diagram (sceenshot2). The ‘dragNode’ is copied before from the palette to the diagram (screenshot1).
My problem is that I can’t read the ‘draggedNode’.properties ( see.screenshot2 / 3) .

myDiagram.nodeTemplate =
	$(go.Node, "Auto",
		{  
		locationSpot: go.Spot.Top,
		isShadowed: true, shadowBlur: 1,
		shadowOffset: new go.Point(0, 1),
		shadowColor: "rgba(0, 0, 0, .14)",


		mouseDragEnter: (e, obj) => {
			// get node data of the 'target' node in the diagram - it looks OK  in the console!
			var shape = obj.findObject("SHAPE");
			console.log("targetNode.key: " + shape.part.data.key); // key = className
			console.log("targetNode.label: " + shape.part.data.label);
			console.log("targetNode.iconText: " + shape.part.data.iconText);
			console.log("targetNode.subComponents[0].instanceName: " + shape.part.data.subComponents[0].instanceName);
			console.log("targetNode.subComponents[0].icon: " + shape.part.data.subComponents[0].icon);
			console.log("targetNode.subComponents[1].instanceName: " + shape.part.data.subComponents[1].instanceName);
			console.log("targetNode.shape.part.data.subComponents[1].icon: " + shape.part.data.subComponents[1].icon);
			  
			
			// get node data of the dragged node, which should be dragged onto 'target' node in the same diagram
			var diagram = obj.diagram;
			var tool = diagram.toolManager.draggingTool;
			/*
			//var it = tool.copiedParts.iterator; 
			copy nodes the from the palette directly onto a diagram node should not be possible, 
			because the user had to set some node properties before dragging !
			*/
			var it = tool.draggedParts.iterator; 
			console.log("it.count: " + it.count ); // it.count = 1 ; looks OK - I drag only 1 node
			while (it.next()) {
					console.log("it :" + it);
					
					// in the console.log there is 1 node data [object Object]
					console.log("it key | value :" + it.key + ": " + it.value); 
					
					// ERROR : I can't read node data [object Object]
					console.log("it key | value[0].key :" + it.key + ": " + it.value[0].key); 
					console.log("it key | value[0].label :" + it.key + ": " + it.value[0].label);
			}
			
			/* My use case is focused only on the 'mouseDragEnter' event to read 
			some draggedNode.properties to set them into the targetNode.properties	
			*/
		  
        },

In the code below you see the setting of the palette node. The code runs successfull (see the palette in the screenshots). But I can’t read the properties later after the dragging!

xmlDoc = parser.parseFromString(components,"text/xml");
	var lComponent = xmlDoc.getElementsByTagName("Component"); 
	var Component = lComponent.item(0); 
    while(Component) {
		if( Component.getAttribute( 'category' ) == 'AppControl' )
		{
			paletteAppControl.model.addNodeData( 
			{ 
                // use case : get this properties later if the node is dragged 
                //- but I can't read it !
				key: Component.getAttribute('name'),
				icon: Component.getAttribute('icon'), 
				iconText: Component.getAttribute('iconText'), 
				label: Component.getAttribute('label'),
				figure:"Rectangle"
			}
			); 
		}
		Component = Component.nextElementSibling;
    } // end while(Component )

Thanks for help!

Since you declared the mouseDragEnter event handler on the Node of the node template, the second argument will always be a Node copied from that template. So you do not need to find the “SHAPE” object and use its shape.part.

Furthermore that argument will always be in the target diagram, so its diagram property will never be the source diagram/palette. When a cross-diagram drag-and-drop is happening, it is only the source diagram’s DraggingTool that is running. The dragging operation makes use of the target diagram’s DraggingTool, but that tool is not running – i.e. Diagram.currentTool will not be a DraggingTool.

mouseDragEnter: (e, node) => {
  // node.key of stationary Node
  if (e.diagram.currentTool instanceof go.DraggingTool) {
    // dragging within myDiagram
  } else {
    // dragging from a different Diagram
  }
}

Dear support,
sorry - but the same problem occurs. I understand the principle of the two different draggingTools (diagram and palette). I want to drag nodes in 'myDiagram" .
What is in my code wrong ?
Thanks!

myDiagram =
	$(go.Diagram, "divmyDiagram",
		{ 	maxSelectionCount: 1, // users can select only one part at a time,
			"undoManager.isEnabled": true,
		}
	);
	
	myDiagram.nodeTemplate =
	$(go.Node, "Auto",
		{  
		locationSpot: go.Spot.Top,
		isShadowed: true, shadowBlur: 1,
		shadowOffset: new go.Point(0, 1),
		shadowColor: "rgba(0, 0, 0, .14)",

		
		mouseDragEnter: (e, obj) => {
			
			// node.key of stationary Node
			  if (e.diagram.currentTool instanceof go.DraggingTool) {
				// dragging within myDiagram
				console.log("dragging within myDiagram");
				var tool = myDiagram.toolManager.draggingTool;
				var it = tool.draggedParts.iterator; 
				console.log("it.count: " + it.count ); // it.count = 1 ; looks OK - I drag only 1 node
				while (it.next()) {
						console.log("it :" + it);
						
						// in the console.log there is 1 node data [object Object]
						console.log("it key | value :" + it.key + ": " + it.value); 
						
						// ERROR : I can't read node data [object Object]
						console.log("it key | value[0].key :" + it.key + ": " + it.value[0].key); 
						console.log("it key | value[0].label :" + it.key + ": " + it.value[0].label);
				}
			  } else {
				// dragging from a different Diagram (paletteDiagramm)
				return;
			  }  

I’m still unsure what you want to do, but I hope you realize what the type of DraggingTool.draggedParts is: DraggingTool | GoJS API

It’s a Map mapping Parts to DraggingInfos. So I don’t know why you are trying to get it.value[0].

DraggingInfo is: DraggingInfo | GoJS API

Hallo Walter,
thanks for your patience to solve my problem !
Maybe a I haven’t the correct understanding to get node properties …
Please look at the screenshot which explain my use case - Thanks!

myDiagram =
	$(go.Diagram, "divDiagram",
		{ 	maxSelectionCount: 1, 
			"undoManager.isEnabled": true,
		}
	);
	
	myDiagram.nodeTemplate =
	$(go.Node, "Auto",
		{  
		locationSpot: go.Spot.Top,
		isShadowed: true, shadowBlur: 1,
		shadowOffset: new go.Point(0, 1),
		shadowColor: "rgba(0, 0, 0, .14)",

		
		mouseDragEnter: (e, obj) => {

			  if (e.diagram.currentTool instanceof go.DraggingTool) {
				// dragging within myDiagram
				console.log("dragging within myDiagram");
				var aPart= null;
				var tool = myWorkspace.toolManager.draggingTool;
				var mapIt = tool.draggedParts.iterator; 
				console.log("mapIt.count: " + mapIt.count ); // it.count = 1 ; looks OK - I drag only 1 node
				while (mapIt.next()) {
					aPart = mapIt.value;
					console.log("Part : " + aPart ); 
					console.log("Part.key : " + aPart.key );  // ???
					console.log("Part.value :" + aPart.value);   // ???
				}
				
			  } else {
				// do nothing;  should not be possible dragging directly from palette to diagram in my use case
				console.log("dragging from a different Diagram");
			  }
			
        },
			  

I just tried this, and it seems to do what I think you are asking for:

    $(go.Node, . . .,
      {
        . . .
        mouseDragEnter: (e, node) => {
          const tool = e.diagram.currentTool;
          if (tool instanceof go.DraggingTool) {
            const coll = tool.draggedParts || tool.copiedParts;
            if (coll) console.log(coll.count, coll.first().key.data);
          }
        }
      },
      . . .

It writes out the data object for the first dragged Part.

Perfect - it works!
I try to understand the ‘key’ in “coll.first().key.data” ? ! ?
Thanks !

const tool = e.diagram.currentTool;
if (tool instanceof go.DraggingTool)
{
	const coll = tool.draggedParts; // tool.copiedParts;
	if (coll) {
		console.log(coll.count, coll.first().key.data);
		console.log(coll.first().key.data.key);
		console.log(coll.first().key.data.label);
		console.log(coll.first().key.data.iconText);
	}
}

Remember what I said above about the type of DraggingTool.draggedParts?
DraggingTool | GoJS API
The items in a Map are key/value pairs.

const part = coll.first().key;
console.log(part.data.key, part.data.label, part.data.iconText);

OK - I understand now your code. The const ‘part’ contains the key.
I close this thread and continue the evaluation.

Thanks for your support !