-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractivity.html
538 lines (467 loc) · 19.6 KB
/
interactivity.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Tutorial for interactivity with GoJS." />
<title>GoJS Interactivity</title>
<!-- Copyright 1998-2016 by Northwoods Software Corporation. -->
<link href="../assets/css/bootstrap.min.css" rel="stylesheet" >
<!-- custom CSS after bootstrap -->
<link href="../assets/css/main.css" rel="stylesheet" type="text/css"/>
<link href="../assets/css/highlight.css" rel="stylesheet" type="text/css" media="all" />
<script src="../assets/js/highlight.js"></script>
<script src="go.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-1506307-5', 'auto');
ga('send', 'pageview');
</script>
</head>
<body>
<!-- non-fixed navbar -->
<nav id="api-nav" class="navbar navbar-inverse navbar-top">
<div class="container">
<div class="navbar-header" data-toggle="collapse" data-target="#navbar">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!--<a class="navbar-brand" href="#">GoJS</a>-->
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li class="active"><a href="../index.html">Home</a></li>
<li><a href="../learn/index.html">Learn</a></li>
<li><a href="../samples/index.html">Samples</a></li>
<li><a href="../intro/index.html">Intro</a></li>
<li><a href="../api/index.html">API</a></li>
<li><a href="https://www.nwoods.com/components/evalform.htm">Register</a></li>
<li><a href="../doc/download.html">Download</a></li>
<li><a href="https://forum.nwoods.com/c/gojs">Forum</a></li>
<li><a href="https://www.nwoods.com/support/query.htm">Contact</a></li>
<li class="buy"><a href="https://www.nwoods.com/sales/ordering.htm">Buy</a></li>
<li class="activate"><a href="https://www.nwoods.com/app/activate.aspx?sku=gojs">Activate</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<div id="bannertop" class="jumbotron banner">
<div class="container">
<h1><span>GoJS Interactivity</span></h1>
</div>
</div>
<div class="container-fluid learn-container">
<h2>Built-in GoJS Interactivity</h2>
<p>
<b>GoJS</b> implements a number of common editing operations, such as manipulating parts (moving, adding, copying, cutting, and deleting). These editing abilities are accessible via mouse (or touch) and keyboard by default, and can also be invoked programatically in JavaScript.
</p>
<p>
The following Diagram defines a node template and has four nodes in its model:
</p>
<pre><code>
var $ = go.GraphObject.make;
myDiagram = $(go.Diagram, "myDiagramDiv",
{
initialContentAlignment: go.Spot.Center,
"undoManager.isEnabled": true
});
// define a simple Node template
myDiagram.nodeTemplate =
$(go.Node, "Auto",
$(go.Shape, "Rectangle",
new go.Binding("fill", "color")),
$(go.TextBlock,
{ margin: 6, font: "18px sans-serif" },
new go.Binding("text", "key"))
);
myDiagram.model = new go.GraphLinksModel(
[
{ key: "Alpha", color: "lightblue" },
{ key: "Beta", color: "orange" },
{ key: "Gamma", color: "lightgreen" },
{ key: "Delta", color: "pink" }
]);
</code></pre>
<!-- LIVE -->
<div id="myDiagramDiv" class="diagramStyling" style="width:700px; height:150px"></div>
<script>
function setupDiagram(divname) {
var $ = go.GraphObject.make;
var myDiagram = $(go.Diagram, divname,
{
initialContentAlignment: go.Spot.Center,
"undoManager.isEnabled": true // enable undo & redo
});
myDiagram.nodeTemplate =
$(go.Node, "Auto",
$(go.Shape, "Rectangle",
{ stroke: null, name: "SHAPE" },
new go.Binding("fill", "color")),
$(go.TextBlock,
{ margin: 6, font: "18px sans-serif" },
new go.Binding("text", "key"))
);
myDiagram.model = new go.GraphLinksModel(
[
{ key: "Alpha", color: "lightblue" },
{ key: "Beta", color: "orange" },
{ key: "Gamma", color: "lightgreen" },
{ key: "Delta", color: "pink" }
]);
return myDiagram;
}
setupDiagram("myDiagramDiv")
</script>
<p>
Out of the box, several interactions are available:
<ul>
<li>Click a node to select it. Click and drag on a node to move it around, or click and drag on the diagram background to pan the entire Diagram.
<li>Common keyboard shortcuts such as <code>CTRL-C</code>, <code>CTRL-V</code>, <code>CTRL-X</code> will copy, paste, and cut diagram parts, respectively.
<li>Drag-and-hold allows you to create a selection box for selecting multiple nodes. CTRL-clicking on nodes allows you to toggle their selection.
<li>Since the Diagram's undoManager was enabled, <code>CTRL-Z</code> and <code>CTRL-Y</code> will undo and redo operations. Diagram-panning and selection are not considered undoable operations.
</ul>
</p>
<hr />
<p>
By setting a few properties you can expose more interaction to a user:
</p>
<pre><code>
var $ = go.GraphObject.make;
var myDiagram = $(go.Diagram, divname,
{
initialContentAlignment: go.Spot.Center,
// allow double-click in background to create a new node
"clickCreatingTool.archetypeNodeData": { key: "Node", color: "white" },
// allow Ctrl-G to group and Ctrl-Shift-G to ungroup
"commandHandler.archetypeGroupData": { text: "Group", isGroup: true },
// have mouse wheel events zoom in and out instead of scroll up and down
"toolManager.mouseWheelBehavior": go.ToolManager.WheelZoom,
"undoManager.isEnabled": true // enable undo & redo
});
myDiagram.nodeTemplate =
$(go.Node, "Auto",
$(go.Shape, "Rectangle",
{ stroke: null, name: "SHAPE" },
new go.Binding("fill", "color")),
$(go.TextBlock,
{ margin: 6, font: "18px sans-serif", editable: true },
new go.Binding("text", "key"))
);
myDiagram.model = new go.GraphLinksModel(
[
{ key: "Alpha", color: "lightblue" },
{ key: "Beta", color: "orange" },
{ key: "Gamma", color: "lightgreen", group: "Group1" },
{ key: "Delta", color: "pink", group: "Group1" },
{ key: "Group1", isGroup: true }
]);
</code></pre>
<!-- LIVE -->
<div id="myDiagramDiv2" class="diagramStyling" style="width:700px; height:150px"></div>
<script>
function setupDiagram(divname) {
var $ = go.GraphObject.make;
var myDiagram = $(go.Diagram, divname,
{
initialContentAlignment: go.Spot.Center,
// allow double-click in background to create a new node
"clickCreatingTool.archetypeNodeData": { key: "Node", color: "white" },
// allow Ctrl-G to group and Ctrl-Shift-G to ungroup
"commandHandler.archetypeGroupData": { text: "Group", isGroup: true },
// have mouse wheel events zoom in and out instead of scroll up and down
"toolManager.mouseWheelBehavior": go.ToolManager.WheelZoom,
"undoManager.isEnabled": true // enable undo & redo
});
myDiagram.nodeTemplate =
$(go.Node, "Auto",
$(go.Shape, "Rectangle",
{ stroke: null, name: "SHAPE" },
new go.Binding("fill", "color")),
$(go.TextBlock,
{ margin: 6, font: "18px sans-serif", editable: true },
new go.Binding("text", "key"))
);
myDiagram.model = new go.GraphLinksModel(
[
{ key: "Alpha", color: "lightblue" },
{ key: "Beta", color: "orange" },
{ key: "Gamma", color: "lightgreen", group: "Group1" },
{ key: "Delta", color: "pink", group: "Group1" },
{ key: "Group1", isGroup: true }
]);
return myDiagram;
}
setupDiagram("myDiagramDiv2")
</script>
<p>
<ul>
<li><code>clickCreatingTool.archetypeNodeData</code> allows a double-click in the background to create a new node with the specified data.
<li><code>commandHandler.archetypeGroupData</code> allows <code>CTRL-G</code> to group a selection of nodes, and <code>CTRL-Shift-G</code> to ungroup.
<li><code>toolManager.mouseWheelBehavior</code> allows the mouse wheel to zoom instead of scroll by default. You can toggle this property by clicking on the mouse-wheel. On touch devices, pinch-zooming is enabled by default.
<li><code>editable</code> to true in the <code>TextBlock</code> definition allows the text to be edited in place. Select a node and then click on the text, or press F2, to begin text editing. Click anywhere else on the diagram or press <code>Tab</code> to finish editing text.
</ul>
</p>
<p>
These interactions are all present in the <a href="../samples/basic.html">basic.html sample</a>. Be sure to also see the <a href="../intro/commands.html">Intro page on GoJS commands</a>.
</p>
<p>
You can turn off portions of Diagram interactivity with several properties, depending on what you want to allow users to do. See the <a href="../intro/permissions.html">Intro page on GoJS permissions</a> for more.
</p>
<h2>Context Menus</h2>
<p>
<b>GoJS</b> provides a mechanism for you to define context menus for any object or for the Diagram itself. In the example below, two context menus are defined, one on the Node template (with one button) and one on the Diagram (with two buttons).
</p>
<pre><code>
var $ = go.GraphObject.make;
var myDiagram = $(go.Diagram, divname,
{
initialContentAlignment: go.Spot.Center,
"undoManager.isEnabled": true // enable undo & redo
});
// this method is called when the context menu button is clicked
function clickButton(e, obj) {
alert('node ' + obj.part.data.key + ' was clicked');
}
// defines a context menu to be referenced in the node template
var contextMenuTemplate =
$(go.Adornment, "Vertical",
$("ContextMenuButton",
$(go.TextBlock, "Click me!"),
{ click: clickButton })
// more ContextMenuButtons would go here
);
myDiagram.nodeTemplate =
$(go.Node, "Auto",
{ contextMenu: contextMenuTemplate }, // set the context menu
$(go.Shape, "Rectangle",
{ stroke: null, name: "SHAPE" },
new go.Binding("fill", "color")),
$(go.TextBlock,
{ margin: 6, font: "18px sans-serif" },
new go.Binding("text", "key"))
);
// this method alerts the current number of nodes in the Diagram
function countNodes(e, obj) {
alert('there are ' + e.diagram.nodes.count + ' nodes');
}
// this method creates a new node and inserts it at the last event's point
function addNode(e, obj) {
var data = { key: "Node", color: "white" };
e.diagram.model.addNodeData(data);
var node = e.diagram.findPartForData(data);
node.location = e.diagram.lastInput.documentPoint;
}
myDiagram.contextMenu =
$(go.Adornment, "Vertical",
$("ContextMenuButton",
$(go.TextBlock, "Count Nodes"),
{ click: countNodes }),
$("ContextMenuButton",
$(go.TextBlock, "Add Node"),
{ click: addNode })
// more ContextMenuButtons would go here
);
myDiagram.model = new go.GraphLinksModel(
[
{ key: "Alpha", color: "lightblue" },
{ key: "Beta", color: "orange" }
]);
return myDiagram;
</code></pre>
<!-- LIVE -->
<div id="myDiagramDiv3" class="diagramStyling" style="width:700px; height:150px"></div>
<script>
function setupDiagram(divname) {
var $ = go.GraphObject.make;
var myDiagram = $(go.Diagram, divname,
{
initialContentAlignment: go.Spot.Center,
"undoManager.isEnabled": true // enable undo & redo
});
// this method is called when the context menu button is clicked
function clickButton(e, obj) {
alert('node ' + obj.part.data.key + ' was clicked');
}
// defines a context menu to be referenced in the node template
var contextMenuTemplate =
$(go.Adornment, "Vertical",
$("ContextMenuButton",
$(go.TextBlock, "Click me!"),
{ click: clickButton })
// more ContextMenuButtons would go here
);
myDiagram.nodeTemplate =
$(go.Node, "Auto",
{ contextMenu: contextMenuTemplate }, // set the context menu
$(go.Shape, "Rectangle",
{ stroke: null, name: "SHAPE" },
new go.Binding("fill", "color")),
$(go.TextBlock,
{ margin: 6, font: "18px sans-serif" },
new go.Binding("text", "key"))
);
// this method alerts the current number of nodes in the Diagram
function countNodes(e, obj) {
alert('there are ' + e.diagram.nodes.count + ' nodes');
}
// this method creates a new node and inserts it at the last event's point
function addNode(e, obj) {
var data = { key: "Node", color: "white" };
e.diagram.model.addNodeData(data);
var node = e.diagram.findPartForData(data);
node.location = e.diagram.lastInput.documentPoint;
}
myDiagram.contextMenu =
$(go.Adornment, "Vertical",
$("ContextMenuButton",
$(go.TextBlock, "Count Nodes"),
{ click: countNodes }),
$("ContextMenuButton",
$(go.TextBlock, "Add Node"),
{ click: addNode })
// more ContextMenuButtons would go here
);
myDiagram.model = new go.GraphLinksModel(
[
{ key: "Alpha", color: "lightblue" },
{ key: "Beta", color: "orange" }
]);
return myDiagram;
}
setupDiagram("myDiagramDiv3")
</script>
<p>
If you right-click (or long-tap on a touch device) on a Node or the Diagram, you will see the <b>GoJS</b> context menu appear with the defined options.
</p>
<p>
The <a href="../samples/basic.html">basic.html sample</a> contains more complex context menu examples. See the <a href="../intro/contextmenus.html">Intro page on GoJS context menus</a> for more.
</p>
<h2>Link interactions</h2>
<p>
<b>GoJS</b> lets users draw new links and re-link existing ones by dragging out from a port or selecting a link and draggings its handles. To enable these tools, some properties need to be set:
</p>
<pre><code>
myDiagram.nodeTemplate =
$(go.Node, "Auto",
$(go.Shape, "Rectangle",
{
stroke: null, name: "SHAPE",
portId: "", cursor: "pointer", // make the Shape the port, not the whole Node
// allow all kinds of links from and to this port:
fromLinkable: true, fromLinkableSelfNode: true, fromLinkableDuplicates: true,
toLinkable: true, toLinkableSelfNode: true, toLinkableDuplicates: true
},
new go.Binding("fill", "color")),
$(go.TextBlock,
{ margin: 6, font: "18px sans-serif" },
new go.Binding("text", "key"))
);
myDiagram.linkTemplate =
$(go.Link,
{
toShortLength: 4,
// allow the user to relink existing links:
relinkableFrom: true, relinkableTo: true
},
$(go.Shape,
{ strokeWidth: 2 }),
$(go.Shape,
{ toArrow: "Standard", stroke: null })
);
myDiagram.model = new go.GraphLinksModel(
[
{ key: "Alpha", color: "lightblue" },
{ key: "Beta", color: "orange" },
{ key: "Gamma", color: "lightgreen" },
{ key: "Delta", color: "pink" }
],
[
{ from: 'Alpha', to: 'Beta' },
{ from: 'Alpha', to: 'Delta' },
]
);
</code></pre>
<!-- LIVE -->
<div id="myDiagramDiv4" class="diagramStyling" style="width:700px; height:150px"></div>
<script>
function setupDiagram(divname) {
var $ = go.GraphObject.make;
var myDiagram = $(go.Diagram, divname,
{
initialContentAlignment: go.Spot.Center,
"undoManager.isEnabled": true // enable undo & redo
});
myDiagram.nodeTemplate =
$(go.Node, "Auto",
$(go.Shape, "Rectangle",
{
stroke: null, name: "SHAPE",
// make the Shape the port, not the whole Node
portId: "", cursor: "pointer",
// allow all kinds of links from and to this port:
fromLinkable: true, fromLinkableSelfNode: true, fromLinkableDuplicates: true,
toLinkable: true, toLinkableSelfNode: true, toLinkableDuplicates: true
},
new go.Binding("fill", "color")),
$(go.TextBlock,
{ margin: 10, font: "18px sans-serif" },
new go.Binding("text", "key"))
);
myDiagram.linkTemplate =
$(go.Link,
{
toShortLength: 4,
// allow the user to relink existing links:
relinkableFrom: true, relinkableTo: true
},
$(go.Shape,
{ strokeWidth: 2 }),
$(go.Shape,
{ toArrow: "Standard", stroke: null })
);
myDiagram.model = new go.GraphLinksModel(
[
{ key: "Alpha", color: "lightblue" },
{ key: "Beta", color: "orange" },
{ key: "Gamma", color: "lightgreen" },
{ key: "Delta", color: "pink" }
],
[
{ from: 'Alpha', to: 'Beta' },
{ from: 'Alpha', to: 'Delta' },
]);
return myDiagram;
}
setupDiagram("myDiagramDiv4")
</script>
<p>
In the above example:
<ul>
<li>The <code>Shape</code> has a <code>pordId</code> set to make it the port instead of the entire node. Then several <code>...linkable</code> properties are set, allowing each node to link to itself and others.
<li>To create a new link to another node, drag from the port (the edge of the Shape that is not behind the TextBlock) to any node, including itself.
<li>In the link template, <code>relinkable...</code> properties are set, which create handles on the link when selected. You can drag these handles to change a link's from and to node.
</ul>
</p>
<p>
<b>GoJS</b> allows linking and re-linking to abide by custom criteria in which source and destination nodes are valid. You can read about this in the <a href="../intro/validation.html">Intro page on Validation</a>.
</p>
<p>
<b>GoJS</b> links have many interesting properties that are covered in depth in the <a href="../intro/links.html">Intro page on Links</a>.
</p>
<p class="footer">
GoJS ® by Northwoods Software. Copyright © 1998-2016 <a href="https://www.nwoods.com" target="_blank">Northwoods Software</a> ®
</p>
</div> <!-- end main -->
<div class="banner" id="bannerbottom">
<!-- text in banner-->
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script async src="../assets/js/bootstrap.min.js"></script>
</body>
</html>