1 | define(["./_base/kernel", "require", "./has", "./has!host-browser?./_base/xhr"], function(dojo, require, has, xhr){ |
---|
2 | // module: |
---|
3 | // dojo/text |
---|
4 | // summary: |
---|
5 | // This module implements the !dojo/text plugin and the dojo.cache API. |
---|
6 | // description: |
---|
7 | // We choose to include our own plugin to leverage functionality already contained in dojo |
---|
8 | // and thereby reduce the size of the plugin compared to various foreign loader implementations. |
---|
9 | // Also, this allows foreign AMD loaders to be used without their plugins. |
---|
10 | // |
---|
11 | // CAUTION: this module is designed to optionally function synchronously to support the dojo v1.x synchronous |
---|
12 | // loader. This feature is outside the scope of the CommonJS plugins specification. |
---|
13 | |
---|
14 | var getText; |
---|
15 | if(has("host-browser")){ |
---|
16 | getText= function(url, sync, load){ |
---|
17 | xhr("GET", {url:url, sync:!!sync, load:load}); |
---|
18 | }; |
---|
19 | }else{ |
---|
20 | // TODOC: only works for dojo AMD loader |
---|
21 | if(require.getText){ |
---|
22 | getText= require.getText; |
---|
23 | }else{ |
---|
24 | console.error("dojo/text plugin failed to load because loader does not support getText"); |
---|
25 | } |
---|
26 | } |
---|
27 | |
---|
28 | var |
---|
29 | theCache= {}, |
---|
30 | |
---|
31 | strip= function(text){ |
---|
32 | //Strips <?xml ...?> declarations so that external SVG and XML |
---|
33 | //documents can be added to a document without worry. Also, if the string |
---|
34 | //is an HTML document, only the part inside the body tag is returned. |
---|
35 | if(text){ |
---|
36 | text= text.replace(/^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im, ""); |
---|
37 | var matches= text.match(/<body[^>]*>\s*([\s\S]+)\s*<\/body>/im); |
---|
38 | if(matches){ |
---|
39 | text= matches[1]; |
---|
40 | } |
---|
41 | }else{ |
---|
42 | text = ""; |
---|
43 | } |
---|
44 | return text; |
---|
45 | }, |
---|
46 | |
---|
47 | notFound = {}, |
---|
48 | |
---|
49 | pending = {}, |
---|
50 | |
---|
51 | result= { |
---|
52 | dynamic: |
---|
53 | // the dojo/text caches it's own resources because of dojo.cache |
---|
54 | true, |
---|
55 | |
---|
56 | normalize:function(id, toAbsMid){ |
---|
57 | // id is something like (path may be relative): |
---|
58 | // |
---|
59 | // "path/to/text.html" |
---|
60 | // "path/to/text.html!strip" |
---|
61 | var parts= id.split("!"), |
---|
62 | url= parts[0]; |
---|
63 | return (/^\./.test(url) ? toAbsMid(url) : url) + (parts[1] ? "!" + parts[1] : ""); |
---|
64 | }, |
---|
65 | |
---|
66 | load:function(id, require, load){ |
---|
67 | // id is something like (path is always absolute): |
---|
68 | // |
---|
69 | // "path/to/text.html" |
---|
70 | // "path/to/text.html!strip" |
---|
71 | var |
---|
72 | parts= id.split("!"), |
---|
73 | stripFlag= parts.length>1, |
---|
74 | absMid= parts[0], |
---|
75 | url = require.toUrl(parts[0]), |
---|
76 | text = notFound, |
---|
77 | finish = function(text){ |
---|
78 | load(stripFlag ? strip(text) : text); |
---|
79 | }; |
---|
80 | if(absMid in theCache){ |
---|
81 | text = theCache[absMid]; |
---|
82 | }else if(url in require.cache){ |
---|
83 | text = require.cache[url]; |
---|
84 | }else if(url in theCache){ |
---|
85 | text = theCache[url]; |
---|
86 | } |
---|
87 | if(text===notFound){ |
---|
88 | if(pending[url]){ |
---|
89 | pending[url].push(finish); |
---|
90 | }else{ |
---|
91 | var pendingList = pending[url] = [finish]; |
---|
92 | getText(url, !require.async, function(text){ |
---|
93 | theCache[absMid]= theCache[url]= text; |
---|
94 | for(var i = 0; i<pendingList.length;){ |
---|
95 | pendingList[i++](text); |
---|
96 | } |
---|
97 | delete pending[url]; |
---|
98 | }); |
---|
99 | } |
---|
100 | }else{ |
---|
101 | finish(text); |
---|
102 | } |
---|
103 | } |
---|
104 | }; |
---|
105 | |
---|
106 | dojo.cache= function(/*String||Object*/module, /*String*/url, /*String||Object?*/value){ |
---|
107 | // * (string string [value]) => (module, url, value) |
---|
108 | // * (object [value]) => (module, value), url defaults to "" |
---|
109 | // |
---|
110 | // * if module is an object, then it must be convertable to a string |
---|
111 | // * (module, url) module + (url ? ("/" + url) : "") must be a legal argument to require.toUrl |
---|
112 | // * value may be a string or an object; if an object then may have the properties "value" and/or "sanitize" |
---|
113 | var key; |
---|
114 | if(typeof module=="string"){ |
---|
115 | if(/\//.test(module)){ |
---|
116 | // module is a version 1.7+ resolved path |
---|
117 | key = module; |
---|
118 | value = url; |
---|
119 | }else{ |
---|
120 | // module is a version 1.6- argument to dojo.moduleUrl |
---|
121 | key = require.toUrl(module.replace(/\./g, "/") + (url ? ("/" + url) : "")); |
---|
122 | } |
---|
123 | }else{ |
---|
124 | key = module + ""; |
---|
125 | value = url; |
---|
126 | } |
---|
127 | var |
---|
128 | val = (value != undefined && typeof value != "string") ? value.value : value, |
---|
129 | sanitize = value && value.sanitize; |
---|
130 | |
---|
131 | if(typeof val == "string"){ |
---|
132 | //We have a string, set cache value |
---|
133 | theCache[key] = val; |
---|
134 | return sanitize ? strip(val) : val; |
---|
135 | }else if(val === null){ |
---|
136 | //Remove cached value |
---|
137 | delete theCache[key]; |
---|
138 | return null; |
---|
139 | }else{ |
---|
140 | //Allow cache values to be empty strings. If key property does |
---|
141 | //not exist, fetch it. |
---|
142 | if(!(key in theCache)){ |
---|
143 | getText(key, true, function(text){ |
---|
144 | theCache[key]= text; |
---|
145 | }); |
---|
146 | } |
---|
147 | return sanitize ? strip(theCache[key]) : theCache[key]; |
---|
148 | } |
---|
149 | }; |
---|
150 | |
---|
151 | return result; |
---|
152 | |
---|
153 | /*===== |
---|
154 | dojo.cache = function(module, url, value){ |
---|
155 | // summary: |
---|
156 | // A getter and setter for storing the string content associated with the |
---|
157 | // module and url arguments. |
---|
158 | // description: |
---|
159 | // If module is a string that contains slashes, then it is interpretted as a fully |
---|
160 | // resolved path (typically a result returned by require.toUrl), and url should not be |
---|
161 | // provided. This is the preferred signature. If module is a string that does not |
---|
162 | // contain slashes, then url must also be provided and module and url are used to |
---|
163 | // call `dojo.moduleUrl()` to generate a module URL. This signature is deprecated. |
---|
164 | // If value is specified, the cache value for the moduleUrl will be set to |
---|
165 | // that value. Otherwise, dojo.cache will fetch the moduleUrl and store it |
---|
166 | // in its internal cache and return that cached value for the URL. To clear |
---|
167 | // a cache value pass null for value. Since XMLHttpRequest (XHR) is used to fetch the |
---|
168 | // the URL contents, only modules on the same domain of the page can use this capability. |
---|
169 | // The build system can inline the cache values though, to allow for xdomain hosting. |
---|
170 | // module: String||Object |
---|
171 | // If a String with slashes, a fully resolved path; if a String without slashes, the |
---|
172 | // module name to use for the base part of the URL, similar to module argument |
---|
173 | // to `dojo.moduleUrl`. If an Object, something that has a .toString() method that |
---|
174 | // generates a valid path for the cache item. For example, a dojo._Url object. |
---|
175 | // url: String |
---|
176 | // The rest of the path to append to the path derived from the module argument. If |
---|
177 | // module is an object, then this second argument should be the "value" argument instead. |
---|
178 | // value: String||Object? |
---|
179 | // If a String, the value to use in the cache for the module/url combination. |
---|
180 | // If an Object, it can have two properties: value and sanitize. The value property |
---|
181 | // should be the value to use in the cache, and sanitize can be set to true or false, |
---|
182 | // to indicate if XML declarations should be removed from the value and if the HTML |
---|
183 | // inside a body tag in the value should be extracted as the real value. The value argument |
---|
184 | // or the value property on the value argument are usually only used by the build system |
---|
185 | // as it inlines cache content. |
---|
186 | // example: |
---|
187 | // To ask dojo.cache to fetch content and store it in the cache (the dojo["cache"] style |
---|
188 | // of call is used to avoid an issue with the build system erroneously trying to intern |
---|
189 | // this example. To get the build system to intern your dojo.cache calls, use the |
---|
190 | // "dojo.cache" style of call): |
---|
191 | // | //If template.html contains "<h1>Hello</h1>" that will be |
---|
192 | // | //the value for the text variable. |
---|
193 | // | var text = dojo["cache"]("my.module", "template.html"); |
---|
194 | // example: |
---|
195 | // To ask dojo.cache to fetch content and store it in the cache, and sanitize the input |
---|
196 | // (the dojo["cache"] style of call is used to avoid an issue with the build system |
---|
197 | // erroneously trying to intern this example. To get the build system to intern your |
---|
198 | // dojo.cache calls, use the "dojo.cache" style of call): |
---|
199 | // | //If template.html contains "<html><body><h1>Hello</h1></body></html>", the |
---|
200 | // | //text variable will contain just "<h1>Hello</h1>". |
---|
201 | // | var text = dojo["cache"]("my.module", "template.html", {sanitize: true}); |
---|
202 | // example: |
---|
203 | // Same example as previous, but demostrates how an object can be passed in as |
---|
204 | // the first argument, then the value argument can then be the second argument. |
---|
205 | // | //If template.html contains "<html><body><h1>Hello</h1></body></html>", the |
---|
206 | // | //text variable will contain just "<h1>Hello</h1>". |
---|
207 | // | var text = dojo["cache"](new dojo._Url("my/module/template.html"), {sanitize: true}); |
---|
208 | return val; //String |
---|
209 | }; |
---|
210 | =====*/ |
---|
211 | }); |
---|
212 | |
---|