js畫圖開發庫--mxgraph--[groups-組.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">
//覆寫檢查是否根節點
mxGraph.prototype.isValidRoot = function()
{
return false;
};
// 如果選擇多個單元格, 不清除選擇
var graphHandlerMouseDown = mxGraphHandler.prototype.mouseDown;
mxGraphHandler.prototype.mouseDown = function(sender, me)
{
graphHandlerMouseDown.apply(this, arguments);
if (this.graph.isCellSelected(me.getCell()) && this.graph.getSelectionCount() > 1)
{
this.delayedSelection = false;
}
};
// 覆寫元素初始化事件
var graphHandlerGetInitialCellForEvent = mxGraphHandler.prototype.getInitialCellForEvent;
mxGraphHandler.prototype.getInitialCellForEvent = function(me)
{
var model = this.graph.getModel();
var psel = model.getParent(this.graph.getSelectionCell());
var cell = graphHandlerGetInitialCellForEvent.apply(this, arguments);
var parent = model.getParent(cell);
if (psel == null || (psel != cell && psel != parent))
{
while (!this.graph.isCellSelected(cell) && !this.graph.isCellSelected(parent) &&
model.isVertex(parent) && !this.graph.isValidRoot(parent))
{
cell = parent;
parent = this.graph.getModel().getParent(cell);
}
}
return cell;
};
// 滑鼠延遲選擇子元素
var graphHandlerIsDelayedSelection = mxGraphHandler.prototype.isDelayedSelection;
mxGraphHandler.prototype.isDelayedSelection = function(cell)
{
var result = graphHandlerIsDelayedSelection.apply(this, arguments);
var model = this.graph.getModel();
var psel = model.getParent(this.graph.getSelectionCell());
var parent = model.getParent(cell);
if (psel == null || (psel != cell && psel != parent))
{
if (!this.graph.isCellSelected(cell) && model.isVertex(parent) && !this.graph.isValidRoot(parent))
{
result = true;
}
}
return result;
};
// 元素組延遲選擇
mxGraphHandler.prototype.selectDelayed = function(me)
{
var cell = me.getCell();
if (cell == null)
{
cell = this.cell;
}
var model = this.graph.getModel();
var parent = model.getParent(cell);
while (this.graph.isCellSelected(cell) && model.isVertex(parent) && !this.graph.isValidRoot(parent))
{
cell = parent;
parent = model.getParent(cell);
}
this.graph.selectCellForEvent(cell, me.getEvent());
};
// 傳回最後標明的父節點
mxPanningHandler.prototype.getCellForPopupEvent = function(me)
{
var cell = me.getCell();
var model = this.graph.getModel();
var parent = model.getParent(cell);
while (model.isVertex(parent) && !this.graph.isValidRoot(parent))
{
if (this.graph.isCellSelected(parent))
{
cell = parent;
}
parent = model.getParent(parent);
}
return cell;
};
// 程式在此啟動
function main(container)
{
// 浏覽器相容性檢測
if (!mxClient.isBrowserSupported())
{
mxUtils.error('Browser is not supported!', 200, false);
}
else
{
// 去鋸齒效果
mxRectangleShape.prototype.crisp = true;
// 在容器中建立圖形
var graph = new mxGraph(container);
graph.constrainChildren = false;
graph.extendParents = false;
graph.extendParentsOnAdd = false;
// 可選擇畫布自适應
//graph.setResizeContainer(true);
// 啟用浏覽器預設右鍵下拉清單
new mxRubberband(graph);
// 建立預設窗體
var parent = graph.getDefaultParent();
// 開啟更新事務
graph.getModel().beginUpdate();
try
{
var v1 = graph.insertVertex(parent, null, 'Hello,', 20, 20, 120, 60);
var v2 = graph.insertVertex(v1, null, 'World!', 90, 20, 60, 20);
}
finally
{
// 結束更新事務
graph.getModel().endUpdate();
}
}
};
</script>
</head>
<!-- 頁面載入時啟動程式 -->
<body οnlοad="main(document.getElementById('graphContainer'))">
<!-- 建立帶網格桌面和曲線的一個容器 -->
<div id="graphContainer"
style="overflow:hidden;width:321px;height:241px;background:url('editors/images/grid.gif');cursor:default;">
</div>
</body>
</html>