forked from NorthwoodsSoftware/GoJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorgChartExtras.html
355 lines (338 loc) · 14.3 KB
/
orgChartExtras.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Org Chart Extras</title>
<meta name="description" content="An org chart that also shows non-tree-structure relationships." />
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Copyright 1998-2019 by Northwoods Software Corporation. -->
<script src="../release/go.js"></script>
<script src="../assets/js/goSamples.js"></script> <!-- this is only for the GoJS Samples framework -->
<script id="code">
function init() {
if (window.goSamples) goSamples(); // init for these samples -- you don't need to call this
var $ = go.GraphObject.make; // for conciseness in defining templates
myDiagram =
$(go.Diagram, "myDiagramDiv", // must be the ID or reference to div
{
// make sure users can only create trees
validCycle: go.Diagram.CycleDestinationTree,
// users can select only one part at a time
maxSelectionCount: 1,
layout:
$(go.TreeLayout,
{
treeStyle: go.TreeLayout.StyleLastParents,
arrangement: go.TreeLayout.ArrangementHorizontal,
// properties for most of the tree:
angle: 90,
layerSpacing: 35,
// properties for the "last parents":
alternateAngle: 0,
alternateLayerSpacing: 35,
alternateAlignment: go.TreeLayout.AlignmentStart,
alternateNodeIndent: 10,
alternateNodeIndentPastParent: 1.0,
alternateNodeSpacing: 10,
alternateLayerSpacing: 30,
alternateLayerSpacingParentOverlap: 1.0,
alternatePortSpot: new go.Spot(0.01, 1, 10, 0),
alternateChildPortSpot: go.Spot.Left
}),
// support editing the properties of the selected person in HTML
"ChangedSelection": onSelectionChanged,
"TextEdited": onTextEdited,
// enable undo & redo
"undoManager.isEnabled": true
});
// when the document is modified, add a "*" to the title and enable the "Save" button
myDiagram.addDiagramListener("Modified", function(e) {
var button = document.getElementById("SaveButton");
if (button) button.disabled = !myDiagram.isModified;
var idx = document.title.indexOf("*");
if (myDiagram.isModified) {
if (idx < 0) document.title += "*";
} else {
if (idx >= 0) document.title = document.title.substr(0, idx);
}
});
var graygrad = $(go.Brush, "Linear",
{ 0: "rgb(125, 125, 125)", 0.5: "rgb(86, 86, 86)", 1: "rgb(86, 86, 86)" });
// when a node is double-clicked, add a child to it
function nodeDoubleClick(e, obj) {
var clicked = obj.part;
if (clicked !== null) {
var thisemp = clicked.data;
myDiagram.startTransaction("add employee");
var nextkey = (myDiagram.model.nodeDataArray.length + 1).toString();
var newemp = { key: nextkey, name: "(new person)", title: "" };
myDiagram.model.addNodeData(newemp);
myDiagram.model.addLinkData({ from: thisemp.key, to: nextkey });
myDiagram.commitTransaction("add employee");
}
}
// this is used to determine feedback during drags
function mayWorkFor(node1, node2) {
if (!(node1 instanceof go.Node)) return false; // must be a Node
if (node1 === node2) return false; // cannot work for yourself
if (node2.isInTreeOf(node1)) return false; // cannot work for someone who works for you
return true;
}
// This function provides a common style for most of the TextBlocks.
// Some of these values may be overridden in a particular TextBlock.
function textStyle() {
return { font: "9pt sans-serif", stroke: "white" };
}
// define the Node template
myDiagram.nodeTemplate =
$(go.Node, "Auto",
{ doubleClick: nodeDoubleClick },
{ // handle dragging a Node onto a Node to (maybe) change the reporting relationship
mouseDragEnter: function(e, node, prev) {
var diagram = node.diagram;
var selnode = diagram.selection.first();
if (!mayWorkFor(selnode, node)) return;
var shape = node.findObject("SHAPE");
if (shape) shape.fill = "darkred";
},
mouseDragLeave: function(e, node, next) {
var shape = node.findObject("SHAPE");
if (shape) shape.fill = graygrad;
},
mouseDrop: function(e, node) {
var diagram = node.diagram;
var selnode = diagram.selection.first(); // assume just one Node in selection
if (mayWorkFor(selnode, node)) {
// find any existing link into the selected node
var link = selnode.findTreeParentLink();
if (link !== null) { // reconnect any existing link
link.fromNode = node;
} else { // else create a new link
diagram.toolManager.linkingTool.insertLink(node, node.port, selnode, selnode.port);
}
}
}
},
// for sorting, have the Node.text be the data.name
new go.Binding("text", "name"),
// bind the Part.layerName to control the Node's layer depending on whether it isSelected
new go.Binding("layerName", "isSelected", function(sel) { return sel ? "Foreground" : ""; }).ofObject(),
// define the node's outer shape
$(go.Shape, "RoundedRectangle",
{
name: "SHAPE",
fill: graygrad, stroke: "black",
portId: "", fromLinkable: true, toLinkable: true, cursor: "pointer"
}),
// define the panel where the text will appear
$(go.Panel, "Table",
{
maxSize: new go.Size(150, 999),
margin: new go.Margin(3, 3, 0, 3),
defaultAlignment: go.Spot.Left
},
$(go.RowColumnDefinition, { column: 2, width: 4 }),
$(go.TextBlock, // the name
{
row: 0, column: 0, columnSpan: 5,
font: "bold 9pt sans-serif",
editable: true, isMultiline: false,
stroke: "white", minSize: new go.Size(10, 14),
name: "name"
},
new go.Binding("text", "name").makeTwoWay()),
$(go.TextBlock, "Title: ", textStyle(),
{ row: 1, column: 0 }),
$(go.TextBlock, textStyle(),
{
row: 1, column: 1, columnSpan: 4,
editable: true, isMultiline: false,
minSize: new go.Size(10, 14),
margin: new go.Margin(0, 0, 0, 3),
name: "title"
},
new go.Binding("text", "title").makeTwoWay()),
$(go.TextBlock, "ID: ", textStyle(), // the ID and the boss
{ row: 2, column: 0 }),
$(go.TextBlock, textStyle(),
{ row: 2, column: 1 },
new go.Binding("text", "key")),
$(go.TextBlock, "Boss: ", textStyle(),
{ row: 2, column: 3 }),
$(go.TextBlock, textStyle(),
{ row: 2, column: 4 },
new go.Binding("text", "parent")),
$(go.TextBlock, // the comments
{
row: 3, column: 0, columnSpan: 5,
font: "italic 9pt sans-serif",
wrap: go.TextBlock.WrapFit,
editable: true, // by default newlines are allowed
stroke: "white",
minSize: new go.Size(10, 14),
name: "comments"
},
new go.Binding("text", "comments").makeTwoWay()),
$("TreeExpanderButton",
{ row: 4, columnSpan: 99, alignment: go.Spot.Center })
) // end Table Panel
); // end Node
// define the Link template
myDiagram.linkTemplate =
$(go.Link, go.Link.Orthogonal,
{ corner: 5, relinkableFrom: true, relinkableTo: true },
$(go.Shape, { strokeWidth: 2 })); // the link shape
myDiagram.linkTemplateMap.add("Support",
$(go.Link, go.Link.Bezier,
{ isLayoutPositioned: false, isTreeLink: false, curviness: -50 },
{ relinkableFrom: true, relinkableTo: true },
$(go.Shape,
{ stroke: "green", strokeWidth: 2 }),
$(go.Shape,
{ toArrow: "OpenTriangle", stroke: "green", strokeWidth: 2 }),
$(go.TextBlock,
new go.Binding("text", "text"),
{
stroke: "green", background: "rgba(255,255,255,0.75)",
maxSize: new go.Size(80, NaN)
})));
myDiagram.linkTemplateMap.add("Motion",
$(go.Link, go.Link.Bezier,
{ isLayoutPositioned: false, isTreeLink: false, curviness: -50 },
{ relinkableFrom: true, relinkableTo: true },
$(go.Shape,
{ stroke: "orange", strokeWidth: 2 }),
$(go.Shape,
{ toArrow: "OpenTriangle", stroke: "orange", strokeWidth: 2 }),
$(go.TextBlock,
new go.Binding("text", "text"),
{
stroke: "orange", background: "rgba(255,255,255,0.75)",
maxSize: new go.Size(80, NaN)
})));
// read in the JSON-format data from the "mySavedModel" element
load();
}
// Allow the user to edit text when a single node is selected
function onSelectionChanged(e) {
var node = e.diagram.selection.first();
if (node instanceof go.Node) {
updateProperties(node.data);
} else {
updateProperties(null);
}
}
// Update the HTML elements for editing the properties of the currently selected node, if any
function updateProperties(data) {
if (data === null) {
document.getElementById("propertiesPanel").style.display = "none";
document.getElementById("name").value = "";
document.getElementById("title").value = "";
document.getElementById("comments").value = "";
} else {
document.getElementById("propertiesPanel").style.display = "block";
document.getElementById("name").value = data.name || "";
document.getElementById("title").value = data.title || "";
document.getElementById("comments").value = data.comments || "";
}
}
// This is called when the user has finished inline text-editing
function onTextEdited(e) {
var tb = e.subject;
if (tb === null || !tb.name) return;
var node = tb.part;
if (node instanceof go.Node) {
updateData(tb.text, tb.name);
updateProperties(node.data);
}
}
// Update the data fields when the text is changed
function updateData(text, field) {
var node = myDiagram.selection.first();
// maxSelectionCount = 1, so there can only be one Part in this collection
var data = node.data;
if (node instanceof go.Node && data !== null) {
var model = myDiagram.model;
model.startTransaction("modified " + field);
if (field === "name") {
model.setDataProperty(data, "name", text);
} else if (field === "title") {
model.setDataProperty(data, "title", text);
} else if (field === "comments") {
model.setDataProperty(data, "comments", text);
}
model.commitTransaction("modified " + field);
}
}
// Show the diagram's model in JSON format
function save() {
document.getElementById("mySavedModel").value = myDiagram.model.toJson();
myDiagram.isModified = false;
}
function load() {
myDiagram.model = go.Model.fromJson(document.getElementById("mySavedModel").value);
}
</script>
</head>
<body onload="init()">
<div id="sample">
<div id="myDiagramDiv" style="background-color: white; border: solid 1px black; height: 550px"></div>
<div>
<div id="propertiesPanel" style="display: none; background-color: aliceblue; border: solid 1px black">
<b>Properties</b><br />
Name: <input type="text" id="name" value="" onchange="updateData(this.value, 'name')" /><br />
Title: <input type="text" id="title" value="" onchange="updateData(this.value, 'title')" /><br />
Comments: <input type="text" id="comments" value="" onchange="updateData(this.value, 'comments')" /><br />
</div>
</div>
<p>
Double click on a node in order to add a person.
Drag a node onto another in order to change relationships.
You can also draw a link from a node's background to other nodes that have no "boss".
</p>
<p>
This is the <a href="orgChartEditor.html">Org Chart Editor</a> sample,
but each node includes a TreeExpanderButton,
and there are additional non-tree links connecting some of the nodes.
</p>
<div>
<div>
<button id="SaveButton" onclick="save()">Write</button>
<button onclick="load()">Read</button>
Diagram Model saved in JSON format:
</div>
<textarea id="mySavedModel" style="width:100%;height:300px">
{ "class": "go.GraphLinksModel",
"nodeDataArray": [
{"key":"1", "name":"Corrado 'Junior' Soprano", "title":"The Boss"},
{"key":"2", "name":"Tony Soprano", "title":"Underboss"},
{"key":"3", "name":"Herman 'Hesh' Rabkin", "title":"Advisor"},
{"key":"4", "name":"Paulie Walnuts", "title":"Capo"},
{"key":"5", "name":"Ralph Cifaretto", "title":"Capo MIA"},
{"key":"6", "name":"Silvio Dante", "title":"Consigliere"},
{"key":"7", "name":"Bobby Baccilien", "title":"Capo"},
{"key":"8", "name":"Sal Bonpensiero", "title":"MIA"},
{"key":"9", "name":"Christopher Moltisanti", "title":"Made Man"},
{"key":"10", "name":"Furio Giunta", "title":"Muscle"},
{"key":"11", "name":"Patsy Parisi", "title":"Accountant"}
],
"linkDataArray": [
{"from":"1", "to":"2"},
{"from":"1", "to":"3"},
{"from":"2", "to":"4"},
{"from":"2", "to":"5"},
{"from":"2", "to":"6"},
{"from":"2", "to":"7"},
{"from":"4", "to":"8"},
{"from":"4", "to":"9"},
{"from":"4", "to":"10"},
{"from":"4", "to":"11"},
{"from":"11", "to":"6", "category":"Support", "text":"50% support"},
{"from":"9", "to":"7", "category":"Motion", "text":"will change here in 2 months"}
]
}
</textarea>
</div>
</div>
</body>
</html>