UNPKG

11.6 kBJavaScriptView Raw
1/*
2* Copyright (C) 1998-2021 by Northwoods Software Corporation. All Rights Reserved.
3*/
4// These are the definitions for all of the predefined templates and tool archetypes.
5// You do not need to load this file in order to use the default templates and archetypes.
6// Although we have tried to provide definitions here that are faithful to how they
7// are actually implemented, there may be some differences from what is in the library.
8// Caution: these may change in a future version.
9import * as go from '../release/go-module.js';
10// Set up the default templates that each Diagram starts off with.
11function setupDiagramTemplates(diagram) {
12 // Node Templates
13 const nodeTemplateMap = new go.Map();
14 // create the default Node template
15 const archnode = new go.Node();
16 const nodet = new go.TextBlock();
17 nodet.bind(new go.Binding('text', '', go.Binding.toString));
18 archnode.add(nodet);
19 nodeTemplateMap.add('', archnode);
20 // create the default Comment Node template
21 const archcmnt = new go.Node();
22 const nodec = new go.TextBlock();
23 nodec.stroke = 'brown';
24 nodec.bind(new go.Binding('text', '', go.Binding.toString));
25 archcmnt.add(nodec);
26 nodeTemplateMap.add('Comment', archcmnt);
27 // create the default Link Label Node template
28 const archllab = new go.Node();
29 archllab.selectable = false;
30 archllab.avoidable = false;
31 const nodel = new go.Shape();
32 nodel.figure = 'Ellipse';
33 nodel.fill = 'black';
34 nodel.stroke = null;
35 nodel.desiredSize = new go.Size(3, 3);
36 archllab.add(nodel);
37 nodeTemplateMap.add('LinkLabel', archllab);
38 diagram.nodeTemplateMap = nodeTemplateMap;
39 // Group Templates
40 const groupTemplateMap = new go.Map();
41 // create the default Group template
42 const archgrp = new go.Group();
43 archgrp.selectionObjectName = 'GROUPPANEL';
44 archgrp.type = go.Panel.Vertical;
45 const grpt = new go.TextBlock();
46 grpt.font = 'bold 12pt sans-serif';
47 grpt.bind(new go.Binding('text', '', go.Binding.toString));
48 archgrp.add(grpt);
49 const grppan = new go.Panel(go.Panel.Auto);
50 grppan.name = 'GROUPPANEL';
51 const grpbord = new go.Shape();
52 grpbord.figure = 'Rectangle';
53 grpbord.fill = 'rgba(128,128,128,0.2)';
54 grpbord.stroke = 'black';
55 grppan.add(grpbord);
56 const phold = new go.Placeholder();
57 phold.padding = new go.Margin(5, 5, 5, 5);
58 grppan.add(phold);
59 archgrp.add(grppan);
60 groupTemplateMap.add('', archgrp);
61 diagram.groupTemplateMap = groupTemplateMap;
62 // Link Templates
63 const linkTemplateMap = new go.Map();
64 // create the default Link template
65 const archlink = new go.Link();
66 const archpath = new go.Shape();
67 archpath.isPanelMain = true;
68 archlink.add(archpath);
69 const archarrow = new go.Shape();
70 archarrow.toArrow = 'Standard';
71 archarrow.fill = 'black';
72 archarrow.stroke = null;
73 archarrow.strokeWidth = 0;
74 archlink.add(archarrow);
75 linkTemplateMap.add('', archlink);
76 // create the default Comment Link template
77 const archcmntlink = new go.Link();
78 const archcmntpath = new go.Shape();
79 archcmntpath.isPanelMain = true;
80 archcmntpath.stroke = 'brown';
81 archcmntlink.add(archcmntpath);
82 linkTemplateMap.add('Comment', archcmntlink);
83 diagram.linkTemplateMap = linkTemplateMap;
84}
85// Set up the default Panel.itemTemplate.
86function setupDefaultItemTemplate(panel) {
87 const architem = new go.Panel();
88 const itemtxt = new go.TextBlock();
89 itemtxt.bind(new go.Binding('text', '', go.Binding.toString));
90 architem.add(itemtxt);
91 panel.itemTemplate = architem;
92}
93// Set up the diagram's selection Adornments
94function setupSelectionAdornments(diagram) {
95 // create the default Adornment for selection
96 let selad = new go.Adornment();
97 selad.type = go.Panel.Auto;
98 let seladhandle = new go.Shape();
99 seladhandle.fill = null;
100 seladhandle.stroke = 'dodgerblue';
101 seladhandle.strokeWidth = 3;
102 selad.add(seladhandle);
103 const selplace = new go.Placeholder();
104 selplace.margin = new go.Margin(1.5, 1.5, 1.5, 1.5);
105 selad.add(selplace);
106 diagram.nodeSelectionAdornmentTemplate = selad;
107 // reuse the default Node Adornment for selection
108 diagram.groupSelectionAdornmentTemplate = selad;
109 // create the default Link Adornment for selection
110 selad = new go.Adornment();
111 selad.type = go.Panel.Link;
112 seladhandle = new go.Shape();
113 seladhandle.isPanelMain = true;
114 seladhandle.fill = null;
115 seladhandle.stroke = 'dodgerblue';
116 seladhandle.strokeWidth = 3; // ?? zero to use selection object's strokeWidth is often not wide enough
117 selad.add(seladhandle);
118 diagram.linkSelectionAdornmentTemplate = selad;
119}
120// Set up the background Grid Panel.
121function setupDefaultBackgroundGrid(diagram) {
122 const grid = new go.Panel(go.Panel.Grid);
123 grid.name = 'GRID';
124 let hlines = new go.Shape();
125 hlines.figure = 'LineH';
126 hlines.stroke = 'lightgray';
127 hlines.strokeWidth = 0.5;
128 hlines.interval = 1;
129 grid.add(hlines);
130 hlines = new go.Shape();
131 hlines.figure = 'LineH';
132 hlines.stroke = 'gray';
133 hlines.strokeWidth = 0.5;
134 hlines.interval = 5;
135 grid.add(hlines);
136 hlines = new go.Shape();
137 hlines.figure = 'LineH';
138 hlines.stroke = 'gray';
139 hlines.strokeWidth = 1;
140 hlines.interval = 10;
141 grid.add(hlines);
142 let vlines = new go.Shape();
143 vlines.figure = 'LineV';
144 vlines.stroke = 'lightgray';
145 vlines.strokeWidth = 0.5;
146 vlines.interval = 1;
147 grid.add(vlines);
148 vlines = new go.Shape();
149 vlines.figure = 'LineV';
150 vlines.stroke = 'gray';
151 vlines.strokeWidth = 0.5;
152 vlines.interval = 5;
153 grid.add(vlines);
154 vlines = new go.Shape();
155 vlines.figure = 'LineV';
156 vlines.stroke = 'gray';
157 vlines.strokeWidth = 1;
158 vlines.interval = 10;
159 grid.add(vlines);
160 grid.visible = false; // by default the grid is not visible
161 // Create the Part that holds the grid.
162 // const gridpart = new go.Part();
163 // gridpart.add(grid);
164 // gridpart.layerName = 'Grid'; // goes in the "Grid" layer
165 // gridpart.zOrder = 0; // to make it easier for other background parts to be behind the grid
166 // gridpart.isInDocumentBounds = false; // never part of the document bounds
167 // gridpart.isAnimated = false; // not animated
168 // gridpart.pickable = false; // user cannot pick it with mouse/touch/stylus
169 // gridpart.locationObjectName = 'GRID';
170 // diagram.add(gridpart);
171 // So then: diagram.grid === grid
172 // BUT, the gridpart is not actually in the Diagram.parts collection,
173 // and that Part cannot be replaced; so the above code is commented out.
174 // Instead, this works in an existing GoJS environment:
175 diagram.grid = grid;
176}
177// Set up the "viewport" box part that is the initial value of Overview.box.
178function setupOverviewBox(overview) {
179 const box = new go.Part();
180 const s = new go.Shape();
181 s.stroke = 'magenta';
182 s.strokeWidth = 2;
183 s.fill = 'transparent';
184 s.name = 'BOXSHAPE';
185 box.selectable = true;
186 box.selectionObjectName = 'BOXSHAPE';
187 box.locationObjectName = 'BOXSHAPE';
188 // box.resizable = true;
189 box.resizeObjectName = 'BOXSHAPE';
190 box.cursor = 'move';
191 box.add(s);
192 // only resize the bottom-right corner
193 const ad = new go.Adornment();
194 ad.type = go.Panel.Spot;
195 ad.locationSpot = go.Spot.Center;
196 const ph = new go.Placeholder();
197 ph.isPanelMain = true;
198 ad.add(ph);
199 const hnd = new go.Shape();
200 hnd.alignmentFocus = go.Spot.Center;
201 hnd.figure = 'Rectangle';
202 hnd.desiredSize = new go.Size(64, 64);
203 hnd.cursor = 'se-resize';
204 hnd.alignment = go.Spot.BottomRight;
205 ad.add(hnd);
206 box.resizeAdornmentTemplate = ad;
207 overview.box = box;
208}
209// Set up LinkingBaseTool's default temporary nodes and link.
210function setupLinkingToolTemporaryNodesAndLink(tool) {
211 // LinkingTool.temporaryLink
212 const link = new go.Link();
213 const path = new go.Shape();
214 path.isPanelMain = true;
215 path.stroke = 'blue';
216 link.add(path);
217 const arrow = new go.Shape();
218 arrow.toArrow = 'Standard';
219 arrow.fill = 'blue';
220 arrow.stroke = 'blue';
221 link.add(arrow);
222 link.layerName = 'Tool';
223 tool.temporaryLink = link;
224 // LinkingTool.temporaryFromNode and .temporaryFromPort
225 const fromNode = new go.Node();
226 const fromPort = new go.Shape();
227 fromPort.portId = '';
228 fromPort.figure = 'Rectangle';
229 fromPort.fill = null;
230 fromPort.stroke = 'magenta';
231 fromPort.strokeWidth = 2;
232 fromPort.desiredSize = new go.Size(1, 1);
233 fromNode.add(fromPort);
234 fromNode.selectable = false;
235 fromNode.layerName = 'Tool';
236 tool.temporaryFromNode = fromNode;
237 tool.temporaryFromPort = fromPort;
238 // LinkingTool.temporaryToNode and .temporaryToPort
239 const toNode = new go.Node();
240 const toPort = new go.Shape();
241 toPort.portId = '';
242 toPort.figure = 'Rectangle';
243 toPort.fill = null;
244 toPort.stroke = 'magenta';
245 toPort.strokeWidth = 2;
246 toPort.desiredSize = new go.Size(1, 1);
247 toNode.add(toPort);
248 toNode.selectable = false;
249 toNode.layerName = 'Tool';
250 tool.temporaryToNode = toNode;
251 tool.temporaryToPort = toPort;
252}
253// Set up RelinkingTool's default handle archetypes
254function setupRelinkingToolHandles(tool) {
255 let h = new go.Shape();
256 h.figure = 'Diamond';
257 h.desiredSize = new go.Size(8, 8);
258 h.fill = 'lightblue';
259 h.stroke = 'dodgerblue';
260 h.cursor = 'pointer';
261 h.segmentIndex = 0;
262 tool.fromHandleArchetype = h;
263 h = new go.Shape();
264 h.figure = 'Diamond';
265 h.desiredSize = new go.Size(8, 8);
266 h.fill = 'lightblue';
267 h.stroke = 'dodgerblue';
268 h.cursor = 'pointer';
269 h.segmentIndex = -1;
270 tool.toHandleArchetype = h;
271}
272// Set up LinkReshapingTool's default handle archetypes
273function setupLinkReshapingToolHandles(tool) {
274 let h = new go.Shape();
275 h.figure = 'Rectangle';
276 h.desiredSize = new go.Size(6, 6);
277 h.fill = 'lightblue';
278 h.stroke = 'dodgerblue';
279 tool.handleArchetype = h;
280 h = new go.Shape();
281 h.figure = 'Diamond';
282 h.desiredSize = new go.Size(8, 8);
283 h.fill = 'lightblue';
284 h.stroke = 'dodgerblue';
285 h.cursor = 'move';
286 tool.midHandleArchetype = h;
287}
288// Set up ResizingTool's default handle archetype
289function setupResizingToolHandles(tool) {
290 const h = new go.Shape();
291 h.alignmentFocus = go.Spot.Center;
292 h.figure = 'Rectangle';
293 h.desiredSize = new go.Size(6, 6);
294 h.fill = 'lightblue';
295 h.stroke = 'dodgerblue';
296 h.strokeWidth = 1;
297 h.cursor = 'pointer';
298 tool.handleArchetype = h;
299}
300// Set up RotatingTool's default handle archetype
301function setupRotatingToolHandles(tool) {
302 const h = new go.Shape();
303 h.figure = 'Ellipse';
304 h.desiredSize = new go.Size(8, 8);
305 h.fill = 'lightblue';
306 h.stroke = 'dodgerblue';
307 h.strokeWidth = 1;
308 h.cursor = 'pointer';
309 tool.handleArchetype = h;
310}
311// Set up DragSelectingTool's default box
312function setupDragSelectingToolBox(tool) {
313 const b = new go.Part();
314 b.layerName = 'Tool';
315 b.selectable = false;
316 const r = new go.Shape();
317 r.name = 'SHAPE';
318 r.figure = 'Rectangle';
319 r.fill = null;
320 r.stroke = 'magenta';
321 b.add(r);
322 tool.box = b;
323}