js画图开发库--mxgraph--[serverview-服务器视图.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 = '/mxgraph/javascript/src';
</script>
<!-- 引入支持库文件 -->
<script type="text/javascript" src="/mxgraph/javascript/src/js/mxClient.js"></script>
<!-- 覆盖所需的服务器端的图像要使用的某些方法。-->
<script type="text/javascript">
// 编辑器使用非透明背景
var previousStartEditing = mxCellEditor.prototype.startEditing;
mxCellEditor.prototype.startEditing = function (cell, trigger)
{
previousStartEditing.apply(this, arguments);
var state = this.graph.getView().getState(cell);
if (state != null)
{
var color = mxUtils.getValue(state.style, mxConstants.STYLE_FILLCOLOR, 'white');
this.textarea.style.background = color;
}
};
// 替换在图表视图中的事件触发机制
// FIXME: Since we do not render the label we don't have the label bounds
// here which means hit detection will only work for the vertex bounds,
// the edge but not for overlapping labels or most part of the edge labels.
mxGraphView.prototype.installListeners = function()
{
var graph = this.graph;
var container = graph.container;
if (container != null)
{
var md = (mxClient.IS_TOUCH) ? 'touchstart' : 'mousedown';
var mm = (mxClient.IS_TOUCH) ? 'touchmove' : 'mousemove';
var mu = (mxClient.IS_TOUCH) ? 'touchend' : 'mouseup';
// Adds basic listeners for graph event dispatching
mxEvent.addListener(container, md,
mxUtils.bind(this, function(evt)
{
var pt = mxUtils.convertPoint(graph.container,
mxEvent.getClientX(evt), mxEvent.getClientY(evt));
var cell = graph.getCellAt(pt.x, pt.y);
var state = this.getState(cell);
if (state != null)
{
graph.fireMouseEvent(mxEvent.MOUSE_DOWN,
new mxMouseEvent(evt, state));
}
// Condition to avoid scrollbar events starting a rubberband
// selection
else if (this.isContainerEvent(evt) &&
((!mxClient.IS_IE &&
!mxClient.IS_GC && !mxClient.IS_OP && !mxClient.IS_SF) ||
!this.isScrollEvent(evt)))
{
graph.fireMouseEvent(mxEvent.MOUSE_DOWN,
new mxMouseEvent(evt));
}
})
);
mxEvent.addListener(container, mm,
mxUtils.bind(this, function(evt)
{
var pt = mxUtils.convertPoint(graph.container,
mxEvent.getClientX(evt), mxEvent.getClientY(evt));
var cell = graph.getCellAt(pt.x, pt.y);
var state = this.getState(cell);
if (state != null)
{
graph.fireMouseEvent(mxEvent.MOUSE_MOVE,
new mxMouseEvent(evt, state));
}
else if (this.isContainerEvent(evt))
{
graph.fireMouseEvent(mxEvent.MOUSE_MOVE,
new mxMouseEvent(evt));
}
})
);
mxEvent.addListener(container, mu,
mxUtils.bind(this, function(evt)
{
var pt = mxUtils.convertPoint(graph.container,
mxEvent.getClientX(evt), mxEvent.getClientY(evt));
var cell = graph.getCellAt(pt.x, pt.y);
var state = this.getState(cell);
if (state != null)
{
graph.fireMouseEvent(mxEvent.MOUSE_UP,
new mxMouseEvent(evt, state));
}
else if (this.isContainerEvent(evt))
{
graph.fireMouseEvent(mxEvent.MOUSE_UP,
new mxMouseEvent(evt));
}
})
);
// 添加双击事件
mxEvent.addListener(container, 'dblclick',
mxUtils.bind(this, function(evt)
{
var pt = mxUtils.convertPoint(graph.container,
mxEvent.getClientX(evt), mxEvent.getClientY(evt));
var cell = graph.getCellAt(pt.x, pt.y);
graph.dblClick(evt, cell);
})
);
// 增加了基本的手势的处理侦听器
mxEvent.addListener(document, md,
mxUtils.bind(this, function(evt)
{
if (this.isContainerEvent(evt))
{
graph.panningHandler.hideMenu();
}
})
);
mxEvent.addListener(document, mm,
mxUtils.bind(this, function(evt)
{
// 鼠标一开容器时隐藏工具提示
if (graph.tooltipHandler != null &&
graph.tooltipHandler.isHideOnHover())
{
graph.tooltipHandler.hide();
}
if (this.captureDocumentGesture &&
graph.isMouseDown &&
!mxEvent.isConsumed(evt))
{
graph.fireMouseEvent(mxEvent.MOUSE_MOVE,
new mxMouseEvent(evt));
}
})
);
mxEvent.addListener(document, mu,
mxUtils.bind(this, function(evt)
{
if (this.captureDocumentGesture)
{
graph.fireMouseEvent(mxEvent.MOUSE_UP,
new mxMouseEvent(evt));
}
})
);
}
};
</script>
<!-- 示例代码 -->
<script type="text/javascript">
// 程序在此方法中启动
function main(container)
{
// 检查浏览器支持
if (!mxClient.isBrowserSupported())
{
mxUtils.error('Browser is not supported!', 200, false);
}
else
{
// 在容器中创建图形
var graph = new mxGraph(container);
// 保存当前的图像
var img = null;
var loader = new Image();
loader.src = 'images/loading.gif';
loader.style.zIndex = 1;
loader.style.visibility = 'hidden';
loader.style.position = 'absolute';
graph.container.insertBefore(loader, graph.container.firstChild);
// 禁用基于DOM的图形渲染,图形和显示图像的每个验证步骤,使用导出的servlet
graph.view.rendering = false;
// 图形加载完成后进行重绘验证
graph.view.validate = function()
{
loader.style.left = graph.container.scrollLeft + graph.container.clientWidth / 2 - 32;
loader.style.top = graph.container.scrollTop + graph.container.clientHeight / 2 - 32;
loader.style.visibility = 'visible';
mxGraphView.prototype.validate.apply(this, arguments);
var t0 = new Date().getTime();
var bounds = graph.getGraphBounds();
// 需要注意的是,我们的观点,这使我们能够向客户端发送一个自定义XML文本。没有变量被用在这里,被发送给服务器后更新完整视图。
var node = mxUtils.getViewXml(graph, 1);
var xml = encodeURIComponent(mxUtils.getXml(node));
var onload = function(req)
{
var dt = (new Date().getTime() - t0) / 1000;
//mxLog.debug('post returned after '+dt+' secs');
if (req.getStatus() == 200)
{
var image =new Image();
// 禁用图像打扰事件。提示:IE启动时在图像上的下拉菜单事件被忽略
if (mxClient.IS_IE)
{
graph.view.isContainerEvent = function(evt)
{
var source = mxEvent.getSource(evt);
return (source == image ||
source == this.graph.container ||
source.parentNode == this.backgroundPane ||
(source.parentNode != null &&
source.parentNode.parentNode == this.backgroundPane) ||
source == this.canvas.parentNode ||
source == this.canvas ||
source == this.backgroundPane ||
source == this.drawPane ||
source == this.overlayPane);
};
}
// 如果旧的图像被删除后新图像从服务器收到后减少闪烁
image.onload = function()
{
if (img != null)
{
img.parentNode.removeChild(img);
}
img = image;
graph.container.insertBefore(img, graph.container.firstChild);
loader.style.visibility = 'hidden';
var dt = (new Date().getTime() - t0) / 1000;
//mxLog.debug('received '+img.clientWidth+'x'+img.clientHeight+' pixels in '+dt+' secs');
graph.setEnabled(true);
};
// URL包含时间戳,以避免缓存在浏览器中
image.src = '/ServerView?'+new Date().getTime();
image.style.position = 'absolute';
image.style.left = bounds.x - 4;
image.style.top = bounds.y - 4;
image.style.zIndex = -1;
}
};
var onerror = function(req)
{
//mxLog.debug('error: '+req.getStatus());
}
//mxLog.debug('sent '+(xml.length/1024)+' KB');
graph.setEnabled(false);
new mxXmlRequest('/ServerView', 'xml='+xml).send(onload, onerror);
};
// 容器自适应
//graph.setResizeContainer(true);
// Enables rubberband selection
new mxRubberband(graph);
graph.setConnectable(true);
graph.setPanning(true);
graph.setTooltips(true);
// 创建默认窗体
var parent = graph.getDefaultParent();
//mxLog.show();
// 开启更新事务
graph.getModel().beginUpdate();
try
{
var v1 = graph.insertVertex(parent, null, 'Hello,', 20, 20, 80, 30);
var v2 = graph.insertVertex(parent, null, 'World!', 200, 150, 80, 30);
var e1 = graph.insertEdge(parent, null, '', v1, v2);
}
finally
{
// 结束更新事务
graph.getModel().endUpdate();
}
}
};
</script>
</head>
<!-- 页面载入时启动程序 -->
<body οnlοad="main(document.getElementById('graphContainer'))">
<!-- 创建带网格壁纸和曲线的一个容器 -->
<div id="graphContainer"
style="overflow:auto;width:500px;height:500px;border: black solid 1px;cursor:default;">
</div>
<br>
<strong>Important:</strong>
<p>
To use this example, start com.mxgraph.examples.web.main in Java
and point your<br>browser to:
要使用这个例子中,启动com.mxgraph.examples.web.main的Java
<br>您的浏览器指向到:
<a href="http://localhost:8080/mxgraph/javascript/examples/serverview.html">http://localhost:8080/mxgraph/javascript/examples/serverview.html</a>
</p>
</body>
</html>