-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageLoader.js
More file actions
462 lines (371 loc) · 14.6 KB
/
PageLoader.js
File metadata and controls
462 lines (371 loc) · 14.6 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
if(!javaxt) var javaxt={};
if(!javaxt.dhtml) javaxt.dhtml={};
//******************************************************************************
//** PageLoader
//*****************************************************************************/
/**
* Used to dynamically load html documents.
*
******************************************************************************/
javaxt.dhtml.PageLoader = function(config) {
var me = this;
var head = document.getElementsByTagName("head")[0];
var debug = false;
var includes;
var init = function(){
if (config){
if (config.debug===true) debug = true;
}
};
//**************************************************************************
//** loadPage
//**************************************************************************
/** Used to replace the body of the current document with html from another
* document found at a given url. Dynamically loads any css stylesheets and
* javascripts sourced in the html document.
*/
this.loadPage = function(url, onSuccess, onFail, onUpdate){
me.load(url,
function(html, title, inlineScripts){
//Set title
document.title = title;
//Update body
var body = document.getElementsByTagName("body")[0];
body.innerHTML = html;
//Add inline scripts
for (var i=0; i<inlineScripts.length; i++){
var script = inlineScripts[i].firstChild.nodeValue;
var type = inlineScripts[i].getAttribute("type");
if (type=="module"){
script = script.replace("//<![CDATA[","").replace("//]]>","");
var s = document.createElement("script");
s.setAttribute("type", type);
s.innerHTML = script;
body.appendChild(s);
}
else{
eval(script);
}
}
//Dispatch onload event. Warning: ES6 modules with imports may not be ready...
dispatchEvent("load");
if (onSuccess) onSuccess.apply(me, []);
},
onFail,
onUpdate
);
};
//**************************************************************************
//** load
//**************************************************************************
/** Used to fetch an html document found at a given url. Loads any css
* stylesheets and javascripts sourced in the html document. Returns the
* raw html, document title, and any inline scripts via the callback. It
* is up to the caller to decide what to do with the html (e.g. replace
* element) and when to execute the inline scripts.
*/
this.load = function(url, onSuccess, onFail, onUpdate){
get(url, function(html){
var div = document.createElement("div");
div.innerHTML = html;
//Get title
var title;
var titles = div.getElementsByTagName("title");
if (titles.length>0){
title = titles[0].innerHTML;
}
//Get scripts
var inlineScripts = [];
var externalScripts = []; //urls
var scripts = div.getElementsByTagName("script");
for (var i=0; i<scripts.length; i++){
if (scripts[i].src.length>0){
externalScripts.push(scripts[i].src);
}
else{
inlineScripts.push(scripts[i]);
}
}
//Get external style sheets
var css = []; //urls
var cssNodes = [];
var links = div.getElementsByTagName("link");
for (var i=0; i<links.length; i++){
if (links[i].rel=="stylesheet"){
if (links[i].href.length>0){
css.push(links[i].href);
cssNodes.push(links[i]);
}
}
}
//Remove unused/unwanted nodes
removeNodes(titles);
removeNodes(div.getElementsByTagName("description"));
removeNodes(div.getElementsByTagName("keywords"));
removeNodes(div.getElementsByTagName("meta"));
removeNodes(scripts);
removeNodes(links);
removeNodes(cssNodes);
//Remove comments
var comments = [];
for (var i=0; i<div.childNodes.length; i++){
var node = div.childNodes[i];
if (node.nodeType==8){
comments.push(node);
}
}
while (comments.length>0){
div.removeChild(comments[0]);
comments.shift();
}
//Get html and delete div
html = div.innerHTML;
div = null;
//Load includes
loadIncludes(
css,
externalScripts,
function(){
if (onSuccess) onSuccess.apply(me, [html, title, inlineScripts]);
},
onUpdate
);
}, onFail);
};
//**************************************************************************
//** loadApp
//**************************************************************************
/** Used to fetch an application xml document found at a given url. Loads
* any css stylesheets and javascripts sourced in the xml document. Returns
* the name and main function used to instantiate the application.
*/
this.loadApp = function(url, onSuccess, onFail, onUpdate){
get(url, function(text, xml){
//Parse app info
var appInfo = {};
var application = xml.getElementsByTagName("application")[0];
if (application){
appInfo.name = application.getAttribute("name");
appInfo.main = application.getAttribute("main");
}
//Parse includes
var scripts = [];
var css = [];
var includes = xml.getElementsByTagName("includes")[0].childNodes;
for (var i=0; i<includes.length; i++){
var include = includes[i];
if (include.nodeType==1){
var type = include.getAttribute("type");
if (type==="text/javascript"){
scripts.push(include.getAttribute("src"));
}
else if (type==="text/css"){
css.push(include.getAttribute("href"));
}
}
}
//Load includes and call the onSuccess callback
loadIncludes(
css,
scripts,
function(){
if (onSuccess){
appInfo.init = function(){
var cls = stringToFunction(this.main);
return newInstance(cls).apply(arguments);
};
onSuccess.apply(me, [appInfo, xml]);
}
},
onUpdate
);
}, onFail);
};
//**************************************************************************
//** loadIncludes
//**************************************************************************
/** Dynamically loads javascript and stylesheets.
*/
var loadIncludes = function(css, scripts, onComplete, onUpdate){
parseIncludes();
var addInclude = function(category, key){
if (category=="css") return true; //css order is important. skipping css files might mess up the order
var obj = includes[category];
for (var k in obj){
if (obj.hasOwnProperty(k)){
if (k==key) return false;
}
}
return true;
};
var arr = [];
//Generate list of stylesheets to include
for (var i=0; i<css.length; i++){
if (addInclude("css", css[i])){
var link = document.createElement("link");
link.setAttribute("rel", "stylesheet");
link.setAttribute("type", "text/css");
link.setAttribute("href", css[i]);
arr.push(link);
includes.css[css[i]] = true;
}
else{
log("Skipping " + css[i] + "...");
}
}
//Generate list of javascripts to include
for (var i=0; i<scripts.length; i++){
if (addInclude("scripts", scripts[i])){
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", scripts[i]);
arr.push(script);
includes.scripts[scripts[i]] = true;
}
else{
log("Skipping " + scripts[i] + "...");
}
}
//Load includes
if (arr.length>0){
var t = arr.length;
var loadResource = function(obj){
var processResponse = function(){
var percentComplete = Math.round((1-(arr.length/t))*100);
if (percentComplete===100 && arr.length>0) percentComplete = 99;
log(percentComplete + "%");
if (onUpdate!=null){
onUpdate({
totalIncludes: t,
remainingIncludes: arr.length,
percentComplete: percentComplete
});
}
if (arr.length>0) loadResource(arr.shift());
else {
if (onComplete!=null) onComplete.apply(me, []);
}
};
obj.onload = function() {
processResponse();
};
obj.onerror = function() {
processResponse();
};
head.appendChild(obj);
};
loadResource(arr.shift());
}
else{
if (onComplete!=null) onComplete.apply(me, []);
}
};
//**************************************************************************
//** parseIncludes
//**************************************************************************
var parseIncludes = function(){
if (includes) return;
includes = {
css: {},
scripts: {}
};
var scripts = document.getElementsByTagName("script");
for (var i=0; i<scripts.length; i++){
if (scripts[i].src.length>0){
includes.scripts[scripts[i].src] = true;
}
}
var css = document.getElementsByTagName("link");
for (var i=0; i<css.length; i++){
if (css[i].rel=="stylesheet"){
if (css[i].href.length>0){
includes.css[css[i].href] = true;
}
}
}
};
//**************************************************************************
//** removeNodes
//**************************************************************************
var removeNodes = function(nodes){
while (nodes.length>0){
var node = nodes[0];
var parent = node.parentNode;
if (!parent) break;
parent.removeChild(node);
}
};
//**************************************************************************
//** stringToFunction
//**************************************************************************
/** Converts a string to a function or class. Example:
* var DateInput = stringToFunction("javaxt.dhtml.DateInput");
* var dateInput = new DateInput(...);
*/
var stringToFunction = function(str) {
var arr = str.split(".");
var fn = (window || this);
for (var i = 0, len = arr.length; i < len; i++) {
fn = fn[arr[i]];
}
if (typeof fn !== "function") {
throw new Error("function not found");
}
return fn;
};
//**************************************************************************
//** newInstance
//**************************************************************************
/** Used to instantiate a new instance of a given class. Example:
* var app = newInstance(cls).apply(arguments);
*/
var newInstance = function(cls){
var fn = (new Function("return function () { };"))();
fn.prototype = cls.prototype;
var self = new fn();
return {
apply: function(args) {
cls.apply(self, args);
return self;
}
};
};
//**************************************************************************
//** dispatchEvent
//**************************************************************************
/** Fires a given window event (e.g. "load")
*/
var dispatchEvent = function(name){
var evt;
try{
evt = new Event(name);
}
catch(e){ //e.g. IE
evt = document.createEvent('Event');
evt.initEvent(name, false, false);
}
window.dispatchEvent(evt);
};
//**************************************************************************
//** log
//**************************************************************************
var log = function(str){
if (debug) console.log(str);
};
//**************************************************************************
//** get
//**************************************************************************
var get = function(url, success, onFail){
javaxt.dhtml.utils.get(url, {
success: function(text, xml, url, request){
request.abort();
request = null;
if (success) success.apply(me, [text, xml, url]);
},
failure: function(request){
if (onFail) onFail.apply(me, [request]);
}
});
};
init();
};