js画图开发库--mxgraph--[portrefs-按接口连接.html]

 js画图开发库--mxgraph--[portrefs-按接口连接.html] 

 

 
js画图开发库--mxgraph--[portrefs-按接口连接.html]
 

<!Doctype html>
<html xmlns=http://www.w3.org/1999/xhtml>
	<head>
	<meta http-equiv=Content-Type content="text/html;charset=utf-8">
	<title>按接口连接</title>

	<!-- 如果本文件的包与src不是在同一个目录,就要将basepath设置到src目录下 -->
	<script type="text/javascript">
		mxBasePath = '../src';
	</script>

	<!-- 引入支持库文件 -->
	<script type="text/javascript" src="../src/js/mxClient.js"></script>
	
	<!-- 示例代码 -->
	<script type="text/javascript">
		function main(container)
		{
			// 替换接口图片
			mxConstraintHandler.prototype.pointImage = new mxImage('images/dot.gif', 10, 10);
			
			var graph = new mxGraph(container);
			graph.setConnectable(true);
			
			// 禁用端口自动处理。这将禁用mxGraph.cellConnected的复位功能。请注意,此功能可能是有用的,如果浮动和固定连接相结合。 
			graph.setPortsEnabled(false);
			
			// 启动浏览器默认右键菜单
			new mxRubberband(graph);
			
			// 创建默认窗口
			var parent = graph.getDefaultParent();

			// 各接口状态相同
			var ports = new Array();
			
			// NOTE: Constraint is used later for orthogonal edge routing (currently ignored)
			ports['w'] = {x: 0, y: 0.5, perimeter: true, constraint: 'west'};
			ports['e'] = {x: 1, y: 0.5, perimeter: true, constraint: 'east'};
			ports['n'] = {x: 0.5, y: 0, perimeter: true, constraint: 'north'};
			ports['s'] = {x: 0.5, y: 1, perimeter: true, constraint: 'south'};
			ports['nw'] = {x: 0, y: 0, perimeter: true, constraint: 'north west'};
			ports['ne'] = {x: 1, y: 0, perimeter: true, constraint: 'north east'};
			ports['sw'] = {x: 0, y: 1, perimeter: true, constraint: 'south west'};
			ports['se'] = {x: 1, y: 1, perimeter: true, constraint: 'south east'};

			// 创建接口数组
			var ports2 = new Array();
			
			// NOTE: 用于连接时的点
			ports2['in1'] = {x: 0, y: 0, perimeter: true, constraint: 'west'};
			ports2['in2'] = {x: 0, y: 0.25, perimeter: true, constraint: 'west'};
			ports2['in3'] = {x: 0, y: 0.5, perimeter: true, constraint: 'west'};
			ports2['in4'] = {x: 0, y: 0.75, perimeter: true, constraint: 'west'};
			ports2['in5'] = {x: 0, y: 1, perimeter: true, constraint: 'west'};

			ports2['out1'] = {x: 0.5, y: 0, perimeter: true, constraint: 'north east'};
			ports2['out2'] = {x: 1, y: 0.5, perimeter: true, constraint: 'east'};
			ports2['out3'] = {x: 0.5, y: 1, perimeter: true, constraint: 'south east'};
			
			//返回端口
			mxShape.prototype.getPorts = function()
			{
				return ports;
			};

			mxTriangle.prototype.getPorts = function()
			{
				return ports2;
			};

			// 禁止浮动自动连接
			graph.connectionHandler.isConnectableCell = function(cell)
			{
			   return false;
			};
			mxEdgeHandler.prototype.isConnectableCell = function(cell)
			{
				return graph.connectionHandler.isConnectableCell(cell);
			};
			
			// 禁用现有的端口功能
			graph.view.getTerminalPort = function(state, terminal, source)
			{
				return terminal;
			};
			
			// 对于一个给定的元素返回的所有可能的端口
			graph.getAllConnectionConstraints = function(terminal, source)
			{
				if (terminal != null && terminal.shape != null &&
					terminal.shape instanceof mxStencilShape)
				{
					// for stencils with existing constraints...
					if (terminal.shape.stencil != null)
					{
						return terminal.shape.stencil.constraints;
					}
				}
				else if (terminal != null && this.model.isVertex(terminal.cell))
				{
					if (terminal.shape != null)
					{
						var ports = terminal.shape.getPorts();
						var cstrs = new Array();
						
						for (var id in ports)
						{
							var port = ports[id];
							
							var cstr = new mxConnectionConstraint(new mxPoint(port.x, port.y), port.perimeter);
							cstr.id = id;
							cstrs.push(cstr);
						}
						
						return cstrs;
					}
				}
				
				return null;
			};
			
			// 将端口设置为给定的连接
			graph.setConnectionConstraint = function(edge, terminal, source, constraint)
			{
				if (constraint != null)
				{
					var key = (source) ? mxConstants.STYLE_SOURCE_PORT : mxConstants.STYLE_TARGET_PORT;
					
					if (constraint == null || constraint.id == null)
					{
						this.setCellStyles(key, null, [edge]);
					}
					else if (constraint.id != null)
					{
						this.setCellStyles(key, constraint.id, [edge]);
					}
				}
			};
			
			// 返回在给定的连接的端口
			graph.getConnectionConstraint = function(edge, terminal, source)
			{
				var key = (source) ? mxConstants.STYLE_SOURCE_PORT : mxConstants.STYLE_TARGET_PORT;
				var id = edge.style[key];
				
				if (id != null)
				{
					var c =  new mxConnectionConstraint(null, null);
					c.id = id;
					
					return c;
				}
				
				return null;
			};

			// 返回重定向约束的实际端口
			graphGetConnectionPoint = graph.getConnectionPoint;
			graph.getConnectionPoint = function(vertex, constraint)
			{
				if (constraint.id != null && vertex != null && vertex.shape != null)
				{
					var port = vertex.shape.getPorts()[constraint.id];
					
					if (port != null)
					{
						constraint = new mxConnectionConstraint(new mxPoint(port.x, port.y), port.perimeter);
					}
				}
				
				return graphGetConnectionPoint.apply(this, arguments);
			};
				
			// 开始更新事务
			graph.getModel().beginUpdate();
			try
			{
				var v1 = graph.insertVertex(parent, null, 'A', 20, 20, 100, 40);
				var v2 = graph.insertVertex(parent, null, 'B', 80, 100, 100, 100,
						'shape=ellipse;perimeter=ellipsePerimeter');
				var v3 = graph.insertVertex(parent, null, 'C', 190, 30, 100, 60,
						'shape=triangle;perimeter=trianglePerimeter;direction=south');
				var e1 = graph.insertEdge(parent, null, '', v1, v2, 'sourcePort=s;targetPort=nw');
				var e2 = graph.insertEdge(parent, null, '', v1, v3, 'sourcePort=e;targetPort=out3');
			}
			finally
			{
				// 结束更新事务
				graph.getModel().endUpdate();
			}
			
			// 连接线的折线风格
			/*
			graph.getStylesheet().getDefaultEdgeStyle()['edgeStyle'] = 'orthogonalEdgeStyle';
			var mxUtilsGetPortConstraints = mxUtils.getPortConstraints;
			mxUtils.getPortConstraints = function(terminal, edge, source, defaultValue)
			{
				var key = (source) ? mxConstants.STYLE_SOURCE_PORT : mxConstants.STYLE_TARGET_PORT;
				var id = edge.style[key];
				
				var port = terminal.shape.getPorts()[id];
				
				// TODO: Add support for rotation, direction
				if (port != null)
				{
					return port.constraint;
				}
				
				return mxUtilsGetPortConstraints.apply(this, arguments);
			};
			// Connect preview
			graph.connectionHandler.createEdgeState = function(me)
			{
				var edge = graph.createEdge(null, null, null, null, null);
				
				return new mxCellState(this.graph.view, edge, this.graph.getCellStyle(edge));
			};
			*/
		};
	</script>
</head>
<!-- 页面载入时启动程序 -->
<body onload="main(document.getElementById('graphContainer'))">
	<!-- 创建带网格壁纸和曲线的一个容器  -->
	<div id="graphContainer"
		style="overflow:hidden;position:relative;width:321px;height:241px;background:url('editors/images/grid.gif');cursor:default;">
	</div>
</body>
</html>

 

 

 

你可能感兴趣的:(js画图开发库,mxgraph,portrefs,按接口连接.html])