2022-08-30 15:36:33 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021-present The maxGraph project Contributors
|
|
|
|
Copyright (c) 2006-2020, JGraph Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-09-07 09:07:27 +00:00
|
|
|
import {
|
|
|
|
Graph,
|
2022-01-08 01:49:35 +00:00
|
|
|
constants,
|
2021-09-07 09:07:27 +00:00
|
|
|
InternalEvent,
|
2022-01-08 01:49:35 +00:00
|
|
|
Client,
|
2021-09-07 09:07:27 +00:00
|
|
|
Point,
|
|
|
|
Outline,
|
|
|
|
EdgeStyle,
|
2022-01-08 01:49:35 +00:00
|
|
|
KeyHandler,
|
2021-09-07 09:07:27 +00:00
|
|
|
CompactTreeLayout,
|
|
|
|
LayoutManager,
|
|
|
|
CellOverlay,
|
|
|
|
ImageBox,
|
|
|
|
utils,
|
2022-01-08 01:49:35 +00:00
|
|
|
MaxToolbar,
|
2021-09-07 09:07:27 +00:00
|
|
|
} from '@maxgraph/core';
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
import { globalTypes } from '../.storybook/preview';
|
|
|
|
|
|
|
|
export default {
|
2021-04-25 03:39:40 +00:00
|
|
|
title: 'Layouts/OrgChart',
|
2021-04-24 12:30:27 +00:00
|
|
|
argTypes: {
|
|
|
|
...globalTypes,
|
|
|
|
contextMenu: {
|
|
|
|
type: 'boolean',
|
2021-08-30 14:20:26 +00:00
|
|
|
defaultValue: false,
|
|
|
|
},
|
|
|
|
},
|
2021-04-24 12:30:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const Template = ({ label, ...args }) => {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
|
|
|
|
const container = document.createElement('div');
|
|
|
|
container.style.position = 'relative';
|
|
|
|
container.style.overflow = 'hidden';
|
|
|
|
container.style.width = `${args.width}px`;
|
|
|
|
container.style.height = `${args.height}px`;
|
|
|
|
container.style.background = 'url(/images/grid.gif)';
|
|
|
|
container.style.cursor = 'default';
|
|
|
|
div.appendChild(container);
|
|
|
|
|
2021-09-10 05:17:59 +00:00
|
|
|
// Should we allow overriding constants?
|
2021-04-24 12:30:27 +00:00
|
|
|
// Makes the shadow brighter
|
2022-01-08 01:49:35 +00:00
|
|
|
//constants.SHADOWCOLOR = '#C0C0C0';
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
const outline = document.getElementById('outlineContainer');
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
if (!args.contextMenu) InternalEvent.disableContextMenu(container);
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
// Sets a gradient background
|
2022-01-08 01:49:35 +00:00
|
|
|
if (Client.IS_GC || Client.IS_SF) {
|
2021-04-24 12:30:27 +00:00
|
|
|
container.style.background =
|
|
|
|
'-webkit-gradient(linear, 0% 0%, 0% 100%, from(#FFFFFF), to(#E7E7E7))';
|
2022-01-08 01:49:35 +00:00
|
|
|
} else if (Client.IS_NS) {
|
2021-08-30 14:20:26 +00:00
|
|
|
container.style.background = '-moz-linear-gradient(top, #FFFFFF, #E7E7E7)';
|
2021-04-24 12:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates the graph inside the given container
|
2021-08-30 14:20:26 +00:00
|
|
|
const graph = new Graph(container);
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
// 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;
|
2021-09-10 05:17:59 +00:00
|
|
|
|
|
|
|
const panningHandler = graph.getPlugin('PanningHandler');
|
|
|
|
|
|
|
|
panningHandler.useLeftButtonForPanning = true;
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
// 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
|
2021-09-10 05:17:59 +00:00
|
|
|
panningHandler.popupMenuHandler = false;
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
// Creates the outline (navigator, overview) for moving
|
|
|
|
// around the graph in the top, right corner of the window.
|
2021-08-30 14:20:26 +00:00
|
|
|
const outln = new Outline(graph, outline);
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
// Disables tooltips on touch devices
|
2022-01-08 01:49:35 +00:00
|
|
|
graph.setTooltips(!Client.IS_TOUCH);
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
// Set some stylesheet options for the visual appearance of vertices
|
|
|
|
let style = graph.getStylesheet().getDefaultVertexStyle();
|
2021-05-02 06:04:34 +00:00
|
|
|
style.shape = 'label';
|
2021-04-24 12:30:27 +00:00
|
|
|
|
2022-01-08 01:49:35 +00:00
|
|
|
style.verticalAlign = constants.ALIGN.MIDDLE;
|
|
|
|
style.align = constants.ALIGN.LEFT;
|
2021-05-02 06:04:34 +00:00
|
|
|
style.spacingLeft = 54;
|
2021-04-24 12:30:27 +00:00
|
|
|
|
2021-05-02 06:04:34 +00:00
|
|
|
style.gradientColor = '#7d85df';
|
|
|
|
style.strokeColor = '#5d65df';
|
|
|
|
style.fillColor = '#adc5ff';
|
2021-04-24 12:30:27 +00:00
|
|
|
|
2021-05-02 13:56:17 +00:00
|
|
|
style.fontColor = '#1d258f';
|
2021-05-02 06:04:34 +00:00
|
|
|
style.fontFamily = 'Verdana';
|
|
|
|
style.fontSize = '12';
|
|
|
|
style.fontStyle = '1';
|
2021-04-24 12:30:27 +00:00
|
|
|
|
2021-05-02 06:04:34 +00:00
|
|
|
style.shadow = '1';
|
|
|
|
style.rounded = '1';
|
|
|
|
style.glass = '1';
|
2021-04-24 12:30:27 +00:00
|
|
|
|
2021-05-02 06:04:34 +00:00
|
|
|
style.image = '/images/dude3.png';
|
|
|
|
style.imageWidth = '48';
|
|
|
|
style.imageHeight = '48';
|
|
|
|
style.spacing = 8;
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
// Sets the default style for edges
|
|
|
|
style = graph.getStylesheet().getDefaultEdgeStyle();
|
2021-05-02 06:04:34 +00:00
|
|
|
style.rounded = true;
|
|
|
|
style.strokeWidth = 3;
|
|
|
|
style.exitX = 0.5; // center
|
|
|
|
style.exitY = 1.0; // bottom
|
|
|
|
style.exitPerimeter = 0; // disabled
|
|
|
|
style.entryX = 0.5; // center
|
|
|
|
style.entryY = 0; // top
|
|
|
|
style.entryPerimeter = 0; // disabled
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
// Disable the following for straight lines
|
2021-08-30 14:20:26 +00:00
|
|
|
style.edge = EdgeStyle.TopToBottom;
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
// Stops editing on enter or escape keypress
|
2022-01-08 01:49:35 +00:00
|
|
|
const keyHandler = new KeyHandler(graph);
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
// Enables automatic layout on the graph and installs
|
|
|
|
// a tree layout for all groups who's children are
|
|
|
|
// being changed, added or removed.
|
2021-08-30 14:20:26 +00:00
|
|
|
const layout = new CompactTreeLayout(graph, false);
|
2021-04-24 12:30:27 +00:00
|
|
|
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
|
2021-08-30 14:20:26 +00:00
|
|
|
layout.isVertexMovable = function (cell) {
|
2021-04-24 12:30:27 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
const layoutMgr = new LayoutManager(graph);
|
2021-04-24 12:30:27 +00:00
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
layoutMgr.getLayout = function (cell) {
|
2021-04-24 12:30:27 +00:00
|
|
|
if (cell.getChildCount() > 0) {
|
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-10 05:17:59 +00:00
|
|
|
const popupMenuHandler = graph.getPlugin('PopupMenuHandler');
|
|
|
|
|
2021-04-24 12:30:27 +00:00
|
|
|
// Installs a popupmenu handler using local function (see below).
|
2021-09-10 05:17:59 +00:00
|
|
|
popupMenuHandler.factoryMethod = function (menu, cell, evt) {
|
2021-04-24 12:30:27 +00:00
|
|
|
return createPopupMenu(graph, menu, cell, evt);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Fix for wrong preferred size
|
|
|
|
const oldGetPreferredSizeForCell = graph.getPreferredSizeForCell;
|
2021-08-30 14:20:26 +00:00
|
|
|
graph.getPreferredSizeForCell = function (cell) {
|
2021-04-24 12:30:27 +00:00
|
|
|
const 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
|
2021-08-30 14:20:26 +00:00
|
|
|
graph.cellRenderer.getTextScale = function (state) {
|
2021-04-24 12:30:27 +00:00
|
|
|
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)
|
2021-08-30 14:20:26 +00:00
|
|
|
graph.cellRenderer.getLabelValue = function (state) {
|
2021-04-24 12:30:27 +00:00
|
|
|
let result = state.cell.value;
|
|
|
|
|
2021-04-25 03:39:40 +00:00
|
|
|
if (state.cell.isVertex()) {
|
2021-04-24 12:30:27 +00:00
|
|
|
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).
|
|
|
|
const parent = graph.getDefaultParent();
|
|
|
|
|
|
|
|
// Adds the root vertex of the tree
|
2022-01-08 01:49:35 +00:00
|
|
|
graph.batchUpdate(() => {
|
2021-04-24 12:30:27 +00:00
|
|
|
const w = graph.container.offsetWidth;
|
|
|
|
const v1 = graph.insertVertex(
|
|
|
|
parent,
|
|
|
|
'treeRoot',
|
|
|
|
'Organization',
|
|
|
|
w / 2 - 30,
|
|
|
|
20,
|
|
|
|
140,
|
|
|
|
60,
|
2022-04-17 06:58:35 +00:00
|
|
|
{ image: '/images/house.png' }
|
2021-04-24 12:30:27 +00:00
|
|
|
);
|
|
|
|
graph.updateCellSize(v1);
|
|
|
|
addOverlays(graph, v1, false);
|
2022-01-08 01:49:35 +00:00
|
|
|
});
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
const content = document.createElement('div');
|
|
|
|
content.style.padding = '4px';
|
|
|
|
div.appendChild(content);
|
2022-01-08 01:49:35 +00:00
|
|
|
const tb = new MaxToolbar(content);
|
2021-04-24 12:30:27 +00:00
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
tb.addItem('Zoom In', 'images/zoom_in32.png', function (evt) {
|
2021-04-24 12:30:27 +00:00
|
|
|
graph.zoomIn();
|
|
|
|
});
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
tb.addItem('Zoom Out', 'images/zoom_out32.png', function (evt) {
|
2021-04-24 12:30:27 +00:00
|
|
|
graph.zoomOut();
|
|
|
|
});
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
tb.addItem('Actual Size', 'images/view_1_132.png', function (evt) {
|
2021-04-24 12:30:27 +00:00
|
|
|
graph.zoomActual();
|
|
|
|
});
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
tb.addItem('Print', 'images/print32.png', function (evt) {
|
2021-06-06 13:04:44 +00:00
|
|
|
const preview = new PrintPreview(graph, 1);
|
2021-04-24 12:30:27 +00:00
|
|
|
preview.open();
|
|
|
|
});
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
tb.addItem('Poster Print', 'images/press32.png', function (evt) {
|
|
|
|
const pageCount = utils.prompt('Enter maximum page count', '1');
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
if (pageCount != null) {
|
2021-08-30 14:20:26 +00:00
|
|
|
const scale = utils.getScaleForPageCount(pageCount, graph);
|
2021-06-06 13:04:44 +00:00
|
|
|
const preview = new PrintPreview(graph, scale);
|
2021-04-24 12:30:27 +00:00
|
|
|
preview.open();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Function to create the entries in the popupmenu
|
|
|
|
function createPopupMenu(graph, menu, cell, evt) {
|
2022-01-08 01:49:35 +00:00
|
|
|
const model = graph.getDataModel();
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
if (cell != null) {
|
2021-04-25 03:39:40 +00:00
|
|
|
if (cell.isVertex()) {
|
2021-08-30 14:20:26 +00:00
|
|
|
menu.addItem('Add child', '/images/overlays/check.png', function () {
|
|
|
|
addChild(graph, cell);
|
|
|
|
});
|
2021-04-24 12:30:27 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
menu.addItem('Edit label', '/images/text.gif', function () {
|
2021-04-24 12:30:27 +00:00
|
|
|
graph.startEditingAtCell(cell);
|
|
|
|
});
|
|
|
|
|
2021-04-25 03:39:40 +00:00
|
|
|
if (cell.id != 'treeRoot' && cell.isVertex()) {
|
2021-08-30 14:20:26 +00:00
|
|
|
menu.addItem('Delete', '/images/delete.gif', function () {
|
2021-04-24 12:30:27 +00:00
|
|
|
deleteSubtree(graph, cell);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
menu.addSeparator();
|
|
|
|
}
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
menu.addItem('Fit', '/images/zoom.gif', function () {
|
2021-04-24 12:30:27 +00:00
|
|
|
graph.fit();
|
|
|
|
});
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
menu.addItem('Actual', '/images/zoomactual.gif', function () {
|
2021-04-24 12:30:27 +00:00
|
|
|
graph.zoomActual();
|
|
|
|
});
|
|
|
|
|
|
|
|
menu.addSeparator();
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
menu.addItem('Print', '/images/print.gif', function () {
|
2021-06-06 13:04:44 +00:00
|
|
|
const preview = new PrintPreview(graph, 1);
|
2021-04-24 12:30:27 +00:00
|
|
|
preview.open();
|
|
|
|
});
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
menu.addItem('Poster Print', '/images/print.gif', function () {
|
|
|
|
const pageCount = utils.prompt('Enter maximum page count', '1');
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
if (pageCount != null) {
|
2021-08-30 14:20:26 +00:00
|
|
|
const scale = utils.getScaleForPageCount(pageCount, graph);
|
2021-06-06 13:04:44 +00:00
|
|
|
const preview = new PrintPreview(graph, scale);
|
2021-04-24 12:30:27 +00:00
|
|
|
preview.open();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addOverlays(graph, cell, addDeleteIcon) {
|
2021-08-30 14:20:26 +00:00
|
|
|
let overlay = new CellOverlay(new ImageBox('images/add.png', 24, 24), 'Add child');
|
2021-04-24 12:30:27 +00:00
|
|
|
overlay.cursor = 'hand';
|
2022-01-08 01:49:35 +00:00
|
|
|
overlay.align = constants.ALIGN.CENTER;
|
2021-08-30 14:20:26 +00:00
|
|
|
overlay.addListener(InternalEvent.CLICK, (sender, evt) => {
|
|
|
|
addChild(graph, cell);
|
|
|
|
});
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
graph.addCellOverlay(cell, overlay);
|
|
|
|
|
|
|
|
if (addDeleteIcon) {
|
2021-08-30 14:20:26 +00:00
|
|
|
overlay = new CellOverlay(new ImageBox('images/close.png', 30, 30), 'Delete');
|
2021-04-24 12:30:27 +00:00
|
|
|
overlay.cursor = 'hand';
|
2021-08-30 14:20:26 +00:00
|
|
|
overlay.offset = new Point(-4, 8);
|
2022-01-08 01:49:35 +00:00
|
|
|
overlay.align = constants.ALIGN.RIGHT;
|
|
|
|
overlay.verticalAlign = constants.ALIGN.TOP;
|
2021-08-30 14:20:26 +00:00
|
|
|
overlay.addListener(InternalEvent.CLICK, (sender, evt) => {
|
|
|
|
deleteSubtree(graph, cell);
|
|
|
|
});
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
graph.addCellOverlay(cell, overlay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addChild(graph, cell) {
|
2022-01-08 01:49:35 +00:00
|
|
|
const model = graph.getDataModel();
|
2021-04-24 12:30:27 +00:00
|
|
|
const parent = graph.getDefaultParent();
|
|
|
|
let vertex;
|
|
|
|
|
|
|
|
model.beginUpdate();
|
|
|
|
try {
|
|
|
|
vertex = graph.insertVertex(parent, null, 'Double click to set name');
|
2021-04-25 03:39:40 +00:00
|
|
|
const geometry = vertex.getGeometry();
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
// Updates the geometry of the vertex with the
|
|
|
|
// preferred size computed in the graph
|
|
|
|
const 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
|
|
|
|
const 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;
|
2021-08-30 14:20:26 +00:00
|
|
|
edge.geometry.offset = new Point(0, -20);
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
addOverlays(graph, vertex, true);
|
|
|
|
} finally {
|
|
|
|
model.endUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
return vertex;
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteSubtree(graph, cell) {
|
|
|
|
// Gets the subtree from cell downwards
|
|
|
|
const cells = [];
|
2021-08-30 14:20:26 +00:00
|
|
|
graph.traverse(cell, true, function (vertex) {
|
2021-04-24 12:30:27 +00:00
|
|
|
cells.push(vertex);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
graph.removeCells(cells);
|
|
|
|
}
|
|
|
|
|
|
|
|
return div;
|
2021-08-30 14:20:26 +00:00
|
|
|
};
|
2021-04-24 12:30:27 +00:00
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
export const Default = Template.bind({});
|