js画图开发库--mxgraph--[scrollbars-选择条.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>
<style type="text/css" media="screen">
table.title {
border-color: black;
border-collapse: collapse;
cursor: move;
}
table.title th {
font-size: 10pt;
font-family: Verdana;
white-space: nowrap;
background: lightgray;
font-weight: bold;
}
table.erd {
font-size: 10pt;
font-family: Verdana;
border-color: black;
border-collapse: collapse;
overflow: auto;
cursor: move;
white-space: nowrap;
background: #C3D9FF;
}
table.erd td {
border-color: black;
}
</style>
<!-- 如果本文件的包与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)
{
// 浏览器兼容检测
if (!mxClient.isBrowserSupported())
{
mxUtils.error('Browser is not supported!', 200, false);
}
else
{
// 移动时网格对齐
mxConnectionHandler.prototype.movePreviewAway = true;
// 版本样式选择
mxClient.NO_FO = mxClient.NO_FO || mxClient.IS_GC || mxClient.IS_SF;
// 启用移动预览
mxGraphHandler.prototype.htmlPreview = true;
// 移出连接线在顶部
mxConnectionHandler.prototype.moveIconFront = true;
// 定义悬停时显示的连接线图标
mxConnectionHandler.prototype.connectImage = new mxImage('images/connector.gif', 16, 16);
// 覆盖连接目标周围的高亮边框
mxConnectionHandler.prototype.getTargetPerimeterPoint = function(state, me)
{
// 通过使用当前的行更新行中指定的节点确定的目标周界点的y坐标
var y = me.getY();
if (this.currentRowNode != null)
{
y = getRowY(state, this.currentRowNode);
}
// 检查侧边的终端离开
var x = state.x;
if (this.previous.getCenterX() > state.getCenterX())
{
x += state.width;
}
return new mxPoint(x, y);
};
// 覆盖源周围的连接点预览
mxConnectionHandler.prototype.getSourcePerimeterPoint = function(state, next, me)
{
var y = me.getY();
if (this.sourceRowNode != null)
{
y = getRowY(state, this.sourceRowNode);
}
//检查侧边的终端离开
var x = state.x;
if (next.x > state.getCenterX())
{
x += state.width;
}
return new mxPoint(x, y);
};
// 禁止连接到无效行
mxConnectionHandler.prototype.isValidTarget = function(cell)
{
return this.currentRowNode != null;
};
// 在容器中创建图形
var graph = new mxGraph(container);
// 更改默认样式
graph.stylesheet.getDefaultVertexStyle()[mxConstants.STYLE_VERTICAL_ALIGN] = mxConstants.ALIGN_TOP;
graph.stylesheet.getDefaultVertexStyle()[mxConstants.STYLE_PERIMETER] =
mxPerimeter.EntityPerimeter;
graph.stylesheet.getDefaultVertexStyle()[mxConstants.STYLE_SHADOW] = true;
delete graph.stylesheet.getDefaultVertexStyle()[mxConstants.STYLE_STROKECOLOR];
// 用于HTML标签
graph.stylesheet.getDefaultVertexStyle()[mxConstants.STYLE_OVERFLOW] = 'fill';
// 使用实体边框样式作为默认
graph.stylesheet.getDefaultEdgeStyle()[mxConstants.STYLE_EDGE] =
mxEdgeStyle.EntityRelation;
graph.stylesheet.getDefaultEdgeStyle()[mxConstants.STYLE_STROKECOLOR] = 'black';
graph.stylesheet.getDefaultEdgeStyle()[mxConstants.STYLE_FONTCOLOR] = 'black';
// 允许新的连接,但不允许现有的连接被改变成简化的
graph.setCellsDisconnectable(false);
graph.setAllowDanglingEdges(false);
graph.setCellsEditable(false);
graph.setConnectable(true);
// 折叠
graph.isCellFoldable = function(cell, collapse)
{
return this.getModel().isVertex(cell);
};
// 连接状态
graph.isCellConnectable = function(cell)
{
return !this.isCellCollapsed(cell);
};
// 允许HTML标记中的所有标签
graph.setHtmlLabels(true);
// 滚动事件
graph.cellRenderer.isLabelEvent = function(state, evt)
{
var source = mxEvent.getSource(evt);
// FIXME: 在GC中没有滚动事件
return state.text != null &&
source != state.text.node &&
source != state.text.node.getElementsByTagName('div')[0];
};
// 添加滚动条的最外层的div和保持DIV的位置和大小相同的作为顶点
var oldRedrawLabel = graph.cellRenderer.redrawLabel;
graph.cellRenderer.redrawLabel = function(state)
{
oldRedrawLabel.apply(this, arguments); // "supercall"
var graph = state.view.graph;
if (graph.getModel().isVertex(state.cell) && state.text != null)
{
// 滚动条的div
var s = graph.view.scale;
var div = state.text.node.getElementsByTagName('div')[0];
if (div != null)
{
// 添加标题的表格单元格的高度
var oh = 0;
if (div.previousSibling != null)
{
oh = div.previousSibling.firstChild.firstChild.firstChild.offsetHeight;
}
div.style.width = (state.width / s) + 'px';
div.style.height = ((state.height / s) - oh) + 'px';
div.style.zoom = s;
// 安装更新连接的边的监听程序
if (div.scrollHandler == null)
{
div.scrollHandler = true;
mxEvent.addListener(div, 'scroll',
mxUtils.bind(this, function(evt)
{
graph.view.invalidate(state.cell, false, true);
graph.view.validate();
})
);
mxEvent.addListener(div, 'mouseup',
mxUtils.bind(this, function(evt)
{
if (!this.isLabelEvent(state, evt))
{
graph.view.invalidate(state.cell, false, true);
graph.view.validate();
}
})
);
}
}
}
};
// 添加一个新的功能来更新当前行根据给定的事件,并返回该行的DOM节点
graph.connectionHandler.updateRow = function(target)
{
while (target != null && target.nodeName != 'TR')
{
target = target.parentNode;
}
this.currentRow = null;
// 检查如果我们处理正确的表中的行
if (target != null && target.parentNode.parentNode.className == 'erd')
{
// 存储当前行号在一个属性,以便它可以被检索到创建的预览和最终边缘
var rowNumber = 0;
var current = target.parentNode.firstChild;
while (target != current && current != null)
{
current = current.nextSibling;
rowNumber++;
}
this.currentRow = rowNumber + 1;
}
else
{
target = null;
}
return target;
};
// 添加位置的连接图标上的鼠标事件的目标(行)
graph.connectionHandler.updateIcons = function(state, icons, me)
{
var target = me.getSource();
target = this.updateRow(target);
if (target != null && this.currentRow != null)
{
var div = target.parentNode.parentNode.parentNode;
icons[0].node.style.visibility = 'visible';
icons[0].bounds.x = state.x + target.offsetLeft + Math.min(state.width,
target.offsetWidth) - this.icons[0].bounds.width - 2;
icons[0].bounds.y = state.y + target.offsetTop + target.offsetHeight / 2 -
this.icons[0].bounds.height / 2 - div.scrollTop + div.offsetTop;
icons[0].redraw();
this.currentRowNode = target;
}
else
{
icons[0].node.style.visibility = 'hidden';
}
};
// 在预览边缘状态更新目标行
var oldMouseMove = graph.connectionHandler.mouseMove;
graph.connectionHandler.mouseMove = function(sender, me)
{
if (this.edgeState != null)
{
this.currentRowNode = this.updateRow(me.getSource());
if (this.currentRow != null)
{
this.edgeState.cell.value.setAttribute('targetRow', this.currentRow);
}
else
{
this.edgeState.cell.value.setAttribute('targetRow', '0');
}
// 销毁通过图像在IE浏览器的图标,以防止事件重定向
if (this.selectedIcon != null)
{
this.selectedIcon.destroy();
this.selectedIcon = null;
}
}
oldMouseMove.apply(this, arguments);
};
// 创建可用于预览的边缘状态
graph.connectionHandler.createEdgeState = function(me)
{
var relation = doc.createElement('Relation');
relation.setAttribute('sourceRow', this.currentRow || '0');
relation.setAttribute('targetRow', '0');
var edge = this.createEdge(relation);
var style = this.graph.getCellStyle(edge);
var state = new mxCellState(this.graph.view, edge, style);
// Stores the source row in the handler
this.sourceRowNode = this.currentRowNode;
return state;
};
// 返回空边和销毁的单元格标记的标签。
graph.getLabel = function(cell)
{
if (this.getModel().isVertex(cell))
{
if (this.isCellCollapsed(cell))
{
return '<table width="100%" border="1" cellpadding="4" class="title">' +
'<tr><th colspan="2">Customers</th></tr>' +
'</table>';
}
else
{
return '<table width="100%" border="1" cellpadding="4" class="title">' +
'<tr><th colspan="2">Customers</th></tr>' +
'</table>'+
'<div style="overflow:auto;cursor:default;">'+
'<table width="100%" height="100%" border="1" cellpadding="4" class="erd">' +
'<tr><td>' +
'<img align="center" src="images/key.png"/>' +
'<img align="center" src="images/plus.png"/>' +
'</td><td>' +
'<u>customerId</u></td></tr><tr><td></td><td>number</td></tr>' +
'<tr><td></td><td>firstName</td></tr><tr><td></td><td>lastName</td></tr>' +
'<tr><td></td><td>streetAddress</td></tr><tr><td></td><td>city</td></tr>' +
'<tr><td></td><td>state</td></tr><tr><td></td><td>zip</td></tr>' +
'</table></div>';
}
}
else
{
return '';
}
};
// 用于各个单元格的用户对象(数据)
var doc = mxUtils.createXmlDocument();
// 同样应该被用于创建XML节点表的描述和行(最可能是作为子节点)
var relation = doc.createElement('Relation');
relation.setAttribute('sourceRow', '4');
relation.setAttribute('targetRow', '6');
// 浏览器默认菜单
new mxRubberband(graph);
// 添加按键监听处理程序
new mxKeyHandler(graph);
// 创建默认窗体
var parent = graph.getDefaultParent();
// 更新事务开始
var width = 160;
graph.getModel().beginUpdate();
try
{
var v1 = graph.insertVertex(parent, null, '', 20, 20, width, 0);
var v2 = graph.insertVertex(parent, null, '', 400, 150, width, 0);
var e1 = graph.insertEdge(parent, null, relation, v1, v2);
// 更新单元格的高度(覆盖宽度表格的宽度设置为100%)
graph.updateCellSize(v1);
v1.geometry.width = width;
v1.geometry.alternateBounds = new mxRectangle(0, 0, width, 27);
//更新单元格的高度(覆盖宽度表格的宽度设置为100%)
graph.updateCellSize(v2);
v2.geometry.width = width;
v2.geometry.alternateBounds = new mxRectangle(0, 0, width, 27);
}
finally
{
// 更新结束
graph.getModel().endUpdate();
}
}
};
// 实现鼠标悬停表格中间出现的图标
mxGraphView.prototype.updateFloatingTerminalPoint = function(edge, start, end, source)
{
var next = this.getNextPoint(edge, end, source);
var div = start.text.node.getElementsByTagName('div')[0];
var x = start.x;
var y = start.getCenterY();
// 检查侧栏的终端离开
if (next.x > x + start.width / 2)
{
x += start.width;
}
if (div != null)
{
y = start.getCenterY() - div.scrollTop;
if (mxUtils.isNode(edge.cell.value) && !this.graph.isCellCollapsed(start.cell))
{
var attr = (source) ? "sourceRow" : "targetRow";
var row = parseInt(edge.cell.value.getAttribute(attr));
// HTML标签包含一个外部内置表
var table = div.getElementsByTagName('table')[0];
var trs = table.getElementsByTagName('tr');
var tr = trs[Math.min(trs.length - 1, row - 1)];
// 获取源或目标行的垂直中心
if (tr != null)
{
y = getRowY(start, tr);
}
}
// 保持垂直坐标
y = Math.min(start.y + start.height, Math.max(start.y + div.offsetTop, y));
// 更新的最近点的垂直位置,如果我们不处理连接的预览,在这种情况下,无论edgeState或absolutePoints为空
if (edge != null && edge.absolutePoints != null)
{
next.y = y;
}
}
edge.setAbsoluteTerminalPoint(new mxPoint(x, y), source);
// 沿着共同的航点的航线多个入边的边缘有一个共同的目标行
if (source && mxUtils.isNode(edge.cell.value) && start != null && end != null)
{
var edges = this.graph.getEdgesBetween(start.cell, end.cell, true);
var tmp = [];
// 过滤器的边缘具有相同的源行
var row = edge.cell.value.getAttribute('targetRow');
for (var i = 0; i < edges.length; i++)
{
if (mxUtils.isNode(edges[i].value) &&
edges[i].value.getAttribute('targetRow') == row)
{
tmp.push(edges[i]);
}
}
edges = tmp;
if (edges.length > 1 && edge.cell == edges[edges.length - 1])
{
// 查找的垂直中心
var states = [];
var y = 0;
for (var i = 0; i < edges.length; i++)
{
states[i] = this.getState(edges[i]);
y += states[i].absolutePoints[0].y;
}
y /= edges.length;
for (var i = 0; i < states.length; i++)
{
var x = states[i].absolutePoints[1].x;
if (states[i].absolutePoints.length < 5)
{
states[i].absolutePoints.splice(2, 0, new mxPoint(x, y));
}
else
{
states[i].absolutePoints[2] = new mxPoint(x, y);
}
// 必须重绘以前的边缘的变化点
if (i < states.length - 1)
{
this.graph.cellRenderer.redraw(states[i]);
}
}
}
}
};
// 定义全局的辅助功能得到y坐标对于一个给定的单元格的状态和行
var getRowY = function(state, tr)
{
var div = tr.parentNode.parentNode.parentNode;
var y = state.y + tr.offsetTop + tr.offsetHeight / 2 - div.scrollTop + div.offsetTop;
y = Math.min(state.y + state.height, Math.max(state.y + div.offsetTop, y));
return y;
};
</script>
</head>
<!-- 页面载入时启动程序 -->
<body οnlοad="main(document.getElementById('graphContainer'))">
<!-- 创建一个带网格壁纸的容器。宽度,高度和光标的风格是仅适用于IE -->
<div id="graphContainer"
style="cursor:default;height:100%;width:100%;position:absolute;top:0px;left:0px;bottom:0px;right:0px;background:url('editors/images/grid.gif')">
</div>
</body>
</html>