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,
|
|
|
|
InternalEvent,
|
2022-01-08 01:49:35 +00:00
|
|
|
RubberBandHandler,
|
|
|
|
MaxLog,
|
2021-09-07 09:07:27 +00:00
|
|
|
GraphView,
|
|
|
|
Point,
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers,
|
|
|
|
eventUtils,
|
2021-09-07 09:07:27 +00:00
|
|
|
} 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 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);
|
2022-01-08 01:49:35 +00:00
|
|
|
const graphHandler = graph.getPlugin('SelectionHandler');
|
2021-09-10 05:17:59 +00:00
|
|
|
graphHandler.scaleGrid = true;
|
2021-04-15 04:09:10 +00:00
|
|
|
graph.setPanning(true);
|
|
|
|
|
|
|
|
// Enables rubberband selection
|
2022-01-08 01:49:35 +00:00
|
|
|
if (args.rubberBand) new RubberBandHandler(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) ||
|
2022-01-08 01:49:35 +00:00
|
|
|
eventUtils.getSource(evt) == canvas
|
2021-08-30 14:20:26 +00:00
|
|
|
);
|
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) {
|
2022-01-08 01:49:35 +00:00
|
|
|
MaxLog.show();
|
|
|
|
MaxLog.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
|
2022-01-08 01:49:35 +00:00
|
|
|
graph.batchUpdate(() => {
|
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);
|
2022-01-08 01:49:35 +00:00
|
|
|
});
|
2021-04-15 04:09:10 +00:00
|
|
|
|
|
|
|
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(
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers.button('+', function () {
|
2021-08-30 14:20:26 +00:00
|
|
|
graph.zoomIn();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
controller.appendChild(
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers.button('-', function () {
|
2021-08-30 14:20:26 +00:00
|
|
|
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({});
|