-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.js
More file actions
240 lines (222 loc) · 7.39 KB
/
Copy pathUtils.js
File metadata and controls
240 lines (222 loc) · 7.39 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Utils.js - Utils...
//
"use strict";
var Utils = {
debug: 0,
console: { // we must load Utils first to dynamically load Polyfill, but we maybe need a console on old browsers or if the console is closed in IE, Edge
consoleLog: {
value: ""
},
bApplyOnMethod: undefined,
console: (typeof console !== "undefined") ? console : null,
rawLog: function (fnMethod, sLevel, aArgs) {
if (sLevel) {
aArgs.unshift(sLevel);
}
if (fnMethod) {
if (this.bApplyOnMethod === undefined) {
try {
this.bApplyOnMethod = fnMethod.apply; // try to access apply method (old IE8 does not support apply on e.g. console.log)
this.bApplyOnMethod = true;
} catch (e) {
this.bApplyOnMethod = false;
}
}
if (this.bApplyOnMethod) {
fnMethod.apply(console, aArgs);
} else { // old IE8 does not support apply on e.g. console.log
Function.prototype.apply.call(fnMethod, console, aArgs);
}
}
if (this.consoleLog) {
this.consoleLog.value += aArgs.join(" ") + ((sLevel !== null) ? "\n" : "");
}
},
log: function () {
this.rawLog(this.console && this.console.log, "", Array.prototype.slice.call(arguments));
},
debug: function () {
this.rawLog(this.console && this.console.debug, "DEBUG:", Array.prototype.slice.call(arguments));
},
info: function () {
this.rawLog(this.console && this.console.info, "INFO:", Array.prototype.slice.call(arguments));
},
warn: function () {
this.rawLog(this.console && this.console.warn, "WARN:", Array.prototype.slice.call(arguments));
},
error: function () {
this.rawLog(this.console && this.console.error, "ERROR:", Array.prototype.slice.call(arguments));
},
changeLog: function (oLog) {
var oldLog = this.consoleLog;
this.consoleLog = oLog;
if (oldLog && oldLog.value && oLog) { // some log entires collected?
oLog.value += oldLog.value; // take collected log entries
}
}
},
fnLoadScriptOrStyle: function (script, sFullUrl, fnSuccess, fnError) {
// inspired by https://github.com/requirejs/requirejs/blob/master/require.js
var iIEtimeoutCount = 3,
onScriptLoad = function (event) {
var node = event.currentTarget || event.srcElement;
if (Utils.debug > 1) {
Utils.console.debug("onScriptLoad: " + node.src || node.href);
}
node.removeEventListener("load", onScriptLoad, false);
node.removeEventListener("error", onScriptError, false); // eslint-disable-line no-use-before-define
// now getEventListeners(node) should be empty again
if (fnSuccess) {
fnSuccess(sFullUrl);
}
},
onScriptError = function (event) {
var node = event.currentTarget || event.srcElement;
if (Utils.debug > 1) {
Utils.console.debug("onScriptError: " + node.src || node.href);
}
node.removeEventListener("load", onScriptLoad, false);
node.removeEventListener("error", onScriptError, false);
if (fnError) {
fnError(sFullUrl);
}
},
onScriptReadyStateChange = function (event) { // for IE
var node, iTimeout;
if (event) {
node = event.currentTarget || event.srcElement;
} else {
node = script;
}
if (node.detachEvent) {
node.detachEvent("onreadystatechange", onScriptReadyStateChange);
}
if (Utils.debug > 1) {
Utils.console.debug("onScriptReadyStateChange: " + node.src || node.href);
}
// 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: " + (node.src || node.href) + " Waiting " + iTimeout + "ms (count=" + iIEtimeoutCount + ")");
setTimeout(function () {
onScriptReadyStateChange(); // check again
}, iTimeout);
} else {
// iIEtimeoutCount = 3;
Utils.console.error("onScriptReadyStateChange: Cannot load file " + (node.src || node.href) + " readystate=" + node.readyState);
if (fnError) {
fnError(sFullUrl);
}
}
} else if (fnSuccess) {
fnSuccess(sFullUrl);
}
};
if (script.readyState) { // IE
iIEtimeoutCount = 3;
script.attachEvent("onreadystatechange", onScriptReadyStateChange);
} else { // Others
script.addEventListener("load", onScriptLoad, false);
script.addEventListener("error", onScriptError, false);
}
document.getElementsByTagName("head")[0].appendChild(script);
return sFullUrl;
},
loadScript: function (sUrl, fnSuccess, fnError) {
var script, sFullUrl;
script = document.createElement("script");
script.type = "text/javascript";
script.charset = "utf-8";
// script.defer = "defer"; // only for IE?
script.async = true;
script.src = sUrl;
sFullUrl = script.src;
this.fnLoadScriptOrStyle(script, sFullUrl, fnSuccess, fnError);
},
loadStyle: function (sUrl, fnSuccess, fnError) {
var link, sFullUrl;
link = document.createElement("link");
link.rel = "stylesheet";
link.href = sUrl;
sFullUrl = link.href;
this.fnLoadScriptOrStyle(link, sFullUrl, fnSuccess, fnError);
},
strNumFormat: function (s, iLen, sFillChar) {
var i;
s = String(s);
if (sFillChar === null) {
sFillChar = " ";
}
for (i = s.length; i < iLen; i += 1) {
s = sFillChar + s;
}
return s;
},
strZeroFormat: function (s, iLen) {
return Utils.strNumFormat(s, iLen, "0");
},
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);
},
objectAssign: function (oTarget) { // varargs // Object.assign is ES6, not in IE
var oTo = oTarget,
i,
oNextSource,
sNextKey;
for (i = 1; i < arguments.length; i += 1) {
oNextSource = arguments[i];
for (sNextKey in oNextSource) {
if (oNextSource.hasOwnProperty(sNextKey)) {
oTo[sNextKey] = oNextSource[sNextKey];
}
}
}
return oTo;
},
stringTrimLeft: function (s) {
return s.replace(/^[\s\uFEFF\xA0]+/g, "");
},
stringTrimRight: function (s) {
return s.replace(/[\s\uFEFF\xA0]+$/g, "");
},
stringStartsWith: function (sStr, sFind, iPos) {
iPos = iPos || 0;
return sStr.indexOf(sFind, iPos) === iPos;
},
stringEndsWith: function (str, find) {
return str.indexOf(find, str.length - find.length) !== -1;
},
stringCapitalize: function (str) {
return str.charAt(0).toUpperCase() + str.substring(1);
},
regExpEscape: function (s) {
return s.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"); // (github.com/benjamingr/RegExp.escape), one / removed
},
getChangedParameters: function (current, initial) {
var oChanged = {},
sName;
for (sName in current) {
if (current.hasOwnProperty(sName)) {
if (current[sName] !== initial[sName]) {
oChanged[sName] = current[sName];
}
}
}
return oChanged;
},
localStorage: null,
initLocalStorage: function () {
try {
Utils.localStorage = window.localStorage; // due to a bug in Edge this will throw an error when hosting locally (https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8816771/)
} catch (e) {
Utils.console.log("initLocalStorage: " + e);
}
}
};
if (typeof module !== "undefined" && module.exports) {
module.exports = Utils;
}
// end