-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtils.js
193 lines (168 loc) · 6.43 KB
/
Utils.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
182
183
184
185
186
187
188
189
190
191
192
193
// Utils.js - Utililities for CPCBasic
// (c) Marco Vieth, 2019
// https://benchmarko.github.io/CPCBasic/
//
/* globals ArrayBuffer, Uint8Array */
"use strict";
var Utils = {
debug: 0,
console: typeof window !== "undefined" ? window.console : global.console, // browser or node.js
fnLoadScriptOrStyle: function (script, fnSuccess, fnError) {
// inspired by https://github.com/requirejs/requirejs/blob/master/require.js
var iIEtimeoutCount = 3,
onScriptLoad = function (event) {
var sType = event.type, // "load" or "error"
node = event.currentTarget || event.srcElement,
sFullUrl = node.src || node.href, // src for script, href for link
sKey = node.getAttribute("data-key");
if (Utils.debug > 1) {
Utils.console.debug("onScriptLoad:", node.src || node.href);
}
node.removeEventListener("load", onScriptLoad, false);
node.removeEventListener("error", onScriptLoad, false); // eslint-disable-line no-use-before-define
if (sType === "load") {
fnSuccess(sFullUrl, sKey);
} else {
fnError(sFullUrl, sKey);
}
},
onScriptReadyStateChange = function (event) { // for old IE8
var node = event ? (event.currentTarget || event.srcElement) : script,
sFullUrl = node.src || node.href, // src for script, href for link
sKey = node.getAttribute("data-key"),
iTimeout = 200; // some delay
if (node.detachEvent) {
node.detachEvent("onreadystatechange", onScriptReadyStateChange);
}
if (Utils.debug > 1) {
Utils.console.debug("onScriptReadyStateChange: " + sFullUrl);
}
// check also: https://stackoverflow.com/questions/1929742/can-script-readystate-be-trusted-to-detect-the-end-of-dynamic-script-loading
if (node.readyState !== "loaded" && node.readyState !== "complete") {
if (node.readyState === "loading" && iIEtimeoutCount) {
iIEtimeoutCount -= 1;
iTimeout = 200; // some delay
Utils.console.error("onScriptReadyStateChange: Still loading: " + sFullUrl + " Waiting " + iTimeout + "ms (count=" + iIEtimeoutCount + ")");
setTimeout(function () {
onScriptReadyStateChange(undefined); // check again
}, iTimeout);
} else {
// iIEtimeoutCount = 3;
Utils.console.error("onScriptReadyStateChange: Cannot load file " + sFullUrl + " readystate=" + node.readyState);
fnError(sFullUrl, sKey);
}
} else {
fnSuccess(sFullUrl, sKey);
}
};
if (script.readyState) { // old IE8
iIEtimeoutCount = 3;
script.attachEvent("onreadystatechange", onScriptReadyStateChange);
} else { // Others
script.addEventListener("load", onScriptLoad, false);
script.addEventListener("error", onScriptLoad, false);
}
document.getElementsByTagName("head")[0].appendChild(script);
},
loadScript: function (sUrl, fnSuccess, fnError, sKey) {
var script;
script = document.createElement("script");
script.type = "text/javascript";
script.charset = "utf-8";
script.async = true;
script.src = sUrl;
script.setAttribute("data-key", sKey);
this.fnLoadScriptOrStyle(script, fnSuccess, fnError);
},
loadStyle: function (sUrl, fnSuccess, fnError) {
var link;
link = document.createElement("link");
link.rel = "stylesheet";
link.href = sUrl;
this.fnLoadScriptOrStyle(link, fnSuccess, fnError);
},
dateFormat: function (d) {
return d.getFullYear() + "/" + ("0" + (d.getMonth() + 1)).slice(-2) + "/" + ("0" + d.getDate()).slice(-2) + " "
+ ("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2) + ":" + ("0" + d.getSeconds()).slice(-2) + "." + ("0" + d.getMilliseconds()).slice(-3);
},
stringCapitalize: function (str) { // capitalize first letter
return str.charAt(0).toUpperCase() + str.substring(1);
},
numberWithCommas: function (x) {
// https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript
var aParts = String(x).split(".");
aParts[0] = aParts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return aParts.join(".");
},
toRadians: function (deg) {
return deg * Math.PI / 180;
},
toDegrees: function (rad) {
return rad * 180 / Math.PI;
},
bSupportsBinaryLiterals: (function () { // does the browser support binary literals?
try {
Function("0b01"); // eslint-disable-line no-new-func
} catch (e) {
return false;
}
return true;
}()),
bSupportReservedNames: (function () { // does the browser support reserved names (delete, new, return) in dot notation? (not old IE8; "goto" is ok)
try {
Function("({}).return()"); // eslint-disable-line no-new-func
} catch (e) {
return false;
}
return true;
}()),
localStorage: (function () {
var rc;
try {
rc = typeof window !== "undefined" ? window.localStorage : null; // due to a bug in MS Edge this will throw an error when hosting locally (https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8816771/)
} catch (e) {
rc = null;
}
return rc;
}()),
atob: typeof window !== "undefined" && window.atob && window.atob.bind ? window.atob.bind(window) : null, // we need bind: https://stackoverflow.com/questions/9677985/uncaught-typeerror-illegal-invocation-in-chrome
btoa: typeof window !== "undefined" && window.btoa && window.btoa.bind ? window.btoa.bind(window) : null,
string2Uint8Array: function (data) {
var buf = new ArrayBuffer(data.length),
view = new Uint8Array(buf),
i;
for (i = 0; i < data.length; i += 1) {
view[i] = data.charCodeAt(i);
}
return view;
},
composeError: function (name, oError, message, value, pos, line, hidden) {
var iEndPos;
if (name !== undefined) {
oError.name = name;
}
if (message !== undefined) {
oError.message = message;
}
if (value !== undefined) {
oError.value = value;
}
if (pos !== undefined) {
oError.pos = pos;
}
// Safari: Some additional properties are already defined: line, column. Shall we use "cause" property now?
if (line !== oError.line) {
oError.line = line;
}
if (hidden !== undefined) {
oError.hidden = hidden;
}
iEndPos = (oError.pos || 0) + ((oError.value !== undefined) ? String(oError.value).length : 0);
oError.shortMessage = oError.message + (oError.line !== undefined ? " in " + oError.line : " at pos " + (oError.pos || 0) + "-" + iEndPos) + ": " + oError.value;
oError.message += (oError.line !== undefined ? " in " + oError.line : "") + " at pos " + (oError.pos || 0) + "-" + iEndPos + ": " + oError.value;
return oError;
}
};
if (typeof module !== "undefined" && module.exports) {
module.exports = Utils;
}