2021-09-07 09:07:27 +00:00
|
|
|
import {
|
|
|
|
Graph,
|
2022-01-08 01:49:35 +00:00
|
|
|
RubberBandHandler,
|
2021-09-07 09:07:27 +00:00
|
|
|
ConnectionHandler,
|
|
|
|
ImageBox,
|
2022-01-08 01:49:35 +00:00
|
|
|
MaxToolbar,
|
|
|
|
GraphDataModel,
|
|
|
|
KeyHandler,
|
2021-09-07 09:07:27 +00:00
|
|
|
Cell,
|
|
|
|
Geometry,
|
|
|
|
InternalEvent,
|
|
|
|
utils,
|
2022-01-08 01:49:35 +00:00
|
|
|
styleUtils,
|
|
|
|
gestureUtils,
|
2021-09-07 09:07:27 +00:00
|
|
|
} from '@maxgraph/core';
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
import { globalTypes } from '../.storybook/preview';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
title: 'Toolbars/DynamicToolbar',
|
|
|
|
argTypes: {
|
|
|
|
...globalTypes,
|
|
|
|
rubberBand: {
|
|
|
|
type: 'boolean',
|
2021-08-30 14:20:26 +00:00
|
|
|
defaultValue: true,
|
|
|
|
},
|
|
|
|
},
|
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);
|
|
|
|
|
|
|
|
// Defines an icon for creating new connections in the connection handler.
|
|
|
|
// This will automatically disable the highlighting of the source vertex.
|
2021-08-30 14:20:26 +00:00
|
|
|
ConnectionHandler.prototype.connectImage = new ImageBox(
|
2021-04-25 03:39:40 +00:00
|
|
|
'/images/connector.gif',
|
|
|
|
16,
|
|
|
|
16
|
|
|
|
);
|
|
|
|
|
|
|
|
// Creates the div for the toolbar
|
|
|
|
const tbContainer = document.createElement('div');
|
|
|
|
tbContainer.style.position = 'absolute';
|
|
|
|
tbContainer.style.overflow = 'hidden';
|
|
|
|
tbContainer.style.padding = '2px';
|
|
|
|
tbContainer.style.left = '0px';
|
|
|
|
tbContainer.style.top = '0px';
|
|
|
|
tbContainer.style.width = '24px';
|
|
|
|
tbContainer.style.bottom = '0px';
|
|
|
|
|
|
|
|
div.appendChild(tbContainer);
|
|
|
|
|
|
|
|
// Creates new toolbar without event processing
|
2022-01-08 01:49:35 +00:00
|
|
|
const toolbar = new MaxToolbar(tbContainer);
|
2021-04-25 03:39:40 +00:00
|
|
|
toolbar.enabled = false;
|
|
|
|
|
|
|
|
// Creates the model and the graph inside the container
|
|
|
|
// using the fastest rendering available on the browser
|
2022-01-08 01:49:35 +00:00
|
|
|
const model = new GraphDataModel();
|
2021-08-30 14:20:26 +00:00
|
|
|
const graph = new Graph(container, model);
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Enables new connections in the graph
|
|
|
|
graph.setConnectable(true);
|
|
|
|
graph.setMultigraph(false);
|
|
|
|
|
|
|
|
// Stops editing on enter or escape keypress
|
2022-01-08 01:49:35 +00:00
|
|
|
const keyHandler = new KeyHandler(graph);
|
2021-04-25 03:39:40 +00:00
|
|
|
|
2022-01-08 01:49:35 +00:00
|
|
|
if (args.rubberBand) new RubberBandHandler(graph);
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
addVertex('/images/rectangle.gif', 100, 40, '');
|
|
|
|
addVertex('/images/rounded.gif', 100, 40, 'shape=rounded');
|
|
|
|
addVertex('/images/ellipse.gif', 40, 40, 'shape=ellipse');
|
|
|
|
addVertex('/images/rhombus.gif', 40, 40, 'shape=rhombus');
|
|
|
|
addVertex('/images/triangle.gif', 40, 40, 'shape=triangle');
|
|
|
|
addVertex('/images/cylinder.gif', 40, 40, 'shape=cylinder');
|
|
|
|
addVertex('/images/actor.gif', 30, 40, 'shape=actor');
|
|
|
|
|
|
|
|
function addVertex(icon, w, h, style) {
|
2021-08-30 14:20:26 +00:00
|
|
|
const vertex = new Cell(null, new Geometry(0, 0, w, h), style);
|
2021-04-25 03:39:40 +00:00
|
|
|
vertex.setVertex(true);
|
2021-08-30 14:20:26 +00:00
|
|
|
|
2021-04-25 03:39:40 +00:00
|
|
|
const img = addToolbarItem(graph, toolbar, vertex, icon);
|
|
|
|
img.enabled = true;
|
2021-08-30 14:20:26 +00:00
|
|
|
|
|
|
|
graph.getSelectionModel().addListener(InternalEvent.CHANGE, () => {
|
2021-04-25 03:39:40 +00:00
|
|
|
const tmp = graph.isSelectionEmpty();
|
2022-01-08 01:49:35 +00:00
|
|
|
styleUtils.setOpacity(img, tmp ? 100 : 20);
|
2021-04-25 03:39:40 +00:00
|
|
|
img.enabled = tmp;
|
|
|
|
});
|
|
|
|
}
|
2021-08-30 14:20:26 +00:00
|
|
|
|
2021-04-25 03:39:40 +00:00
|
|
|
function addToolbarItem(graph, toolbar, prototype, image) {
|
|
|
|
// Function that is executed when the image is dropped on
|
|
|
|
// the graph. The cell argument points to the cell under
|
|
|
|
// the mousepointer if there is one.
|
|
|
|
const funct = (graph, evt, cell, x, y) => {
|
|
|
|
graph.stopEditing(false);
|
2021-08-30 14:20:26 +00:00
|
|
|
|
2022-01-08 01:49:35 +00:00
|
|
|
const vertex = graph.getDataModel().cloneCell(prototype);
|
2021-04-25 03:39:40 +00:00
|
|
|
vertex.geometry.x = x;
|
|
|
|
vertex.geometry.y = y;
|
2021-08-30 14:20:26 +00:00
|
|
|
|
2021-04-25 03:39:40 +00:00
|
|
|
graph.addCell(vertex);
|
|
|
|
graph.setSelectionCell(vertex);
|
|
|
|
};
|
2021-08-30 14:20:26 +00:00
|
|
|
|
2021-04-25 03:39:40 +00:00
|
|
|
// Creates the image which is used as the drag icon (preview)
|
|
|
|
const img = toolbar.addMode(null, image, (evt, cell) => {
|
|
|
|
const pt = graph.getPointForEvent(evt);
|
|
|
|
funct(graph, evt, cell, pt.x, pt.y);
|
|
|
|
});
|
2021-08-30 14:20:26 +00:00
|
|
|
|
2021-04-25 03:39:40 +00:00
|
|
|
// Disables dragging if element is disabled. This is a workaround
|
|
|
|
// for wrong event order in IE. Following is a dummy listener that
|
|
|
|
// is invoked as the last listener in IE.
|
2021-08-30 14:20:26 +00:00
|
|
|
InternalEvent.addListener(img, 'mousedown', (evt) => {
|
2021-04-25 03:39:40 +00:00
|
|
|
// do nothing
|
|
|
|
});
|
2021-08-30 14:20:26 +00:00
|
|
|
|
2021-04-25 03:39:40 +00:00
|
|
|
// This listener is always called first before any other listener
|
|
|
|
// in all browsers.
|
2021-08-30 14:20:26 +00:00
|
|
|
InternalEvent.addListener(img, 'mousedown', (evt) => {
|
2021-04-25 03:39:40 +00:00
|
|
|
if (img.enabled == false) {
|
2021-08-30 14:20:26 +00:00
|
|
|
InternalEvent.consume(evt);
|
2021-04-25 03:39:40 +00:00
|
|
|
}
|
|
|
|
});
|
2021-08-30 14:20:26 +00:00
|
|
|
|
2022-01-08 01:49:35 +00:00
|
|
|
gestureUtils.makeDraggable(img, graph, funct);
|
2021-04-25 03:39:40 +00:00
|
|
|
return img;
|
|
|
|
}
|
|
|
|
|
|
|
|
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({});
|