2021-08-30 14:20:26 +00:00
|
|
|
import maxgraph from '@maxgraph/core';
|
2021-04-15 04:09:10 +00:00
|
|
|
|
2021-04-21 04:54:09 +00:00
|
|
|
import { globalTypes } from '../.storybook/preview';
|
2021-04-19 12:56:58 +00:00
|
|
|
|
2021-04-15 04:09:10 +00:00
|
|
|
export default {
|
2021-04-19 12:56:58 +00:00
|
|
|
title: 'Backgrounds/Grid',
|
|
|
|
argTypes: {
|
2021-04-21 04:54:09 +00:00
|
|
|
...globalTypes,
|
2021-04-20 12:22:55 +00:00
|
|
|
contextMenu: {
|
|
|
|
type: 'boolean',
|
2021-08-30 14:20:26 +00:00
|
|
|
defaultValue: false,
|
2021-04-20 12:22:55 +00:00
|
|
|
},
|
|
|
|
rubberBand: {
|
|
|
|
type: 'boolean',
|
2021-08-30 14:20:26 +00:00
|
|
|
defaultValue: true,
|
|
|
|
},
|
|
|
|
},
|
2021-04-15 04:09:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const Template = ({ label, ...args }) => {
|
|
|
|
const {
|
2021-08-30 14:20:26 +00:00
|
|
|
Graph,
|
|
|
|
InternalEvent,
|
|
|
|
Rubberband,
|
|
|
|
mxLog,
|
|
|
|
GraphView,
|
|
|
|
Point,
|
2021-04-15 04:09:10 +00:00
|
|
|
mxDomHelpers,
|
2021-08-30 14:20:26 +00:00
|
|
|
EventUtils,
|
|
|
|
} = maxgraph;
|
2021-04-15 04:09:10 +00:00
|
|
|
|
|
|
|
const div = document.createElement('div');
|
|
|
|
|
|
|
|
const container = document.createElement('div');
|
|
|
|
container.style.position = 'relative';
|
|
|
|
container.style.overflow = 'hidden';
|
2021-04-19 12:56:58 +00:00
|
|
|
container.style.width = `${args.width}px`;
|
|
|
|
container.style.height = `${args.height}px`;
|
2021-04-15 04:09:10 +00:00
|
|
|
container.style.cursor = 'default';
|
|
|
|
div.appendChild(container);
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
if (!args.contextMenu) InternalEvent.disableContextMenu(container);
|
2021-04-15 04:09:10 +00:00
|
|
|
|
|
|
|
// Creates the graph inside the given container
|
2021-08-30 14:20:26 +00:00
|
|
|
var graph = new Graph(container);
|
2021-04-15 04:09:10 +00:00
|
|
|
graph.graphHandler.scaleGrid = true;
|
|
|
|
graph.setPanning(true);
|
|
|
|
|
|
|
|
// Enables rubberband selection
|
2021-08-30 14:20:26 +00:00
|
|
|
if (args.rubberBand) new Rubberband(graph);
|
2021-04-15 04:09:10 +00:00
|
|
|
|
|
|
|
let repaintGrid;
|
|
|
|
|
|
|
|
// Create grid dynamically (requires canvas)
|
2021-08-30 14:20:26 +00:00
|
|
|
(function () {
|
|
|
|
try {
|
2021-04-15 04:09:10 +00:00
|
|
|
var canvas = document.createElement('canvas');
|
|
|
|
canvas.style.position = 'absolute';
|
|
|
|
canvas.style.top = '0px';
|
|
|
|
canvas.style.left = '0px';
|
|
|
|
canvas.style.zIndex = -1;
|
|
|
|
graph.container.appendChild(canvas);
|
2021-08-30 14:20:26 +00:00
|
|
|
|
2021-04-15 04:09:10 +00:00
|
|
|
var ctx = canvas.getContext('2d');
|
2021-08-30 14:20:26 +00:00
|
|
|
|
2021-04-15 04:09:10 +00:00
|
|
|
// Modify event filtering to accept canvas as container
|
2021-08-30 14:20:26 +00:00
|
|
|
var mxGraphViewIsContainerEvent = GraphView.prototype.isContainerEvent;
|
|
|
|
GraphView.prototype.isContainerEvent = function (evt) {
|
|
|
|
return (
|
|
|
|
mxGraphViewIsContainerEvent.apply(this, arguments) ||
|
|
|
|
EventUtils.getSource(evt) == canvas
|
|
|
|
);
|
2021-04-15 04:09:10 +00:00
|
|
|
};
|
2021-08-30 14:20:26 +00:00
|
|
|
|
2021-04-15 04:09:10 +00:00
|
|
|
var s = 0;
|
|
|
|
var gs = 0;
|
2021-08-30 14:20:26 +00:00
|
|
|
var tr = new Point();
|
2021-04-15 04:09:10 +00:00
|
|
|
var w = 0;
|
|
|
|
var h = 0;
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
repaintGrid = function () {
|
|
|
|
if (ctx != null) {
|
2021-04-15 04:09:10 +00:00
|
|
|
var bounds = graph.getGraphBounds();
|
|
|
|
var width = Math.max(bounds.x + bounds.width, graph.container.clientWidth);
|
|
|
|
var height = Math.max(bounds.y + bounds.height, graph.container.clientHeight);
|
|
|
|
var sizeChanged = width != w || height != h;
|
2021-08-30 14:20:26 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
graph.view.scale != s ||
|
|
|
|
graph.view.translate.x != tr.x ||
|
|
|
|
graph.view.translate.y != tr.y ||
|
|
|
|
gs != graph.gridSize ||
|
|
|
|
sizeChanged
|
|
|
|
) {
|
2021-04-15 04:09:10 +00:00
|
|
|
tr = graph.view.translate.clone();
|
|
|
|
s = graph.view.scale;
|
|
|
|
gs = graph.gridSize;
|
|
|
|
w = width;
|
|
|
|
h = height;
|
2021-08-30 14:20:26 +00:00
|
|
|
|
2021-04-15 04:09:10 +00:00
|
|
|
// Clears the background if required
|
2021-08-30 14:20:26 +00:00
|
|
|
if (!sizeChanged) {
|
2021-04-15 04:09:10 +00:00
|
|
|
ctx.clearRect(0, 0, w, h);
|
2021-08-30 14:20:26 +00:00
|
|
|
} else {
|
2021-04-15 04:09:10 +00:00
|
|
|
canvas.setAttribute('width', w);
|
|
|
|
canvas.setAttribute('height', h);
|
|
|
|
}
|
|
|
|
|
|
|
|
var tx = tr.x * s;
|
|
|
|
var ty = tr.y * s;
|
|
|
|
|
|
|
|
// Sets the distance of the grid lines in pixels
|
|
|
|
var minStepping = graph.gridSize;
|
|
|
|
var stepping = minStepping * s;
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
if (stepping < minStepping) {
|
2021-04-15 04:09:10 +00:00
|
|
|
var count = Math.round(Math.ceil(minStepping / stepping) / 2) * 2;
|
|
|
|
stepping = count * stepping;
|
|
|
|
}
|
|
|
|
|
|
|
|
var xs = Math.floor((0 - tx) / stepping) * stepping + tx;
|
|
|
|
var xe = Math.ceil(w / stepping) * stepping;
|
|
|
|
var ys = Math.floor((0 - ty) / stepping) * stepping + ty;
|
|
|
|
var ye = Math.ceil(h / stepping) * stepping;
|
|
|
|
|
|
|
|
xe += Math.ceil(stepping);
|
|
|
|
ye += Math.ceil(stepping);
|
|
|
|
|
|
|
|
var ixs = Math.round(xs);
|
|
|
|
var ixe = Math.round(xe);
|
|
|
|
var iys = Math.round(ys);
|
|
|
|
var iye = Math.round(ye);
|
|
|
|
|
|
|
|
// Draws the actual grid
|
|
|
|
ctx.strokeStyle = '#f6f6f6';
|
|
|
|
ctx.beginPath();
|
2021-08-30 14:20:26 +00:00
|
|
|
|
|
|
|
for (var x = xs; x <= xe; x += stepping) {
|
2021-04-15 04:09:10 +00:00
|
|
|
x = Math.round((x - tx) / stepping) * stepping + tx;
|
|
|
|
var ix = Math.round(x);
|
2021-08-30 14:20:26 +00:00
|
|
|
|
2021-04-15 04:09:10 +00:00
|
|
|
ctx.moveTo(ix + 0.5, iys + 0.5);
|
|
|
|
ctx.lineTo(ix + 0.5, iye + 0.5);
|
|
|
|
}
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
for (var y = ys; y <= ye; y += stepping) {
|
2021-04-15 04:09:10 +00:00
|
|
|
y = Math.round((y - ty) / stepping) * stepping + ty;
|
|
|
|
var iy = Math.round(y);
|
2021-08-30 14:20:26 +00:00
|
|
|
|
2021-04-15 04:09:10 +00:00
|
|
|
ctx.moveTo(ixs + 0.5, iy + 0.5);
|
|
|
|
ctx.lineTo(ixe + 0.5, iy + 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.closePath();
|
|
|
|
ctx.stroke();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-08-30 14:20:26 +00:00
|
|
|
} catch (e) {
|
2021-04-15 04:09:10 +00:00
|
|
|
mxLog.show();
|
|
|
|
mxLog.debug('Using background image');
|
2021-08-30 14:20:26 +00:00
|
|
|
|
|
|
|
container.style.backgroundImage = "url('editors/images/grid.gif')";
|
2021-04-15 04:09:10 +00:00
|
|
|
}
|
2021-08-30 14:20:26 +00:00
|
|
|
|
|
|
|
var mxGraphViewValidateBackground = GraphView.prototype.validateBackground;
|
|
|
|
GraphView.prototype.validateBackground = function () {
|
2021-04-15 04:09:10 +00:00
|
|
|
mxGraphViewValidateBackground.apply(this, arguments);
|
|
|
|
repaintGrid();
|
|
|
|
};
|
|
|
|
})();
|
2021-08-30 14:20:26 +00:00
|
|
|
|
2021-04-15 04:09:10 +00:00
|
|
|
// Gets the default parent for inserting new cells. This
|
|
|
|
// is normally the first child of the root (ie. layer 0).
|
|
|
|
var parent = graph.getDefaultParent();
|
2021-08-30 14:20:26 +00:00
|
|
|
|
2021-04-15 04:09:10 +00:00
|
|
|
// Adds cells to the model in a single step
|
|
|
|
graph.getModel().beginUpdate();
|
2021-08-30 14:20:26 +00:00
|
|
|
try {
|
2021-04-15 04:09:10 +00:00
|
|
|
var v1 = graph.insertVertex(parent, null, 'Hello,', 20, 20, 80, 30);
|
|
|
|
var v2 = graph.insertVertex(parent, null, 'World!', 200, 150, 80, 30);
|
|
|
|
var e1 = graph.insertEdge(parent, null, '', v1, v2);
|
2021-08-30 14:20:26 +00:00
|
|
|
} finally {
|
2021-04-15 04:09:10 +00:00
|
|
|
// Updates the display
|
|
|
|
graph.getModel().endUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
graph.centerZoom = false;
|
2021-08-30 14:20:26 +00:00
|
|
|
|
2021-04-15 04:09:10 +00:00
|
|
|
const controller = document.createElement('div');
|
|
|
|
div.appendChild(controller);
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
controller.appendChild(
|
|
|
|
mxDomHelpers.button('+', function () {
|
|
|
|
graph.zoomIn();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
controller.appendChild(
|
|
|
|
mxDomHelpers.button('-', function () {
|
|
|
|
graph.zoomOut();
|
|
|
|
})
|
|
|
|
);
|
2021-04-15 04:09:10 +00:00
|
|
|
|
|
|
|
return div;
|
2021-08-30 14:20:26 +00:00
|
|
|
};
|
2021-04-15 04:09:10 +00:00
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
export const Default = Template.bind({});
|