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.
|
|
|
|
*/
|
|
|
|
|
2022-01-08 01:49:35 +00:00
|
|
|
import { Graph, constants } from '@maxgraph/core';
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
import { globalTypes } from '../.storybook/preview';
|
2022-01-08 01:49:35 +00:00
|
|
|
import { clone } from '@maxgraph/core/util/cloneUtils';
|
|
|
|
import { button } from '@maxgraph/core/util/domHelpers';
|
|
|
|
import { load } from '@maxgraph/core/util/MaxXmlRequest';
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
title: 'Xml_Json/FileIO',
|
|
|
|
argTypes: {
|
2021-08-30 14:20:26 +00:00
|
|
|
...globalTypes,
|
|
|
|
},
|
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);
|
|
|
|
|
|
|
|
// Program starts here. Creates a sample graph in the
|
|
|
|
// DOM node with the specified ID. This function is invoked
|
|
|
|
// from the onLoad event handler of the document (see below).
|
|
|
|
function main(container) {
|
|
|
|
// Checks if browser is supported
|
2022-01-08 01:49:35 +00:00
|
|
|
if (!Client.isBrowserSupported()) {
|
2021-04-25 03:39:40 +00:00
|
|
|
// Displays an error message if the browser is
|
|
|
|
// not supported.
|
2021-09-01 14:04:33 +00:00
|
|
|
alert('Browser is not supported!', 200, false);
|
2021-04-25 03:39:40 +00:00
|
|
|
} else {
|
|
|
|
// 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
|
|
|
|
|
|
|
graph.setEnabled(false);
|
|
|
|
graph.setPanning(true);
|
|
|
|
graph.setTooltips(true);
|
2022-01-08 01:49:35 +00:00
|
|
|
graph.getPlugin('PanningHandler').useLeftButtonForPanning = true;
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Adds a highlight on the cell under the mousepointer
|
2021-07-12 12:13:45 +00:00
|
|
|
new CellTracker(graph);
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Changes the default vertex style in-place
|
|
|
|
let style = graph.getStylesheet().getDefaultVertexStyle();
|
2022-01-08 01:49:35 +00:00
|
|
|
style.shape = constants.SHAPE.ROUNDED;
|
2021-06-06 13:04:44 +00:00
|
|
|
style.perimiter = Perimeter.RectanglePerimeter;
|
2021-05-02 06:04:34 +00:00
|
|
|
style.gradientColor = 'white';
|
|
|
|
style.perimeterSpacing = 4;
|
|
|
|
style.shadow = true;
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
style = graph.getStylesheet().getDefaultEdgeStyle();
|
2021-05-02 06:04:34 +00:00
|
|
|
style.labelBackgroundColor = 'white';
|
2021-04-25 03:39:40 +00:00
|
|
|
|
2021-07-31 09:55:25 +00:00
|
|
|
style = clone(style);
|
2022-01-08 01:49:35 +00:00
|
|
|
style.startArrow = constants.ARROW.CLASSIC;
|
2021-04-25 03:39:40 +00:00
|
|
|
graph.getStylesheet().putCellStyle('2way', style);
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
graph.isHtmlLabel = function (cell) {
|
2021-04-25 03:39:40 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Larger grid size yields cleaner layout result
|
|
|
|
graph.gridSize = 20;
|
|
|
|
|
|
|
|
// Creates a layout algorithm to be used
|
|
|
|
// with the graph
|
2021-07-12 12:13:45 +00:00
|
|
|
const layout = new MxFastOrganicLayout(graph);
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Moves stuff wider apart than usual
|
|
|
|
layout.forceConstant = 140;
|
|
|
|
|
|
|
|
// Adds a button to execute the layout
|
|
|
|
this.el2.appendChild(
|
2021-08-30 14:20:26 +00:00
|
|
|
button('Arrange', function (evt) {
|
2021-04-25 03:39:40 +00:00
|
|
|
const parent = graph.getDefaultParent();
|
|
|
|
layout.execute(parent);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Load cells and layouts the graph
|
2022-01-08 01:49:35 +00:00
|
|
|
graph.batchUpdate(() => {
|
2021-04-25 03:39:40 +00:00
|
|
|
// Loads the custom file format (TXT file)
|
|
|
|
parse(graph, 'fileio.txt');
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
// Loads the Graph file format (XML file)
|
2021-04-25 03:39:40 +00:00
|
|
|
// read(graph, 'fileio.xml');
|
|
|
|
|
|
|
|
// Gets the default parent for inserting new cells. This
|
|
|
|
// is normally the first child of the root (ie. layer 0).
|
|
|
|
const parent = graph.getDefaultParent();
|
|
|
|
|
|
|
|
// Executes the layout
|
|
|
|
layout.execute(parent);
|
2022-01-08 01:49:35 +00:00
|
|
|
});
|
2021-04-25 03:39:40 +00:00
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
graph.dblClick = function (evt, cell) {
|
2022-01-08 01:49:35 +00:00
|
|
|
const mxe = new EventObject(InternalEvent.DOUBLE_CLICK, { event: evt, cell });
|
2021-04-25 03:39:40 +00:00
|
|
|
this.fireEvent(mxe);
|
|
|
|
|
|
|
|
if (
|
|
|
|
this.isEnabled() &&
|
2021-08-30 14:20:26 +00:00
|
|
|
!InternalEvent.isConsumed(evt) &&
|
2021-04-25 03:39:40 +00:00
|
|
|
!mxe.isConsumed() &&
|
|
|
|
cell != null
|
|
|
|
) {
|
2021-08-30 14:20:26 +00:00
|
|
|
alert(`Show properties for cell ${cell.customId || cell.getId()}`);
|
2021-04-25 03:39:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom parser for simple file format
|
|
|
|
function parse(graph, filename) {
|
2022-01-08 01:49:35 +00:00
|
|
|
const model = graph.getDataModel();
|
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();
|
|
|
|
|
2021-07-31 09:55:25 +00:00
|
|
|
const req = load(filename);
|
2021-04-25 03:39:40 +00:00
|
|
|
const text = req.getText();
|
|
|
|
|
|
|
|
const lines = text.split('\n');
|
|
|
|
|
|
|
|
// Creates the lookup table for the vertices
|
|
|
|
const vertices = [];
|
|
|
|
|
|
|
|
// Parses all lines (vertices must be first in the file)
|
2022-01-08 01:49:35 +00:00
|
|
|
graph.batchUpdate(() => {
|
2021-04-25 03:39:40 +00:00
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
|
|
// Ignores comments (starting with #)
|
|
|
|
const colon = lines[i].indexOf(':');
|
|
|
|
|
|
|
|
if (lines[i].substring(0, 1) != '#' || colon == -1) {
|
|
|
|
const comma = lines[i].indexOf(',');
|
|
|
|
const value = lines[i].substring(colon + 2, lines[i].length);
|
|
|
|
|
|
|
|
if (comma == -1 || comma > colon) {
|
|
|
|
const key = lines[i].substring(0, colon);
|
|
|
|
|
|
|
|
if (key.length > 0) {
|
2021-08-30 14:20:26 +00:00
|
|
|
vertices[key] = graph.insertVertex(parent, null, value, 0, 0, 80, 70);
|
2021-04-25 03:39:40 +00:00
|
|
|
}
|
|
|
|
} else if (comma < colon) {
|
|
|
|
// Looks up the vertices in the lookup table
|
|
|
|
const source = vertices[lines[i].substring(0, comma)];
|
|
|
|
const target = vertices[lines[i].substring(comma + 1, colon)];
|
|
|
|
|
|
|
|
if (source != null && target != null) {
|
|
|
|
const e = graph.insertEdge(parent, null, value, source, target);
|
|
|
|
|
|
|
|
// Uses the special 2-way style for 2-way labels
|
|
|
|
if (value.indexOf('2-Way') >= 0) {
|
|
|
|
e.style = '2way';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-08 01:49:35 +00:00
|
|
|
});
|
2021-04-25 03:39:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
// Parses the Graph XML file format
|
2021-04-25 03:39:40 +00:00
|
|
|
function read(graph, filename) {
|
2021-07-31 09:55:25 +00:00
|
|
|
const req = load(filename);
|
2021-04-25 03:39:40 +00:00
|
|
|
const root = req.getDocumentElement();
|
2022-01-08 01:49:35 +00:00
|
|
|
const dec = new Codec(root.ownerDocument);
|
2021-04-25 03:39:40 +00:00
|
|
|
|
2022-01-08 01:49:35 +00:00
|
|
|
dec.decode(root, graph.getDataModel());
|
2021-04-25 03:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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({});
|