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,
|
2021-09-07 12:21:22 +00:00
|
|
|
TextShape,
|
2022-01-08 01:49:35 +00:00
|
|
|
Effects,
|
2021-09-07 09:07:27 +00:00
|
|
|
InternalEvent,
|
2022-01-08 01:49:35 +00:00
|
|
|
constants,
|
2021-09-07 09:07:27 +00:00
|
|
|
Perimeter,
|
2022-01-08 01:49:35 +00:00
|
|
|
Codec,
|
|
|
|
xmlUtils,
|
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: 'Misc/DynamicLoading',
|
|
|
|
argTypes: {
|
2021-08-30 14:20:26 +00:00
|
|
|
...globalTypes,
|
|
|
|
},
|
2021-04-25 03:39:40 +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';
|
|
|
|
let requestId = 0;
|
|
|
|
|
|
|
|
// Speedup the animation
|
2021-09-07 12:21:22 +00:00
|
|
|
TextShape.prototype.enableBoundingBox = false;
|
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
|
|
|
|
|
|
|
// Disables all built-in interactions
|
|
|
|
graph.setEnabled(false);
|
|
|
|
|
|
|
|
// Handles clicks on cells
|
2021-08-30 14:20:26 +00:00
|
|
|
graph.addListener(InternalEvent.CLICK, function (sender, evt) {
|
2021-04-25 03:39:40 +00:00
|
|
|
const cell = evt.getProperty('cell');
|
|
|
|
|
|
|
|
if (cell != null) {
|
|
|
|
load(graph, cell);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Changes the default vertex style in-place
|
|
|
|
const style = graph.getStylesheet().getDefaultVertexStyle();
|
2022-01-08 01:49:35 +00:00
|
|
|
style.shape = constants.SHAPE.ELLIPSE;
|
2022-10-28 13:37:02 +00:00
|
|
|
style.perimeter = Perimeter.EllipsePerimeter;
|
2021-05-02 06:04:34 +00:00
|
|
|
style.gradientColor = 'white';
|
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();
|
|
|
|
|
2022-10-28 13:37:02 +00:00
|
|
|
const cx = args.width / 2;
|
|
|
|
const cy = args.height / 2;
|
2021-04-25 03:39:40 +00:00
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
const cell = graph.insertVertex(parent, '0-0', '0-0', cx - 20, cy - 15, 60, 40);
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Animates the changes in the graph model
|
2022-01-08 01:49:35 +00:00
|
|
|
graph.getDataModel().addListener(InternalEvent.CHANGE, function (sender, evt) {
|
2021-04-25 03:39:40 +00:00
|
|
|
const { changes } = evt.getProperty('edit');
|
2022-01-08 01:49:35 +00:00
|
|
|
Effects.animateChanges(graph, changes);
|
2021-04-25 03:39:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Loads the links for the given cell into the given graph
|
|
|
|
// by requesting the respective data in the server-side
|
|
|
|
// (implemented for this demo using the server-function)
|
|
|
|
function load(graph, cell) {
|
|
|
|
if (cell.isVertex()) {
|
2022-10-28 13:37:02 +00:00
|
|
|
const cx = args.width / 2;
|
|
|
|
const cy = args.height / 2;
|
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(() => {
|
2021-04-25 03:39:40 +00:00
|
|
|
const xml = server(cell.id);
|
2022-01-08 01:49:35 +00:00
|
|
|
const doc = xmlUtils.parseXml(xml);
|
|
|
|
const dec = new Codec(doc);
|
2022-10-28 13:37:02 +00:00
|
|
|
|
2021-04-25 03:39:40 +00:00
|
|
|
const model = dec.decode(doc.documentElement);
|
|
|
|
|
|
|
|
// Removes all cells which are not in the response
|
2022-10-28 13:37:02 +00:00
|
|
|
for (const key in graph.getDataModel().cells) {
|
2022-01-08 01:49:35 +00:00
|
|
|
const tmp = graph.getDataModel().getCell(key);
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
if (tmp != cell && tmp.isVertex()) {
|
|
|
|
graph.removeCells([tmp]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merges the response model with the client model
|
2022-10-28 13:37:02 +00:00
|
|
|
graph.getDataModel().mergeChildren(model.getRoot().getChildAt(0), parent);
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Moves the given cell to the center
|
|
|
|
let geo = cell.getGeometry();
|
|
|
|
|
|
|
|
if (geo != null) {
|
|
|
|
geo = geo.clone();
|
|
|
|
geo.x = cx - geo.width / 2;
|
|
|
|
geo.y = cy - geo.height / 2;
|
|
|
|
|
2022-01-08 01:49:35 +00:00
|
|
|
graph.getDataModel().setGeometry(cell, geo);
|
2021-04-25 03:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a list of the new vertices, if there is more
|
|
|
|
// than the center vertex which might have existed
|
|
|
|
// previously, then this needs to be changed to analyze
|
|
|
|
// the target model before calling mergeChildren above
|
|
|
|
const vertices = [];
|
|
|
|
|
2022-10-28 13:37:02 +00:00
|
|
|
for (const key in graph.getDataModel().cells) {
|
2022-01-08 01:49:35 +00:00
|
|
|
const tmp = graph.getDataModel().getCell(key);
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
if (tmp != cell && tmp.isVertex()) {
|
|
|
|
vertices.push(tmp);
|
|
|
|
|
|
|
|
// Changes the initial location "in-place"
|
|
|
|
// to get a nice animation effect from the
|
|
|
|
// center to the radius of the circle
|
|
|
|
const geo = tmp.getGeometry();
|
|
|
|
|
|
|
|
if (geo != null) {
|
|
|
|
geo.x = cx - geo.width / 2;
|
|
|
|
geo.y = cy - geo.height / 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arranges the response in a circle
|
|
|
|
const cellCount = vertices.length;
|
|
|
|
const phi = (2 * Math.PI) / cellCount;
|
|
|
|
const r = Math.min(
|
2022-10-28 13:37:02 +00:00
|
|
|
args.width / 4,
|
|
|
|
args.height / 4
|
2021-04-25 03:39:40 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
for (let i = 0; i < cellCount; i++) {
|
|
|
|
let geo = vertices[i].getGeometry();
|
|
|
|
|
|
|
|
if (geo != null) {
|
|
|
|
geo = geo.clone();
|
|
|
|
geo.x += r * Math.sin(i * phi);
|
|
|
|
geo.y += r * Math.cos(i * phi);
|
|
|
|
|
2022-01-08 01:49:35 +00:00
|
|
|
graph.getDataModel().setGeometry(vertices[i], geo);
|
2021-04-25 03:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-08 01:49:35 +00:00
|
|
|
});
|
2021-04-25 03:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simulates the existence of a server that can crawl the
|
|
|
|
// big graph with a certain depth and create a graph model
|
|
|
|
// for the traversed cells, which is then sent to the client
|
|
|
|
function server(cellId) {
|
|
|
|
// Increments the request ID as a prefix for the cell IDs
|
|
|
|
requestId++;
|
|
|
|
|
|
|
|
// Creates a local graph with no display
|
2021-08-30 14:20:26 +00:00
|
|
|
const graph = new 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(() => {
|
2021-04-25 03:39:40 +00:00
|
|
|
const v0 = graph.insertVertex(parent, cellId, 'Dummy', 0, 0, 60, 40);
|
|
|
|
const cellCount = parseInt(Math.random() * 16) + 4;
|
|
|
|
|
|
|
|
// Creates the random links and cells for the response
|
|
|
|
for (let i = 0; i < cellCount; i++) {
|
|
|
|
const id = `${requestId}-${i}`;
|
|
|
|
const v = graph.insertVertex(parent, id, id, 0, 0, 60, 40);
|
|
|
|
const e = graph.insertEdge(parent, null, `Link ${i}`, v0, v);
|
|
|
|
}
|
2022-01-08 01:49:35 +00:00
|
|
|
});
|
2021-04-25 03:39:40 +00:00
|
|
|
|
2022-01-08 01:49:35 +00:00
|
|
|
const enc = new Codec();
|
|
|
|
const node = enc.encode(graph.getDataModel());
|
2021-04-25 03:39:40 +00:00
|
|
|
|
2022-01-08 01:49:35 +00:00
|
|
|
return xmlUtils.getXml(node);
|
2021-04-25 03:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
load(graph, cell);
|
|
|
|
|
|
|
|
return container;
|
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({});
|