I’m creating an application which will allow a user to first move and position a node. Once the node is placed, the user needs to expand this node. I’m having an issue that when the node is expanded, in a tree layout, the node expands in the wrong direction.
I’m using a tree layout and the layout direction is set to 90 (Top-Down). When I move the node I set the isLayoutPositioned=false to keep the node in place. Unfortunately, the node expands upward instead of downward.
Initial View after node was moved.
View after the node was expanded. (Expanded upward)
Code:
POC<script src="../release/go.js"></script>
<script id="code">
function init() {
//if (window.goSamples) goSamples(); // init for these samples -- you don't need to call this
var $ = go.GraphObject.make; // for conciseness in defining templates
myDiagram =
$(go.Diagram, "myDiagramDiv", // must be the ID or reference to div
{
initialAutoScale: go.Diagram.UniformToFill,
initialContentAlignment: go.Spot.TopCenter,
layout: $(go.TreeLayout, { angle: 90 }),
"ModelChanged": function(e) {
if (e.isTransactionFinished) {
document.getElementById("savedModel").textContent = e.model.toJson();
}
},
"SelectionMoved": function(e) {
console.log('moved');
e.subject.each(function(p) {
if (p instanceof go.Node) {
p.isLayoutPositioned = false;
}
});
}
});
// define the Node template
myDiagram.nodeTemplate =
$(go.Node, "Vertical",
{ locationSpot: go.Spot.Center,
isTreeExpanded: false,
isTreeLeaf: false },
new go.Binding("location").makeTwoWay(),
$(go.Panel, "Auto", { name: "BODY" },
$(go.Shape, "RoundedRectangle",
new go.Binding("fill"),
new go.Binding("stroke")),
$(go.TextBlock,
{ font: "bold 12pt Arial, sans-serif"}),
new go.Binding("text")),
$("TreeExpanderButton",
{
name: 'TREEBUTTON',
width: 20, height: 20,
alignment: go.Spot.TopRight,
alignmentFocus: go.Spot.Center,
// customize the expander behavior to
// create children if the node has never been expanded
click: function(e, obj) { // OBJ is the Button
obj.visible = false;
var node = obj.part; // get the Node containing this Button
if (node === null) return;
e.handled = true;
expandNode(node);
}
}
) // end TreeExpanderButton
);
// define the Link template to be minimal
myDiagram.linkTemplate =
$(go.Link,
{ selectable: false, routing: go.Link.AvoidsNodes, corner: 10},
new go.Binding("points").makeTwoWay(),
$(go.Shape,
{ strokeWidth: 1, stroke: "blue" },
new go.Binding("stroke", "color"),
new go.Binding("strokeWidth", "width"),
new go.Binding("strokeDashArray", "dash")
),
);
let nodeArray = [];
nodeArray.push({
key: 1,
text: 'Start',
fill: go.Brush.randomColor(),
isLayoutPositioned: false
});
myDiagram.model.nodeDataArray = nodeArray;
}
function expandNode(node) {
var diagram = node.diagram;
var data = node.data;
if (!data.everExpanded) {
// only create children once per node
diagram.model.setDataProperty(data, "everExpanded", true);
var numchildren = Math.floor(Math.random() * 4) + 2;
for (let i = 0; i < numchildren; i++) {
let newNode = {
key: Math.floor((Math.random() * 10000) + 1),
text: i.toString(),
fill: go.Brush.randomColor(),
isLayoutPositioned: false,
};
diagram.model.addNodeData(newNode);
diagram.model.addLinkData({from: node.data.key, to: newNode.key, color: 'blue', isLayoutPositioned: false});
}
}
// this behavior is generic for most expand/collapse tree buttons:
if (node.isTreeExpanded) {
diagram.commandHandler.collapseTree(node);
} else {
diagram.commandHandler.expandTree(node);
}
diagram.commitTransaction("CollapseExpandTree");
}
</script>
<textarea id="savedModel" style="width:100%;height:250px"></textarea>