2021-09-07 09:07:27 +00:00
|
|
|
import {
|
|
|
|
Graph,
|
2022-01-08 01:49:35 +00:00
|
|
|
ObjectCodec,
|
|
|
|
DomHelpers,
|
|
|
|
CodecRegistry,
|
2021-09-07 09:07:27 +00:00
|
|
|
InternalEvent,
|
2022-01-08 01:49:35 +00:00
|
|
|
Client,
|
|
|
|
Codec,
|
|
|
|
domUtils,
|
|
|
|
xmlUtils,
|
|
|
|
popup,
|
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: 'Xml_Json/JsonData',
|
|
|
|
argTypes: {
|
2021-08-30 14:20:26 +00:00
|
|
|
...globalTypes,
|
|
|
|
},
|
2021-04-25 03:39:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const Template = ({ label, ...args }) => {
|
2022-01-08 01:49:35 +00:00
|
|
|
Client.setImageBasePath('/images');
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Register a new codec
|
|
|
|
function CustomData(value) {
|
|
|
|
this.value = value;
|
|
|
|
}
|
2022-01-08 01:49:35 +00:00
|
|
|
const codec = new ObjectCodec(new CustomData());
|
2021-08-30 14:20:26 +00:00
|
|
|
codec.encode = function (enc, obj) {
|
2021-04-25 03:39:40 +00:00
|
|
|
const node = enc.document.createElement('CustomData');
|
2022-01-08 01:49:35 +00:00
|
|
|
domUtils.setTextContent(node, JSON.stringify(obj));
|
2021-04-25 03:39:40 +00:00
|
|
|
return node;
|
|
|
|
};
|
2021-08-30 14:20:26 +00:00
|
|
|
codec.decode = function (dec, node, into) {
|
2022-01-08 01:49:35 +00:00
|
|
|
const obj = JSON.parse(domUtils.getTextContent(node));
|
2021-04-25 03:39:40 +00:00
|
|
|
obj.constructor = CustomData;
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
};
|
2022-01-08 01:49:35 +00:00
|
|
|
CodecRegistry.register(codec);
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Disables the built-in context menu
|
2021-08-30 14:20:26 +00:00
|
|
|
if (!args.contextMenu) InternalEvent.disableContextMenu(container);
|
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
|
|
|
|
|
|
|
// Enables rubberband selection
|
2022-01-08 01:49:35 +00:00
|
|
|
if (args.rubberBand) new RubberBandHandler(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 v1 = graph.insertVertex(parent, null, 'Hello,', 20, 20, 80, 30);
|
|
|
|
v1.data = new CustomData('v1');
|
|
|
|
const v2 = graph.insertVertex(parent, null, 'World!', 200, 150, 80, 30);
|
|
|
|
v2.data = new CustomData('v2');
|
|
|
|
const e1 = graph.insertEdge(parent, null, '', v1, v2);
|
2022-01-08 01:49:35 +00:00
|
|
|
});
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
const buttons = document.createElement('div');
|
|
|
|
div.appendChild(buttons);
|
|
|
|
|
|
|
|
buttons.appendChild(
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers.button('Show JSON', function () {
|
|
|
|
const encoder = new Codec();
|
|
|
|
const node = encoder.encode(graph.getDataModel());
|
|
|
|
popup(xmlUtils.getXml(node), true);
|
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({});
|