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