maxGraph/javascript/examples/stencils.html

333 lines
10 KiB
HTML
Raw Normal View History

2012-05-21 20:32:26 +00:00
<!--
2013-10-28 08:56:36 +00:00
Copyright (c) 2006-2013, JGraph Ltd
2012-05-21 20:32:26 +00:00
Stencils example for mxGraph. This example demonstrates using
an XML file to define new stencils to be used as shapes. See
docs/stencils.xsd for the XML schema file.
-->
<html>
<head>
<title>Stencils example for mxGraph</title>
<!-- Sets the basepath for the library if not in same directory -->
<script type="text/javascript">
mxBasePath = '../src';
</script>
<!-- Loads and initializes the library -->
<script type="text/javascript" src="../src/js/mxClient.js"></script>
<!-- Example code -->
<script type="text/javascript">
// 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 the browser is supported
if (!mxClient.isBrowserSupported())
{
// Displays an error message if the browser is not supported.
mxUtils.error('Browser is not supported!', 200, false);
}
else
{
// Sets the global shadow color
mxConstants.SHADOWCOLOR = '#C0C0C0';
mxConstants.SHADOW_OPACITY = 0.5;
mxConstants.SHADOW_OFFSET_X = 4;
mxConstants.SHADOW_OFFSET_Y = 4;
2013-05-23 16:14:48 +00:00
mxConstants.HANDLE_FILLCOLOR = '#99ccff';
mxConstants.HANDLE_STROKECOLOR = '#0088cf';
mxConstants.VERTEX_SELECTION_COLOR = '#00a8ff';
2012-05-21 20:32:26 +00:00
2014-07-25 06:48:01 +00:00
// Enables connections along the outline
mxConnectionHandler.prototype.outlineConnect = true;
mxEdgeHandler.prototype.manageLabelHandle = true;
mxEdgeHandler.prototype.outlineConnect = true;
mxCellHighlight.prototype.keepOnTop = true;
// Enable rotation handle
mxVertexHandler.prototype.rotationEnabled = true;
2012-05-21 20:32:26 +00:00
// Uses the shape for resize previews
mxVertexHandler.prototype.createSelectionShape = function(bounds)
{
var key = this.state.style[mxConstants.STYLE_SHAPE];
var stencil = mxStencilRegistry.getStencil(key);
var shape = null;
if (stencil != null)
{
2013-05-23 16:14:48 +00:00
shape = new mxShape(stencil);
2012-05-21 20:32:26 +00:00
shape.apply(this.state);
}
else
{
shape = new this.state.shape.constructor();
}
2014-07-25 06:48:01 +00:00
shape.outline = true;
2012-05-21 20:32:26 +00:00
shape.bounds = bounds;
2013-05-23 16:14:48 +00:00
shape.stroke = mxConstants.HANDLE_STROKECOLOR;
2012-05-21 20:32:26 +00:00
shape.strokewidth = this.getSelectionStrokeWidth();
shape.isDashed = this.isSelectionDashed();
2013-05-23 16:14:48 +00:00
shape.isShadow = false;
2012-05-21 20:32:26 +00:00
return shape;
};
2013-07-26 16:59:11 +00:00
// Defines a custom stencil via the canvas API as defined here:
// http://jgraph.github.io/mxgraph/docs/js-api/files/util/mxXmlCanvas2D-js.html
function CustomShape()
{
mxShape.call(this);
};
mxUtils.extend(CustomShape, mxShape);
CustomShape.prototype.paintBackground = function(c, x, y, w, h)
{
c.translate(x, y);
// Head
c.ellipse(w / 4, 0, w / 2, h / 4);
c.fillAndStroke();
c.begin();
c.moveTo(w / 2, h / 4);
c.lineTo(w / 2, 2 * h / 3);
// Arms
c.moveTo(w / 2, h / 3);
c.lineTo(0, h / 3);
c.moveTo(w / 2, h / 3);
c.lineTo(w, h / 3);
// Legs
c.moveTo(w / 2, 2 * h / 3);
c.lineTo(0, h);
c.moveTo(w / 2, 2 * h / 3);
c.lineTo(w, h);
c.end();
c.stroke();
};
// Replaces existing actor shape
2017-11-30 09:21:18 +00:00
mxCellRenderer.registerShape('customShape', CustomShape);
2013-07-26 16:59:11 +00:00
2012-05-21 20:32:26 +00:00
// Loads the stencils into the registry
var req = mxUtils.load('stencils.xml');
var root = req.getDocumentElement();
var shape = root.firstChild;
while (shape != null)
{
if (shape.nodeType == mxConstants.NODETYPE_ELEMENT)
{
mxStencilRegistry.addStencil(shape.getAttribute('name'), new mxStencil(shape));
}
shape = shape.nextSibling;
}
2014-05-05 08:30:00 +00:00
mxEvent.disableContextMenu(container);
2012-05-21 20:32:26 +00:00
// Creates the graph inside the given container
var graph = new mxGraph(container);
graph.setConnectable(true);
2013-05-23 16:14:48 +00:00
graph.setTooltips(true);
2014-05-05 08:30:00 +00:00
graph.setPanning(true);
2013-05-23 16:14:48 +00:00
graph.getTooltipForCell = function(cell)
{
if (cell != null)
{
return cell.style;
}
return null;
};
2012-05-21 20:32:26 +00:00
// Changes default styles
var style = graph.getStylesheet().getDefaultEdgeStyle();
style[mxConstants.STYLE_EDGE] = 'orthogonalEdgeStyle';
style = graph.getStylesheet().getDefaultVertexStyle();
style[mxConstants.STYLE_FILLCOLOR] = '#adc5ff';
style[mxConstants.STYLE_GRADIENTCOLOR] = '#7d85df';
style[mxConstants.STYLE_SHADOW] = '1';
// Enables rubberband selection
new mxRubberband(graph);
// Gets the default parent for inserting new cells. This
// is normally the first child of the root (ie. layer 0).
var parent = graph.getDefaultParent();
// Adds cells to the model in a single step
graph.getModel().beginUpdate();
try
{
var v1 = graph.insertVertex(parent, null, 'A1', 20, 20, 40, 80, 'shape=and');
var v2 = graph.insertVertex(parent, null, 'A2', 20, 220, 40, 80, 'shape=and');
var v3 = graph.insertVertex(parent, null, 'X1', 160, 110, 80, 80, 'shape=xor');
var e1 = graph.insertEdge(parent, null, '', v1, v3);
e1.geometry.points = [new mxPoint(90, 60), new mxPoint(90, 130)];
var e2 = graph.insertEdge(parent, null, '', v2, v3);
e2.geometry.points = [new mxPoint(90, 260), new mxPoint(90, 170)];
2013-07-26 16:59:11 +00:00
var v4 = graph.insertVertex(parent, null, 'A3', 520, 20, 40, 80, 'shape=customShape;flipH=1');
2012-05-21 20:32:26 +00:00
var v5 = graph.insertVertex(parent, null, 'A4', 520, 220, 40, 80, 'shape=and;flipH=1');
var v6 = graph.insertVertex(parent, null, 'X2', 340, 110, 80, 80, 'shape=xor;flipH=1');
var e3 = graph.insertEdge(parent, null, '', v4, v6);
e3.geometry.points = [new mxPoint(490, 60), new mxPoint(130, 130)];
var e4 = graph.insertEdge(parent, null, '', v5, v6);
e4.geometry.points = [new mxPoint(490, 260), new mxPoint(130, 170)];
var v7 = graph.insertVertex(parent, null, 'O1', 250, 260, 80, 60, 'shape=or;direction=south');
var e5 = graph.insertEdge(parent, null, '', v6, v7);
e5.geometry.points = [new mxPoint(310, 150)];
var e6 = graph.insertEdge(parent, null, '', v3, v7);
e6.geometry.points = [new mxPoint(270, 150)];
var e7 = graph.insertEdge(parent, null, '', v7, v5);
e7.geometry.points = [new mxPoint(290, 370)];
}
finally
{
// Updates the display
graph.getModel().endUpdate();
}
document.body.appendChild(mxUtils.button('FlipH', function()
{
2013-05-23 16:14:48 +00:00
graph.toggleCellStyles(mxConstants.STYLE_FLIPH);
2012-05-21 20:32:26 +00:00
}));
document.body.appendChild(mxUtils.button('FlipV', function()
{
2013-05-23 16:14:48 +00:00
graph.toggleCellStyles(mxConstants.STYLE_FLIPV);
2012-05-21 20:32:26 +00:00
}));
document.body.appendChild(document.createTextNode('\u00a0'));
document.body.appendChild(document.createTextNode('\u00a0'));
document.body.appendChild(document.createTextNode('\u00a0'));
document.body.appendChild(document.createTextNode('\u00a0'));
document.body.appendChild(mxUtils.button('Rotate', function()
{
var cell = graph.getSelectionCell();
if (cell != null)
{
var geo = graph.getCellGeometry(cell);
if (geo != null)
{
graph.getModel().beginUpdate();
try
{
// Rotates the size and position in the geometry
geo = geo.clone();
geo.x += geo.width / 2 - geo.height / 2;
geo.y += geo.height / 2 - geo.width / 2;
var tmp = geo.width;
geo.width = geo.height;
geo.height = tmp;
graph.getModel().setGeometry(cell, geo);
// Reads the current direction and advances by 90 degrees
var state = graph.view.getState(cell);
if (state != null)
{
var dir = state.style[mxConstants.STYLE_DIRECTION] || 'east'/*default*/;
if (dir == 'east')
{
dir = 'south';
}
else if (dir == 'south')
{
dir = 'west';
}
else if (dir == 'west')
{
dir = 'north';
}
else if (dir == 'north')
{
dir = 'east';
}
graph.setCellStyles(mxConstants.STYLE_DIRECTION, dir, [cell]);
}
}
finally
{
graph.getModel().endUpdate();
}
}
}
}));
document.body.appendChild(document.createTextNode('\u00a0'));
document.body.appendChild(document.createTextNode('\u00a0'));
document.body.appendChild(document.createTextNode('\u00a0'));
document.body.appendChild(document.createTextNode('\u00a0'));
document.body.appendChild(mxUtils.button('And', function()
{
graph.setCellStyles(mxConstants.STYLE_SHAPE, 'and');
}));
document.body.appendChild(mxUtils.button('Or', function()
{
graph.setCellStyles(mxConstants.STYLE_SHAPE, 'or');
}));
document.body.appendChild(mxUtils.button('Xor', function()
{
graph.setCellStyles(mxConstants.STYLE_SHAPE, 'xor');
}));
2013-05-23 16:14:48 +00:00
document.body.appendChild(document.createTextNode('\u00a0'));
document.body.appendChild(document.createTextNode('\u00a0'));
document.body.appendChild(document.createTextNode('\u00a0'));
document.body.appendChild(document.createTextNode('\u00a0'));
document.body.appendChild(mxUtils.button('Style', function()
{
var cell = graph.getSelectionCell();
if (cell != null)
{
var style = mxUtils.prompt('Style', graph.getModel().getStyle(cell));
if (style != null)
{
graph.getModel().setStyle(cell, style);
}
}
}));
2014-05-05 08:30:00 +00:00
document.body.appendChild(mxUtils.button('+', function()
{
graph.zoomIn();
}));
document.body.appendChild(mxUtils.button('-', function()
{
graph.zoomOut();
}));
2012-05-21 20:32:26 +00:00
}
};
</script>
</head>
2012-07-20 10:35:30 +00:00
<!-- Page passes the container for the graph to the program -->
2012-05-21 20:32:26 +00:00
<body onload="main(document.getElementById('graphContainer'))">
<!-- Creates a container for the graph with a grid wallpaper -->
<div id="graphContainer"
2012-12-18 13:09:38 +00:00
style="position:relative;overflow:hidden;width:601px;height:401px;background:url('editors/images/grid.gif');cursor:default;">
2012-05-21 20:32:26 +00:00
</div>
</body>
</html>