js画图开发库--mxgraph--[permissions-权限.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()
{
// 检查浏览器支持
if (!mxClient.isBrowserSupported())
{
mxUtils.error('Browser is not supported!', 200, false);
}
else
{
// 去锯齿效果 in SVG
mxRectangleShape.prototype.crisp = true;
// 定义创建新的连接,在连接处理程序中的图标。 这将自动禁用高亮显示的源点。
mxConnectionHandler.prototype.connectImage = new mxImage('images/connector.gif', 16, 16);
// 在图形中创建容器
var container = document.createElement('div');
container.style.position = 'absolute';
container.style.overflow = 'hidden';
container.style.left = '00px';
container.style.top = '40px';
container.style.right = '0px';
container.style.bottom = '0px';
container.style.background = 'url("editors/images/grid.gif")';
document.body.appendChild(container);
// IE浏览器支持
if (mxClient.IS_QUIRKS)
{
document.body.style.overflow = 'hidden';
new mxDivResizer(container);
}
// 在天气中创建图形
var graph = new mxGraph(container);
// 启用提示工具
graph.setMultigraph(false);
graph.setAllowLoops(true);
// 使用默认下拉菜单和权限管理
var rubberband = new mxRubberband(graph);
var keyHandler = new mxKeyHandler(graph);
// 启用删除按钮
keyHandler.bindKey(46, function(evt)
{
if (graph.isEnabled())
{
graph.removeCells();
}
});
// 私有变量之间共享变量
var currentPermission = null;
var apply = function(permission)
{
graph.clearSelection();
permission.apply(graph);
graph.setEnabled(true);
graph.setTooltips(true);
// 在小范围内显示图标 - 如果显示大图会非常慢
graph.refresh();
currentPermission = permission;
};
apply(new Permission());
// 所有权限
var button = mxUtils.button('所有权限Allow All', function(evt)
{
apply(new Permission());
});
document.body.appendChild(button);
// 仅连接权限
var button = mxUtils.button('仅连接权限Connect Only', function(evt)
{
apply(new Permission(false, true, false, false, true));
});
document.body.appendChild(button);
// 仅连接线可变
var button = mxUtils.button('仅连接线可变 Edges Only', function(evt)
{
apply(new Permission(false, false, true, false, false));
});
document.body.appendChild(button);
// 仅元素可变
var button = mxUtils.button('仅元素可变Vertices Only', function(evt)
{
apply(new Permission(false, false, false, true, false));
});
document.body.appendChild(button);
// 仅可被选择
var button = mxUtils.button('仅可被选择Select Only', function(evt)
{
apply(new Permission(false, false, false, false, false));
});
document.body.appendChild(button);
// 锁定
var button = mxUtils.button('锁定Locked', function(evt)
{
apply(new Permission(true, false));
});
document.body.appendChild(button);
// 已禁用
var button = mxUtils.button('已禁用Disabled', function(evt)
{
graph.clearSelection();
graph.setEnabled(false);
graph.setTooltips(false);
});
document.body.appendChild(button);
// 继承回调函数使用许可对象。这可以通过分配各开关(如setMovable),这种方法是更灵活的,
// 不覆盖任何现有的行为或设置,并允许中要使用的功能的动态条件。查看更多的功能扩展(如isSelectable)的规范。
var oldDisconnectable = graph.isCellDisconnectable;
graph.isCellDisconnectable = function(cell, terminal, source)
{
return oldDisconnectable.apply(this, arguments) &&
currentPermission.editEdges;
};
var oldTerminalPointMovable = graph.isTerminalPointMovable;
graph.isTerminalPointMovable = function(cell)
{
return oldTerminalPointMovable.apply(this, arguments) &&
currentPermission.editEdges;
};
var oldBendable = graph.isCellBendable;
graph.isCellBendable = function(cell)
{
return oldBendable.apply(this, arguments) &&
currentPermission.editEdges;
};
var oldLabelMovable = graph.isLabelMovable;
graph.isLabelMovable = function(cell)
{
return oldLabelMovable.apply(this, arguments) &&
currentPermission.editEdges;
};
var oldMovable = graph.isCellMovable;
graph.isCellMovable = function(cell)
{
return oldMovable.apply(this, arguments) &&
currentPermission.editVertices;
};
var oldResizable = graph.isCellResizable;
graph.isCellResizable = function(cell)
{
return oldResizable.apply(this, arguments) &&
currentPermission.editVertices;
};
var oldEditable = graph.isCellEditable;
graph.isCellEditable = function(cell)
{
return oldEditable.apply(this, arguments) &&
(this.getModel().isVertex(cell) &&
currentPermission.editVertices) ||
(this.getModel().isEdge(cell) &&
currentPermission.editEdges);
};
var oldDeletable = graph.isCellDeletable;
graph.isCellDeletable = function(cell)
{
return oldDeletable.apply(this, arguments) &&
(this.getModel().isVertex(cell) &&
currentPermission.editVertices) ||
(this.getModel().isEdge(cell) &&
currentPermission.editEdges);
};
var oldCloneable = graph.isCellCloneable;
graph.isCellCloneable = function(cell)
{
return oldCloneable.apply(this, arguments) &&
currentPermission.cloneCells;
};
// 创建默认窗体
var parent = graph.getDefaultParent();
// 开启更新事务
graph.getModel().beginUpdate();
try
{
var v1 = graph.insertVertex(parent, null, 'Hello,', 20, 20, 80, 30);
var v2 = graph.insertVertex(parent, null, 'Hello,', 200, 20, 80, 30);
var v3 = graph.insertVertex(parent, null, 'World!', 200, 150, 80, 30);
var e1 = graph.insertEdge(parent, null, 'Connection', v1, v3);
}
finally
{
// 结束更新事务
graph.getModel().endUpdate();
}
}
};
// 权限设置
function Permission(locked, createEdges, editEdges, editVertices, cloneCells)
{
this.locked = (locked != null) ? locked : false;
this.createEdges = (createEdges != null) ? createEdges : true;
this.editEdges = (editEdges != null) ? editEdges : true;;
this.editVertices = (editVertices != null) ? editVertices : true;;
this.cloneCells = (cloneCells != null) ? cloneCells : true;;
};
Permission.prototype.apply = function(graph)
{
graph.setConnectable(this.createEdges);
graph.setCellsLocked(this.locked);
};
</script>
</head>
<!-- 页面载入时启动程序 -->
<body onload="main();">
</body>
</html>