[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/yuilib/3.17.2/get-nodejs/ -> get-nodejs-debug.js (source)

   1  /*
   2  YUI 3.17.2 (build 9c3c78e)
   3  Copyright 2014 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 {Function} 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) {' +
  76              'return (function () {'+ data + '\n;return YUI;}).apply(global);' +
  77          '};', url);
  78  
  79          /*global YUI:true */
  80          YUI = mod.exports(YUI);
  81  
  82          mod.loaded = true;
  83  
  84          cb(null, url);
  85      };
  86  
  87      /**
  88      * Fetches the content from a remote URL or a file from disc and passes the content
  89      * off to `_exec` for parsing
  90      * @method _include
  91      * @private
  92      * @param {String} url The URL/File path to fetch the content from
  93      * @param {Function} cb The callback to fire once the content has been executed via `_exec`
  94      */
  95      Y.Get._include = function (url, cb) {
  96          var cfg,
  97              mod,
  98              self = this;
  99  
 100          if (url.match(/^https?:\/\//)) {
 101              cfg = {
 102                  url: url,
 103                  timeout: self.timeout
 104              };
 105              request(cfg, function (err, response, body) {
 106                  if (err) {
 107                      Y.log(err, 'error', 'get');
 108                      cb(err, url);
 109                  } else {
 110                      Y.Get._exec(body, url, cb);
 111                  }
 112              });
 113          } else {
 114              try {
 115                  // Try to resolve paths relative to the module that required yui.
 116                  url = Module._findPath(url, Module._resolveLookupPaths(url, module.parent.parent)[1]);
 117  
 118                  if (Y.config.useSync) {
 119                      //Needs to be in useSync
 120                      mod = fs.readFileSync(url,'utf8');
 121                  } else {
 122                      fs.readFile(url, 'utf8', function (err, mod) {
 123                          if (err) {
 124                              cb(err, url);
 125                          } else {
 126                              Y.Get._exec(mod, url, cb);
 127                          }
 128                      });
 129                      return;
 130                  }
 131              } catch (err) {
 132                  cb(err, url);
 133                  return;
 134              }
 135  
 136              Y.Get._exec(mod, url, cb);
 137          }
 138      };
 139  
 140  
 141      /**
 142      * Override for Get.script for loading local or remote YUI modules.
 143      * @method js
 144      * @param {Array|String} s The URL's to load into this context
 145      * @param {Object} options Transaction options
 146      */
 147      Y.Get.js = function(s, options) {
 148          var urls = Y.Array(s), url, i, l = urls.length, c= 0,
 149              check = function() {
 150                  if (c === l) {
 151                      pass(options);
 152                  }
 153              };
 154  
 155  
 156          /*jshint loopfunc: true */
 157          for (i=0; i<l; i++) {
 158              url = urls[i];
 159              if (Y.Lang.isObject(url)) {
 160                  url = url.url;
 161              }
 162  
 163              url = url.replace(/'/g, '%27');
 164              Y.log('URL: ' + url, 'info', 'get');
 165              Y.Get._include(url, function(err, url) {
 166                  if (!Y.config) {
 167                      Y.config = {
 168                          debug: true
 169                      };
 170                  }
 171                  if (options.onProgress) {
 172                      options.onProgress.call(options.context || Y, url);
 173                  }
 174                  Y.log('After Load: ' + url, 'info', 'get');
 175                  if (err) {
 176                      fail(options, err);
 177                      Y.log('----------------------------------------------------------', 'error', 'get');
 178                      Y.log(err, 'error', 'get');
 179                      Y.log('----------------------------------------------------------', 'error', 'get');
 180                  } else {
 181                      c++;
 182                      check();
 183                  }
 184              });
 185          }
 186  
 187          //Keeping Signature in the browser.
 188          return {
 189              execute: function() {}
 190          };
 191      };
 192  
 193      /**
 194      * Alias for `Y.Get.js`
 195      * @method script
 196      */
 197      Y.Get.script = Y.Get.js;
 198  
 199      //Place holder for SS Dom access
 200      Y.Get.css = function(s, cb) {
 201          Y.log('Y.Get.css is not supported, just reporting that it has loaded but not fetching.', 'warn', 'get');
 202          pass(cb);
 203      };
 204  
 205  
 206  
 207  }, '3.17.2');


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1