|
1 /* |
|
2 YUI 3.10.3 (build 2fb5187) |
|
3 Copyright 2013 Yahoo! Inc. All rights reserved. |
|
4 Licensed under the BSD License. |
|
5 http://yuilibrary.com/license/ |
|
6 */ |
|
7 |
|
8 YUI.add('get', function (Y, NAME) { |
|
9 |
|
10 /** |
|
11 * NodeJS specific Get module used to load remote resources. |
|
12 * It contains the same signature as the default Get module so there is no code change needed. |
|
13 * @module get-nodejs |
|
14 * @class GetNodeJS |
|
15 */ |
|
16 |
|
17 var Module = require('module'), |
|
18 |
|
19 path = require('path'), |
|
20 fs = require('fs'), |
|
21 request = require('request'), |
|
22 end = function(cb, msg, result) { |
|
23 if (Y.Lang.isFunction(cb.onEnd)) { |
|
24 cb.onEnd.call(Y, msg, result); |
|
25 } |
|
26 }, pass = function(cb) { |
|
27 if (Y.Lang.isFunction(cb.onSuccess)) { |
|
28 cb.onSuccess.call(Y, cb); |
|
29 } |
|
30 end(cb, 'success', 'success'); |
|
31 }, fail = function(cb, er) { |
|
32 er.errors = [er]; |
|
33 if (Y.Lang.isFunction(cb.onFailure)) { |
|
34 cb.onFailure.call(Y, er, cb); |
|
35 } |
|
36 end(cb, er, 'fail'); |
|
37 }; |
|
38 |
|
39 |
|
40 Y.Get = function() { |
|
41 }; |
|
42 |
|
43 //Setup the default config base path |
|
44 Y.config.base = path.join(__dirname, '../'); |
|
45 |
|
46 YUI.require = require; |
|
47 YUI.process = process; |
|
48 |
|
49 /** |
|
50 * Takes the raw JS files and wraps them to be executed in the YUI context so they can be loaded |
|
51 * into the YUI object |
|
52 * @method _exec |
|
53 * @private |
|
54 * @param {String} data The JS to execute |
|
55 * @param {String} url The path to the file that was parsed |
|
56 * @param {Callback} cb The callback to execute when this is completed |
|
57 * @param {Error} cb.err=null Error object |
|
58 * @param {String} cb.url The URL that was just parsed |
|
59 */ |
|
60 |
|
61 Y.Get._exec = function(data, url, cb) { |
|
62 if (data.charCodeAt(0) === 0xFEFF) { |
|
63 data = data.slice(1); |
|
64 } |
|
65 |
|
66 var mod = new Module(url, module); |
|
67 mod.filename = url; |
|
68 mod.paths = Module._nodeModulePaths(path.dirname(url)); |
|
69 if (typeof YUI._getLoadHook === 'function') { |
|
70 data = YUI._getLoadHook(data, url); |
|
71 } |
|
72 mod._compile('module.exports = function (YUI) {' + data + '\n;return YUI;};', url); |
|
73 |
|
74 /*global YUI:true */ |
|
75 YUI = mod.exports(YUI); |
|
76 |
|
77 mod.loaded = true; |
|
78 |
|
79 cb(null, url); |
|
80 }; |
|
81 |
|
82 /** |
|
83 * Fetches the content from a remote URL or a file from disc and passes the content |
|
84 * off to `_exec` for parsing |
|
85 * @method _include |
|
86 * @private |
|
87 * @param {String} url The URL/File path to fetch the content from |
|
88 * @param {Callback} cb The callback to fire once the content has been executed via `_exec` |
|
89 */ |
|
90 Y.Get._include = function (url, cb) { |
|
91 var cfg, |
|
92 mod, |
|
93 self = this; |
|
94 |
|
95 if (url.match(/^https?:\/\//)) { |
|
96 cfg = { |
|
97 url: url, |
|
98 timeout: self.timeout |
|
99 }; |
|
100 request(cfg, function (err, response, body) { |
|
101 if (err) { |
|
102 cb(err, url); |
|
103 } else { |
|
104 Y.Get._exec(body, url, cb); |
|
105 } |
|
106 }); |
|
107 } else { |
|
108 try { |
|
109 // Try to resolve paths relative to the module that required yui. |
|
110 url = Module._findPath(url, Module._resolveLookupPaths(url, module.parent.parent)[1]); |
|
111 |
|
112 if (Y.config.useSync) { |
|
113 //Needs to be in useSync |
|
114 mod = fs.readFileSync(url,'utf8'); |
|
115 } else { |
|
116 fs.readFile(url, 'utf8', function (err, mod) { |
|
117 if (err) { |
|
118 cb(err, url); |
|
119 } else { |
|
120 Y.Get._exec(mod, url, cb); |
|
121 } |
|
122 }); |
|
123 return; |
|
124 } |
|
125 } catch (err) { |
|
126 cb(err, url); |
|
127 return; |
|
128 } |
|
129 |
|
130 Y.Get._exec(mod, url, cb); |
|
131 } |
|
132 }; |
|
133 |
|
134 |
|
135 /** |
|
136 * Override for Get.script for loading local or remote YUI modules. |
|
137 * @method js |
|
138 * @param {Array|String} s The URL's to load into this context |
|
139 * @param {Object} options Transaction options |
|
140 */ |
|
141 Y.Get.js = function(s, options) { |
|
142 var urls = Y.Array(s), url, i, l = urls.length, c= 0, |
|
143 check = function() { |
|
144 if (c === l) { |
|
145 pass(options); |
|
146 } |
|
147 }; |
|
148 |
|
149 |
|
150 /*jshint loopfunc: true */ |
|
151 for (i=0; i<l; i++) { |
|
152 url = urls[i]; |
|
153 if (Y.Lang.isObject(url)) { |
|
154 url = url.url; |
|
155 } |
|
156 |
|
157 url = url.replace(/'/g, '%27'); |
|
158 Y.Get._include(url, function(err, url) { |
|
159 if (!Y.config) { |
|
160 Y.config = { |
|
161 debug: true |
|
162 }; |
|
163 } |
|
164 if (options.onProgress) { |
|
165 options.onProgress.call(options.context || Y, url); |
|
166 } |
|
167 if (err) { |
|
168 fail(options, err); |
|
169 } else { |
|
170 c++; |
|
171 check(); |
|
172 } |
|
173 }); |
|
174 } |
|
175 |
|
176 //Keeping Signature in the browser. |
|
177 return { |
|
178 execute: function() {} |
|
179 }; |
|
180 }; |
|
181 |
|
182 /** |
|
183 * Alias for `Y.Get.js` |
|
184 * @method script |
|
185 */ |
|
186 Y.Get.script = Y.Get.js; |
|
187 |
|
188 //Place holder for SS Dom access |
|
189 Y.Get.css = function(s, cb) { |
|
190 pass(cb); |
|
191 }; |
|
192 |
|
193 |
|
194 |
|
195 }, '@VERSION@'); |