forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorgchart.html
More file actions
439 lines (363 loc) · 12 KB
/
orgchart.html
File metadata and controls
439 lines (363 loc) · 12 KB
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<!--
Copyright (c) 2006-2013, JGraph Ltd
Orgchart example for mxGraph. This example demonstrates using
automatic layouts, fit to page zoom and poster print (across
multiple pages).
-->
<html>
<head>
<title>Orgchart example for mxGraph</title>
<!-- Sets the basepath for the library if not in same directory -->
<script type="text/javascript">
mxBasePath = '../src';
</script>
<!-- Loads and initializes the library -->
<script type="text/javascript" src="../src/js/mxClient.js"></script>
<!-- Example code -->
<script type="text/javascript">
// Makes the shadow brighter
mxConstants.SHADOWCOLOR = '#C0C0C0';
// Program starts here. Creates a sample graph in the
// DOM node with the specified ID. This function is invoked
// from the onLoad event handler of the document (see below).
function main()
{
// Checks if browser is supported
if (!mxClient.isBrowserSupported())
{
// Displays an error message if the browser is
// not supported.
mxUtils.error('Browser is not supported!', 200, false);
}
else
{
// Workaround for Internet Explorer ignoring certain styles
var container = document.createElement('div');
container.style.position = 'absolute';
container.style.overflow = 'hidden';
container.style.left = '0px';
container.style.top = '0px';
container.style.right = '0px';
container.style.bottom = '0px';
var outline = document.getElementById('outlineContainer');
mxEvent.disableContextMenu(container);
if (mxClient.IS_QUIRKS)
{
document.body.style.overflow = 'hidden';
new mxDivResizer(container);
new mxDivResizer(outline);
}
// Sets a gradient background
if (mxClient.IS_GC || mxClient.IS_SF)
{
container.style.background = '-webkit-gradient(linear, 0% 0%, 0% 100%, from(#FFFFFF), to(#E7E7E7))';
}
else if (mxClient.IS_NS)
{
container.style.background = '-moz-linear-gradient(top, #FFFFFF, #E7E7E7)';
}
else if (mxClient.IS_IE)
{
container.style.filter = 'progid:DXImageTransform.Microsoft.Gradient('+
'StartColorStr=\'#FFFFFF\', EndColorStr=\'#E7E7E7\', GradientType=0)';
}
document.body.appendChild(container);
// Creates the graph inside the given container
var graph = new mxGraph(container);
// Enables automatic sizing for vertices after editing and
// panning by using the left mouse button.
graph.setCellsMovable(false);
graph.setAutoSizeCells(true);
graph.setPanning(true);
graph.centerZoom = false;
graph.panningHandler.useLeftButtonForPanning = true;
// Displays a popupmenu when the user clicks
// on a cell (using the left mouse button) but
// do not select the cell when the popup menu
// is displayed
graph.panningHandler.popupMenuHandler = false;
// Creates the outline (navigator, overview) for moving
// around the graph in the top, right corner of the window.
var outln = new mxOutline(graph, outline);
// Disables tooltips on touch devices
graph.setTooltips(!mxClient.IS_TOUCH);
// Set some stylesheet options for the visual appearance of vertices
var style = graph.getStylesheet().getDefaultVertexStyle();
style[mxConstants.STYLE_SHAPE] = 'label';
style[mxConstants.STYLE_VERTICAL_ALIGN] = mxConstants.ALIGN_MIDDLE;
style[mxConstants.STYLE_ALIGN] = mxConstants.ALIGN_LEFT;
style[mxConstants.STYLE_SPACING_LEFT] = 54;
style[mxConstants.STYLE_GRADIENTCOLOR] = '#7d85df';
style[mxConstants.STYLE_STROKECOLOR] = '#5d65df';
style[mxConstants.STYLE_FILLCOLOR] = '#adc5ff';
style[mxConstants.STYLE_FONTCOLOR] = '#1d258f';
style[mxConstants.STYLE_FONTFAMILY] = 'Verdana';
style[mxConstants.STYLE_FONTSIZE] = '12';
style[mxConstants.STYLE_FONTSTYLE] = '1';
style[mxConstants.STYLE_SHADOW] = '1';
style[mxConstants.STYLE_ROUNDED] = '1';
style[mxConstants.STYLE_GLASS] = '1';
style[mxConstants.STYLE_IMAGE] = 'editors/images/dude3.png';
style[mxConstants.STYLE_IMAGE_WIDTH] = '48';
style[mxConstants.STYLE_IMAGE_HEIGHT] = '48';
style[mxConstants.STYLE_SPACING] = 8;
// Sets the default style for edges
style = graph.getStylesheet().getDefaultEdgeStyle();
style[mxConstants.STYLE_ROUNDED] = true;
style[mxConstants.STYLE_STROKEWIDTH] = 3;
style[mxConstants.STYLE_EXIT_X] = 0.5; // center
style[mxConstants.STYLE_EXIT_Y] = 1.0; // bottom
style[mxConstants.STYLE_EXIT_PERIMETER] = 0; // disabled
style[mxConstants.STYLE_ENTRY_X] = 0.5; // center
style[mxConstants.STYLE_ENTRY_Y] = 0; // top
style[mxConstants.STYLE_ENTRY_PERIMETER] = 0; // disabled
// Disable the following for straight lines
style[mxConstants.STYLE_EDGE] = mxEdgeStyle.TopToBottom;
// Stops editing on enter or escape keypress
var keyHandler = new mxKeyHandler(graph);
// Enables automatic layout on the graph and installs
// a tree layout for all groups who's children are
// being changed, added or removed.
var layout = new mxCompactTreeLayout(graph, false);
layout.useBoundingBox = false;
layout.edgeRouting = false;
layout.levelDistance = 60;
layout.nodeDistance = 16;
// Allows the layout to move cells even though cells
// aren't movable in the graph
layout.isVertexMovable = function(cell)
{
return true;
};
var layoutMgr = new mxLayoutManager(graph);
layoutMgr.getLayout = function(cell)
{
if (cell.getChildCount() > 0)
{
return layout;
}
};
// Installs a popupmenu handler using local function (see below).
graph.popupMenuHandler.factoryMethod = function(menu, cell, evt)
{
return createPopupMenu(graph, menu, cell, evt);
};
// Fix for wrong preferred size
var oldGetPreferredSizeForCell = graph.getPreferredSizeForCell;
graph.getPreferredSizeForCell = function(cell)
{
var result = oldGetPreferredSizeForCell.apply(this, arguments);
if (result != null)
{
result.width = Math.max(120, result.width - 40);
}
return result;
};
// Sets the maximum text scale to 1
graph.cellRenderer.getTextScale = function(state)
{
return Math.min(1, state.view.scale);
};
// Dynamically adds text to the label as we zoom in
// (without affecting the preferred size for new cells)
graph.cellRenderer.getLabelValue = function(state)
{
var result = state.cell.value;
if (state.view.graph.getModel().isVertex(state.cell))
{
if (state.view.scale > 1)
{
result += '\nDetails 1';
}
if (state.view.scale > 1.3)
{
result += '\nDetails 2';
}
}
return result;
};
// Gets the default parent for inserting new cells. This
// is normally the first child of the root (ie. layer 0).
var parent = graph.getDefaultParent();
// Adds the root vertex of the tree
graph.getModel().beginUpdate();
try
{
var w = graph.container.offsetWidth;
var v1 = graph.insertVertex(parent, 'treeRoot',
'Organization', w/2 - 30, 20, 140, 60, 'image=editors/images/house.png');
graph.updateCellSize(v1);
addOverlays(graph, v1, false);
}
finally
{
// Updates the display
graph.getModel().endUpdate();
}
var content = document.createElement('div');
content.style.padding = '4px';
var tb = new mxToolbar(content);
tb.addItem('Zoom In', 'images/zoom_in32.png',function(evt)
{
graph.zoomIn();
});
tb.addItem('Zoom Out', 'images/zoom_out32.png',function(evt)
{
graph.zoomOut();
});
tb.addItem('Actual Size', 'images/view_1_132.png',function(evt)
{
graph.zoomActual();
});
tb.addItem('Print', 'images/print32.png',function(evt)
{
var preview = new mxPrintPreview(graph, 1);
preview.open();
});
tb.addItem('Poster Print', 'images/press32.png',function(evt)
{
var pageCount = mxUtils.prompt('Enter maximum page count', '1');
if (pageCount != null)
{
var scale = mxUtils.getScaleForPageCount(pageCount, graph);
var preview = new mxPrintPreview(graph, scale);
preview.open();
}
});
wnd = new mxWindow('Tools', content, 0, 0, 200, 66, false);
wnd.setMaximizable(false);
wnd.setScrollable(false);
wnd.setResizable(false);
wnd.setVisible(true);
}
};
// Function to create the entries in the popupmenu
function createPopupMenu(graph, menu, cell, evt)
{
var model = graph.getModel();
if (cell != null)
{
if (model.isVertex(cell))
{
menu.addItem('Add child', 'editors/images/overlays/check.png', function()
{
addChild(graph, cell);
});
}
menu.addItem('Edit label', 'editors/images/text.gif', function()
{
graph.startEditingAtCell(cell);
});
if (cell.id != 'treeRoot' &&
model.isVertex(cell))
{
menu.addItem('Delete', 'editors/images/delete.gif', function()
{
deleteSubtree(graph, cell);
});
}
menu.addSeparator();
}
menu.addItem('Fit', 'editors/images/zoom.gif', function()
{
graph.fit();
});
menu.addItem('Actual', 'editors/images/zoomactual.gif', function()
{
graph.zoomActual();
});
menu.addSeparator();
menu.addItem('Print', 'editors/images/print.gif', function()
{
var preview = new mxPrintPreview(graph, 1);
preview.open();
});
menu.addItem('Poster Print', 'editors/images/print.gif', function()
{
var pageCount = mxUtils.prompt('Enter maximum page count', '1');
if (pageCount != null)
{
var scale = mxUtils.getScaleForPageCount(pageCount, graph);
var preview = new mxPrintPreview(graph, scale);
preview.open();
}
});
};
function addOverlays(graph, cell, addDeleteIcon)
{
var overlay = new mxCellOverlay(new mxImage('images/add.png', 24, 24), 'Add child');
overlay.cursor = 'hand';
overlay.align = mxConstants.ALIGN_CENTER;
overlay.addListener(mxEvent.CLICK, mxUtils.bind(this, function(sender, evt)
{
addChild(graph, cell);
}));
graph.addCellOverlay(cell, overlay);
if (addDeleteIcon)
{
overlay = new mxCellOverlay(new mxImage('images/close.png', 30, 30), 'Delete');
overlay.cursor = 'hand';
overlay.offset = new mxPoint(-4, 8);
overlay.align = mxConstants.ALIGN_RIGHT;
overlay.verticalAlign = mxConstants.ALIGN_TOP;
overlay.addListener(mxEvent.CLICK, mxUtils.bind(this, function(sender, evt)
{
deleteSubtree(graph, cell);
}));
graph.addCellOverlay(cell, overlay);
}
};
function addChild(graph, cell)
{
var model = graph.getModel();
var parent = graph.getDefaultParent();
var vertex;
model.beginUpdate();
try
{
vertex = graph.insertVertex(parent, null, 'Double click to set name');
var geometry = model.getGeometry(vertex);
// Updates the geometry of the vertex with the
// preferred size computed in the graph
var size = graph.getPreferredSizeForCell(vertex);
geometry.width = size.width;
geometry.height = size.height;
// Adds the edge between the existing cell
// and the new vertex and executes the
// automatic layout on the parent
var edge = graph.insertEdge(parent, null, '', cell, vertex);
// Configures the edge label "in-place" to reside
// at the end of the edge (x = 1) and with an offset
// of 20 pixels in negative, vertical direction.
edge.geometry.x = 1;
edge.geometry.y = 0;
edge.geometry.offset = new mxPoint(0, -20);
addOverlays(graph, vertex, true);
}
finally
{
model.endUpdate();
}
return vertex;
};
function deleteSubtree(graph, cell)
{
// Gets the subtree from cell downwards
var cells = [];
graph.traverse(cell, true, function(vertex)
{
cells.push(vertex);
return true;
});
graph.removeCells(cells);
};
</script>
</head>
<!-- Calls the main function after the page has loaded. Container is dynamically created. -->
<body onload="main();">
<!-- Creates a container for the outline -->
<div id="outlineContainer"
style="z-index:1;position:absolute;overflow:hidden;top:0px;right:0px;width:160px;height:120px;background:transparent;border-style:solid;border-color:lightgray;">
</div>
</body>
</html>