-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdrawio-renderer.js
181 lines (149 loc) · 4.4 KB
/
drawio-renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
window.addEventListener("load", function(){
let permittedTagNames = [ "DIV", "SPAN", "SECTION", "BODY" ];
addStyle("https://laingsimon.github.io/render-diagram/drawio-renderer.css");
addScript("https://www.draw.io/js/viewer.min.js");
waitForDrawIo(function(timeout) {
let diagrams = document.querySelectorAll(".drawio-diagram");
diagrams.forEach(function(diagram){
if (permittedTagNames.indexOf(diagram.tagName) === -1) {
return; //not included in a permitted tag
}
if (timeout) {
showError(diagram, "Unable to load draw.io renderer");
return;
}
processDiagram(diagram);
});
})
})
function waitForDrawIo(callback, attempt) {
if (!attempt) {
attempt = 0;
}
if (typeof GraphViewer === "undefined") {
if (attempt >= 5) {
callback(true);
return;
}
window.setTimeout(function() {
waitForDrawIo(callback, attempt + 1);
}, 500);
return;
}
callback(false);
}
function addStyle(url){
let link = document.createElement("link");
link.setAttribute("rel", "stylesheet");
link.setAttribute("type", "text/css");
link.setAttribute("href", url);
document.head.appendChild(link);
}
function addScript(url){
let script = document.createElement("script");
script.setAttribute("src", url);
document.body.appendChild(script);
}
function processDiagram(diagram){
try {
let url = diagram.getAttribute("data-diagram-url");
let data = diagram.getAttribute("data-diagram-data");
if (data) {
addDiagram(data, diagram, getUserOptions(diagram));
return;
}
if (url) {
diagram.innerHTML = "";
diagram.className += " drawio-diagram-loading";
loadDataFromUrlThen(url, function(data, error) {
try {
if (error) {
showError(diagram, "Url: <a href=\"" + url + "\" target=\"_blank\">" + url + "</a><br />Status: " + error.status);
return;
}
let dataMatch = data != null ? data.match(/\<diagram .+?\>(.+)\<\/diagram\>/) : null;
if (dataMatch != null && dataMatch.length >= 2) {
data = dataMatch[1];
}
addDiagram(data, diagram, getUserOptions(diagram));
} catch (e) {
showError(diagram, e);
}
});
return;
}
showError(diagram, "No means to process diagram, no drawio-diagram-data or drawio-diagram-url attribute");
} catch (e) {
showError(diagram, e);
}
}
function showError(diagram, errorHtml){
diagram.className = diagram.className.replace(" drawio-diagram-loading", "") + " drawio-diagram-error";
diagram.innerHTML = "Error loading diagram<br />" + errorHtml;
}
function loadDataFromUrlThen(url, callback) {
let xmlhttp = null;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4)
{
if (xmlhttp.status==200) {
callback(xmlhttp.responseText, null);
} else {
callback(null, { status: xmlhttp.status, text: xmlhttp.responseText });
}
return;
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function addDiagram(diagramData, replaceElement, userOptions){
let div = document.createElement("div");
div.setAttribute("class", "mxgraph");
div.setAttribute("data-mxgraph", JSON.stringify(getMxGraphData(diagramData, userOptions)));
replaceElement.parentElement.insertBefore(div, replaceElement);
replaceElement.parentElement.removeChild(replaceElement);
window.setTimeout(function(){
let svgElements = div.getElementsByTagName("svg");
if (svgElements.length === 0) {
let errorDiv = document.createElement("div");
div.parentElement.insertBefore(errorDiv, div);
div.parentElement.removeChild(div);
showError(errorDiv, div.innerText);
}
}, 500);
GraphViewer.processElements();
}
function getMxGraphData(diagramData, userOptions){
let options = Object.assign({}, getDefaultOptions());
if (userOptions) {
options = Object.assign(options, userOptions);
}
return Object.assign(options, {
xml: "<mxfile version=\"10.6.5\"><diagram>" + diagramData + "</diagram></mxfile>"
});
}
function getUserOptions(diagram) {
let options = diagram.getAttribute("data-diagram-options");
if (!options) {
return null;
}
return JSON.parse(options);
}
function getDefaultOptions(){
return {
highlight: "none",
target: "self",
lightbox: false,
nav: true
};
}