2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('get', function (Y, NAME) {
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.
17 var Module = require('module'),
19 path = require('path'),
21 request = require('request'),
22 end = function(cb, msg, result) {
23 //Y.log('Get end: ' + cb.onEnd);
24 if (Y.Lang.isFunction(cb.onEnd)) {
25 cb.onEnd.call(Y, msg, result);
27 }, pass = function(cb) {
28 //Y.log('Get pass: ' + cb.onSuccess);
29 if (Y.Lang.isFunction(cb.onSuccess)) {
30 cb.onSuccess.call(Y, cb);
32 end(cb, 'success', 'success');
33 }, fail = function(cb, er) {
34 //Y.log('Get fail: ' + er);
36 if (Y.Lang.isFunction(cb.onFailure)) {
37 cb.onFailure.call(Y, er, cb);
46 //Setup the default config base path
47 Y.config.base = path.join(__dirname, '../');
49 YUI.require = require;
50 YUI.process = process;
53 * Takes the raw JS files and wraps them to be executed in the YUI context so they can be loaded
57 * @param {String} data The JS to execute
58 * @param {String} url The path to the file that was parsed
59 * @param {Callback} cb The callback to execute when this is completed
60 * @param {Error} cb.err=null Error object
61 * @param {String} cb.url The URL that was just parsed
64 Y.Get._exec = function(data, url, cb) {
65 if (data.charCodeAt(0) === 0xFEFF) {
69 var mod = new Module(url, module);
71 mod.paths = Module._nodeModulePaths(path.dirname(url));
72 if (typeof YUI._getLoadHook === 'function') {
73 data = YUI._getLoadHook(data, url);
75 mod._compile('module.exports = function (YUI) {' + data + '\n;return YUI;};', url);
78 YUI = mod.exports(YUI);
86 * Fetches the content from a remote URL or a file from disc and passes the content
87 * off to `_exec` for parsing
90 * @param {String} url The URL/File path to fetch the content from
91 * @param {Callback} cb The callback to fire once the content has been executed via `_exec`
93 Y.Get._include = function (url, cb) {
98 if (url.match(/^https?:\/\//)) {
101 timeout: self.timeout
103 request(cfg, function (err, response, body) {
105 Y.log(err, 'error', 'get');
108 Y.Get._exec(body, url, cb);
113 // Try to resolve paths relative to the module that required yui.
114 url = Module._findPath(url, Module._resolveLookupPaths(url, module.parent.parent)[1]);
116 if (Y.config.useSync) {
117 //Needs to be in useSync
118 mod = fs.readFileSync(url,'utf8');
120 fs.readFile(url, 'utf8', function (err, mod) {
124 Y.Get._exec(mod, url, cb);
134 Y.Get._exec(mod, url, cb);
140 * Override for Get.script for loading local or remote YUI modules.
142 * @param {Array|String} s The URL's to load into this context
143 * @param {Object} options Transaction options
145 Y.Get.js = function(s, options) {
146 var urls = Y.Array(s), url, i, l = urls.length, c= 0,
154 /*jshint loopfunc: true */
155 for (i=0; i<l; i++) {
157 if (Y.Lang.isObject(url)) {
161 url = url.replace(/'/g, '%27');
162 Y.log('URL: ' + url, 'info', 'get');
163 Y.Get._include(url, function(err, url) {
169 if (options.onProgress) {
170 options.onProgress.call(options.context || Y, url);
172 Y.log('After Load: ' + url, 'info', 'get');
175 Y.log('----------------------------------------------------------', 'error', 'get');
176 Y.log(err, 'error', 'get');
177 Y.log('----------------------------------------------------------', 'error', 'get');
185 //Keeping Signature in the browser.
187 execute: function() {}
192 * Alias for `Y.Get.js`
195 Y.Get.script = Y.Get.js;
197 //Place holder for SS Dom access
198 Y.Get.css = function(s, cb) {
199 Y.log('Y.Get.css is not supported, just reporting that it has loaded but not fetching.', 'warn', 'get');