This repository was archived by the owner on Oct 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmodules.js
More file actions
155 lines (144 loc) 路 4.07 KB
/
modules.js
File metadata and controls
155 lines (144 loc) 路 4.07 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
/*
* Modules
* Copyright (C) Codexa Organization.
*/
if (!app) {
var app = {};
}
(function(window, undefined) {
'use strict';
function loadModule(url, callback, deep) {
// Validate params
if (!url) {
callback('bad-params');
}
// Get module
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.responseType = "document";
request.overrideMimeType("text/html");
request.addEventListener("load", function(e) {
if(this.status === 200) {
var response = this.response;
if (deep) {
var elements = response.querySelectorAll("script, link");
var loading = {};
for (var i = 0; i < elements.length; i++) {
(function() {
var element = elements[i];
var name = element.tagName;
if(name === "SCRIPT" ? element.src : element.href) {
var type = element.type;
var url = name === "SCRIPT" ? element.src : element.href;
var data = element.dataset;
if(!loading[url]) {
loading[url] = [];
var req = new XMLHttpRequest();
req.open("GET", url, true);
req.responseType = "text";
req.addEventListener("load", function(e) {
var done = true;
if(this.status === 200) {
var inline = response.createElement(name === "SCRIPT" ? "SCRIPT" : "STYLE");
var text = this.response;
text = text.replace(/\[ORIGIN_OF_MAIN_DOCUMENT\]/g, window.location.origin ? window.location.origin : window.location.protocol + "//" + window.location.host);
inline.type = type;
if(name === "SCRIPT") {
inline.textContent = text.replace(/<\/(script)/ig, '<\\\/$1') +
'\n//# sourceURL=' + url;
} else {
inline.textContent = text.replace(/</g, '%3C').replace(/>/g, '%3E') + // Hack to not trip up getHTML() regex and keep editor.css cleaner.
'\n/*# sourceURL=' + url + '*/';
}
for (var key in data) {
inline.dataset[key] = data[key];
}
loading[url][0].parentNode.replaceChild(inline, loading[url][0]);
for (var i = 1; i < loading[url].length; i++) {
loading[url][i].parentNode.removeChild(loading[url][i]);
}
delete loading[url];
for(var x in loading) {
done = false;
break;
}
if (done) {
callback(null, createBlob(response));
}
}
}, false);
req.send();
}
loading[url].push(element);
}
})();
}
} else {
callback(null, createBlob(response));
}
} else {
callback(this.status);
}
}, false);
request.send();
}
function fillFrame(url, destinations, callback) {
// Validate params
if (!url || !destinations) {
callback('bad-params');
}
if (!Array.isArray(destinations)) {
destinations = [destinations];
}
// Fill frames
destinations.forEach(function(t){
t.src = url;
});
// Done!
callback();
}
function createBlob(response) {
var moduleBlob = new Blob([response.documentElement.outerHTML], {type: "text/html"});
var moduleURL = URL.createObjectURL ? URL.createObjectURL(moduleBlob) : URL.webkitCreateObjectURL ? URL.webkitCreateObjectURL(moduleBlob) : null;
return moduleURL;
}
app.modules = {
fill: function (url, destinations, callback) {
fillFrame(url, destinations, function(e){
if (e) {
console.log(e);
} else {
callback();
}
});
},
load: function (url, destinations, callback, store, deep) {
console.log('Loading '+url);
if (store) {
loadModule(url, function(e,b){
if (e) {
console.log(e);
} else {
fillFrame(b, destinations, function(e){
if (e) {
console.log(e);
} else {
console.log('Finished loading '+url);
callback(b);
}
});
}
}, deep);
} else {
fillFrame(url, destinations, function(e){
if (e) {
console.log(e);
} else {
console.log('Finished loading '+url);
callback();
}
});
}
}
};
})(this);