2021-09-07 12:21:22 +00:00
|
|
|
import { Graph, InternalEvent, GraphHandler, RubberBand } from '@maxgraph/core';
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
import { globalTypes } from '../.storybook/preview';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
title: 'Layouts/Constituent',
|
|
|
|
argTypes: {
|
|
|
|
...globalTypes,
|
|
|
|
contextMenu: {
|
|
|
|
type: 'boolean',
|
2021-08-30 14:20:26 +00:00
|
|
|
defaultValue: false,
|
2021-04-24 12:30:27 +00:00
|
|
|
},
|
|
|
|
rubberBand: {
|
|
|
|
type: 'boolean',
|
2021-08-30 14:20:26 +00:00
|
|
|
defaultValue: true,
|
|
|
|
},
|
|
|
|
},
|
2021-04-24 12:30:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const Template = ({ label, ...args }) => {
|
|
|
|
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';
|
|
|
|
|
|
|
|
// Disables the built-in context menu
|
2021-08-30 14:20:26 +00:00
|
|
|
InternalEvent.disableContextMenu(container);
|
2021-04-24 12:30:27 +00:00
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
class MyCustomGraphHandler extends GraphHandler {
|
2021-04-24 12:30:27 +00:00
|
|
|
/**
|
|
|
|
* Redirects start drag to parent.
|
|
|
|
*/
|
|
|
|
getInitialCellForEvent(me) {
|
|
|
|
let cell = super.getInitialCellForEvent(me);
|
|
|
|
if (this.graph.isPart(cell)) {
|
2021-04-25 10:47:53 +00:00
|
|
|
cell = cell.getParent();
|
2021-04-24 12:30:27 +00:00
|
|
|
}
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
class MyCustomGraph extends Graph {
|
2021-04-24 12:30:27 +00:00
|
|
|
foldingEnabled = false;
|
|
|
|
|
|
|
|
recursiveResize = true;
|
|
|
|
|
|
|
|
isPart(cell) {
|
|
|
|
// Helper method to mark parts with constituent=1 in the style
|
|
|
|
return this.getCurrentCellStyle(cell).constituent == '1';
|
|
|
|
}
|
|
|
|
|
|
|
|
selectCellForEvent(cell, evt) {
|
|
|
|
// Redirects selection to parent
|
|
|
|
if (this.isPart(cell)) {
|
2021-04-25 03:39:40 +00:00
|
|
|
cell = cell.getParent();
|
2021-04-24 12:30:27 +00:00
|
|
|
}
|
|
|
|
super.selectCellForEvent(cell, evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
createGraphHandler() {
|
|
|
|
return new MyCustomGraphHandler(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates the graph inside the given container
|
|
|
|
const graph = new MyCustomGraph(container);
|
|
|
|
|
|
|
|
// Enables rubberband selection
|
2021-09-07 12:21:22 +00:00
|
|
|
new RubberBand(graph);
|
2021-04-24 12:30:27 +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
|
|
|
|
graph.batchUpdate(() => {
|
|
|
|
const v1 = graph.insertVertex({
|
|
|
|
parent,
|
|
|
|
position: [20, 20],
|
|
|
|
size: [120, 70],
|
|
|
|
});
|
|
|
|
const v2 = graph.insertVertex({
|
|
|
|
parent: v1,
|
|
|
|
value: 'Constituent',
|
|
|
|
position: [20, 20],
|
|
|
|
size: [80, 30],
|
|
|
|
style: 'constituent=1;',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return container;
|
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({});
|