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,
|
|
|
|
ConnectionHandler,
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers,
|
2021-09-07 09:07:27 +00:00
|
|
|
EdgeHandler,
|
|
|
|
InternalEvent,
|
|
|
|
Point,
|
|
|
|
CellHighlight,
|
2022-01-08 01:49:35 +00:00
|
|
|
constants,
|
2021-09-07 09:07:27 +00:00
|
|
|
VertexHandler,
|
2022-01-08 01:49:35 +00:00
|
|
|
RubberBandHandler,
|
2021-09-07 09:07:27 +00:00
|
|
|
Shape,
|
|
|
|
StencilShape,
|
2021-09-07 12:21:22 +00:00
|
|
|
StencilShapeRegistry,
|
2021-09-07 09:07:27 +00:00
|
|
|
CellRenderer,
|
|
|
|
utils,
|
2022-01-08 01:49:35 +00:00
|
|
|
load,
|
2021-09-07 09:07:27 +00:00
|
|
|
} from '@maxgraph/core';
|
2023-10-12 21:41:37 +00:00
|
|
|
import {
|
|
|
|
contextMenuTypes,
|
|
|
|
contextMenuValues,
|
|
|
|
globalTypes,
|
|
|
|
globalValues,
|
|
|
|
rubberBandTypes,
|
|
|
|
rubberBandValues,
|
|
|
|
} from './shared/args.js';
|
|
|
|
// style required by RubberBand
|
|
|
|
import '@maxgraph/core/css/common.css';
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
title: 'Shapes/Stencils',
|
|
|
|
argTypes: {
|
2023-10-12 21:41:37 +00:00
|
|
|
...contextMenuTypes,
|
2021-04-25 03:39:40 +00:00
|
|
|
...globalTypes,
|
2023-10-12 21:41:37 +00:00
|
|
|
...rubberBandTypes,
|
|
|
|
},
|
|
|
|
args: {
|
|
|
|
...contextMenuValues,
|
|
|
|
...globalValues,
|
|
|
|
...rubberBandValues,
|
2021-08-30 14:20:26 +00:00
|
|
|
},
|
2021-04-25 03:39:40 +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
|
|
|
// Allow overriding constants?
|
2021-04-25 03:39:40 +00:00
|
|
|
// Sets the global shadow color
|
2022-01-08 01:49:35 +00:00
|
|
|
// constants.SHADOWCOLOR = '#C0C0C0';
|
|
|
|
// constants.SHADOW_OPACITY = 0.5;
|
|
|
|
// constants.SHADOW_OFFSET_X = 4;
|
|
|
|
// constants.SHADOW_OFFSET_Y = 4;
|
|
|
|
// constants.HANDLE_FILLCOLOR = '#99ccff';
|
|
|
|
// constants.HANDLE_STROKECOLOR = '#0088cf';
|
|
|
|
// constants.VERTEX_SELECTION_COLOR = '#00a8ff';
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Enables connections along the outline
|
2021-08-30 14:20:26 +00:00
|
|
|
ConnectionHandler.prototype.outlineConnect = true;
|
|
|
|
EdgeHandler.prototype.manageLabelHandle = true;
|
|
|
|
EdgeHandler.prototype.outlineConnect = true;
|
|
|
|
CellHighlight.prototype.keepOnTop = true;
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Enable rotation handle
|
2021-08-30 14:20:26 +00:00
|
|
|
VertexHandler.prototype.rotationEnabled = true;
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Uses the shape for resize previews
|
2021-08-30 14:20:26 +00:00
|
|
|
VertexHandler.prototype.createSelectionShape = function (bounds) {
|
2021-05-02 06:04:34 +00:00
|
|
|
const key = this.state.style.shape;
|
2021-09-07 12:21:22 +00:00
|
|
|
const stencil = StencilShapeRegistry.getStencil(key);
|
2021-04-25 03:39:40 +00:00
|
|
|
let shape = null;
|
|
|
|
|
|
|
|
if (stencil != null) {
|
2021-08-30 14:20:26 +00:00
|
|
|
shape = new Shape(stencil);
|
2021-04-25 03:39:40 +00:00
|
|
|
shape.apply(this.state);
|
|
|
|
} else {
|
|
|
|
shape = new this.state.shape.constructor();
|
|
|
|
}
|
|
|
|
|
|
|
|
shape.outline = true;
|
|
|
|
shape.bounds = bounds;
|
2022-01-08 01:49:35 +00:00
|
|
|
shape.stroke = constants.HANDLE_STROKECOLOR;
|
2021-04-25 03:39:40 +00:00
|
|
|
shape.strokewidth = this.getSelectionStrokeWidth();
|
|
|
|
shape.isDashed = this.isSelectionDashed();
|
|
|
|
shape.isShadow = false;
|
|
|
|
return shape;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Defines a custom stencil via the canvas API as defined here:
|
|
|
|
// http://jgraph.github.io/mxgraph/docs/js-api/files/util/mxXmlCanvas2D-js.html
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
class CustomShape extends Shape {
|
2021-04-25 03:39:40 +00:00
|
|
|
paintBackground(c, x, y, w, h) {
|
|
|
|
c.translate(x, y);
|
|
|
|
|
|
|
|
// Head
|
|
|
|
c.ellipse(w / 4, 0, w / 2, h / 4);
|
|
|
|
c.fillAndStroke();
|
|
|
|
|
|
|
|
c.begin();
|
|
|
|
c.moveTo(w / 2, h / 4);
|
|
|
|
c.lineTo(w / 2, (2 * h) / 3);
|
|
|
|
|
|
|
|
// Arms
|
|
|
|
c.moveTo(w / 2, h / 3);
|
|
|
|
c.lineTo(0, h / 3);
|
|
|
|
c.moveTo(w / 2, h / 3);
|
|
|
|
c.lineTo(w, h / 3);
|
|
|
|
|
|
|
|
// Legs
|
|
|
|
c.moveTo(w / 2, (2 * h) / 3);
|
|
|
|
c.lineTo(0, h);
|
|
|
|
c.moveTo(w / 2, (2 * h) / 3);
|
|
|
|
c.lineTo(w, h);
|
|
|
|
c.end();
|
|
|
|
|
|
|
|
c.stroke();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replaces existing actor shape
|
2021-08-30 14:20:26 +00:00
|
|
|
CellRenderer.registerShape('customShape', CustomShape);
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Loads the stencils into the registry
|
2021-04-25 10:47:53 +00:00
|
|
|
const req = load('stencils.xml');
|
2021-04-25 03:39:40 +00:00
|
|
|
const root = req.getDocumentElement();
|
|
|
|
let shape = root.firstChild;
|
|
|
|
|
|
|
|
while (shape != null) {
|
2022-01-08 01:49:35 +00:00
|
|
|
if (shape.nodeType === constants.NODETYPE.ELEMENT) {
|
2021-09-07 12:21:22 +00:00
|
|
|
StencilShapeRegistry.addStencil(
|
|
|
|
shape.getAttribute('name'),
|
|
|
|
new StencilShape(shape)
|
|
|
|
);
|
2021-04-25 03:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shape = shape.nextSibling;
|
|
|
|
}
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
if (!args.contextMenu) InternalEvent.disableContextMenu(container);
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Creates the graph inside the given container
|
2021-08-30 14:20:26 +00:00
|
|
|
const graph = new Graph(container);
|
2021-04-25 03:39:40 +00:00
|
|
|
graph.setConnectable(true);
|
|
|
|
graph.setTooltips(true);
|
|
|
|
graph.setPanning(true);
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
graph.getTooltipForCell = function (cell) {
|
2021-04-25 03:39:40 +00:00
|
|
|
if (cell != null) {
|
|
|
|
return cell.style;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Changes default styles
|
|
|
|
let style = graph.getStylesheet().getDefaultEdgeStyle();
|
2021-05-02 06:04:34 +00:00
|
|
|
style.edge = 'orthogonalEdgeStyle';
|
2021-04-25 03:39:40 +00:00
|
|
|
style = graph.getStylesheet().getDefaultVertexStyle();
|
2021-05-02 06:04:34 +00:00
|
|
|
style.fillColor = '#adc5ff';
|
|
|
|
style.gradientColor = '#7d85df';
|
|
|
|
style.shadow = '1';
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Enables rubberband selection
|
2022-01-08 01:49:35 +00:00
|
|
|
if (args.rubberBand) new RubberBandHandler(graph);
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// 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 cells to the model in a single step
|
2022-01-08 01:49:35 +00:00
|
|
|
graph.batchUpdate(() => {
|
2022-04-17 06:58:35 +00:00
|
|
|
const v1 = graph.insertVertex(parent, null, 'A1', 20, 20, 40, 80, { shape: 'and' });
|
|
|
|
const v2 = graph.insertVertex(parent, null, 'A2', 20, 220, 40, 80, { shape: 'and' });
|
|
|
|
const v3 = graph.insertVertex(parent, null, 'X1', 160, 110, 80, 80, { shape: 'xor' });
|
2021-04-25 03:39:40 +00:00
|
|
|
const e1 = graph.insertEdge(parent, null, '', v1, v3);
|
2021-08-30 14:20:26 +00:00
|
|
|
e1.geometry.points = [new Point(90, 60), new Point(90, 130)];
|
2021-04-25 03:39:40 +00:00
|
|
|
const e2 = graph.insertEdge(parent, null, '', v2, v3);
|
2021-08-30 14:20:26 +00:00
|
|
|
e2.geometry.points = [new Point(90, 260), new Point(90, 170)];
|
2021-04-25 03:39:40 +00:00
|
|
|
|
2022-04-17 06:58:35 +00:00
|
|
|
const v4 = graph.insertVertex(parent, null, 'A3', 520, 20, 40, 80, {
|
|
|
|
shape: 'customShape',
|
|
|
|
flipH: true,
|
|
|
|
});
|
|
|
|
const v5 = graph.insertVertex(parent, null, 'A4', 520, 220, 40, 80, {
|
|
|
|
shape: 'and',
|
|
|
|
flipH: true,
|
|
|
|
});
|
|
|
|
const v6 = graph.insertVertex(parent, null, 'X2', 340, 110, 80, 80, {
|
|
|
|
shape: 'xor',
|
|
|
|
flipH: true,
|
|
|
|
});
|
2021-04-25 03:39:40 +00:00
|
|
|
const e3 = graph.insertEdge(parent, null, '', v4, v6);
|
2021-08-30 14:20:26 +00:00
|
|
|
e3.geometry.points = [new Point(490, 60), new Point(130, 130)];
|
2021-04-25 03:39:40 +00:00
|
|
|
const e4 = graph.insertEdge(parent, null, '', v5, v6);
|
2021-08-30 14:20:26 +00:00
|
|
|
e4.geometry.points = [new Point(490, 260), new Point(130, 170)];
|
2021-04-25 03:39:40 +00:00
|
|
|
|
2022-04-17 06:58:35 +00:00
|
|
|
const v7 = graph.insertVertex(parent, null, 'O1', 250, 260, 80, 60, {
|
|
|
|
shape: 'or',
|
|
|
|
direction: 'south',
|
|
|
|
});
|
2021-04-25 03:39:40 +00:00
|
|
|
const e5 = graph.insertEdge(parent, null, '', v6, v7);
|
2021-08-30 14:20:26 +00:00
|
|
|
e5.geometry.points = [new Point(310, 150)];
|
2021-04-25 03:39:40 +00:00
|
|
|
const e6 = graph.insertEdge(parent, null, '', v3, v7);
|
2021-08-30 14:20:26 +00:00
|
|
|
e6.geometry.points = [new Point(270, 150)];
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
const e7 = graph.insertEdge(parent, null, '', v7, v5);
|
2021-08-30 14:20:26 +00:00
|
|
|
e7.geometry.points = [new Point(290, 370)];
|
2022-01-08 01:49:35 +00:00
|
|
|
});
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
const buttons = document.createElement('div');
|
|
|
|
div.appendChild(buttons);
|
|
|
|
|
|
|
|
buttons.appendChild(
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers.button('FlipH', function () {
|
2021-05-02 06:04:34 +00:00
|
|
|
graph.toggleCellStyles('flipH');
|
2021-04-25 03:39:40 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
buttons.appendChild(
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers.button('FlipV', function () {
|
2021-05-02 06:04:34 +00:00
|
|
|
graph.toggleCellStyles('flipV');
|
2021-04-25 03:39:40 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
buttons.appendChild(document.createTextNode('\u00a0'));
|
|
|
|
buttons.appendChild(document.createTextNode('\u00a0'));
|
|
|
|
buttons.appendChild(document.createTextNode('\u00a0'));
|
|
|
|
buttons.appendChild(document.createTextNode('\u00a0'));
|
|
|
|
|
|
|
|
buttons.appendChild(
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers.button('Rotate', function () {
|
2021-04-25 03:39:40 +00:00
|
|
|
const cell = graph.getSelectionCell();
|
|
|
|
|
|
|
|
if (cell != null) {
|
2022-01-08 01:49:35 +00:00
|
|
|
let geo = cell.getGeometry();
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
if (geo != null) {
|
2022-01-08 01:49:35 +00:00
|
|
|
graph.batchUpdate(() => {
|
2021-04-25 03:39:40 +00:00
|
|
|
// Rotates the size and position in the geometry
|
|
|
|
geo = geo.clone();
|
|
|
|
geo.x += geo.width / 2 - geo.height / 2;
|
|
|
|
geo.y += geo.height / 2 - geo.width / 2;
|
|
|
|
const tmp = geo.width;
|
|
|
|
geo.width = geo.height;
|
|
|
|
geo.height = tmp;
|
2022-01-08 01:49:35 +00:00
|
|
|
graph.getDataModel().setGeometry(cell, geo);
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Reads the current direction and advances by 90 degrees
|
|
|
|
const state = graph.view.getState(cell);
|
|
|
|
|
|
|
|
if (state != null) {
|
2021-08-30 14:20:26 +00:00
|
|
|
let dir = state.style.direction || 'east'; /* default */
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
if (dir === 'east') {
|
|
|
|
dir = 'south';
|
|
|
|
} else if (dir === 'south') {
|
|
|
|
dir = 'west';
|
|
|
|
} else if (dir === 'west') {
|
|
|
|
dir = 'north';
|
|
|
|
} else if (dir === 'north') {
|
|
|
|
dir = 'east';
|
|
|
|
}
|
|
|
|
|
2021-05-02 06:04:34 +00:00
|
|
|
graph.setCellStyles('direction', dir, [cell]);
|
2021-04-25 03:39:40 +00:00
|
|
|
}
|
2022-01-08 01:49:35 +00:00
|
|
|
});
|
2021-04-25 03:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
buttons.appendChild(document.createTextNode('\u00a0'));
|
|
|
|
buttons.appendChild(document.createTextNode('\u00a0'));
|
|
|
|
buttons.appendChild(document.createTextNode('\u00a0'));
|
|
|
|
buttons.appendChild(document.createTextNode('\u00a0'));
|
|
|
|
|
|
|
|
buttons.appendChild(
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers.button('And', function () {
|
2021-05-02 06:04:34 +00:00
|
|
|
graph.setCellStyles('shape', 'and');
|
2021-04-25 03:39:40 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
buttons.appendChild(
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers.button('Or', function () {
|
2021-05-02 06:04:34 +00:00
|
|
|
graph.setCellStyles('shape', 'or');
|
2021-04-25 03:39:40 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
buttons.appendChild(
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers.button('Xor', function () {
|
2021-05-02 06:04:34 +00:00
|
|
|
graph.setCellStyles('shape', 'xor');
|
2021-04-25 03:39:40 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
buttons.appendChild(document.createTextNode('\u00a0'));
|
|
|
|
buttons.appendChild(document.createTextNode('\u00a0'));
|
|
|
|
buttons.appendChild(document.createTextNode('\u00a0'));
|
|
|
|
buttons.appendChild(document.createTextNode('\u00a0'));
|
|
|
|
|
|
|
|
buttons.appendChild(
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers.button('Style', function () {
|
2021-04-25 03:39:40 +00:00
|
|
|
const cell = graph.getSelectionCell();
|
|
|
|
|
|
|
|
if (cell != null) {
|
2021-08-30 14:20:26 +00:00
|
|
|
const style = utils.prompt('Style', cell.getStyle());
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
if (style != null) {
|
2022-01-08 01:49:35 +00:00
|
|
|
graph.getDataModel().setStyle(cell, style);
|
2021-04-25 03:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
buttons.appendChild(
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers.button('+', function () {
|
2021-04-25 03:39:40 +00:00
|
|
|
graph.zoomIn();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
buttons.appendChild(
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers.button('-', function () {
|
2021-04-25 03:39:40 +00:00
|
|
|
graph.zoomOut();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return div;
|
2021-08-30 14:20:26 +00:00
|
|
|
};
|
2021-04-25 03:39:40 +00:00
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
export const Default = Template.bind({});
|