-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselection.html
288 lines (272 loc) · 10.4 KB
/
selection.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>GoJS Selection -- Northwoods Software</title>
<!-- Copyright 1998-2016 by Northwoods Software Corporation. -->
<script src="go.js"></script>
<script src="goIntro.js"></script>
</head>
<body onload="goIntro()">
<div id="container" class="container-fluid">
<div id="content">
<h2>Selection</h2>
<p>
Users normally select <a>Part</a>s manually by clicking on them and they deselect them by clicking in the background or pressing the Esc key.
You can select parts programmatically by setting <a>Part.isSelected</a>.
</p>
<p>
The <a>Diagram</a> keeps a collection of selected parts, <a>Diagram.selection</a>.
That collection is read-only -- the only way to select or deselect a Part is by setting its <a>Part.isSelected</a> property.
You can limit how many parts are selected by setting <a>Diagram.maxSelectionCount</a>.
Prevent all selection by the user by setting <a>Diagram.allowSelect</a> to false.
Or prevent a particular Part from being selected by setting <a>Part.selectable</a> to false.
</p>
<p>
You can show that a part is selected by either or both of two general techniques: adding <a>Adornment</a>s or
changing the appearance of some of the elements in the visual tree of the selected Part.
</p>
<h3>Selection Adornments</h3>
<p>
It is common to display that a Part is selected by having it show a selection <a>Adornment</a> when the Part is selected.
This is normally a blue rectangle surrounding the Part.
This is the default behavior; if you do not want such an adornment, you can set <a>Part.selectionAdorned</a> to false.
</p>
<p>
If you do want a selection adornment but want something different than the standard one, you can customize it.
Such customization can be done by setting the <a>Part.selectionAdornmentTemplate</a>.
In this example, nodes get thick blue rounded rectangles surrounding the selected node,
and links get thick blue lines following the selected link's path.
</p>
<p>
Note that an <a>Adornment</a> is just a <a>Part</a>.
Adornments for nodes must contain a <a>Placeholder</a> in their visual tree.
The Placeholder gets positioned where the selected object is.
</p>
<p>
Adornments for links are assumed to be panels of <a>Panel.type</a> that is <a>Panel.Link</a>.
Hence the main element may be a <a>Shape</a> that gets the geometry of the selected Link's path shape,
and the other elements of the adornment may be positioned on or near the segments of the link route just as for a regular <a>Link</a>.
</p>
<pre data-language="javascript" id="adornment">
diagram.nodeTemplate =
$(go.Node, "Auto",
new go.Binding("location", "loc", go.Point.parse),
$(go.Shape, "RoundedRectangle", { fill: "lightgray" }),
$(go.TextBlock,
{ margin: 5 },
new go.Binding("text", "key")),
{
selectionAdornmentTemplate:
$(go.Adornment, "Auto",
$(go.Shape, "RoundedRectangle",
{ fill: null, stroke: "dodgerblue", strokeWidth: 8 }),
$(go.Placeholder)
) // end Adornment
}
);
diagram.linkTemplate =
$(go.Link,
$(go.Shape, { strokeWidth: 2 }),
$(go.Shape, { toArrow: "Standard" }),
{
selectionAdornmentTemplate:
$(go.Adornment,
$(go.Shape,
{ isPanelMain: true, stroke: "dodgerblue", strokeWidth: 8 }),
$(go.Shape,
{ toArrow: "Standard", fill: "dodgerblue", stroke: null, scale: 2.5 })
) // end Adornment
}
);
var nodeDataArray = [
{ key: "Alpha", loc: "0 0" },
{ key: "Beta", loc: "200 50" }
];
var linkDataArray = [
{ from: "Alpha", to: "Beta" }
];
diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
</pre>
<script>goCode("adornment", 600, 100)</script>
<h3>More complex Adornments</h3>
<p>
The custom <a>Adornment</a> for a <a>Node</a> need not be only a simple <a>Shape</a> outlining the selected node.
Here is an adornment that adds a button to the adornment which inserts a node and a link to that new node.
</p>
<pre data-language="javascript" id="complex">
function addNodeAndLink(e, b) {
// take a button panel in an Adornment, get its Adornment, and then get its adorned Node
var node = b.part.adornedPart;
// we are modifying the model, so conduct a transaction
var diagram = node.diagram;
diagram.startTransaction("add node and link");
// have the Model add the node data
var newnode = { key: "N" };
diagram.model.addNodeData(newnode);
// locate the node initially where the parent node is
diagram.findNodeForData(newnode).location = node.location;
// and then add a link data connecting the original node with the new one
var newlink = { from: node.data.key, to: newnode.key };
diagram.model.addLinkData(newlink);
// finish the transaction -- will automatically perform a layout
diagram.commitTransaction("add node and link");
}
diagram.nodeTemplate =
$(go.Node, "Auto",
$(go.Shape, "RoundedRectangle", { fill: "lightgray" }),
$(go.TextBlock,
{ margin: 5 },
new go.Binding("text", "key")),
{
selectionAdornmentTemplate:
$(go.Adornment, "Spot",
$(go.Panel, "Auto",
// this Adornment has a rectangular blue Shape around the selected node
$(go.Shape, { fill: null, stroke: "dodgerblue", strokeWidth: 3 }),
$(go.Placeholder)
),
// and this Adornment has a Button to the right of the selected node
$("Button",
{ alignment: go.Spot.Right, alignmentFocus: go.Spot.Left,
click: addNodeAndLink }, // define click behavior for Button in Adornment
$(go.TextBlock, "ADD", // the Button content
{ font: "bold 6pt sans-serif" })
)
) // end Adornment
}
);
diagram.layout = $(go.TreeLayout);
var nodeDataArray = [
{ key: "Alpha" },
{ key: "Beta" }
];
var linkDataArray = [
{ from: "Alpha", to: "Beta" }
];
diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
</pre>
<script>goCode("complex", 600, 200)</script>
<p>
Select any node and click the "ADD" button.
Note how the diagram is automatically laid out as a tree.
</p>
<h3>Data Binding</h3>
<p>
Like all <a>Part</a>s, <a>Adornment</a>s support data binding.
If the adorned Part has a data binding (i.e. if <a>Part.data</a> is non-null),
all adornments for that part will also be bound to the same data object.
</p>
<pre data-language="javascript" id="binding">
diagram.nodeTemplate =
$(go.Node, "Auto",
new go.Binding("location", "loc", go.Point.parse),
$(go.Shape, "RoundedRectangle", { fill: "lightgray", strokeWidth: 2 },
new go.Binding("stroke", "color")),
$(go.TextBlock,
{ margin: 5 },
new go.Binding("text", "key")),
{
selectionAdornmentTemplate:
$(go.Adornment, "Auto",
$(go.Shape,
{ fill: null, stroke: "dodgerblue", strokeWidth: 6 },
new go.Binding("stroke", "color")),
$(go.Placeholder)
) // end Adornment
}
);
var nodeDataArray = [
{ key: "Alpha", loc: "0 0", color: "blue" },
{ key: "Beta", loc: "200 50", color: "red" }
];
var linkDataArray = [
{ from: "Alpha", to: "Beta" }
];
diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
</pre>
<script>goCode("binding", 600, 100)</script>
<p>
Notice how each Adornment has the same color as the selected node's data.color.
</p>
<h3>Selection appearance changes</h3>
<p>
Adding a selection adornment is not the only way to indicate visually that a <a>Part</a> is selected.
You can also modify the appearance of one or more objects in your Part.
</p>
<p>
One way to do this is with data binding.
Here we data bind the <a>Shape.fill</a> to the <a>Part.isSelected</a> property
with a converter function that converts the boolean value to a color string or brush.
We also turn off the regular rectangular blue selection adornment.
</p>
<pre data-language="javascript" id="isSelected">
diagram.nodeTemplate =
$(go.Node, "Auto",
{ selectionAdorned: false }, // don't bother with any selection adornment
new go.Binding("location", "loc", go.Point.parse),
$(go.Shape, "RoundedRectangle", { fill: "lightgray", strokeWidth: 2 },
// when this Part.isSelected changes value, change this Shape.fill value:
new go.Binding("fill", "isSelected", function(sel) {
if (sel) return "cyan"; else return "lightgray";
}).ofObject("")), // The object named "" is the root visual element, the Node itself
$(go.TextBlock,
{ margin: 5 },
new go.Binding("text", "key"))
);
var nodeDataArray = [
{ key: "Alpha", loc: "0 0" },
{ key: "Beta", loc: "200 50" }
];
var linkDataArray = [
{ from: "Alpha", to: "Beta" }
];
diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
</pre>
<script>goCode("isSelected", 600, 100)</script>
<p>
Now when you select a node its background color changes to cyan.
</p>
<p>
More generally you can execute code to modify the Part when <a>Part.isSelected</a> has changed value.
In this example we will have the same side effects as the previous example.
</p>
<pre data-language="javascript" id="selectionChanged">
function onSelectionChanged(node) {
var icon = node.findObject("Icon");
if (icon !== null) {
if (node.isSelected)
icon.fill = "cyan";
else
icon.fill = "lightgray";
}
}
diagram.nodeTemplate =
$(go.Node, "Auto",
{ selectionAdorned: false, // don't bother with any selection adornment
selectionChanged: onSelectionChanged }, // executed when Part.isSelected has changed
new go.Binding("location", "loc", go.Point.parse),
$(go.Shape, "RoundedRectangle",
{ name: "Icon", fill: "lightgray", strokeWidth: 2 }),
$(go.TextBlock,
{ margin: 5 },
new go.Binding("text", "key"))
);
var nodeDataArray = [
{ key: "Alpha", loc: "0 0" },
{ key: "Beta", loc: "200 50" }
];
var linkDataArray = [
{ from: "Alpha", to: "Beta" }
];
diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
</pre>
<script>goCode("selectionChanged", 600, 100)</script>
<p>
There are some restrictions on what you can do in such an event handler:
you should not select or deselect any parts, and you should not add or remove any parts from the diagram.
</p>
</div>
</div>
</body>
</html>