UNPKG

13.2 kBHTMLView Raw
1<!DOCTYPE html>
2<html lang="en">
3<head>
4<meta charset="utf-8"/>
5<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover"/>
6<meta name="description" content="TypeScript: Test a diagram by simulating abstract input events."/>
7<link rel="stylesheet" href="../assets/css/style.css"/>
8<!-- Copyright 1998-2021 by Northwoods Software Corporation. -->
9<title>Simulating Input Events</title>
10</head>
11
12<body>
13 <!-- This top nav is not part of the sample code -->
14 <nav id="navTop" class="w-full z-30 top-0 text-white bg-nwoods-primary">
15 <div class="w-full container max-w-screen-lg mx-auto flex flex-wrap sm:flex-nowrap items-center justify-between mt-0 py-2">
16 <div class="md:pl-4">
17 <a class="text-white hover:text-white no-underline hover:no-underline
18 font-bold text-2xl lg:text-4xl rounded-lg hover:bg-nwoods-secondary " href="../">
19 <h1 class="mb-0 p-1 ">GoJS</h1>
20 </a>
21 </div>
22 <button id="topnavButton" class="rounded-lg sm:hidden focus:outline-none focus:ring" aria-label="Navigation">
23 <svg fill="currentColor" viewBox="0 0 20 20" class="w-6 h-6">
24 <path id="topnavOpen" fill-rule="evenodd" d="M3 5a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 10a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM9 15a1 1 0 011-1h6a1 1 0 110 2h-6a1 1 0 01-1-1z" clip-rule="evenodd"></path>
25 <path id="topnavClosed" class="hidden" fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path>
26 </svg>
27 </button>
28 <div id="topnavList" class="hidden sm:block items-center w-auto mt-0 text-white p-0 z-20">
29 <ul class="list-reset list-none font-semibold flex justify-end flex-wrap sm:flex-nowrap items-center px-0 pb-0">
30 <li class="p-1 sm:p-0"><a class="topnav-link" href="../learn/">Learn</a></li>
31 <li class="p-1 sm:p-0"><a class="topnav-link" href="../samples/">Samples</a></li>
32 <li class="p-1 sm:p-0"><a class="topnav-link" href="../intro/">Intro</a></li>
33 <li class="p-1 sm:p-0"><a class="topnav-link" href="../api/">API</a></li>
34 <li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/products/register.html">Register</a></li>
35 <li class="p-1 sm:p-0"><a class="topnav-link" href="../download.html">Download</a></li>
36 <li class="p-1 sm:p-0"><a class="topnav-link" href="https://forum.nwoods.com/c/gojs/11">Forum</a></li>
37 <li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/contact.html"
38 target="_blank" rel="noopener" onclick="getOutboundLink('https://www.nwoods.com/contact.html', 'contact');">Contact</a></li>
39 <li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/sales/index.html"
40 target="_blank" rel="noopener" onclick="getOutboundLink('https://www.nwoods.com/sales/index.html', 'buy');">Buy</a></li>
41 </ul>
42 </div>
43 </div>
44 <hr class="border-b border-gray-600 opacity-50 my-0 py-0" />
45 </nav>
46 <div class="md:flex flex-col md:flex-row md:min-h-screen w-full max-w-screen-xl mx-auto">
47 <div id="navSide" class="flex flex-col w-full md:w-48 text-gray-700 bg-white flex-shrink-0"></div>
48 <!-- * * * * * * * * * * * * * -->
49 <!-- Start of GoJS sample code -->
50
51
52 <div class="p-4 w-full">
53 <div id="sample">
54 <div style="width: 100%; display: flex; justify-content: space-between">
55 <div id="myPaletteDiv" style="width: 80px; height: 400px; margin-right: 2px; border: solid 1px black"></div>
56 <div id="myDiagramDiv" style="flex-grow: 1; height: 400px; border: solid 1px black"></div>
57 </div>
58 <p>
59 To simulate mouse events the buttons below use the <b>Robot</b> class that is defined in <a href="Robot.ts">Robot.ts</a>.
60 </p>
61 <p>
62 Click these buttons in order from top to bottom:<br />
63 <button id="dragFromPalette">Drag From Palette</button><br />
64 <button id="copyNode">Copy Node</button><br />
65 <button id="dragSelectNodes">Drag Select Nodes</button><br />
66 <button id="clickContextMenu">Context Menu Click Alpha</button><br />
67 <button id="deleteSelection">Delete</button><br />
68 </p>
69 <p>
70 Clicking operations:<br />
71 <button id="clickLambda">Click Lambda</button><br />
72 <button id="doubleClickLambda">Double Click Lambda</button><br />
73 </p>
74 <p>
75 The <a>UndoManager</a> has been enabled in the main Diagram.
76 Give focus to the Diagram and you can undo everything and then redo everything to confirm what was executed by the Robot.
77 </p>
78 <div id="myStatus" style="color:green"></div>
79 </div>
80
81 <script type="module" id="code">
82 import * as go from "../release/go-module.js";
83 import { Robot } from './Robot.js';
84
85 if (window.goSamples) goSamples(); // init for the samples -- you don't need to call this
86 const $ = go.GraphObject.make; // for conciseness in defining templates
87
88 function showProperties(e, obj) {
89 const node = obj.part.adornedPart;
90 if (node === null || node.data === null)
91 return;
92 let msg = 'Context clicked: ' + node.data.key + '. ';
93 msg += 'Selection includes:';
94 myDiagram.selection.each(function(part) {
95 msg += ' ' + part.toString();
96 });
97 const stat = document.getElementById('myStatus');
98 if (stat !== null)
99 stat.textContent = msg;
100 }
101 function nodeClicked(e, obj) {
102 const evt = e.copy();
103 const node = obj.part;
104 if (node === null || node.data === null)
105 return;
106 const type = evt.clickCount === 2 ? 'Double-Clicked: ' : 'Clicked: ';
107 const msg = type + node.data.key + '. ';
108 const stat = document.getElementById('myStatus');
109 if (stat !== null)
110 stat.textContent = msg;
111 }
112 const myDiagram =
113 $(go.Diagram, 'myDiagramDiv', // must name or refer to the DIV HTML element
114 {
115 nodeTemplate:
116 $(go.Node, 'Auto',
117 {
118 click: nodeClicked,
119 doubleClick: nodeClicked,
120 contextMenu:
121 $('ContextMenu',
122 $('ContextMenuButton',
123 $(go.TextBlock, 'Properties'),
124 { click: showProperties }))
125 },
126 $(go.Shape, 'Rectangle', { fill: 'lightgray' },
127 { portId: '', fromLinkable: true, toLinkable: true, cursor: 'pointer' }),
128 $(go.TextBlock, { margin: 3 },
129 new go.Binding('text', 'key'))
130 ),
131 model: new go.GraphLinksModel([
132 { key: 'Lambda' },
133 { key: 'Mu' }
134 ], [
135 { from: 'Lambda', to: 'Mu' }
136 ]),
137 'undoManager.isEnabled': true
138 });
139
140 // a shared Robot that can be used by all commands for this one Diagram
141 const myRobot = new Robot(myDiagram); // defined in Robot.js
142
143 // initialize the Palette that is on the left side of the page
144 const myPalette =
145 $(go.Palette, 'myPaletteDiv', // must name or refer to the DIV HTML element
146 {
147 nodeTemplate: myDiagram.nodeTemplate,
148 model: new go.GraphLinksModel([
149 { key: 'Alpha' },
150 { key: 'Beta' },
151 { key: 'Gamma' },
152 { key: 'Delta' }
153 ])
154 });
155
156 function dragFromPalette() {
157 // simulate a drag-and-drop between Diagrams:
158 const dragdrop = { sourceDiagram: myPalette, targetDiagram: myDiagram };
159 myRobot.mouseDown(5, 5, 0, dragdrop); // this should be where the Alpha node is in the source myPalette
160 myRobot.mouseMove(60, 60, 100, dragdrop);
161 myRobot.mouseUp(100, 100, 200, dragdrop); // this is where the node will be dropped in the target myDiagram
162 // If successful in dragging a node from the Palette into the Diagram,
163 // the DraggingTool will perform a transaction.
164 }
165
166 function copyNode() {
167 const alpha = myDiagram.findNodeForKey('Alpha');
168 if (alpha === null)
169 return;
170 const loc = alpha.actualBounds.center;
171 const options = { control: true, alt: true };
172 // Simulate a mouse drag to move the Alpha node:
173 myRobot.mouseDown(loc.x, loc.y, 0, options);
174 myRobot.mouseMove(loc.x + 80, loc.y + 50, 50, options);
175 myRobot.mouseMove(loc.x + 20, loc.y + 100, 100, options);
176 myRobot.mouseUp(loc.x + 20, loc.y + 100, 150, options);
177 // If successful, will have made a copy of the "Alpha" node below it.
178 // Alternatively you could copy the Node using commands:
179 // myDiagram.commandHandler.copySelection();
180 // myDiagram.commandHandler.pasteSelection(new go.Point(loc.x+20, loc.y+100));
181 }
182
183 function dragSelectNodes() {
184 const alpha = myDiagram.findNodeForKey('Alpha');
185 if (alpha === null)
186 return;
187 const alpha2 = myDiagram.findNodeForKey('Alpha2');
188 if (alpha2 === null)
189 return;
190 const coll = new go.Set();
191 coll.add(alpha);
192 coll.add(alpha2);
193 const area = myDiagram.computePartsBounds(coll);
194 area.inflate(30, 30);
195 // Simulate dragging in the background around the two Alpha nodes.
196 // This uses timestamps to pretend to wait a while to avoid activating the PanningTool.
197 // Hopefully this mouse down does not hit any Part, but in the Diagram's background:
198 myRobot.mouseDown(area.x, area.y, 0);
199 // NOTE that this mouseMove timestamp needs to be > myDiagram.toolManager.dragSelectingTool.delay:
200 myRobot.mouseMove(area.centerX, area.centerY, 200);
201 myRobot.mouseUp(area.right, area.bottom, 250);
202 // Now should have selected both "Alpha" and "Alpha2" using the DragSelectingTool.
203 // Alternatively you could select the Nodes programmatically:
204 // alpha.isSelected = true;
205 // alpha2.isSelected = true;
206 }
207
208 function clickContextMenu() {
209 const alpha = myDiagram.findNodeForKey('Alpha');
210 if (alpha === null)
211 return;
212 const loc = alpha.location;
213 // right click on Alpha
214 myRobot.mouseDown(loc.x + 10, loc.y + 10, 0, { right: true });
215 myRobot.mouseUp(loc.x + 10, loc.y + 10, 100, { right: true });
216 // Alternatively you could invoke the Show Context Menu command directly:
217 // myDiagram.commandHandler.showContextMenu(alpha);
218 // move mouse over first context menu button
219 myRobot.mouseMove(loc.x + 20, loc.y + 20, 200);
220 // and click that button
221 myRobot.mouseDown(loc.x + 20, loc.y + 20, 300);
222 myRobot.mouseUp(loc.x + 20, loc.y + 20, 350);
223 // This should have invoked the ContextMenuButton's click function, showProperties,
224 // which should have put a green message in the myStatus DIV.
225 }
226
227 function deleteSelection() {
228 // Simulate clicking the "Del" key:
229 myRobot.keyDown('Del');
230 myRobot.keyUp('Del');
231 // Now the selected Nodes are deleted.
232 // Alternatively you could invoke the Delete command directly:
233 // myDiagram.commandHandler.deleteSelection();
234 }
235
236 function clickLambda() {
237 const lambda = myDiagram.findNodeForKey('Lambda');
238 if (lambda === null)
239 return;
240 const loc = lambda.location;
241 // click on Lambda
242 myRobot.mouseDown(loc.x + 10, loc.y + 10, 0, {});
243 myRobot.mouseUp(loc.x + 10, loc.y + 10, 100, {});
244 // Clicking is just a sequence of input events.
245 // There is no command in CommandHandler for such a basic gesture.
246 }
247
248 function doubleClickLambda() {
249 const lambda = myDiagram.findNodeForKey('Lambda');
250 if (lambda === null)
251 return;
252 const loc = lambda.location;
253 // double-click on Lambda
254 myRobot.mouseDown(loc.x + 10, loc.y + 10, 0, {});
255 myRobot.mouseUp(loc.x + 10, loc.y + 10, 100, {});
256 myRobot.mouseDown(loc.x + 10, loc.y + 10, 200, { clickCount: 2 });
257 myRobot.mouseUp(loc.x + 10, loc.y + 10, 300, { clickCount: 2 });
258 }
259
260 document.getElementById("dragFromPalette").onclick = dragFromPalette;
261 document.getElementById("copyNode").onclick = copyNode;
262 document.getElementById("dragSelectNodes").onclick = dragSelectNodes;
263 document.getElementById("clickContextMenu").onclick = clickContextMenu;
264 document.getElementById("deleteSelection").onclick = deleteSelection;
265 document.getElementById("clickLambda").onclick = clickLambda;
266 document.getElementById("doubleClickLambda").onclick = doubleClickLambda;
267
268 window.myDiagram = myDiagram; // Attach to the window for console debugging
269 </script>
270 </div>
271 <!-- * * * * * * * * * * * * * -->
272 <!-- End of GoJS sample code -->
273 </div>
274</body>
275<!-- This script is part of the gojs.net website, and is not needed to run the sample -->
276<script src="../assets/js/goSamples.js"></script>
277</html>