js畫圖開發庫--mxgraph--[scrollbars-選擇條.html]
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5SNhRWZ1UWZkFWZ1cTL3I2N40SY2E2MtcTOkVWLxIWNlNzMyEzLcVDMwYzLcBDOwAzLcRnbl1GajFGd0F2LcRWYvxGc19CXt92YuUWelRXauwGZvw1LcpDc0RHaiojIsJye.png)
<!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%" cellpadding="4" class="title">' +
'<tr><th colspan="2">Customers</th></tr>' +
'</table>';
}
else
{
return '<table width="100%" cellpadding="4" class="title">' +
'<tr><th colspan="2">Customers</th></tr>' +
'</table>'+
'<div style="overflow:auto;cursor:default;">'+
'<table width="100%" height="100%" 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>