Mercurial > wikked
annotate static/js/text.js @ 77:d5caf4030999
Added moment.js for formatting dates.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 27 Feb 2013 22:02:02 -0800 |
parents | 8250c977bc50 |
children |
rev | line source |
---|---|
48
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 /** |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 * @license RequireJS text 2.0.4 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 * Available via the MIT or new BSD license. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 * see: http://github.com/requirejs/text for details |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 */ |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 /*jslint regexp: true */ |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 /*global require: false, XMLHttpRequest: false, ActiveXObject: false, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 define: false, window: false, process: false, Packages: false, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 java: false, location: false */ |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 define(['module'], function (module) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 'use strict'; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 var text, fs, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'], |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 xmlRegExp = /^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 bodyRegExp = /<body[^>]*>\s*([\s\S]+)\s*<\/body>/im, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 hasLocation = typeof location !== 'undefined' && location.href, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 defaultProtocol = hasLocation && location.protocol && location.protocol.replace(/\:/, ''), |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 defaultHostName = hasLocation && location.hostname, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 defaultPort = hasLocation && (location.port || undefined), |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 buildMap = [], |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 masterConfig = (module.config && module.config()) || {}; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 text = { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 version: '2.0.4', |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 strip: function (content) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 //Strips <?xml ...?> declarations so that external SVG and XML |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 //documents can be added to a document without worry. Also, if the string |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 //is an HTML document, only the part inside the body tag is returned. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 if (content) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 content = content.replace(xmlRegExp, ""); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 var matches = content.match(bodyRegExp); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 if (matches) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 content = matches[1]; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 } else { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 content = ""; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 return content; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 }, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 jsEscape: function (content) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 return content.replace(/(['\\])/g, '\\$1') |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 .replace(/[\f]/g, "\\f") |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 .replace(/[\b]/g, "\\b") |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 .replace(/[\n]/g, "\\n") |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 .replace(/[\t]/g, "\\t") |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 .replace(/[\r]/g, "\\r") |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
51 .replace(/[\u2028]/g, "\\u2028") |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 .replace(/[\u2029]/g, "\\u2029"); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
53 }, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
55 createXhr: masterConfig.createXhr || function () { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
56 //Would love to dump the ActiveX crap in here. Need IE 6 to die first. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
57 var xhr, i, progId; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
58 if (typeof XMLHttpRequest !== "undefined") { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
59 return new XMLHttpRequest(); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
60 } else if (typeof ActiveXObject !== "undefined") { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
61 for (i = 0; i < 3; i += 1) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
62 progId = progIds[i]; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
63 try { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
64 xhr = new ActiveXObject(progId); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
65 } catch (e) {} |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
66 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
67 if (xhr) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
68 progIds = [progId]; // so faster next time |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
69 break; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
70 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
71 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
72 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
73 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 return xhr; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
75 }, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
76 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
77 /** |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
78 * Parses a resource name into its component parts. Resource names |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
79 * look like: module/name.ext!strip, where the !strip part is |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
80 * optional. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
81 * @param {String} name the resource name |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
82 * @returns {Object} with properties "moduleName", "ext" and "strip" |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
83 * where strip is a boolean. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
84 */ |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
85 parseName: function (name) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
86 var modName, ext, temp, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
87 strip = false, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
88 index = name.indexOf("."), |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
89 isRelative = name.indexOf('./') === 0 || |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
90 name.indexOf('../') === 0; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
91 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
92 if (index !== -1 && (!isRelative || index > 1)) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
93 modName = name.substring(0, index); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
94 ext = name.substring(index + 1, name.length); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
95 } else { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
96 modName = name; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
97 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
98 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
99 temp = ext || modName; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
100 index = temp.indexOf("!"); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
101 if (index !== -1) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
102 //Pull off the strip arg. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
103 strip = temp.substring(index + 1) === "strip"; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
104 temp = temp.substring(0, index); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
105 if (ext) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
106 ext = temp; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
107 } else { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
108 modName = temp; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
109 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
110 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
111 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
112 return { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
113 moduleName: modName, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
114 ext: ext, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
115 strip: strip |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
116 }; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
117 }, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
118 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
119 xdRegExp: /^((\w+)\:)?\/\/([^\/\\]+)/, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
120 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
121 /** |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
122 * Is an URL on another domain. Only works for browser use, returns |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
123 * false in non-browser environments. Only used to know if an |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
124 * optimized .js version of a text resource should be loaded |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
125 * instead. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
126 * @param {String} url |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
127 * @returns Boolean |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
128 */ |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
129 useXhr: function (url, protocol, hostname, port) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
130 var uProtocol, uHostName, uPort, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
131 match = text.xdRegExp.exec(url); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
132 if (!match) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
133 return true; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
134 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
135 uProtocol = match[2]; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
136 uHostName = match[3]; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
137 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
138 uHostName = uHostName.split(':'); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
139 uPort = uHostName[1]; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
140 uHostName = uHostName[0]; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
141 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
142 return (!uProtocol || uProtocol === protocol) && |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
143 (!uHostName || uHostName.toLowerCase() === hostname.toLowerCase()) && |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
144 ((!uPort && !uHostName) || uPort === port); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
145 }, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
146 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
147 finishLoad: function (name, strip, content, onLoad) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
148 content = strip ? text.strip(content) : content; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
149 if (masterConfig.isBuild) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
150 buildMap[name] = content; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
151 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
152 onLoad(content); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
153 }, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
154 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
155 load: function (name, req, onLoad, config) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
156 //Name has format: some.module.filext!strip |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
157 //The strip part is optional. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
158 //if strip is present, then that means only get the string contents |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
159 //inside a body tag in an HTML string. For XML/SVG content it means |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
160 //removing the <?xml ...?> declarations so the content can be inserted |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
161 //into the current doc without problems. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
162 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
163 // Do not bother with the work if a build and text will |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
164 // not be inlined. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
165 if (config.isBuild && !config.inlineText) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
166 onLoad(); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
167 return; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
168 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
169 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
170 masterConfig.isBuild = config.isBuild; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
171 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
172 var parsed = text.parseName(name), |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
173 nonStripName = parsed.moduleName + |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
174 (parsed.ext ? '.' + parsed.ext : ''), |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
175 url = req.toUrl(nonStripName), |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
176 useXhr = (masterConfig.useXhr) || |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
177 text.useXhr; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
178 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
179 //Load the text. Use XHR if possible and in a browser. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
180 if (!hasLocation || useXhr(url, defaultProtocol, defaultHostName, defaultPort)) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
181 text.get(url, function (content) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
182 text.finishLoad(name, parsed.strip, content, onLoad); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
183 }, function (err) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
184 if (onLoad.error) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
185 onLoad.error(err); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
186 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
187 }); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
188 } else { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
189 //Need to fetch the resource across domains. Assume |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
190 //the resource has been optimized into a JS module. Fetch |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
191 //by the module name + extension, but do not include the |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
192 //!strip part to avoid file system issues. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
193 req([nonStripName], function (content) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
194 text.finishLoad(parsed.moduleName + '.' + parsed.ext, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
195 parsed.strip, content, onLoad); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
196 }); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
197 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
198 }, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
199 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
200 write: function (pluginName, moduleName, write, config) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
201 if (buildMap.hasOwnProperty(moduleName)) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
202 var content = text.jsEscape(buildMap[moduleName]); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
203 write.asModule(pluginName + "!" + moduleName, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
204 "define(function () { return '" + |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
205 content + |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
206 "';});\n"); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
207 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
208 }, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
209 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
210 writeFile: function (pluginName, moduleName, req, write, config) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
211 var parsed = text.parseName(moduleName), |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
212 extPart = parsed.ext ? '.' + parsed.ext : '', |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
213 nonStripName = parsed.moduleName + extPart, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
214 //Use a '.js' file name so that it indicates it is a |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
215 //script that can be loaded across domains. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
216 fileName = req.toUrl(parsed.moduleName + extPart) + '.js'; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
217 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
218 //Leverage own load() method to load plugin value, but only |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
219 //write out values that do not have the strip argument, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
220 //to avoid any potential issues with ! in file names. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
221 text.load(nonStripName, req, function (value) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
222 //Use own write() method to construct full module value. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
223 //But need to create shell that translates writeFile's |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
224 //write() to the right interface. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
225 var textWrite = function (contents) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
226 return write(fileName, contents); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
227 }; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
228 textWrite.asModule = function (moduleName, contents) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
229 return write.asModule(moduleName, fileName, contents); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
230 }; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
231 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
232 text.write(pluginName, nonStripName, textWrite, config); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
233 }, config); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
234 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
235 }; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
236 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
237 if (masterConfig.env === 'node' || (!masterConfig.env && |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
238 typeof process !== "undefined" && |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
239 process.versions && |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
240 !!process.versions.node)) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
241 //Using special require.nodeRequire, something added by r.js. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
242 fs = require.nodeRequire('fs'); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
243 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
244 text.get = function (url, callback) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
245 var file = fs.readFileSync(url, 'utf8'); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
246 //Remove BOM (Byte Mark Order) from utf8 files if it is there. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
247 if (file.indexOf('\uFEFF') === 0) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
248 file = file.substring(1); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
249 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
250 callback(file); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
251 }; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
252 } else if (masterConfig.env === 'xhr' || (!masterConfig.env && |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
253 text.createXhr())) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
254 text.get = function (url, callback, errback) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
255 var xhr = text.createXhr(); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
256 xhr.open('GET', url, true); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
257 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
258 //Allow overrides specified in config |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
259 if (masterConfig.onXhr) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
260 masterConfig.onXhr(xhr, url); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
261 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
262 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
263 xhr.onreadystatechange = function (evt) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
264 var status, err; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
265 //Do not explicitly handle errors, those should be |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
266 //visible via console output in the browser. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
267 if (xhr.readyState === 4) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
268 status = xhr.status; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
269 if (status > 399 && status < 600) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
270 //An http 4xx or 5xx error. Signal an error. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
271 err = new Error(url + ' HTTP status: ' + status); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
272 err.xhr = xhr; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
273 errback(err); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
274 } else { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
275 callback(xhr.responseText); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
276 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
277 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
278 }; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
279 xhr.send(null); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
280 }; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
281 } else if (masterConfig.env === 'rhino' || (!masterConfig.env && |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
282 typeof Packages !== 'undefined' && typeof java !== 'undefined')) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
283 //Why Java, why is this so awkward? |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
284 text.get = function (url, callback) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
285 var stringBuffer, line, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
286 encoding = "utf-8", |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
287 file = new java.io.File(url), |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
288 lineSeparator = java.lang.System.getProperty("line.separator"), |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
289 input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)), |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
290 content = ''; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
291 try { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
292 stringBuffer = new java.lang.StringBuffer(); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
293 line = input.readLine(); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
294 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
295 // Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
296 // http://www.unicode.org/faq/utf_bom.html |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
297 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
298 // Note that when we use utf-8, the BOM should appear as "EF BB BF", but it doesn't due to this bug in the JDK: |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
299 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
300 if (line && line.length() && line.charAt(0) === 0xfeff) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
301 // Eat the BOM, since we've already found the encoding on this file, |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
302 // and we plan to concatenating this buffer with others; the BOM should |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
303 // only appear at the top of a file. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
304 line = line.substring(1); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
305 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
306 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
307 stringBuffer.append(line); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
308 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
309 while ((line = input.readLine()) !== null) { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
310 stringBuffer.append(lineSeparator); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
311 stringBuffer.append(line); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
312 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
313 //Make sure we return a JavaScript string and not a Java string. |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
314 content = String(stringBuffer.toString()); //String |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
315 } finally { |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
316 input.close(); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
317 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
318 callback(content); |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
319 }; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
320 } |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
321 |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
322 return text; |
9658edea3121
Now using RequireJS' "text" extension to load all HTML templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
323 }); |