src/cm/media/js/lib/yui/yui_3.10.3/build/get-nodejs/get-nodejs-debug.js
changeset 525 89ef5ed3c48b
equal deleted inserted replaced
524:322d0feea350 525:89ef5ed3c48b
       
     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             //Y.log('Get end: ' + cb.onEnd);
       
    24             if (Y.Lang.isFunction(cb.onEnd)) {
       
    25                 cb.onEnd.call(Y, msg, result);
       
    26             }
       
    27         }, pass = function(cb) {
       
    28             //Y.log('Get pass: ' + cb.onSuccess);
       
    29             if (Y.Lang.isFunction(cb.onSuccess)) {
       
    30                 cb.onSuccess.call(Y, cb);
       
    31             }
       
    32             end(cb, 'success', 'success');
       
    33         }, fail = function(cb, er) {
       
    34             //Y.log('Get fail: ' + er);
       
    35             er.errors = [er];
       
    36             if (Y.Lang.isFunction(cb.onFailure)) {
       
    37                 cb.onFailure.call(Y, er, cb);
       
    38             }
       
    39             end(cb, er, 'fail');
       
    40         };
       
    41 
       
    42 
       
    43     Y.Get = function() {
       
    44     };
       
    45 
       
    46     //Setup the default config base path
       
    47     Y.config.base = path.join(__dirname, '../');
       
    48 
       
    49     YUI.require = require;
       
    50     YUI.process = process;
       
    51 
       
    52     /**
       
    53     * Takes the raw JS files and wraps them to be executed in the YUI context so they can be loaded
       
    54     * into the YUI object
       
    55     * @method _exec
       
    56     * @private
       
    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
       
    62     */
       
    63 
       
    64     Y.Get._exec = function(data, url, cb) {
       
    65         if (data.charCodeAt(0) === 0xFEFF) {
       
    66             data = data.slice(1);
       
    67         }
       
    68 
       
    69         var mod = new Module(url, module);
       
    70         mod.filename = url;
       
    71         mod.paths = Module._nodeModulePaths(path.dirname(url));
       
    72         if (typeof YUI._getLoadHook === 'function') {
       
    73             data = YUI._getLoadHook(data, url);
       
    74         }
       
    75         mod._compile('module.exports = function (YUI) {' + data + '\n;return YUI;};', url);
       
    76 
       
    77         /*global YUI:true */
       
    78         YUI = mod.exports(YUI);
       
    79 
       
    80         mod.loaded = true;
       
    81 
       
    82         cb(null, url);
       
    83     };
       
    84 
       
    85     /**
       
    86     * Fetches the content from a remote URL or a file from disc and passes the content
       
    87     * off to `_exec` for parsing
       
    88     * @method _include
       
    89     * @private
       
    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`
       
    92     */
       
    93     Y.Get._include = function (url, cb) {
       
    94         var cfg,
       
    95             mod,
       
    96             self = this;
       
    97 
       
    98         if (url.match(/^https?:\/\//)) {
       
    99             cfg = {
       
   100                 url: url,
       
   101                 timeout: self.timeout
       
   102             };
       
   103             request(cfg, function (err, response, body) {
       
   104                 if (err) {
       
   105                     Y.log(err, 'error', 'get');
       
   106                     cb(err, url);
       
   107                 } else {
       
   108                     Y.Get._exec(body, url, cb);
       
   109                 }
       
   110             });
       
   111         } else {
       
   112             try {
       
   113                 // Try to resolve paths relative to the module that required yui.
       
   114                 url = Module._findPath(url, Module._resolveLookupPaths(url, module.parent.parent)[1]);
       
   115 
       
   116                 if (Y.config.useSync) {
       
   117                     //Needs to be in useSync
       
   118                     mod = fs.readFileSync(url,'utf8');
       
   119                 } else {
       
   120                     fs.readFile(url, 'utf8', function (err, mod) {
       
   121                         if (err) {
       
   122                             cb(err, url);
       
   123                         } else {
       
   124                             Y.Get._exec(mod, url, cb);
       
   125                         }
       
   126                     });
       
   127                     return;
       
   128                 }
       
   129             } catch (err) {
       
   130                 cb(err, url);
       
   131                 return;
       
   132             }
       
   133 
       
   134             Y.Get._exec(mod, url, cb);
       
   135         }
       
   136     };
       
   137 
       
   138 
       
   139     /**
       
   140     * Override for Get.script for loading local or remote YUI modules.
       
   141     * @method js
       
   142     * @param {Array|String} s The URL's to load into this context
       
   143     * @param {Object} options Transaction options
       
   144     */
       
   145     Y.Get.js = function(s, options) {
       
   146         var urls = Y.Array(s), url, i, l = urls.length, c= 0,
       
   147             check = function() {
       
   148                 if (c === l) {
       
   149                     pass(options);
       
   150                 }
       
   151             };
       
   152 
       
   153 
       
   154         /*jshint loopfunc: true */
       
   155         for (i=0; i<l; i++) {
       
   156             url = urls[i];
       
   157             if (Y.Lang.isObject(url)) {
       
   158                 url = url.url;
       
   159             }
       
   160 
       
   161             url = url.replace(/'/g, '%27');
       
   162             Y.log('URL: ' + url, 'info', 'get');
       
   163             Y.Get._include(url, function(err, url) {
       
   164                 if (!Y.config) {
       
   165                     Y.config = {
       
   166                         debug: true
       
   167                     };
       
   168                 }
       
   169                 if (options.onProgress) {
       
   170                     options.onProgress.call(options.context || Y, url);
       
   171                 }
       
   172                 Y.log('After Load: ' + url, 'info', 'get');
       
   173                 if (err) {
       
   174                     fail(options, err);
       
   175                     Y.log('----------------------------------------------------------', 'error', 'get');
       
   176                     Y.log(err, 'error', 'get');
       
   177                     Y.log('----------------------------------------------------------', 'error', 'get');
       
   178                 } else {
       
   179                     c++;
       
   180                     check();
       
   181                 }
       
   182             });
       
   183         }
       
   184         
       
   185         //Keeping Signature in the browser.
       
   186         return {
       
   187             execute: function() {}
       
   188         };
       
   189     };
       
   190 
       
   191     /**
       
   192     * Alias for `Y.Get.js`
       
   193     * @method script
       
   194     */
       
   195     Y.Get.script = Y.Get.js;
       
   196 
       
   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');
       
   200         pass(cb);
       
   201     };
       
   202 
       
   203 
       
   204 
       
   205 }, '@VERSION@');