maxGraph/packages/html/stories/stashed/Codec.js

278 lines
12 KiB
JavaScript
Raw Normal View History

2021-03-25 06:29:07 +00:00
/**
* Copyright (c) 2006-2013, JGraph Ltd
*
* Codec
2021-03-25 06:29:07 +00:00
*/
import React from 'react';
import mxEvent from '../mxgraph/util/mxEvent';
import mxGraph from '../mxgraph/view/mxGraph';
Finish converting core to ts, JSDoc conversion, consistency+convention changes, example bugfixes (#70) * reorganised directories; removed mx prefix * reduced directory hierarchies; removed mx prefix; type fixes * convert remaining javascript to ts * fix/add types * add type defs * type updates; moved codecs to where they're used * reorganise constants into enums+type additions * removed "Function:" and "Variable:" prefixes from comments, as they aren't needed in JSDoc * removed "Function:", "Variable:" and "Class:" prefixes from comments, as they aren't needed in JSDoc * removed "Function:" prefixes from comments, as they aren't needed in JSDoc * minor changes * convert code example blocks to markdown * module casing updates * converted parameter function documentation to JSDoc * documentation+type updates * removed react subdir (for now) * reorganised various `utils` functions into different files * type updates/bugfixes/workarounds * rename Rubberband and CellEditor to be *Handler to match the other plugins * move codec classes to where they're used to reduce cyclic dependencies * move codec classes to where they're used to reduce cyclic dependencies * type updates/reorganize layout file structure * renamed various files for consistency * import fixes * renamed GraphHandler SelectionHander and various fixes * convert EventObject parameters to objects * add basic better-docs config * update better-docs config * bugfix for shared variables in Graph persisting across instances * fixed accessing handlers in examples; renamed Model to GraphModel * fixed accessing handlers in examples; renamed Model to GraphModel * restored selection model * bugfix * renamed getModel to getDataModel * changed to use graph.batchUpdate() to reduce lines of code * changed to use graph.batchUpdate() to reduce lines of code * finished annotations+added TypeDoc * convert remaining Cell[] instances to CellArray * convert NaturalDocs links to JSDoc
2022-01-08 01:49:35 +00:00
import Codec from '../mxgraph/io/Codec';
2021-03-25 06:29:07 +00:00
import mxUtils from '../mxgraph/util/mxUtils';
import mxConstants from '../mxgraph/util/mxConstants';
import mxEdgeStyle from '../mxgraph/view/mxEdgeStyle';
// Contains a graph description which will be converted
const HTML_TEMPLATE = `
<h1>Codec</h1>
This example demonstrates dynamically creating a graph from XML and
encoding the model into XML, as well as changing the default style for
edges in-place. This graph is embedded in the page.
<div className="mxgraph" style="position:relative;overflow:auto;">
&lt;mxGraphModel&gt;&lt;root&gt;&lt;mxCell id="0"/&gt;&lt;mxCell
id="1" parent="0"/&gt;&lt;mxCell id="2" vertex="1" parent="1"
value="Interval 1"&gt;&lt;mxGeometry x="380" y="0" width="140"
height="30" as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell id="3"
vertex="1" parent="1" value="Interval 2"&gt;&lt;mxGeometry x="200"
y="80" width="380" height="30"
as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell id="4" vertex="1"
parent="1" value="Interval 3"&gt;&lt;mxGeometry x="40" y="140"
width="260" height="30" as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell
id="5" vertex="1" parent="1" value="Interval 4"&gt;&lt;mxGeometry
x="120" y="200" width="240" height="30"
as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell id="6" vertex="1"
parent="1" value="Interval 5"&gt;&lt;mxGeometry x="420" y="260"
width="80" height="30" as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell
id="7" edge="1" source="2" target="3" parent="1"
value="Transfer1"&gt;&lt;mxGeometry as="geometry"&gt;&lt;Array
as="points"&gt;&lt;Object x="420"
y="60"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;mxCell
id="8" edge="1" source="2" target="6" parent="1"
value=""&gt;&lt;mxGeometry as="geometry" relative="1"
y="-30"&gt;&lt;Array as="points"&gt;&lt;Object x="600"
y="60"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;mxCell
id="9" edge="1" source="3" target="4" parent="1"
value="Transfer3"&gt;&lt;mxGeometry as="geometry"&gt;&lt;Array
as="points"&gt;&lt;Object x="260"
y="120"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;mxCell
id="10" edge="1" source="4" target="5" parent="1"
value="Transfer4"&gt;&lt;mxGeometry as="geometry"&gt;&lt;Array
as="points"&gt;&lt;Object x="200"
y="180"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;mxCell
id="11" edge="1" source="4" target="6" parent="1"
value="Transfer5"&gt;&lt;mxGeometry as="geometry" relative="1"
y="-10"&gt;&lt;Array as="points"&gt;&lt;Object x="460"
y="155"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;/root&gt;&lt;/mxGraphModel&gt;
</div>
This graph is embedded in the page.
<div
className="mxgraph"
style="position:relative;background:#eeeeee;border:1px solid gray;overflow:auto;width:400px;height:200px;"
>
&lt;mxGraphModel&gt;&lt;root&gt;&lt;mxCell id="0"/&gt;&lt;mxCell
id="1" parent="0"/&gt;&lt;mxCell id="2" vertex="1" parent="1"
value="Interval 1"&gt;&lt;mxGeometry x="380" y="0" width="140"
height="30" as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell id="3"
vertex="1" parent="1" value="Interval 2"&gt;&lt;mxGeometry x="200"
y="80" width="380" height="30"
as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell id="4" vertex="1"
parent="1" value="Interval 3"&gt;&lt;mxGeometry x="40" y="140"
width="260" height="30" as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell
id="5" vertex="1" parent="1" value="Interval 4"&gt;&lt;mxGeometry
x="120" y="200" width="240" height="30"
as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell id="6" vertex="1"
parent="1" value="Interval 5"&gt;&lt;mxGeometry x="420" y="260"
width="80" height="30" as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell
id="7" edge="1" source="2" target="3" parent="1"
value="Transfer1"&gt;&lt;mxGeometry as="geometry"&gt;&lt;Array
as="points"&gt;&lt;Object x="420"
y="60"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;mxCell
id="8" edge="1" source="2" target="6" parent="1"
value=""&gt;&lt;mxGeometry as="geometry" relative="1"
y="-30"&gt;&lt;Array as="points"&gt;&lt;Object x="600"
y="60"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;mxCell
id="9" edge="1" source="3" target="4" parent="1"
value="Transfer3"&gt;&lt;mxGeometry as="geometry"&gt;&lt;Array
as="points"&gt;&lt;Object x="260"
y="120"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;mxCell
id="10" edge="1" source="4" target="5" parent="1"
value="Transfer4"&gt;&lt;mxGeometry as="geometry"&gt;&lt;Array
as="points"&gt;&lt;Object x="200"
y="180"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;mxCell
id="11" edge="1" source="4" target="6" parent="1"
value="Transfer5"&gt;&lt;mxGeometry as="geometry" relative="1"
y="-10"&gt;&lt;Array as="points"&gt;&lt;Object x="460"
y="155"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;/root&gt;&lt;/mxGraphModel&gt;
</div>
This graph is embedded in the page.
<div
className="mxgraph"
style="position:relative;background:#eeeeee;border:6px solid gray;overflow:auto;width:400px;height:200px;"
>
&lt;mxGraphModel&gt;&lt;root&gt;&lt;mxCell id="0"/&gt;&lt;mxCell
id="1" parent="0"/&gt;&lt;mxCell id="2" vertex="1" parent="1"
value="Interval 1"&gt;&lt;mxGeometry x="380" y="20" width="140"
height="30" as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell id="3"
vertex="1" parent="1" value="Interval 2"&gt;&lt;mxGeometry x="200"
y="80" width="380" height="30"
as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell id="4" vertex="1"
parent="1" value="Interval 3"&gt;&lt;mxGeometry x="40" y="140"
width="260" height="30" as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell
id="5" vertex="1" parent="1" value="Interval 4"&gt;&lt;mxGeometry
x="120" y="200" width="240" height="30"
as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell id="6" vertex="1"
parent="1" value="Interval 5"&gt;&lt;mxGeometry x="420" y="260"
width="80" height="30" as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell
id="7" edge="1" source="2" target="3" parent="1"
value="Transfer1"&gt;&lt;mxGeometry as="geometry"&gt;&lt;Array
as="points"&gt;&lt;Object x="420"
y="60"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;mxCell
id="8" edge="1" source="2" target="6" parent="1"
value="Transfer2"&gt;&lt;mxGeometry as="geometry" relative="1"
y="0"&gt;&lt;Array as="points"&gt;&lt;Object x="600"
y="60"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;mxCell
id="9" edge="1" source="3" target="4" parent="1"
value="Transfer3"&gt;&lt;mxGeometry as="geometry"&gt;&lt;Array
as="points"&gt;&lt;Object x="260"
y="120"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;mxCell
id="10" edge="1" source="4" target="5" parent="1"
value="Transfer4"&gt;&lt;mxGeometry as="geometry"&gt;&lt;Array
as="points"&gt;&lt;Object x="200"
y="180"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;mxCell
id="11" edge="1" source="4" target="6" parent="1"
value="Transfer5"&gt;&lt;mxGeometry as="geometry" relative="1"
y="-10"&gt;&lt;Array as="points"&gt;&lt;Object x="460"
y="155"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;/root&gt;&lt;/mxGraphModel&gt;
</div>
This graph is embedded in the page.
<div
className="mxgraph"
style="position:relative;overflow:hidden;border:6px solid gray;"
>
&lt;mxGraphModel&gt;&lt;root&gt;&lt;mxCell id="0"/&gt;&lt;mxCell
id="1" parent="0"/&gt;&lt;mxCell id="2" vertex="1" parent="1"
value="Interval 1"&gt;&lt;mxGeometry x="380" y="20" width="140"
height="30" as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell id="3"
vertex="1" parent="1" value="Interval 2"&gt;&lt;mxGeometry x="200"
y="80" width="380" height="30"
as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell id="4" vertex="1"
parent="1" value="Interval 3"&gt;&lt;mxGeometry x="40" y="140"
width="260" height="30" as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell
id="5" vertex="1" parent="1" value="Interval 4"&gt;&lt;mxGeometry
x="120" y="200" width="240" height="30"
as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell id="6" vertex="1"
parent="1" value="Interval 5"&gt;&lt;mxGeometry x="420" y="260"
width="80" height="30" as="geometry"/&gt;&lt;/mxCell&gt;&lt;mxCell
id="7" edge="1" source="2" target="3" parent="1"
value="Transfer1"&gt;&lt;mxGeometry as="geometry"&gt;&lt;Array
as="points"&gt;&lt;Object x="420"
y="60"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;mxCell
id="8" edge="1" source="2" target="6" parent="1"
value="Transfer2"&gt;&lt;mxGeometry as="geometry" relative="1"
y="0"&gt;&lt;Array as="points"&gt;&lt;Object x="600"
y="60"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;mxCell
id="9" edge="1" source="3" target="4" parent="1"
value="Transfer3"&gt;&lt;mxGeometry as="geometry"&gt;&lt;Array
as="points"&gt;&lt;Object x="260"
y="120"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;mxCell
id="10" edge="1" source="4" target="5" parent="1"
value="Transfer4"&gt;&lt;mxGeometry as="geometry"&gt;&lt;Array
as="points"&gt;&lt;Object x="200"
y="180"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;mxCell
id="11" edge="1" source="4" target="6" parent="1"
value="Transfer5"&gt;&lt;mxGeometry as="geometry" relative="1"
y="-10"&gt;&lt;Array as="points"&gt;&lt;Object x="460"
y="155"/&gt;&lt;/Array&gt;&lt;/mxGeometry&gt;&lt;/mxCell&gt;&lt;/root&gt;&lt;/mxGraphModel&gt;
</div>
This graph is embedded in the page.`
2021-03-25 06:29:07 +00:00
const divs = document.getElementsByTagName('*');
2021-03-25 06:29:07 +00:00
for (let i = 0; i < divs.length; i += 1) {
if (divs[i].className.toString().indexOf('mxgraph') >= 0) {
(function(container) {
const xml = mxUtils.getTextContent(container);
const xmlDocument = mxUtils.parseXml(xml);
2021-03-25 06:29:07 +00:00
if (
xmlDocument.documentElement != null &&
xmlDocument.documentElement.nodeName === 'mxGraphModel'
) {
const decoder = new Codec(xmlDocument);
const node = xmlDocument.documentElement;
2021-03-25 06:29:07 +00:00
container.innerHTML = '';
2021-03-25 06:29:07 +00:00
const graph = new mxGraph(container);
graph.centerZoom = false;
graph.setTooltips(false);
graph.setEnabled(false);
2021-03-25 06:29:07 +00:00
// Changes the default style for edges "in-place"
const style = graph.getStylesheet().getDefaultEdgeStyle();
style.edge = mxEdgeStyle.ElbowConnector;
2021-03-25 06:29:07 +00:00
// Enables panning with left mouse button
graph.getPlugin('PanningHandler').useLeftButtonForPanning = true;
graph.getPlugin('PanningHandler').ignoreCell = true;
graph.container.style.cursor = 'move';
graph.setPanning(true);
2021-03-25 06:29:07 +00:00
if (divs[i].style.width === '' && divs[i].style.height === '') {
graph.resizeContainer = true;
} else {
// Adds border for fixed size boxes
graph.border = 20;
}
2021-03-25 06:29:07 +00:00
decoder.decode(node, graph.getDataModel());
graph.resizeContainer = false;
2021-03-25 06:29:07 +00:00
// Adds zoom buttons in top, left corner
const buttons = document.createElement('div');
buttons.style.position = 'absolute';
buttons.style.overflow = 'visible';
2021-03-25 06:29:07 +00:00
const bs = graph.getBorderSizes();
buttons.style.top = `${container.offsetTop + bs.y}px`;
buttons.style.left = `${container.offsetLeft + bs.x}px`;
2021-03-25 06:29:07 +00:00
let left = 0;
const bw = 16;
const bh = 16;
2021-03-25 06:29:07 +00:00
function addButton(label, funct) {
const btn = document.createElement('div');
mxUtils.write(btn, label);
btn.style.position = 'absolute';
btn.style.backgroundColor = 'transparent';
btn.style.border = '1px solid gray';
btn.style.textAlign = 'center';
btn.style.fontSize = '10px';
btn.style.cursor = 'hand';
btn.style.width = `${bw}px`;
btn.style.height = `${bh}px`;
btn.style.left = `${left}px`;
btn.style.top = '0px';
2021-03-25 06:29:07 +00:00
mxEvent.addListener(btn, 'click', function(evt) {
funct();
mxEvent.consume(evt);
});
2021-03-25 06:29:07 +00:00
left += bw;
2021-03-25 06:29:07 +00:00
buttons.appendChild(btn);
}
2021-03-25 06:29:07 +00:00
addButton('+', function() {
graph.zoomIn();
});
2021-03-25 06:29:07 +00:00
addButton('-', function() {
graph.zoomOut();
});
2021-03-25 06:29:07 +00:00
if (container.nextSibling != null) {
container.parentNode.insertBefore(buttons, container.nextSibling);
} else {
container.appendChild(buttons);
}
2021-03-25 06:29:07 +00:00
}
})(divs[i]);
}
2021-03-25 06:29:07 +00:00
}