Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rls-debug.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('rls', function(Y) {
  9. /**
  10. * RLS (Remote Loader Service) Support
  11. * @module yui
  12. * @submodule rls
  13. * @class rls
  14. */
  15. Y.rls_handleTimeout = function(o) {
  16. Y.Get.abort(o.tId);
  17. o.purge();
  18. o.message = 'RLS request timed out, fetching loader';
  19. Y.rls_failure(o);
  20. };
  21. Y.rls_handleFailure = function(o) {
  22. o.message = 'RLS request failed, fetching loader';
  23. Y.rls_failure(o);
  24. };
  25. Y.rls_failure = function(o) {
  26. Y.log(o.message, 'warn', 'rls');
  27. YUI.Env.rls_disabled = true;
  28. Y.config.use_rls = false;
  29. if (o.data) {
  30. o.data.unshift('loader');
  31. Y._use(o.data, function(Y, response) {
  32. Y._notify(Y.rls_callback, response, o.data);
  33. //Call the RLS done method, so it can progress the queue
  34. Y.rls_advance();
  35. });
  36. }
  37. };
  38. /**
  39. * Checks the environment for local modules and deals with them before firing off an RLS request.
  40. * This needs to make sure that all dependencies are calculated before it can make an RLS request in
  41. * order to make sure all remote dependencies are evaluated and their requirements are met.
  42. * @method rls_locals
  43. * @private
  44. * @param {YUI} instance The YUI Instance we are working with.
  45. * @param {Array} argz The requested modules.
  46. * @param {Callback} cb The callback to be executed when we are done
  47. * @param {YUI} cb.instance The instance is passed back to the callback
  48. * @param {Array} cb.argz The modified list or modules needed to require
  49. */
  50. Y.rls_locals = function(instance, argz, cb) {
  51. if (YUI.Env.rls_disabled) {
  52. var data = {
  53. message: 'RLS is disabled, moving to loader',
  54. data: argz
  55. };
  56. Y.rls_failure(data);
  57. return;
  58. }
  59. if (instance.config.modules) {
  60. var files = [], asked = Y.Array.hash(argz),
  61. PATH = 'fullpath', f,
  62. mods = instance.config.modules;
  63. for (f in mods) {
  64. if (mods[f][PATH]) {
  65. if (asked[f]) {
  66. files.push(mods[f][PATH]);
  67. if (mods[f].requires) {
  68. Y.Array.each(mods[f].requires, function(f) {
  69. if (!YUI.Env.mods[f]) {
  70. if (mods[f]) {
  71. if (mods[f][PATH]) {
  72. files.push(mods[f][PATH]);
  73. argz.push(f);
  74. }
  75. }
  76. }
  77. });
  78. }
  79. }
  80. }
  81. }
  82. if (files.length) {
  83. Y.Get.script(files, {
  84. onEnd: function(o) {
  85. cb(instance, argz);
  86. },
  87. data: argz
  88. });
  89. } else {
  90. cb(instance, argz);
  91. }
  92. } else {
  93. cb(instance, argz);
  94. }
  95. };
  96. /**
  97. * Check the environment and the local config to determine if a module has already been registered.
  98. * @method rls_needs
  99. * @private
  100. * @param {String} mod The module to check
  101. * @param {YUI} instance The instance to check against.
  102. */
  103. Y.rls_needs = function(mod, instance) {
  104. var self = instance || this,
  105. config = self.config, i,
  106. m = YUI.Env.aliases[mod];
  107. if (m) {
  108. Y.log('We have an alias (' + mod + '), are all the deps available?', 'info', 'rls');
  109. for (i = 0; i < m.length; i++) {
  110. if (Y.rls_needs(m[i])) {
  111. Y.log('Needs (' + mod + ')', 'info', 'rls');
  112. return true;
  113. }
  114. }
  115. Y.log('Does not need (' + mod + ')', 'info', 'rls');
  116. return false;
  117. }
  118. if (!YUI.Env.mods[mod] && !(config.modules && config.modules[mod])) {
  119. Y.log('Needs (' + mod + ')', 'info', 'rls');
  120. return true;
  121. }
  122. Y.log('Does not need (' + mod + ')', 'info', 'rls');
  123. return false;
  124. };
  125. /**
  126. * Implentation for building the remote loader service url.
  127. * @method _rls
  128. * @private
  129. * @param {Array} what the requested modules.
  130. * @since 3.2.0
  131. * @return {string} the url for the remote loader service call, returns false if no modules are required to be fetched (they are in the ENV already).
  132. */
  133. Y._rls = function(what) {
  134. //what.push('intl');
  135. Y.log('Issuing a new RLS Request', 'info', 'rls');
  136. var config = Y.config,
  137. mods = config.modules,
  138. YArray = Y.Array,
  139. YObject = Y.Object,
  140. // the configuration
  141. rls = config.rls || {
  142. m: 1, // required in the template
  143. v: Y.version,
  144. gv: config.gallery,
  145. env: 1, // required in the template
  146. lang: config.lang,
  147. '2in3v': config['2in3'],
  148. '2v': config.yui2,
  149. filt: config.filter,
  150. filts: config.filters,
  151. ignore: config.ignore,
  152. tests: 1 // required in the template
  153. },
  154. // The rls base path
  155. rls_base = config.rls_base || 'http://loader.yahooapis.com/v1/load?',
  156. // the template
  157. rls_tmpl = config.rls_tmpl || function() {
  158. var s = [], param;
  159. for (param in rls) {
  160. if (param in rls && rls[param]) {
  161. s.push(param + '={' + param + '}');
  162. }
  163. }
  164. return s.join('&');
  165. }(),
  166. m = [], asked = {}, o, d, mod, a, j,
  167. w = [],
  168. i, len = what.length,
  169. url;
  170. //Explode our aliases..
  171. for (i = 0; i < len; i++) {
  172. a = YUI.Env.aliases[what[i]];
  173. if (a) {
  174. for (j = 0; j < a.length; j++) {
  175. w.push(a[j]);
  176. }
  177. } else {
  178. w.push(what[i]);
  179. }
  180. }
  181. what = w;
  182. len = what.length;
  183. for (i = 0; i < len; i++) {
  184. asked[what[i]] = 1;
  185. if (Y.rls_needs(what[i])) {
  186. Y.log('Did not find ' + what[i] + ' in YUI.Env.mods or config.modules adding to RLS', 'info', 'rls');
  187. m.push(what[i]);
  188. } else {
  189. Y.log(what[i] + ' was skipped from RLS', 'info', 'rls');
  190. }
  191. }
  192. if (mods) {
  193. for (i in mods) {
  194. if (asked[i] && mods[i].requires && !mods[i].noop) {
  195. len = mods[i].requires.length;
  196. for (o = 0; o < len; o++) {
  197. mod = mods[i].requires[o];
  198. if (Y.rls_needs(mod)) {
  199. m.push(mod);
  200. } else {
  201. d = YUI.Env.mods[mod] || mods[mod];
  202. if (d) {
  203. d = d.details || d;
  204. if (!d.noop) {
  205. if (d.requires) {
  206. YArray.each(d.requires, function(o) {
  207. if (Y.rls_needs(o)) {
  208. m.push(o);
  209. }
  210. });
  211. }
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }
  218. }
  219. YObject.each(YUI.Env.mods, function(i) {
  220. if (asked[i.name]) {
  221. if (i.details && i.details.requires) {
  222. if (!i.noop) {
  223. YArray.each(i.details.requires, function(o) {
  224. if (Y.rls_needs(o)) {
  225. m.push(o);
  226. }
  227. });
  228. }
  229. }
  230. }
  231. });
  232. function addIfNeeded(module) {
  233. if (Y.rls_needs(module)) {
  234. m.unshift(module);
  235. }
  236. }
  237. //Add in the debug modules
  238. if (rls.filt === 'debug') {
  239. YArray.each(['dump', 'yui-log'], addIfNeeded);
  240. }
  241. //If they have a groups config, add the loader-base module
  242. if (Y.config.groups) {
  243. addIfNeeded('loader-base');
  244. }
  245. m = YArray.dedupe(m);
  246. //Strip Duplicates
  247. m = YArray.dedupe(m);
  248. what = YArray.dedupe(what);
  249. if (!m.length) {
  250. //Return here if there no modules to load.
  251. Y.log('RLS request terminated, no modules in m', 'warn', 'rls');
  252. return false;
  253. }
  254. // update the request
  255. rls.m = m.sort(); // cache proxy optimization
  256. rls.env = [].concat(YObject.keys(YUI.Env.mods), YArray.dedupe(YUI._rls_skins)).sort();
  257. rls.tests = Y.Features.all('load', [Y]);
  258. url = Y.Lang.sub(rls_base + rls_tmpl, rls);
  259. config.rls = rls;
  260. config.rls_tmpl = rls_tmpl;
  261. YUI._rls_active = {
  262. asked: what,
  263. attach: m,
  264. inst: Y,
  265. url: url
  266. };
  267. return url;
  268. };
  269. /**
  270. *
  271. * @method rls_oncomplete
  272. * @param {Callback} cb The callback to execute when the RLS request is complete
  273. */
  274. Y.rls_oncomplete = function(cb) {
  275. YUI._rls_active.cb = cb;
  276. };
  277. Y.rls_advance = function() {
  278. var G_ENV = YUI.Env;
  279. G_ENV._rls_in_progress = false;
  280. if (G_ENV._rls_queue.size()) {
  281. G_ENV._rls_queue.next()();
  282. }
  283. };
  284. /**
  285. * Calls the callback registered with Y.rls_oncomplete when the RLS request (and it's dependency requests) is done.
  286. * @method rls_done
  287. * @param {Array} data The modules loaded
  288. */
  289. Y.rls_done = function(data) {
  290. Y.log('RLS Request complete', 'info', 'rls');
  291. data.success = true;
  292. YUI._rls_active.cb(data);
  293. };
  294. /**
  295. * Hash to hang on to the calling RLS instance so we can deal with the return from the server.
  296. * @property _rls_active
  297. * @private
  298. * @type Object
  299. * @static
  300. */
  301. if (!YUI._rls_active) {
  302. YUI._rls_active = {};
  303. }
  304. /**
  305. * An array of skins loaded via RLS to populate the ENV with when making future requests.
  306. * @property _rls_skins
  307. * @private
  308. * @type Array
  309. * @static
  310. */
  311. if (!YUI._rls_skins) {
  312. YUI._rls_skins = [];
  313. }
  314. /**
  315. *
  316. * @method $rls
  317. * @private
  318. * @static
  319. * @param {Object} req The data returned from the RLS server
  320. * @param {String} req.css Does this request need CSS? If so, load the same RLS url with &css=1 attached
  321. * @param {Array} req.module The sorted list of modules to attach to the page.
  322. */
  323. if (!YUI.$rls) {
  324. YUI.$rls = function(req) {
  325. var rls_active = YUI._rls_active,
  326. Y = rls_active.inst;
  327. if (Y) {
  328. Y.log('RLS request received, processing', 'info', 'rls');
  329. if (req.error) {
  330. if (!req.missing) {
  331. req.missing = [];
  332. }
  333. Y.rls_failure({
  334. message: req.error,
  335. data: [].concat(req.modules, req.missing)
  336. });
  337. }
  338. if (YUI.Env && YUI.Env.rls_disabled) {
  339. Y.log('RLS processing on this instance is disabled.', 'warn', 'rls');
  340. return;
  341. }
  342. if (req.css && Y.config.fetchCSS) {
  343. Y.Get.css(rls_active.url + '&css=1');
  344. }
  345. if (req.modules && !req.css) {
  346. if (req.modules.length) {
  347. var loadInt = Y.Array.some(req.modules, function(v) {
  348. return (v.indexOf('lang') === 0);
  349. });
  350. if (loadInt) {
  351. req.modules.unshift('intl');
  352. }
  353. }
  354. Y.Env.bootstrapped = true;
  355. Y.Array.each(req.modules, function(v) {
  356. if (v.indexOf('skin-') > -1) {
  357. Y.log('Found skin (' + v + ') caching module for future requests', 'info', 'rls');
  358. YUI._rls_skins.push(v);
  359. }
  360. });
  361. Y._attach([].concat(req.modules, rls_active.asked));
  362. var additional = req.missing;
  363. if (Y.config.groups) {
  364. if (!additional) {
  365. additional = [];
  366. }
  367. additional = [].concat(additional, rls_active.what);
  368. }
  369. if (additional && Y.Loader) {
  370. Y.log('Making extra Loader request', 'info', 'rls');
  371. var loader = new Y.Loader(rls_active.inst.config);
  372. loader.onEnd = Y.rls_done;
  373. loader.context = Y;
  374. loader.data = additional;
  375. loader.ignoreRegistered = false;
  376. loader.require(additional);
  377. loader.insert(null, (Y.config.fetchCSS) ? null : 'js');
  378. } else {
  379. Y.rls_done({ data: req.modules });
  380. }
  381. }
  382. }
  383. };
  384. }
  385. }, '3.4.0' ,{requires:['get','features']});