UNPKG

12.1 kBJavaScriptView Raw
1/*
2* Copyright (C) 1998-2021 by Northwoods Software Corporation. All Rights Reserved.
3*/
4(function (factory) {
5 if (typeof module === "object" && typeof module.exports === "object") {
6 var v = factory(require, exports);
7 if (v !== undefined) module.exports = v;
8 }
9 else if (typeof define === "function" && define.amd) {
10 define(["require", "exports", "../release/go.js", "./PackedLayout.js"], factory);
11 }
12})(function (require, exports) {
13 "use strict";
14 Object.defineProperty(exports, "__esModule", { value: true });
15 exports.randomize = exports.rebuildGraph = exports.init = void 0;
16 /*
17 * This is an extension and not part of the main GoJS library.
18 * Note that the API for this class may change with any version, even point releases.
19 * If you intend to use an extension in production, you should copy the code to your own source directory.
20 * Extensions can be found in the GoJS kit under the extensions or extensionsTS folders.
21 * See the Extensions intro page (https://gojs.net/latest/intro/extensions.html) for more information.
22 */
23 var go = require("../release/go.js");
24 var PackedLayout_js_1 = require("./PackedLayout.js");
25 var myDiagram;
26 var aspectRatio;
27 var layoutWidth;
28 var layoutHeight;
29 var nodeSpacing;
30 var hasCircularNodes;
31 var numNodes;
32 var nodeMinSide;
33 var nodeMaxSide;
34 var sameSides;
35 // nodes need to be randomized again if any of these change
36 var minSidePrevious;
37 var maxSidePrevious;
38 var sameSidesPrevious;
39 function init() {
40 if (window.goSamples)
41 window.goSamples(); // init for these samples -- you don't need to call this
42 var $ = go.GraphObject.make; // for conciseness in defining templates
43 myDiagram =
44 $(go.Diagram, 'myDiagramDiv', // must be the ID or reference to div
45 {
46 'animationManager.isEnabled': true,
47 layout: $(PackedLayout_js_1.PackedLayout),
48 scale: 0.75, isReadOnly: true
49 });
50 // Nodes have a template with bindings for size, shape, and fill color
51 myDiagram.nodeTemplate =
52 $(go.Node, 'Auto', new go.Binding('visible', 'visible'), $(go.Shape, { strokeWidth: 0 }, new go.Binding('figure', 'figure'), new go.Binding('width', 'width'), new go.Binding('height', 'height'), new go.Binding('fill', 'fill')));
53 myDiagram.model = new go.GraphLinksModel([]);
54 // find the elements in the DOM which control configuration
55 aspectRatio = document.getElementById('aspectRatio');
56 layoutWidth = document.getElementById('width');
57 layoutHeight = document.getElementById('height');
58 nodeSpacing = document.getElementById('nodeSpacing');
59 hasCircularNodes = document.getElementById('hasCircularNodes');
60 numNodes = document.getElementById('numNodes');
61 nodeMinSide = document.getElementById('nodeMinSide');
62 nodeMaxSide = document.getElementById('nodeMaxSide');
63 sameSides = document.getElementById('sameSides');
64 aspectRatio.onkeydown = aspectRatioHandler;
65 // create a layout with the default values
66 rebuildGraph();
67 }
68 exports.init = init;
69 // when arrow keys are pressed and the aspect ratio is below 1, increment using the harmonic sequence
70 // this makes the aspect ratio change as follows: 3:1, 2:1, 1:1, 1:2, 1:3, etc.
71 function aspectRatioHandler(e) {
72 if (e.key === 'ArrowUp' && parseFloat(aspectRatio.value) < 1) {
73 e.preventDefault();
74 var denom = Math.round(1 / parseFloat(aspectRatio.value));
75 aspectRatio.value = (+(1 / (denom - 1)).toFixed(2)) + '';
76 rebuildGraph();
77 }
78 else if (e.key === 'ArrowDown' && parseFloat(aspectRatio.value) <= 1) {
79 e.preventDefault();
80 var denom = Math.round(1 / parseFloat(aspectRatio.value));
81 if (denom < 10) {
82 aspectRatio.value = (+(1 / (denom + 1)).toFixed(2)) + '';
83 rebuildGraph();
84 }
85 }
86 }
87 function validateInput() {
88 if (!aspectRatio.value || parseFloat(aspectRatio.value) <= 0) {
89 aspectRatio.value = '0.1';
90 }
91 if (!layoutWidth.value || parseFloat(layoutWidth.value) <= 0) {
92 layoutWidth.value = '1';
93 }
94 if (!layoutHeight.value || parseFloat(layoutHeight.value) <= 0) {
95 layoutHeight.value = '1';
96 }
97 if (!nodeSpacing.value) {
98 nodeSpacing.value = '0';
99 }
100 if (!numNodes.value || parseInt(numNodes.value) < 1) {
101 numNodes.value = '1';
102 }
103 if (!nodeMinSide.value || parseFloat(nodeMinSide.value) < 1) {
104 nodeMinSide.value = '1';
105 }
106 if (!nodeMaxSide.value || parseFloat(nodeMaxSide.value) < 1) {
107 nodeMaxSide.value = '1';
108 }
109 }
110 function rebuildGraph() {
111 validateInput();
112 var packShape = PackedLayout_js_1.PackedLayout.Elliptical;
113 switch (getRadioValue('packShape')) {
114 case 'Elliptical':
115 packShape = PackedLayout_js_1.PackedLayout.Elliptical;
116 break;
117 case 'Rectangular':
118 packShape = PackedLayout_js_1.PackedLayout.Rectangular;
119 break;
120 case 'Spiral':
121 packShape = PackedLayout_js_1.PackedLayout.Spiral;
122 break;
123 }
124 var packMode = PackedLayout_js_1.PackedLayout.AspectOnly;
125 switch (getRadioValue('packMode')) {
126 case 'AspectOnly':
127 packMode = PackedLayout_js_1.PackedLayout.AspectOnly;
128 break;
129 case 'Fit':
130 packMode = PackedLayout_js_1.PackedLayout.Fit;
131 break;
132 case 'ExpandToFit':
133 packMode = PackedLayout_js_1.PackedLayout.ExpandToFit;
134 break;
135 }
136 var sortMode = PackedLayout_js_1.PackedLayout.None;
137 switch (getRadioValue('sortMode')) {
138 case 'None':
139 sortMode = PackedLayout_js_1.PackedLayout.None;
140 break;
141 case 'MaxSide':
142 sortMode = PackedLayout_js_1.PackedLayout.MaxSide;
143 break;
144 case 'Area':
145 sortMode = PackedLayout_js_1.PackedLayout.Area;
146 break;
147 }
148 var sortOrder = PackedLayout_js_1.PackedLayout.Descending;
149 switch (getRadioValue('sortOrder')) {
150 case 'Descending':
151 sortOrder = PackedLayout_js_1.PackedLayout.Descending;
152 break;
153 case 'Ascending':
154 sortOrder = PackedLayout_js_1.PackedLayout.Ascending;
155 break;
156 }
157 var params = {
158 packMode: packMode,
159 packShape: packShape,
160 sortMode: sortMode,
161 sortOrder: sortOrder,
162 aspectRatio: parseFloat(aspectRatio.value),
163 size: new go.Size(parseFloat(layoutWidth.value), parseFloat(layoutHeight.value)),
164 spacing: parseFloat(nodeSpacing.value),
165 hasCircularNodes: hasCircularNodes.checked,
166 arrangesToOrigin: false
167 };
168 disableInputs(params);
169 if (sameSides.checked !== sameSidesPrevious
170 || parseFloat(nodeMinSide.value) !== minSidePrevious
171 || parseFloat(nodeMaxSide.value) !== maxSidePrevious) {
172 sameSidesPrevious = sameSides.checked;
173 minSidePrevious = parseFloat(nodeMinSide.value);
174 maxSidePrevious = parseFloat(nodeMaxSide.value);
175 randomize();
176 return;
177 }
178 myDiagram.startTransaction('packed layout');
179 generateNodeData();
180 myDiagram.layout = go.GraphObject.make(PackedLayout_js_1.PackedLayout, params /* defined above */);
181 myDiagram.commitTransaction('packed layout');
182 }
183 exports.rebuildGraph = rebuildGraph;
184 function randomize() {
185 myDiagram.model = new go.GraphLinksModel([]);
186 rebuildGraph();
187 }
188 exports.randomize = randomize;
189 function generateNodeData() {
190 var nodeDataArray = myDiagram.model.nodeDataArray;
191 var count = parseInt(numNodes.value);
192 var min = parseFloat(nodeMinSide.value);
193 var max = parseFloat(nodeMaxSide.value);
194 var shapeToPack = getRadioValue('shapeToPack');
195 if (count > nodeDataArray.length) {
196 var arr = new Array();
197 for (var i = nodeDataArray.length; i < count; i++) {
198 var width = Math.floor(Math.random() * (max - min + 1)) + min;
199 var height = sameSides.checked ? width : Math.floor(Math.random() * (max - min + 1)) + min;
200 var color = go.Brush.randomColor(128, 235);
201 arr.push({ width: width, height: height, fill: color, figure: shapeToPack });
202 }
203 myDiagram.model.addNodeDataCollection(arr);
204 }
205 else if (count < nodeDataArray.length) {
206 while (count < nodeDataArray.length) {
207 myDiagram.model.removeNodeData(nodeDataArray[nodeDataArray.length - 1]);
208 }
209 }
210 else {
211 for (var _i = 0, nodeDataArray_1 = nodeDataArray; _i < nodeDataArray_1.length; _i++) {
212 var data = nodeDataArray_1[_i];
213 myDiagram.model.set(data, 'figure', shapeToPack);
214 }
215 }
216 sameSidesPrevious = sameSides.checked;
217 minSidePrevious = min;
218 maxSidePrevious = max;
219 }
220 var hasCircularNodesSavedState = null;
221 var sameSidesSavedState = null;
222 function disableInputs(params) {
223 setRadioButtonsDisabled('packMode', params.packShape === PackedLayout_js_1.PackedLayout.Spiral);
224 aspectRatio.disabled = params.packMode !== PackedLayout_js_1.PackedLayout.AspectOnly || params.packShape === PackedLayout_js_1.PackedLayout.Spiral;
225 layoutWidth.disabled = params.packMode === PackedLayout_js_1.PackedLayout.AspectOnly || params.packShape === PackedLayout_js_1.PackedLayout.Spiral;
226 layoutHeight.disabled = params.packMode === PackedLayout_js_1.PackedLayout.AspectOnly || params.packShape === PackedLayout_js_1.PackedLayout.Spiral;
227 nodeSpacing.disabled = params.packMode === PackedLayout_js_1.PackedLayout.ExpandToFit;
228 hasCircularNodes.disabled = params.packShape === PackedLayout_js_1.PackedLayout.Spiral;
229 if (params.packShape === PackedLayout_js_1.PackedLayout.Spiral) {
230 if (hasCircularNodesSavedState === null) {
231 hasCircularNodesSavedState = hasCircularNodes.checked;
232 }
233 hasCircularNodes.checked = true;
234 params.hasCircularNodes = true;
235 }
236 else if (hasCircularNodesSavedState !== null) {
237 hasCircularNodes.checked = hasCircularNodesSavedState;
238 params.hasCircularNodes = false;
239 hasCircularNodesSavedState = null;
240 }
241 sameSides.disabled = params.hasCircularNodes;
242 if (params.hasCircularNodes) {
243 if (sameSidesSavedState === null) {
244 sameSidesSavedState = sameSides.checked;
245 }
246 sameSides.checked = true;
247 }
248 else if (sameSidesSavedState !== null) {
249 sameSides.checked = sameSidesSavedState;
250 sameSidesSavedState = null;
251 }
252 }
253 function getRadioValue(name) {
254 var radio = document.getElementsByName(name);
255 for (var i = 0; i < radio.length; i++) {
256 if (radio[i].checked)
257 return radio[i].value;
258 }
259 }
260 function setRadioButtonsDisabled(name, disabled) {
261 var radio = document.getElementsByName(name);
262 for (var i = 0; i < radio.length; i++) {
263 radio[i].disabled = disabled;
264 }
265 }
266});