-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathseajs-text.js
168 lines (134 loc) · 3.41 KB
/
seajs-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
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
/**
* The Sea.js plugin for loading text resources such as template, json etc
*/
var global = window
var plugins = {}
var uriCache = {}
function register(o) {
plugins[o.name] = o
}
// normal text
register({
name: "text",
ext: [".tpl", ".html"],
exec: function(uri, content) {
globalEval('define("' + uri + '#", [], "' + jsEscape(content) + '")')
}
})
// json
register({
name: "json",
ext: [".json"],
exec: function(uri, content) {
globalEval('define("' + uri + '#", [], ' + content + ')')
}
})
// for handlebars template
register({
name: "handlebars",
ext: [".handlebars"],
exec: function(uri, content) {
var code = [
'define("' + uri + '#", ["handlebars"], function(require, exports, module) {',
' var source = "' + jsEscape(content) + '"',
' var Handlebars = require("handlebars")["default"]',
' module.exports = function(data, options) {',
' options || (options = {})',
' options.helpers || (options.helpers = {})',
' for (var key in Handlebars.helpers) {',
' options.helpers[key] = options.helpers[key] || Handlebars.helpers[key]',
' }',
' return Handlebars.compile(source)(data, options)',
' }',
'})'
].join('\n')
globalEval(code)
}
})
seajs.on("resolve", function(data) {
var id = data.id
if (!id) return ""
var pluginName
var m
// text!path/to/some.xx
if ((m = id.match(/^(\w+)!(.+)$/)) && isPlugin(m[1])) {
pluginName = m[1]
id = m[2]
}
// http://path/to/a.tpl
// http://path/to/c.json?v2
else if ((m = id.match(/[^?]+(\.\w+)(?:\?|#|$)/))) {
pluginName = getPluginName(m[1])
}
if (pluginName && id.indexOf("#") === -1) {
id += "#"
}
var uri = seajs.resolve(id, data.refUri)
if (pluginName) {
uriCache[uri] = pluginName
}
data.uri = uri
})
seajs.on("request", function(data) {
var name = uriCache[data.uri]
if (name) {
xhr(data.requestUri, function(content) {
plugins[name].exec(data.uri, content)
data.onRequest()
})
data.requested = true
}
})
// Helpers
function isPlugin(name) {
return name && plugins.hasOwnProperty(name)
}
function getPluginName(ext) {
for (var k in plugins) {
if (isPlugin(k)) {
var exts = "," + plugins[k].ext.join(",") + ","
if (exts.indexOf("," + ext + ",") > -1) {
return k
}
}
}
}
function xhr(url, callback) {
var r = global.XMLHttpRequest ?
new global.XMLHttpRequest() :
new global.ActiveXObject("Microsoft.XMLHTTP")
r.open("GET", url, true)
r.onreadystatechange = function() {
if (r.readyState === 4) {
// Support local file
if (r.status > 399 && r.status < 600) {
throw new Error("Could not load: " + url + ", status = " + r.status)
}
else {
callback(r.responseText)
}
}
}
return r.send(null)
}
function globalEval(content) {
if (content && /\S/.test(content)) {
(global.execScript || function(content) {
(global.eval || eval).call(global, content)
})(content)
}
}
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")
}
function pure(uri) {
// Remove timestamp etc
return uri.replace(/\?.*$/, "")
}