[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

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


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