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 |
|
7 | <link rel="stylesheet" href="../assets/css/style.css"/>
|
8 | <title> GoJS Template Maps -- Northwoods Software </title>
|
9 | <link rel="stylesheet" href="../assets/css/prism.css" />
|
10 | </head>
|
11 | <script>
|
12 |
|
13 | window.diagrams = [];
|
14 | window.goCode = function(pre, w, h, parentid, animation) {
|
15 | window.diagrams.push([pre, w, h, parentid, animation]);
|
16 | }
|
17 | </script>
|
18 | <body>
|
19 | <nav id="navTop" class="w-full z-30 top-0 text-white bg-nwoods-primary">
|
20 | <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">
|
21 | <div class="md:pl-4">
|
22 | <a class="text-white hover:text-white no-underline hover:no-underline
|
23 | font-bold text-2xl lg:text-4xl rounded-lg hover:bg-nwoods-secondary " href="../">
|
24 | <h1 class="mb-0 p-1 ">GoJS</h1>
|
25 | </a>
|
26 | </div>
|
27 | <button id="topnavButton" class="rounded-lg sm:hidden focus:outline-none focus:ring" aria-label="Navigation">
|
28 | <svg fill="currentColor" viewBox="0 0 20 20" class="w-6 h-6">
|
29 | <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>
|
30 | <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>
|
31 | </svg>
|
32 | </button>
|
33 | <div id="topnavList" class="hidden sm:block items-center w-auto mt-0 text-white p-0 z-20">
|
34 | <ul class="list-reset list-none font-semibold flex justify-end flex-wrap sm:flex-nowrap items-center px-0 pb-0">
|
35 | <li class="p-1 sm:p-0"><a class="topnav-link" href="../learn/">Learn</a></li>
|
36 | <li class="p-1 sm:p-0"><a class="topnav-link" href="../samples/">Samples</a></li>
|
37 | <li class="p-1 sm:p-0"><a class="topnav-link" href="../intro/">Intro</a></li>
|
38 | <li class="p-1 sm:p-0"><a class="topnav-link" href="../api/">API</a></li>
|
39 | <li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/products/register.html">Register</a></li>
|
40 | <li class="p-1 sm:p-0"><a class="topnav-link" href="../download.html">Download</a></li>
|
41 | <li class="p-1 sm:p-0"><a class="topnav-link" href="https://forum.nwoods.com/c/gojs/11">Forum</a></li>
|
42 | <li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/contact.html"
|
43 | target="_blank" rel="noopener" onclick="getOutboundLink('https://www.nwoods.com/contact.html', 'contact');">Contact</a></li>
|
44 | <li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/sales/index.html"
|
45 | target="_blank" rel="noopener" onclick="getOutboundLink('https://www.nwoods.com/sales/index.html', 'buy');">Buy</a></li>
|
46 | </ul>
|
47 | </div>
|
48 | </div>
|
49 | <hr class="border-b border-gray-600 opacity-50 my-0 py-0" />
|
50 | </nav>
|
51 |
|
52 | <div class="md:flex flex-col md:flex-row md:min-h-screen w-full max-w-screen-xl mx-auto">
|
53 |
|
54 | <div id="navSide" class="flex flex-col w-full md:w-40 lg:w-48 text-gray-700 bg-white flex-shrink-0">
|
55 | <div class="flex-shrink-0 px-8 py-4">
|
56 | <button id="navButton" class="rounded-lg md:hidden focus:outline-none focus:ring" aria-label="Navigation">
|
57 | <svg fill="currentColor" viewBox="0 0 20 20" class="w-6 h-6">
|
58 | <path id="navOpen" 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>
|
59 | <path id="navClosed" 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>
|
60 | </svg>
|
61 | </button>
|
62 | </div>
|
63 | <nav id="navList" class="min-h-screen hidden md:block sidebar-nav flex-grow px-1 lg:px-4 pb-4 md:pb-0 md:overflow-y-auto break-words">
|
64 | <a href="index.html">Basics</a>
|
65 | <a href="buildingObjects.html">Building Parts</a>
|
66 | <a href="usingModels.html">Using Models</a>
|
67 | <a href="dataBinding.html">Data Binding</a>
|
68 | <a href="react.html">GoJS with React</a>
|
69 | <a href="angular.html">GoJS with Angular</a>
|
70 | <a href="textBlocks.html">TextBlocks</a>
|
71 | <a href="shapes.html">Shapes</a>
|
72 | <a href="pictures.html">Pictures</a>
|
73 | <a href="panels.html">Panels</a>
|
74 | <a href="tablePanels.html">Table Panels</a>
|
75 | <a href="brush.html">Brushes</a>
|
76 | <a href="sizing.html">Sizing Objects</a>
|
77 | <a href="itemArrays.html">Item Arrays</a>
|
78 | <a href="changedEvents.html">Changed Events</a>
|
79 | <a href="transactions.html">Transactions</a>
|
80 | <a href="viewport.html">Coordinates</a>
|
81 | <a href="initialView.html">Initial View</a>
|
82 | <a href="collections.html">Collections</a>
|
83 | <a href="links.html">Links</a>
|
84 | <a href="linkLabels.html">Link Labels</a>
|
85 | <a href="connectionPoints.html">Link Points</a>
|
86 | <a href="ports.html">Ports</a>
|
87 | <a href="nodes.html">Nodes</a>
|
88 | <a href="debugging.html">Debugging</a>
|
89 | <a href="layouts.html">Layouts</a>
|
90 | <a href="trees.html">Trees</a>
|
91 | <a href="subtrees.html">SubTrees</a>
|
92 | <a href="groups.html">Groups</a>
|
93 | <a href="subgraphs.html">SubGraphs</a>
|
94 | <a href="sizedGroups.html">Sized Groups</a>
|
95 | <a href="selection.html">Selection</a>
|
96 | <a href="highlighting.html">Highlighting</a>
|
97 | <a href="animation.html">Animation</a>
|
98 | <a href="toolTips.html">ToolTips</a>
|
99 | <a href="contextmenus.html">Context Menus</a>
|
100 | <a href="events.html">Diagram Events</a>
|
101 | <a href="tools.html">Tools</a>
|
102 | <a href="commands.html">Commands</a>
|
103 | <a href="permissions.html">Permissions</a>
|
104 | <a href="validation.html">Validation</a>
|
105 | <a href="HTMLInteraction.html">HTML Interaction</a>
|
106 | <a href="layers.html">Layers & Z-ordering</a>
|
107 | <a href="palette.html">Palette</a>
|
108 | <a href="overview.html">Overview</a>
|
109 | <a href="resizing.html">Resizing Diagrams</a>
|
110 | <a href="replacingDeleting.html">Replacing and Deleting</a>
|
111 | <a href="buttons.html">Buttons</a>
|
112 | <a href="templateMaps.html">Template Maps</a>
|
113 | <a href="legends.html">Legends and Titles</a>
|
114 | <a href="extensions.html">Extensions</a>
|
115 | <a href="geometry.html">Geometry Strings</a>
|
116 | <a href="grids.html">Grid Patterns</a>
|
117 | <a href="graduatedPanels.html">Graduated Panels</a>
|
118 | <a href="makingImages.html">Diagram Images</a>
|
119 | <a href="makingSVG.html">Diagram SVG</a>
|
120 | <a href="printing.html">Printing</a>
|
121 | <a href="serverSideImages.html">Server-side Images</a>
|
122 | <a href="nodeScript.html">GoJS in Node.js</a>
|
123 | <a href="testing.html">Testing</a>
|
124 | <a href="storage.html">Storage</a>
|
125 | <a href="performance.html">Performance</a>
|
126 | <a href="source.html">Building from Source</a>
|
127 | <a href="platforms.html">Platforms</a>
|
128 | <a href="deployment.html">Deployment</a>
|
129 | </nav>
|
130 | </div>
|
131 | <div class="pt-4 px-2 md:px-0 lg:px-4 pb-16 w-full overflow-hidden">
|
132 |
|
133 | <h1>Template Maps</h1>
|
134 | <p>
|
135 | Many of the previous examples have provided custom templates for nodes, groups, or links.
|
136 | Those examples have shown how to make simple adaptations of the templates for particular data instances via data binding.
|
137 | But what if you want to have nodes with drastically different appearances or behaviors in a
|
138 | single diagram at the same time?
|
139 | </p>
|
140 | <p>
|
141 | It is possible to define a node template that includes all possible configurations for all of the kinds
|
142 | of nodes that you want to display. There would need to be a lot of data binding and/or code to make
|
143 | the needed changes. Often you will want to make not-<a>GraphObject.visible</a> large parts of the template in order to make
|
144 | visible the one panel that you want to show. But this technique is difficult to use -- templates get
|
145 | way too complicated too quickly.
|
146 | </p>
|
147 | <p>
|
148 | Instead <b>GoJS</b> supports as many templates as you want -- you choose dynamically which one you want to
|
149 | use to represent a particular node data. This does mean potentially a lot of templates, but each one
|
150 | will be much simpler, easier to write, and easier to maintain.
|
151 | </p>
|
152 | <p>
|
153 | Each <a>Diagram</a> actually holds a <a>Map</a> of templates for each
|
154 | type of <a>Part</a>: <a>Node</a>, <a>Group</a>, and <a>Link</a>.
|
155 | Each Map associates a "category" name with a template.
|
156 | For example, when the diagram wants to create a <a>Node</a> for a particular node data object,
|
157 | the diagram uses that node data's category to look up the node template in the <a>Diagram.nodeTemplateMap</a>.
|
158 | Similar lookups are done using the <a>Diagram.groupTemplateMap</a> and the <a>Diagram.linkTemplateMap</a>.
|
159 | </p>
|
160 | <p>
|
161 | Each <a>Diagram</a> initially has its own template maps stocked with predefined categories.
|
162 | The default category for any data object is the empty string, "".
|
163 | The <a>Diagram.nodeTemplateMap</a> initially contains for the empty string a very simple <a>Node</a> template
|
164 | holding a <a>TextBlock</a> whose <a>TextBlock.text</a> property is data bound to the data converted to a string.
|
165 | You can see the default templates for nodes, groups, and links in a number of the previous examples,
|
166 | such as the <a href="groups.html#GroupsLinks">Groups and Links</a> example.
|
167 | </p>
|
168 | <p>
|
169 | The value of <a>Diagram.nodeTemplate</a> is just the value of <code>thatDiagram.nodeTemplateMap.get("")</code>.
|
170 | Setting <a>Diagram.nodeTemplate</a> just replaces the template in <a>Diagram.nodeTemplateMap</a>
|
171 | named with the empty string.
|
172 | </p>
|
173 | <p>
|
174 | The implementations of all predefined templates are provided in <a href="../extensions/Templates.js">Templates.js</a> in the Extensions directory.
|
175 | You may wish to copy and adapt these definitions when creating your own templates.
|
176 | </p>
|
177 |
|
178 | <h2 id="ExampleOfNodeTemplates">Example of Node templates</h2>
|
179 | <pre class="lang-js" id="templates"><code>
|
180 | // the "simple" template just shows the key string and the color in the background,
|
181 | // but it also includes a tooltip that shows the description
|
182 | var simpletemplate =
|
183 | $(go.Node, "Auto",
|
184 | $(go.Shape, "Ellipse",
|
185 | new go.Binding("fill", "color")),
|
186 | $(go.TextBlock,
|
187 | new go.Binding("text", "key")),
|
188 | {
|
189 | toolTip:
|
190 | $("ToolTip",
|
191 | $(go.TextBlock, { margin: 4 },
|
192 | new go.Binding("text", "desc"))
|
193 | )
|
194 | }
|
195 | );
|
196 |
|
197 | // the "detailed" template shows all of the information in a Table Panel
|
198 | var detailtemplate =
|
199 | $(go.Node, "Auto",
|
200 | $(go.Shape, "RoundedRectangle",
|
201 | new go.Binding("fill", "color")),
|
202 | $(go.Panel, "Table",
|
203 | { defaultAlignment: go.Spot.Left },
|
204 | $(go.TextBlock, { row: 0, column: 0, columnSpan: 2, font: "bold 12pt sans-serif" },
|
205 | new go.Binding("text", "key")),
|
206 | $(go.TextBlock, { row: 1, column: 0 }, "Description:"),
|
207 | $(go.TextBlock, { row: 1, column: 1 }, new go.Binding("text", "desc")),
|
208 | $(go.TextBlock, { row: 2, column: 0 }, "Color:"),
|
209 | $(go.TextBlock, { row: 2, column: 1 }, new go.Binding("text", "color"))
|
210 | )
|
211 | );
|
212 |
|
213 | // create the nodeTemplateMap, holding three node templates:
|
214 | var templmap = new go.Map(); // In TypeScript you could write: new go.Map<string, go.Node>();
|
215 | // for each of the node categories, specify which template to use
|
216 | templmap.add("simple", simpletemplate);
|
217 | templmap.add("detailed", detailtemplate);
|
218 | // for the default category, "", use the same template that Diagrams use by default;
|
219 | // this just shows the key value as a simple TextBlock
|
220 | templmap.add("", diagram.nodeTemplate);
|
221 |
|
222 | diagram.nodeTemplateMap = templmap;
|
223 |
|
224 | diagram.model.nodeDataArray = [
|
225 | { key: "Alpha", desc: "first letter", color: "green" }, // uses default category: ""
|
226 | { key: "Beta", desc: "second letter", color: "lightblue", category: "simple" },
|
227 | { key: "Gamma", desc: "third letter", color: "pink", category: "detailed" },
|
228 | { key: "Delta", desc: "fourth letter", color: "cyan", category: "detailed" }
|
229 | ];
|
230 | </code></pre>
|
231 | <script>goCode("templates", 600, 150)</script>
|
232 | <p>
|
233 | If you hover the mouse over the "Beta" node, you will see the tooltip showing the description string.
|
234 | The detailed template does not bother using tooltips to show extra information because everything is already shown.
|
235 | </p>
|
236 | <p>
|
237 | By default the way that the model and diagram know about the category of a node data or a link data is
|
238 | by looking at its category property.
|
239 | If you want to use a different property on the data, for example because you want to use the category
|
240 | property to mean something different, set <a>Model.nodeCategoryProperty</a> to be the name
|
241 | of the property that results in the actual category string value.
|
242 | Or set <a>Model.nodeCategoryProperty</a> to be the empty string to cause all nodes to use the default node template.
|
243 | </p>
|
244 |
|
245 | <h2 id="ExampleOfItemTemplates">Example of Item Templates</h2>
|
246 | <p>
|
247 | For Panels with a value for <a>Panel.itemArray</a>, there is also the <a>Panel.itemTemplateMap</a>.
|
248 | As with Nodes and Groups and Links, the <a>Panel.itemTemplate</a> is just a reference to the template named
|
249 | with the empty string in the <a>Panel.itemTemplateMap</a>.
|
250 | Similarly, the <a>Panel.itemCategoryProperty</a> names the property on the item data that identifies the
|
251 | template to use from the itemTemplateMap.
|
252 | </p>
|
253 | <pre class="lang-js" id="itemTemplates"><code>
|
254 | // create a template map for items
|
255 | var itemtemplates = new go.Map(); // In TypeScript you could write: new go.Map<string, go.Panel>();
|
256 |
|
257 | // the template when type == "text"
|
258 | itemtemplates.add("text",
|
259 | $(go.Panel,
|
260 | $(go.TextBlock,
|
261 | new go.Binding("text"))
|
262 | ));
|
263 |
|
264 | // the template when type == "button"
|
265 | itemtemplates.add("button",
|
266 | $("Button",
|
267 | $(go.TextBlock,
|
268 | new go.Binding("text")),
|
269 | // convert a function name into a function value,
|
270 | // because functions cannot be represented in JSON format
|
271 | new go.Binding("click", "handler",
|
272 | function(name) {
|
273 | if (name === "alert") return raiseAlert; // defined below
|
274 | return null;
|
275 | })
|
276 | ));
|
277 |
|
278 | diagram.nodeTemplate =
|
279 | $(go.Node, "Vertical",
|
280 | $(go.TextBlock,
|
281 | new go.Binding("text", "key")),
|
282 | $(go.Panel, "Auto",
|
283 | $(go.Shape, { fill: "white" }),
|
284 | $(go.Panel, "Vertical",
|
285 | {
|
286 | margin: 3,
|
287 | defaultAlignment: go.Spot.Left,
|
288 | itemCategoryProperty: "type", // this property controls the template used
|
289 | itemTemplateMap: itemtemplates // map was defined above
|
290 | },
|
291 | new go.Binding("itemArray", "info"))
|
292 | )
|
293 | );
|
294 |
|
295 | function raiseAlert(e, obj) { // here OBJ will be the item Panel
|
296 | var node = obj.part;
|
297 | alert(node.data.key + ": " + obj.data.text);
|
298 | }
|
299 |
|
300 | // The model data includes item arrays in the node data.
|
301 | diagram.model = new go.GraphLinksModel( [
|
302 | { key: "Alpha",
|
303 | info: [
|
304 | { type: "text", text: "some text" },
|
305 | { type: "button", text: "Click me!", handler: "alert"}
|
306 | ]
|
307 | },
|
308 | { key: "Beta",
|
309 | info: [
|
310 | { type: "text", text: "first line" },
|
311 | { type: "button", text: "First Button", handler: "alert"},
|
312 | { type: "text", text: "second line" },
|
313 | { type: "button", text: "Second Button", handler: "alert" }
|
314 | ]
|
315 | }
|
316 | ],[
|
317 | { from: "Alpha", to: "Beta" }
|
318 | ]);
|
319 | </code></pre>
|
320 | <script>goCode("itemTemplates", 600, 150)</script>
|
321 |
|
322 | <h2 id="ExampleOfTableHeaderShowingItemData">Example of Table Header Showing Item Data</h2>
|
323 | <p>
|
324 | The natural way to have a distinct header for a Table Panel is to have the first row (i.e. the first item)
|
325 | hold the data for the header, but have it be styled differently.
|
326 | In this example we define a "Header" item template in the <a>Panel.itemTemplateMap</a>.
|
327 | </p>
|
328 | <pre class="lang-js" id="header"><code>
|
329 | var itemTemplateMap = new go.Map();
|
330 | itemTemplateMap.add("",
|
331 | $(go.Panel, "TableRow",
|
332 | $(go.TextBlock, new go.Binding("text", "name"),
|
333 | { column: 0, margin: 2, font: "bold 10pt sans-serif" }),
|
334 | $(go.TextBlock, new go.Binding("text", "phone"),
|
335 | { column: 1, margin: 2 }),
|
336 | $(go.TextBlock, new go.Binding("text", "loc"),
|
337 | { column: 2, margin: 2 })
|
338 | ));
|
339 | itemTemplateMap.add("Header",
|
340 | $(go.Panel, "TableRow",
|
341 | $(go.TextBlock, new go.Binding("text", "name"),
|
342 | { column: 0, margin: 2, font: "bold 10pt sans-serif" }),
|
343 | $(go.TextBlock, new go.Binding("text", "phone"),
|
344 | { column: 1, margin: 2, font: "bold 10pt sans-serif" }),
|
345 | $(go.TextBlock, new go.Binding("text", "loc"),
|
346 | { column: 2, margin: 2, font: "bold 10pt sans-serif" })
|
347 | ));
|
348 |
|
349 | diagram.nodeTemplate =
|
350 | $(go.Node, "Auto",
|
351 | $(go.Shape, { fill: "white" }),
|
352 | $(go.Panel, "Table",
|
353 | new go.Binding("itemArray", "people"),
|
354 | {
|
355 | defaultAlignment: go.Spot.Left,
|
356 | defaultColumnSeparatorStroke: "black",
|
357 | itemTemplateMap: itemTemplateMap
|
358 | },
|
359 | $(go.RowColumnDefinition,
|
360 | { row: 0, background: "lightgray" }),
|
361 | $(go.RowColumnDefinition,
|
362 | { row: 1, separatorStroke: "black" })
|
363 | )
|
364 | );
|
365 |
|
366 | diagram.model =
|
367 | $(go.GraphLinksModel,
|
368 | {
|
369 | nodeDataArray: [
|
370 | { key: "group1",
|
371 | people: [
|
372 | { name: "Person", phone: "Phone", loc: "Location", category: "Header" },
|
373 | { name: "Alice", phone: "2345", loc: "C4-E18" },
|
374 | { name: "Bob", phone: "9876", loc: "E1-B34" },
|
375 | { name: "Carol", phone: "1111", loc: "C4-E23" },
|
376 | { name: "Ted", phone: "2222", loc: "C4-E197" },
|
377 | { name: "Robert", phone: "5656", loc: "B1-A27" },
|
378 | { name: "Natalie", phone: "5698", loc: "B1-B6" }
|
379 | ] }
|
380 | ],
|
381 | linkDataArray: [
|
382 | ]
|
383 | }
|
384 | );
|
385 | </code></pre>
|
386 | <script>goCode("header", 600, 200)</script>
|
387 | <p>
|
388 | If you do not want to have the header data in the itemArray,
|
389 | and you want to define the header in the node template rather than as an item template,
|
390 | see the example in <a href="itemArrays.html">Item Arrays</a>.
|
391 | </p>
|
392 |
|
393 | <h2 id="ChangingCategoryOfPart">Changing category of a Part</h2>
|
394 | <p>
|
395 | To change the representation of a data object, call <a>Model.setCategoryForNodeData</a>
|
396 | or <a>GraphLinksModel.setCategoryForLinkData</a>.
|
397 | (If you set the <a>Part.category</a> of a data bound <a>Part</a>, it will call the Model method for you.)
|
398 | This causes the diagram to discard any existing Part for the data and re-create it using the new template that
|
399 | is associated with the new category value.
|
400 | </p>
|
401 | <pre class="lang-js" id="changingCategory"><code>
|
402 | // this function changes the category of the node data to cause the Node to be replaced
|
403 | function changeCategory(e, obj) {
|
404 | var node = obj.part;
|
405 | if (node) {
|
406 | var diagram = node.diagram;
|
407 | diagram.startTransaction("changeCategory");
|
408 | var cat = diagram.model.getCategoryForNodeData(node.data);
|
409 | if (cat === "simple")
|
410 | cat = "detailed";
|
411 | else
|
412 | cat = "simple";
|
413 | diagram.model.setCategoryForNodeData(node.data, cat);
|
414 | diagram.commitTransaction("changeCategory");
|
415 | }
|
416 | }
|
417 |
|
418 | // The "simple" template just shows the key string and the color in the background.
|
419 | // There is a Button to invoke the changeCategory function.
|
420 | var simpletemplate =
|
421 | $(go.Node, "Spot",
|
422 | $(go.Panel, "Auto",
|
423 | $(go.Shape, "Ellipse",
|
424 | new go.Binding("fill", "color")),
|
425 | $(go.TextBlock,
|
426 | new go.Binding("text", "key"))
|
427 | ),
|
428 | $("Button",
|
429 | { alignment: go.Spot.TopRight },
|
430 | $(go.Shape, "AsteriskLine", { width: 8, height: 8 }),
|
431 | { click: changeCategory })
|
432 | );
|
433 |
|
434 | // The "detailed" template shows all of the information in a Table Panel.
|
435 | // There is a Button to invoke the changeCategory function.
|
436 | var detailtemplate =
|
437 | $(go.Node, "Spot",
|
438 | $(go.Panel, "Auto",
|
439 | $(go.Shape, "RoundedRectangle",
|
440 | new go.Binding("fill", "color")),
|
441 | $(go.Panel, "Table",
|
442 | { defaultAlignment: go.Spot.Left },
|
443 | $(go.TextBlock, { row: 0, column: 0, columnSpan: 2, font: "bold 12pt sans-serif" },
|
444 | new go.Binding("text", "key")),
|
445 | $(go.TextBlock, { row: 1, column: 0 }, "Description:"),
|
446 | $(go.TextBlock, { row: 1, column: 1 }, new go.Binding("text", "desc")),
|
447 | $(go.TextBlock, { row: 2, column: 0 }, "Color:"),
|
448 | $(go.TextBlock, { row: 2, column: 1 }, new go.Binding("text", "color"))
|
449 | )
|
450 | ),
|
451 | $("Button",
|
452 | { alignment: go.Spot.TopRight },
|
453 | $(go.Shape, "AsteriskLine", { width: 8, height: 8 }),
|
454 | { click: changeCategory })
|
455 | );
|
456 |
|
457 | var templmap = new go.Map(); // In TypeScript you could write: new go.Map<string, go.Node>();
|
458 | templmap.add("simple", simpletemplate);
|
459 | templmap.add("detailed", detailtemplate);
|
460 | diagram.nodeTemplateMap = templmap;
|
461 |
|
462 | diagram.layout = $(go.TreeLayout);
|
463 |
|
464 | diagram.model.nodeDataArray = [
|
465 | { key: "Beta", desc: "second letter", color: "lightblue", category: "simple" },
|
466 | { key: "Gamma", desc: "third letter", color: "pink", category: "detailed" },
|
467 | { key: "Delta", desc: "fourth letter", color: "cyan", category: "detailed" }
|
468 | ];
|
469 | diagram.model.linkDataArray = [
|
470 | { from: "Beta", to: "Gamma" },
|
471 | { from: "Gamma", to: "Delta" }
|
472 | ];
|
473 | </code></pre>
|
474 | <script>goCode("changingCategory", 600, 150)</script>
|
475 | <p>
|
476 | Click on the "asterisk" button on any node to toggle dynamically between the "simple" and the "detailed" category for each node.
|
477 | </p>
|
478 |
|
479 | <h2 id="ChangingTemplateMaps">Changing template maps</h2>
|
480 | <p>
|
481 | You can also replace one or all of the diagram's template maps (e.g. <a>Diagram.nodeTemplateMap</a>)
|
482 | in order to discard and re-create all of the nodes in the diagram.
|
483 | If you are only using the default template for nodes, you would only need to replace the <a>Diagram.nodeTemplate</a>.
|
484 | </p>
|
485 | <p>
|
486 | One common circumstance for doing this is as the <a>Diagram.scale</a> changes.
|
487 | When the user zooms out far enough, there is no point in having too much detail about each of the nodes.
|
488 | </p>
|
489 | <p>
|
490 | If you zoom out in this example, the <a>DiagramEvent</a> listener will detect when the <a>Diagram.scale</a>
|
491 | becomes small enough to use the simpler template for all of the nodes.
|
492 | Zoom in again and suddenly it uses the more detailed template.
|
493 | </p>
|
494 | <pre class="lang-js" id="changingMap"><code>
|
495 | // The "simple" template just shows the key string and the color in the background.
|
496 | var simpletemplate =
|
497 | $(go.Node, "Spot",
|
498 | $(go.Panel, "Auto",
|
499 | $(go.Shape, "Ellipse",
|
500 | new go.Binding("fill", "color")),
|
501 | $(go.TextBlock,
|
502 | new go.Binding("text", "key"))
|
503 | )
|
504 | );
|
505 |
|
506 | // The "detailed" template shows all of the information in a Table Panel.
|
507 | var detailtemplate =
|
508 | $(go.Node, "Spot",
|
509 | $(go.Panel, "Auto",
|
510 | $(go.Shape, "RoundedRectangle",
|
511 | new go.Binding("fill", "color")),
|
512 | $(go.Panel, "Table",
|
513 | { defaultAlignment: go.Spot.Left },
|
514 | $(go.TextBlock, { row: 0, column: 0, columnSpan: 2, font: "bold 12pt sans-serif" },
|
515 | new go.Binding("text", "key")),
|
516 | $(go.TextBlock, { row: 1, column: 0 }, "Description:"),
|
517 | $(go.TextBlock, { row: 1, column: 1 }, new go.Binding("text", "desc")),
|
518 | $(go.TextBlock, { row: 2, column: 0 }, "Color:"),
|
519 | $(go.TextBlock, { row: 2, column: 1 }, new go.Binding("text", "color"))
|
520 | )
|
521 | )
|
522 | );
|
523 |
|
524 | diagram.layout = $(go.TreeLayout);
|
525 |
|
526 | diagram.model.nodeDataArray = [
|
527 | { key: "Beta", desc: "second letter", color: "lightblue" },
|
528 | { key: "Gamma", desc: "third letter", color: "pink" },
|
529 | { key: "Delta", desc: "fourth letter", color: "cyan" }
|
530 | ];
|
531 | diagram.model.linkDataArray = [
|
532 | { from: "Beta", to: "Gamma" },
|
533 | { from: "Gamma", to: "Delta" }
|
534 | ];
|
535 |
|
536 | // initially use the detailed templates
|
537 | diagram.nodeTemplate = detailtemplate;
|
538 |
|
539 | diagram.addDiagramListener("ViewportBoundsChanged",
|
540 | function (e) {
|
541 | if (diagram.scale < 0.9) {
|
542 | diagram.nodeTemplate = simpletemplate;
|
543 | } else {
|
544 | diagram.nodeTemplate = detailtemplate;
|
545 | }
|
546 | });
|
547 |
|
548 | // make accessible to the HTML buttons
|
549 | myDiagram = diagram;
|
550 | </code></pre>
|
551 | <script>goCode("changingMap", 600, 150)</script>
|
552 | <input id="ZoomOut" type="button" onclick="myDiagram.commandHandler.decreaseZoom()" value="Zoom Out" />
|
553 | <input id="ZoomIn" type="button" onclick="myDiagram.commandHandler.increaseZoom()" value="Zoom In" />
|
554 | <p>
|
555 | Caution: if you modify a template <a>Map</a>, there is no notification that the map has changed.
|
556 | You will need to call <a>Diagram.rebuildParts</a> explicitly.
|
557 | If you are replacing the <a>Diagram.nodeTemplate</a> or the <a>Diagram.nodeTemplateMap</a>
|
558 | or the corresponding properties for Groups or Links, the Diagram property setters will automatically
|
559 | call <a>Diagram.rebuildParts</a>.
|
560 | </p>
|
561 | <p>
|
562 | When one or more templates are replaced in a diagram, layouts are automatically performed again.
|
563 | </p>
|
564 |
|
565 | </div>
|
566 | </div>
|
567 |
|
568 | <div class="bg-nwoods-primary">
|
569 | <section class="max-w-screen-lg text-white container mx-auto py-2 px-12">
|
570 | <p id="version" class="leading-none mb-2 my-4">GoJS</p>
|
571 | </section>
|
572 | </div><footer class="bg-nwoods-primary text-white">
|
573 | <div class="container max-w-screen-lg mx-auto px-8">
|
574 | <div class="w-full py-6">
|
575 |
|
576 | <div class="max-w-screen-lg xl:max-w-screen-xl mx-auto px-4 sm:px-6 md:px-8">
|
577 | <ul class="text-sm font-medium pb-14 sm:pb-20 grid grid-cols-1 sm:grid-cols-3 gap-y-10">
|
578 | <li class="list-none row-span-2">
|
579 | <h2 class="text-base font-semibold tracking-wide">GoJS</h2>
|
580 | <ul class="list-none space-y-4 md:space-y-1 px-0">
|
581 | <li>
|
582 | <a href="../samples/index.html">Samples</a>
|
583 | </li>
|
584 | <li>
|
585 | <a href="../learn/index.html">Learn</a>
|
586 | </li>
|
587 | <li>
|
588 | <a href="../intro/index.html">Intro</a>
|
589 | </li>
|
590 | <li>
|
591 | <a href="../api/index.html">API</a>
|
592 | </li>
|
593 | <li>
|
594 | <a href="../changelog.html">Changelog</a>
|
595 | </li>
|
596 | <li>
|
597 | <a href="https://github.com/NorthwoodsSoftware/GoJS">GitHub</a>
|
598 | </li>
|
599 | </ul>
|
600 | </li>
|
601 | <li class="list-none row-span-2">
|
602 | <h2 class="text-base font-semibold tracking-wide">Support</h2>
|
603 | <ul class="list-none space-y-4 md:space-y-1 px-0">
|
604 | <li>
|
605 | <a href="https://www.nwoods.com/contact.html"
|
606 | target="_blank" rel="noopener" onclick="getOutboundLink('https://www.nwoods.com/contact.html', 'contact');">Contact</a>
|
607 | </li>
|
608 | <li>
|
609 | <a href="https://forum.nwoods.com/c/gojs">Forum</a>
|
610 | </li>
|
611 | <li>
|
612 | <a href="https://www.nwoods.com/app/activate.aspx?sku=gojs">Activate</a>
|
613 | </li>
|
614 | <li>
|
615 | <a href="https://www.nwoods.com/sales/index.html"
|
616 | target="_blank" rel="noopener" onclick="getOutboundLink('https://www.nwoods.com/sales/index.html', 'buy');">Buy</a>
|
617 | </li>
|
618 | <li>
|
619 | <a href="https://www.youtube.com/channel/UC9We8EoX596-6XFjJDtZIDg">Videos</a>
|
620 | </li>
|
621 | </ul>
|
622 | </li>
|
623 | <li class="list-none row-span-2">
|
624 | <h2 class="text-base font-semibold tracking-wide">Company</h2>
|
625 | <ul class="list-none space-y-4 md:space-y-1 px-0">
|
626 | <li>
|
627 | <a href="https://www.nwoods.com">Northwoods</a>
|
628 | </li>
|
629 | <li>
|
630 | <a href="https://www.nwoods.com/about.html">About Us</a>
|
631 | </li>
|
632 | <li>
|
633 | <a href="https://www.nwoods.com/contact.html">Contact Us</a>
|
634 | </li>
|
635 | <li>
|
636 | <a href="https://twitter.com/northwoodsgo">Twitter</a>
|
637 | </li>
|
638 |
|
639 | </ul>
|
640 | </li>
|
641 | </ul>
|
642 |
|
643 |
|
644 | <p class="text-sm text-gray-100 md:mb-6">
|
645 | Copyright 1998-2021 <a class="text-white" href="https://www.nwoods.com">Northwoods Software</a>
|
646 | </p>
|
647 | </div>
|
648 | </div>
|
649 | </footer> </body>
|
650 |
|
651 | <script async src="https://www.googletagmanager.com/gtag/js?id=UA-1506307-5"></script>
|
652 | <script>
|
653 | window.dataLayer = window.dataLayer || [];
|
654 | function gtag(){dataLayer.push(arguments);}
|
655 | gtag('js', new Date()); gtag('config', 'UA-1506307-5');
|
656 | var getOutboundLink = function(url, label) {
|
657 | gtag('event', 'click', {
|
658 | 'event_category': 'outbound',
|
659 | 'event_label': label,
|
660 | 'transport_type': 'beacon'
|
661 | });
|
662 | }
|
663 |
|
664 |
|
665 | var topButton = document.getElementById("topnavButton");
|
666 | var topnavList = document.getElementById("topnavList");
|
667 | topButton.addEventListener("click", function() {
|
668 | this.classList.toggle("active");
|
669 | topnavList.classList.toggle("hidden");
|
670 | document.getElementById("topnavOpen").classList.toggle("hidden");
|
671 | document.getElementById("topnavClosed").classList.toggle("hidden");
|
672 | });
|
673 | </script>
|
674 | <script src="../assets/js/prism.js"></script>
|
675 | <script src="../release/go.js"></script>
|
676 | <script src="../extensions/Figures.js"></script> <script src="../assets/js/goDoc.js"></script>
|
677 | <script>
|
678 | document.addEventListener("DOMContentLoaded", function() {
|
679 | if (window.go) document.getElementById('version').textContent = "GoJS version " + go.version;
|
680 | if (window.goDoc) window.goDoc();
|
681 | var d = window.diagrams;
|
682 | for (var i = 0; i < d.length; i++) {
|
683 | var dargs = d[i];
|
684 | goCodeExecute(dargs[0], dargs[1], dargs[2], dargs[3], dargs[4]);
|
685 | }
|
686 | if (window.extra) window.extra();
|
687 | });
|
688 | </script>
|
689 | </html>
|