preload.js
4.82 KB
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
// Global variable for desktop
const mxIsElectron = window && window.process && window.process.type
/**
* Adds meta tag to the page.
*/
function mxmeta(name, content, httpEquiv) {
try {
const s = document.createElement('meta')
if (name != null)
s.setAttribute('name', name)
s.setAttribute('content', content)
if (httpEquiv != null)
s.setAttribute('http-equiv', httpEquiv)
const t = document.getElementsByTagName('meta')[0]
t.parentNode.insertBefore(s, t)
}
catch (e) {
// ignore
}
}
/**
* Synchronously adds scripts to the page.
*/
function mxscript(src, onLoad, id, dataAppKey, noWrite, onError) {
const defer = onLoad == null && !noWrite
if ((urlParams.dev != '1' && typeof document.createElement('canvas').getContext === 'function')
|| onLoad != null || noWrite) {
const s = document.createElement('script')
s.setAttribute('type', 'text/javascript')
s.setAttribute('defer', 'true')
s.setAttribute('src', src)
if (id != null)
s.setAttribute('id', id)
if (dataAppKey != null)
s.setAttribute('data-app-key', dataAppKey)
if (onLoad != null) {
let r = false
s.onload = s.onreadystatechange = function () {
if (!r && (!this.readyState || this.readyState == 'complete')) {
r = true
onLoad()
}
}
}
if (onError != null) {
s.onerror = function (e) {
onError(`Failed to load ${src}`, e)
}
}
const t = document.getElementsByTagName('script')[0]
if (t != null)
t.parentNode.insertBefore(s, t)
}
else {
document.write(`<script src="${src}"${(id != null) ? ` id="${id}" ` : ''
}${(dataAppKey != null) ? ` data-app-key="${dataAppKey}" ` : ''}></scr` + 'ipt>')
}
}
/**
* Asynchronously adds scripts to the page.
*/
function mxinclude(src) {
const g = document.createElement('script')
g.type = 'text/javascript'
g.async = true
g.src = src
const s = document.getElementsByTagName('script')[0]
s.parentNode.insertBefore(g, s)
}
// Checks for local storage
let isLocalStorage = false
try {
isLocalStorage = urlParams.local != '1' && typeof (localStorage) != 'undefined'
}
catch (e) {
// ignored
}
/**
*
* @param {string} ossFileName oss文件名
* @param {string} localFileAddress 本地加载文件路径
* @returns
*/
function getLoadOssFileAddress(ossFileName, localFileAddress) {
const { enableOss, ossAddress } = window.PROJECT_ENV
return enableOss && ossAddress ? `${ossAddress}${ossAddress.endsWith('/') ? '' : '/'}${ossFileName}` : localFileAddress
}
let mxScriptsLoaded = false; let mxWinLoaded = false
// Changes paths for local development environment
if (window.urlParams.dev == '1') {
// Used to request grapheditor/mxgraph sources in dev mode
window.drawDevUrl = `//${location.host}${getProxyPrefix()}/webapp/`
window.geBasePath = `//${location.host}${getProxyPrefix()}/webapp/js/grapheditor`
window.mxBasePath = `//${location.host}${getProxyPrefix()}/webapp/mxgraph`
window.mxForceIncludes = true
// mxscript(`${window.drawDevUrl}js/PreConfig.js`)
mxscript(`${window.drawDevUrl}js/diagramly/Init.js`)
mxscript(`${window.geBasePath}/Init.js`)
mxscript(`${window.mxBasePath}/mxClient.js`)
// Adds all JS code that depends on mxClient. This indirection via Devel.js is
// required in some browsers to make sure mxClient.js (and the files that it
// loads asynchronously) are available when the code loaded in Devel.js runs.
mxscript(`${window.drawDevUrl}js/diagramly/Devel.js`)
mxscript(`${window.drawDevUrl}js/PostConfig.js`)
} else {
(function () {
var hostName = window.location.hostname;
// Supported domains are *.draw.io and the packaged version in Quip
var supportedDomain = (hostName.substring(hostName.length - 8, hostName.length) === '.draw.io') ||
(hostName.substring(hostName.length - 13, hostName.length) === '.diagrams.net');
function loadAppJS() {
mxscript(getLoadOssFileAddress('app.min.js', `${getProxyPrefix()}/webapp/js/app.min.js`), function () {
mxScriptsLoaded = true;
checkAllLoaded();
mxscript(`${getProxyPrefix()}/webapp/js/PostConfig.js`);
});
};
if (!supportedDomain || mxIsElectron) {
mxscript(`${getProxyPrefix()}/webapp/js/PreConfig.js`, loadAppJS);
} else {
loadAppJS();
}
})();
}
function checkAllLoaded() {
if (mxScriptsLoaded && mxWinLoaded) {
bootstrap()
}
};
// Adds basic error handling
window.onerror = function () {
const status = document.getElementById('geStatus')
if (status != null)
status.innerHTML = 'Page could not be loaded. Please try refreshing.'
}
/**
* Main
*/
if (urlParams['dev'] != '1' && typeof document.createElement('canvas').getContext === "function") {
window.addEventListener('load', function () {
mxWinLoaded = true;
checkAllLoaded();
});
}