通过urlParam在MxGraph中打开本地文件 [英] Opening local files in MxGraph via urlParam
问题描述
如何在通过urlParam传递名称的grapheditor中打开本地文件?我在index.html中尝试使用此代码,但是它不起作用.
How can I open a local file in grapheditor passing the name via urlParam? I tried with this code in index.html but it does not work.
var editor = new EditorUi(new Editor(urlParams['chrome'] == '0', themes));
try
{
editor.open(encodeURI(urlParams['xml']));
}
catch (e)
{
mxUtils.error('Cannot open ' + urlParams['xml'] +
': ' + e.message, 280, true);
}
谢谢.
推荐答案
我必须做同样的事情.如果您有在本地运行的服务器来提供文件,这将很有帮助.例如:
I had to do the same. It is helpful if you have a server running locally to serve your files. For example:
python -m SimpleHTTPServer 8000
此命令将启动一个简单的服务器,以便您的浏览器可以发送AJAX请求,以便从文件系统中加载文件.
This command will start a simple server so your browser can send AJAX request in order to load files from your filesystem.
我必须假设以下设置:您在文件index.html中的一个文件夹中运行了mxGraph,并且该文件夹包含另一个包含xml的文件夹.像这样:
I have to assume the following setup: You have mxGraph running in a folder in the file index.html and the same folder contains another folder containing your xmls. Like this:
index.html
/xmls/1.xml
/xmls/2.xml
...
这是您执行命令启动服务器的过程. 现在您可以在这里找到您的应用程序:localhost:8000/index.html
This is were you execute the command to start the server. Now you can reach your application here: localhost:8000/index.html
通过添加GET参数来识别硬盘驱动器上的文件,您是正确的.
You were right by adding a GET parameter in order to identify the file on your hard drive.
例如:localhost:8000/index.html?xml=1.xml
For this example to work the structure should be comparable to the GraphEditor (https://jgraph.github.io/mxgraph/javascript/examples/grapheditor/www/index.html). It would be a good idea to clone this project and play around with it.
现在在index.html中,您可以添加如下内容:
Now in your index.html you might add something like this:
function open_xml_on_init(editorUi) {
var xml_file_path = 'xml/' + urlParams['xml'];
if (urlParams['xml'] != null && urlParams['xml'].length > 0) {
var splitted = xml_file_path.split('/');
var only_name = splitted[splitted.length - 1];
editorUi.editor.filename = only_name;
var req = mxUtils.get(xml_file_path, mxUtils.bind(this, function (req) {
if (req.request.status >= 200 && req.request.status <= 299) {
if (req.request.response.length > 0) {
editorUi.editor.graph.model.beginUpdate();
try {
var xmlElem = mxUtils.parseXml(req.request.response).documentElement;
editorUi.editor.setGraphXml(xmlElem);
}
catch (e) {
console.error(e);
}
finally {
editorUi.editor.graph.model.endUpdate();
}
}
}
}));
}
}
在index.html中的某个地方运行此方法,您就很好了!
Run this method somewhere in your index.html and you are good to go!
open_xml_on_init(editor_ui);
这篇关于通过urlParam在MxGraph中打开本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!