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>