forked from RubyLouvre/avalon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avalon.require.text.js
36 lines (35 loc) · 1.33 KB
/
avalon.require.text.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
define(["avalon"], function() {
function jsEscape(content) {
return content.replace(/(['\\])/g, '\\$1')
.replace(/[\f]/g, "\\f")
.replace(/[\b]/g, "\\b")
.replace(/[\n]/g, "\\n")
.replace(/[\t]/g, "\\t")
.replace(/[\r]/g, "\\r")
.replace(/[\u2028]/g, "\\u2028")
.replace(/[\u2029]/g, "\\u2029");
}
require.config.plugins.text = function(url, y, checkDeps) {
var modules = avalon.modules
var xhr = new (self.XMLHttpRequest || ActiveXObject)("Microsoft.XMLHTTP")
var id = url.replace(/[?#].*/, "")
modules[id] = {}
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
var status = xhr.status;
if (status > 399 && status < 600) {
//An http 4xx or 5xx error. Signal an error.
avalon.error(url + ' HTTP status: ' + status)
} else {
modules[id].state = 2
modules[id].exports = jsEscape(xhr.responseText)
avalon.require.checkDeps()
}
}
}
xhr.open("GET", url, true)
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest")
xhr.send()
return id
}
})