|
1 YUI.add('loader-base', function (Y, NAME) { |
|
2 |
|
3 /** |
|
4 * The YUI loader core |
|
5 * @module loader |
|
6 * @submodule loader-base |
|
7 */ |
|
8 |
|
9 (function() { |
|
10 var VERSION = Y.version, |
|
11 BUILD = '/build/', |
|
12 ROOT = VERSION + '/', |
|
13 CDN_BASE = Y.Env.base, |
|
14 GALLERY_VERSION = 'gallery-2014.02.20-23-55', |
|
15 TNT = '2in3', |
|
16 TNT_VERSION = '4', |
|
17 YUI2_VERSION = '2.9.0', |
|
18 COMBO_BASE = CDN_BASE + 'combo?', |
|
19 META = { |
|
20 version: VERSION, |
|
21 root: ROOT, |
|
22 base: Y.Env.base, |
|
23 comboBase: COMBO_BASE, |
|
24 skin: { |
|
25 defaultSkin: 'sam', |
|
26 base: 'assets/skins/', |
|
27 path: 'skin.css', |
|
28 after: [ |
|
29 'cssreset', |
|
30 'cssfonts', |
|
31 'cssgrids', |
|
32 'cssbase', |
|
33 'cssreset-context', |
|
34 'cssfonts-context' |
|
35 ] |
|
36 }, |
|
37 groups: {}, |
|
38 patterns: {} |
|
39 }, |
|
40 groups = META.groups, |
|
41 yui2Update = function(tnt, yui2, config) { |
|
42 var root = TNT + '.' + |
|
43 (tnt || TNT_VERSION) + '/' + |
|
44 (yui2 || YUI2_VERSION) + BUILD, |
|
45 base = (config && config.base) ? config.base : CDN_BASE, |
|
46 combo = (config && config.comboBase) ? config.comboBase : COMBO_BASE; |
|
47 |
|
48 groups.yui2.base = base + root; |
|
49 groups.yui2.root = root; |
|
50 groups.yui2.comboBase = combo; |
|
51 }, |
|
52 galleryUpdate = function(tag, config) { |
|
53 var root = (tag || GALLERY_VERSION) + BUILD, |
|
54 base = (config && config.base) ? config.base : CDN_BASE, |
|
55 combo = (config && config.comboBase) ? config.comboBase : COMBO_BASE; |
|
56 |
|
57 groups.gallery.base = base + root; |
|
58 groups.gallery.root = root; |
|
59 groups.gallery.comboBase = combo; |
|
60 }; |
|
61 |
|
62 |
|
63 groups[VERSION] = {}; |
|
64 |
|
65 groups.gallery = { |
|
66 ext: false, |
|
67 combine: true, |
|
68 comboBase: COMBO_BASE, |
|
69 update: galleryUpdate, |
|
70 patterns: { |
|
71 'gallery-': {}, |
|
72 'lang/gallery-': {}, |
|
73 'gallerycss-': { |
|
74 type: 'css' |
|
75 } |
|
76 } |
|
77 }; |
|
78 |
|
79 groups.yui2 = { |
|
80 combine: true, |
|
81 ext: false, |
|
82 comboBase: COMBO_BASE, |
|
83 update: yui2Update, |
|
84 patterns: { |
|
85 'yui2-': { |
|
86 configFn: function(me) { |
|
87 if (/-skin|reset|fonts|grids|base/.test(me.name)) { |
|
88 me.type = 'css'; |
|
89 me.path = me.path.replace(/\.js/, '.css'); |
|
90 // this makes skins in builds earlier than |
|
91 // 2.6.0 work as long as combine is false |
|
92 me.path = me.path.replace(/\/yui2-skin/, |
|
93 '/assets/skins/sam/yui2-skin'); |
|
94 } |
|
95 } |
|
96 } |
|
97 } |
|
98 }; |
|
99 |
|
100 galleryUpdate(); |
|
101 yui2Update(); |
|
102 |
|
103 if (YUI.Env[VERSION]) { |
|
104 Y.mix(META, YUI.Env[VERSION], false, [ |
|
105 'modules', |
|
106 'groups', |
|
107 'skin' |
|
108 ], 0, true); |
|
109 } |
|
110 |
|
111 YUI.Env[VERSION] = META; |
|
112 }()); |
|
113 /*jslint forin: true, maxlen: 350 */ |
|
114 |
|
115 /** |
|
116 * Loader dynamically loads script and css files. It includes the dependency |
|
117 * information for the version of the library in use, and will automatically pull in |
|
118 * dependencies for the modules requested. It can also load the |
|
119 * files from the Yahoo! CDN, and it can utilize the combo service provided on |
|
120 * this network to reduce the number of http connections required to download |
|
121 * YUI files. |
|
122 * |
|
123 * @module loader |
|
124 * @main loader |
|
125 * @submodule loader-base |
|
126 */ |
|
127 |
|
128 var NOT_FOUND = {}, |
|
129 NO_REQUIREMENTS = [], |
|
130 MAX_URL_LENGTH = 1024, |
|
131 GLOBAL_ENV = YUI.Env, |
|
132 GLOBAL_LOADED = GLOBAL_ENV._loaded, |
|
133 CSS = 'css', |
|
134 JS = 'js', |
|
135 INTL = 'intl', |
|
136 DEFAULT_SKIN = 'sam', |
|
137 VERSION = Y.version, |
|
138 ROOT_LANG = '', |
|
139 YObject = Y.Object, |
|
140 oeach = YObject.each, |
|
141 yArray = Y.Array, |
|
142 _queue = GLOBAL_ENV._loaderQueue, |
|
143 META = GLOBAL_ENV[VERSION], |
|
144 SKIN_PREFIX = 'skin-', |
|
145 L = Y.Lang, |
|
146 ON_PAGE = GLOBAL_ENV.mods, |
|
147 modulekey, |
|
148 _path = function(dir, file, type, nomin) { |
|
149 var path = dir + '/' + file; |
|
150 if (!nomin) { |
|
151 path += '-min'; |
|
152 } |
|
153 path += '.' + (type || CSS); |
|
154 |
|
155 return path; |
|
156 }; |
|
157 |
|
158 |
|
159 if (!YUI.Env._cssLoaded) { |
|
160 YUI.Env._cssLoaded = {}; |
|
161 } |
|
162 |
|
163 |
|
164 /** |
|
165 * The component metadata is stored in Y.Env.meta. |
|
166 * Part of the loader module. |
|
167 * @property meta |
|
168 * @for YUI |
|
169 */ |
|
170 Y.Env.meta = META; |
|
171 |
|
172 /** |
|
173 * Loader dynamically loads script and css files. It includes the dependency |
|
174 * info for the version of the library in use, and will automatically pull in |
|
175 * dependencies for the modules requested. It can load the |
|
176 * files from the Yahoo! CDN, and it can utilize the combo service provided on |
|
177 * this network to reduce the number of http connections required to download |
|
178 * YUI files. You can also specify an external, custom combo service to host |
|
179 * your modules as well. |
|
180 |
|
181 var Y = YUI(); |
|
182 var loader = new Y.Loader({ |
|
183 filter: 'debug', |
|
184 base: '../../', |
|
185 root: 'build/', |
|
186 combine: true, |
|
187 require: ['node', 'dd', 'console'] |
|
188 }); |
|
189 var out = loader.resolve(true); |
|
190 |
|
191 * @constructor |
|
192 * @class Loader |
|
193 * @param {Object} config an optional set of configuration options. |
|
194 * @param {String} config.base The base dir which to fetch this module from |
|
195 * @param {String} config.comboBase The Combo service base path. Ex: `http://yui.yahooapis.com/combo?` |
|
196 * @param {String} config.root The root path to prepend to module names for the combo service. Ex: `2.5.2/build/` |
|
197 * @param {String|Object} config.filter A filter to apply to result urls. <a href="#property_filter">See filter property</a> |
|
198 * @param {Object} config.filters Per-component filter specification. If specified for a given component, this overrides the filter config. |
|
199 * @param {Boolean} config.combine Use a combo service to reduce the number of http connections required to load your dependencies |
|
200 * @param {Boolean} [config.async=true] Fetch files in async |
|
201 * @param {Array} config.ignore: A list of modules that should never be dynamically loaded |
|
202 * @param {Array} config.force A list of modules that should always be loaded when required, even if already present on the page |
|
203 * @param {HTMLElement|String} config.insertBefore Node or id for a node that should be used as the insertion point for new nodes |
|
204 * @param {Object} config.jsAttributes Object literal containing attributes to add to script nodes |
|
205 * @param {Object} config.cssAttributes Object literal containing attributes to add to link nodes |
|
206 * @param {Number} config.timeout The number of milliseconds before a timeout occurs when dynamically loading nodes. If not set, there is no timeout |
|
207 * @param {Object} config.context Execution context for all callbacks |
|
208 * @param {Function} config.onSuccess Callback for the 'success' event |
|
209 * @param {Function} config.onFailure Callback for the 'failure' event |
|
210 * @param {Function} config.onCSS Callback for the 'CSSComplete' event. When loading YUI components with CSS the CSS is loaded first, then the script. This provides a moment you can tie into to improve the presentation of the page while the script is loading. |
|
211 * @param {Function} config.onTimeout Callback for the 'timeout' event |
|
212 * @param {Function} config.onProgress Callback executed each time a script or css file is loaded |
|
213 * @param {Object} config.modules A list of module definitions. See <a href="#method_addModule">Loader.addModule</a> for the supported module metadata |
|
214 * @param {Object} config.groups A list of group definitions. Each group can contain specific definitions for `base`, `comboBase`, `combine`, and accepts a list of `modules`. |
|
215 * @param {String} config.2in3 The version of the YUI 2 in 3 wrapper to use. The intrinsic support for YUI 2 modules in YUI 3 relies on versions of the YUI 2 components inside YUI 3 module wrappers. These wrappers change over time to accomodate the issues that arise from running YUI 2 in a YUI 3 sandbox. |
|
216 * @param {String} config.yui2 When using the 2in3 project, you can select the version of YUI 2 to use. Valid values are `2.2.2`, `2.3.1`, `2.4.1`, `2.5.2`, `2.6.0`, `2.7.0`, `2.8.0`, `2.8.1` and `2.9.0` [default] -- plus all versions of YUI 2 going forward. |
|
217 */ |
|
218 Y.Loader = function(o) { |
|
219 |
|
220 var self = this; |
|
221 |
|
222 //Catch no config passed. |
|
223 o = o || {}; |
|
224 |
|
225 modulekey = META.md5; |
|
226 |
|
227 /** |
|
228 * Internal callback to handle multiple internal insert() calls |
|
229 * so that css is inserted prior to js |
|
230 * @property _internalCallback |
|
231 * @private |
|
232 */ |
|
233 // self._internalCallback = null; |
|
234 |
|
235 /** |
|
236 * Callback that will be executed when the loader is finished |
|
237 * with an insert |
|
238 * @method onSuccess |
|
239 * @type function |
|
240 */ |
|
241 // self.onSuccess = null; |
|
242 |
|
243 /** |
|
244 * Callback that will be executed if there is a failure |
|
245 * @method onFailure |
|
246 * @type function |
|
247 */ |
|
248 // self.onFailure = null; |
|
249 |
|
250 /** |
|
251 * Callback for the 'CSSComplete' event. When loading YUI components |
|
252 * with CSS the CSS is loaded first, then the script. This provides |
|
253 * a moment you can tie into to improve the presentation of the page |
|
254 * while the script is loading. |
|
255 * @method onCSS |
|
256 * @type function |
|
257 */ |
|
258 // self.onCSS = null; |
|
259 |
|
260 /** |
|
261 * Callback executed each time a script or css file is loaded |
|
262 * @method onProgress |
|
263 * @type function |
|
264 */ |
|
265 // self.onProgress = null; |
|
266 |
|
267 /** |
|
268 * Callback that will be executed if a timeout occurs |
|
269 * @method onTimeout |
|
270 * @type function |
|
271 */ |
|
272 // self.onTimeout = null; |
|
273 |
|
274 /** |
|
275 * The execution context for all callbacks |
|
276 * @property context |
|
277 * @default {YUI} the YUI instance |
|
278 */ |
|
279 self.context = Y; |
|
280 |
|
281 /** |
|
282 * Data that is passed to all callbacks |
|
283 * @property data |
|
284 */ |
|
285 // self.data = null; |
|
286 |
|
287 /** |
|
288 * Node reference or id where new nodes should be inserted before |
|
289 * @property insertBefore |
|
290 * @type string|HTMLElement |
|
291 */ |
|
292 // self.insertBefore = null; |
|
293 |
|
294 /** |
|
295 * The charset attribute for inserted nodes |
|
296 * @property charset |
|
297 * @type string |
|
298 * @deprecated , use cssAttributes or jsAttributes. |
|
299 */ |
|
300 // self.charset = null; |
|
301 |
|
302 /** |
|
303 * An object literal containing attributes to add to link nodes |
|
304 * @property cssAttributes |
|
305 * @type object |
|
306 */ |
|
307 // self.cssAttributes = null; |
|
308 |
|
309 /** |
|
310 * An object literal containing attributes to add to script nodes |
|
311 * @property jsAttributes |
|
312 * @type object |
|
313 */ |
|
314 // self.jsAttributes = null; |
|
315 |
|
316 /** |
|
317 * The base directory. |
|
318 * @property base |
|
319 * @type string |
|
320 * @default http://yui.yahooapis.com/[YUI VERSION]/build/ |
|
321 */ |
|
322 self.base = Y.Env.meta.base + Y.Env.meta.root; |
|
323 |
|
324 /** |
|
325 * Base path for the combo service |
|
326 * @property comboBase |
|
327 * @type string |
|
328 * @default http://yui.yahooapis.com/combo? |
|
329 */ |
|
330 self.comboBase = Y.Env.meta.comboBase; |
|
331 |
|
332 /* |
|
333 * Base path for language packs. |
|
334 */ |
|
335 // self.langBase = Y.Env.meta.langBase; |
|
336 // self.lang = ""; |
|
337 |
|
338 /** |
|
339 * If configured, the loader will attempt to use the combo |
|
340 * service for YUI resources and configured external resources. |
|
341 * @property combine |
|
342 * @type boolean |
|
343 * @default true if a base dir isn't in the config |
|
344 */ |
|
345 self.combine = o.base && |
|
346 (o.base.indexOf(self.comboBase.substr(0, 20)) > -1); |
|
347 |
|
348 /** |
|
349 * The default seperator to use between files in a combo URL |
|
350 * @property comboSep |
|
351 * @type {String} |
|
352 * @default Ampersand |
|
353 */ |
|
354 self.comboSep = '&'; |
|
355 /** |
|
356 * Max url length for combo urls. The default is 1024. This is the URL |
|
357 * limit for the Yahoo! hosted combo servers. If consuming |
|
358 * a different combo service that has a different URL limit |
|
359 * it is possible to override this default by supplying |
|
360 * the maxURLLength config option. The config option will |
|
361 * only take effect if lower than the default. |
|
362 * |
|
363 * @property maxURLLength |
|
364 * @type int |
|
365 */ |
|
366 self.maxURLLength = MAX_URL_LENGTH; |
|
367 |
|
368 /** |
|
369 * Ignore modules registered on the YUI global |
|
370 * @property ignoreRegistered |
|
371 * @default false |
|
372 */ |
|
373 self.ignoreRegistered = o.ignoreRegistered; |
|
374 |
|
375 /** |
|
376 * Root path to prepend to module path for the combo |
|
377 * service |
|
378 * @property root |
|
379 * @type string |
|
380 * @default [YUI VERSION]/build/ |
|
381 */ |
|
382 self.root = Y.Env.meta.root; |
|
383 |
|
384 /** |
|
385 * Timeout value in milliseconds. If set, self value will be used by |
|
386 * the get utility. the timeout event will fire if |
|
387 * a timeout occurs. |
|
388 * @property timeout |
|
389 * @type int |
|
390 */ |
|
391 self.timeout = 0; |
|
392 |
|
393 /** |
|
394 * A list of modules that should not be loaded, even if |
|
395 * they turn up in the dependency tree |
|
396 * @property ignore |
|
397 * @type string[] |
|
398 */ |
|
399 // self.ignore = null; |
|
400 |
|
401 /** |
|
402 * A list of modules that should always be loaded, even |
|
403 * if they have already been inserted into the page. |
|
404 * @property force |
|
405 * @type string[] |
|
406 */ |
|
407 // self.force = null; |
|
408 |
|
409 self.forceMap = {}; |
|
410 |
|
411 /** |
|
412 * Should we allow rollups |
|
413 * @property allowRollup |
|
414 * @type boolean |
|
415 * @default false |
|
416 */ |
|
417 self.allowRollup = false; |
|
418 |
|
419 /** |
|
420 * A filter to apply to result urls. This filter will modify the default |
|
421 * path for all modules. The default path for the YUI library is the |
|
422 * minified version of the files (e.g., event-min.js). The filter property |
|
423 * can be a predefined filter or a custom filter. The valid predefined |
|
424 * filters are: |
|
425 * <dl> |
|
426 * <dt>DEBUG</dt> |
|
427 * <dd>Selects the debug versions of the library (e.g., event-debug.js). |
|
428 * This option will automatically include the Logger widget</dd> |
|
429 * <dt>RAW</dt> |
|
430 * <dd>Selects the non-minified version of the library (e.g., event.js). |
|
431 * </dd> |
|
432 * </dl> |
|
433 * You can also define a custom filter, which must be an object literal |
|
434 * containing a search expression and a replace string: |
|
435 * |
|
436 * myFilter: { |
|
437 * 'searchExp': "-min\\.js", |
|
438 * 'replaceStr': "-debug.js" |
|
439 * } |
|
440 * |
|
441 * @property filter |
|
442 * @type string| {searchExp: string, replaceStr: string} |
|
443 */ |
|
444 // self.filter = null; |
|
445 |
|
446 /** |
|
447 * per-component filter specification. If specified for a given |
|
448 * component, this overrides the filter config. |
|
449 * @property filters |
|
450 * @type object |
|
451 */ |
|
452 self.filters = {}; |
|
453 |
|
454 /** |
|
455 * The list of requested modules |
|
456 * @property required |
|
457 * @type {string: boolean} |
|
458 */ |
|
459 self.required = {}; |
|
460 |
|
461 /** |
|
462 * If a module name is predefined when requested, it is checked againsts |
|
463 * the patterns provided in this property. If there is a match, the |
|
464 * module is added with the default configuration. |
|
465 * |
|
466 * At the moment only supporting module prefixes, but anticipate |
|
467 * supporting at least regular expressions. |
|
468 * @property patterns |
|
469 * @type Object |
|
470 */ |
|
471 // self.patterns = Y.merge(Y.Env.meta.patterns); |
|
472 self.patterns = {}; |
|
473 |
|
474 /** |
|
475 * The library metadata |
|
476 * @property moduleInfo |
|
477 */ |
|
478 // self.moduleInfo = Y.merge(Y.Env.meta.moduleInfo); |
|
479 self.moduleInfo = {}; |
|
480 |
|
481 self.groups = Y.merge(Y.Env.meta.groups); |
|
482 |
|
483 /** |
|
484 * Provides the information used to skin the skinnable components. |
|
485 * The following skin definition would result in 'skin1' and 'skin2' |
|
486 * being loaded for calendar (if calendar was requested), and |
|
487 * 'sam' for all other skinnable components: |
|
488 * |
|
489 * skin: { |
|
490 * // The default skin, which is automatically applied if not |
|
491 * // overriden by a component-specific skin definition. |
|
492 * // Change this in to apply a different skin globally |
|
493 * defaultSkin: 'sam', |
|
494 * |
|
495 * // This is combined with the loader base property to get |
|
496 * // the default root directory for a skin. ex: |
|
497 * // http://yui.yahooapis.com/2.3.0/build/assets/skins/sam/ |
|
498 * base: 'assets/skins/', |
|
499 * |
|
500 * // Any component-specific overrides can be specified here, |
|
501 * // making it possible to load different skins for different |
|
502 * // components. It is possible to load more than one skin |
|
503 * // for a given component as well. |
|
504 * overrides: { |
|
505 * calendar: ['skin1', 'skin2'] |
|
506 * } |
|
507 * } |
|
508 * @property skin |
|
509 * @type {Object} |
|
510 */ |
|
511 self.skin = Y.merge(Y.Env.meta.skin); |
|
512 |
|
513 /* |
|
514 * Map of conditional modules |
|
515 * @since 3.2.0 |
|
516 */ |
|
517 self.conditions = {}; |
|
518 |
|
519 // map of modules with a hash of modules that meet the requirement |
|
520 // self.provides = {}; |
|
521 |
|
522 self.config = o; |
|
523 self._internal = true; |
|
524 |
|
525 self._populateCache(); |
|
526 |
|
527 /** |
|
528 * Set when beginning to compute the dependency tree. |
|
529 * Composed of what YUI reports to be loaded combined |
|
530 * with what has been loaded by any instance on the page |
|
531 * with the version number specified in the metadata. |
|
532 * @property loaded |
|
533 * @type {string: boolean} |
|
534 */ |
|
535 self.loaded = GLOBAL_LOADED[VERSION]; |
|
536 |
|
537 |
|
538 /** |
|
539 * Should Loader fetch scripts in `async`, defaults to `true` |
|
540 * @property async |
|
541 */ |
|
542 |
|
543 self.async = true; |
|
544 |
|
545 self._inspectPage(); |
|
546 |
|
547 self._internal = false; |
|
548 |
|
549 self._config(o); |
|
550 |
|
551 self.forceMap = (self.force) ? Y.Array.hash(self.force) : {}; |
|
552 |
|
553 self.testresults = null; |
|
554 |
|
555 if (Y.config.tests) { |
|
556 self.testresults = Y.config.tests; |
|
557 } |
|
558 |
|
559 /** |
|
560 * List of rollup files found in the library metadata |
|
561 * @property rollups |
|
562 */ |
|
563 // self.rollups = null; |
|
564 |
|
565 /** |
|
566 * Whether or not to load optional dependencies for |
|
567 * the requested modules |
|
568 * @property loadOptional |
|
569 * @type boolean |
|
570 * @default false |
|
571 */ |
|
572 // self.loadOptional = false; |
|
573 |
|
574 /** |
|
575 * All of the derived dependencies in sorted order, which |
|
576 * will be populated when either calculate() or insert() |
|
577 * is called |
|
578 * @property sorted |
|
579 * @type string[] |
|
580 */ |
|
581 self.sorted = []; |
|
582 |
|
583 /* |
|
584 * A list of modules to attach to the YUI instance when complete. |
|
585 * If not supplied, the sorted list of dependencies are applied. |
|
586 * @property attaching |
|
587 */ |
|
588 // self.attaching = null; |
|
589 |
|
590 /** |
|
591 * Flag to indicate the dependency tree needs to be recomputed |
|
592 * if insert is called again. |
|
593 * @property dirty |
|
594 * @type boolean |
|
595 * @default true |
|
596 */ |
|
597 self.dirty = true; |
|
598 |
|
599 /** |
|
600 * List of modules inserted by the utility |
|
601 * @property inserted |
|
602 * @type {string: boolean} |
|
603 */ |
|
604 self.inserted = {}; |
|
605 |
|
606 /** |
|
607 * List of skipped modules during insert() because the module |
|
608 * was not defined |
|
609 * @property skipped |
|
610 */ |
|
611 self.skipped = {}; |
|
612 |
|
613 // Y.on('yui:load', self.loadNext, self); |
|
614 |
|
615 self.tested = {}; |
|
616 |
|
617 /* |
|
618 * Cached sorted calculate results |
|
619 * @property results |
|
620 * @since 3.2.0 |
|
621 */ |
|
622 //self.results = {}; |
|
623 |
|
624 if (self.ignoreRegistered) { |
|
625 //Clear inpage already processed modules. |
|
626 self._resetModules(); |
|
627 } |
|
628 |
|
629 }; |
|
630 |
|
631 Y.Loader.prototype = { |
|
632 /** |
|
633 * Checks the cache for modules and conditions, if they do not exist |
|
634 * process the default metadata and populate the local moduleInfo hash. |
|
635 * @method _populateCache |
|
636 * @private |
|
637 */ |
|
638 _populateCache: function() { |
|
639 var self = this, |
|
640 defaults = META.modules, |
|
641 cache = GLOBAL_ENV._renderedMods, |
|
642 i; |
|
643 |
|
644 if (cache && !self.ignoreRegistered) { |
|
645 for (i in cache) { |
|
646 if (cache.hasOwnProperty(i)) { |
|
647 self.moduleInfo[i] = Y.merge(cache[i]); |
|
648 } |
|
649 } |
|
650 |
|
651 cache = GLOBAL_ENV._conditions; |
|
652 for (i in cache) { |
|
653 if (cache.hasOwnProperty(i)) { |
|
654 self.conditions[i] = Y.merge(cache[i]); |
|
655 } |
|
656 } |
|
657 |
|
658 } else { |
|
659 for (i in defaults) { |
|
660 if (defaults.hasOwnProperty(i)) { |
|
661 self.addModule(defaults[i], i); |
|
662 } |
|
663 } |
|
664 } |
|
665 |
|
666 }, |
|
667 /** |
|
668 * Reset modules in the module cache to a pre-processed state so additional |
|
669 * computations with a different skin or language will work as expected. |
|
670 * @method _resetModules |
|
671 * @private |
|
672 */ |
|
673 _resetModules: function() { |
|
674 var self = this, i, o, |
|
675 mod, name, details; |
|
676 for (i in self.moduleInfo) { |
|
677 if (self.moduleInfo.hasOwnProperty(i)) { |
|
678 mod = self.moduleInfo[i]; |
|
679 name = mod.name; |
|
680 details = (YUI.Env.mods[name] ? YUI.Env.mods[name].details : null); |
|
681 |
|
682 if (details) { |
|
683 self.moduleInfo[name]._reset = true; |
|
684 self.moduleInfo[name].requires = details.requires || []; |
|
685 self.moduleInfo[name].optional = details.optional || []; |
|
686 self.moduleInfo[name].supersedes = details.supercedes || []; |
|
687 } |
|
688 |
|
689 if (mod.defaults) { |
|
690 for (o in mod.defaults) { |
|
691 if (mod.defaults.hasOwnProperty(o)) { |
|
692 if (mod[o]) { |
|
693 mod[o] = mod.defaults[o]; |
|
694 } |
|
695 } |
|
696 } |
|
697 } |
|
698 delete mod.langCache; |
|
699 delete mod.skinCache; |
|
700 if (mod.skinnable) { |
|
701 self._addSkin(self.skin.defaultSkin, mod.name); |
|
702 } |
|
703 } |
|
704 } |
|
705 }, |
|
706 /** |
|
707 Regex that matches a CSS URL. Used to guess the file type when it's not |
|
708 specified. |
|
709 |
|
710 @property REGEX_CSS |
|
711 @type RegExp |
|
712 @final |
|
713 @protected |
|
714 @since 3.5.0 |
|
715 **/ |
|
716 REGEX_CSS: /\.css(?:[?;].*)?$/i, |
|
717 |
|
718 /** |
|
719 * Default filters for raw and debug |
|
720 * @property FILTER_DEFS |
|
721 * @type Object |
|
722 * @final |
|
723 * @protected |
|
724 */ |
|
725 FILTER_DEFS: { |
|
726 RAW: { |
|
727 'searchExp': '-min\\.js', |
|
728 'replaceStr': '.js' |
|
729 }, |
|
730 DEBUG: { |
|
731 'searchExp': '-min\\.js', |
|
732 'replaceStr': '-debug.js' |
|
733 }, |
|
734 COVERAGE: { |
|
735 'searchExp': '-min\\.js', |
|
736 'replaceStr': '-coverage.js' |
|
737 } |
|
738 }, |
|
739 /* |
|
740 * Check the pages meta-data and cache the result. |
|
741 * @method _inspectPage |
|
742 * @private |
|
743 */ |
|
744 _inspectPage: function() { |
|
745 var self = this, v, m, req, mr, i; |
|
746 |
|
747 //Inspect the page for CSS only modules and mark them as loaded. |
|
748 for (i in self.moduleInfo) { |
|
749 if (self.moduleInfo.hasOwnProperty(i)) { |
|
750 v = self.moduleInfo[i]; |
|
751 if (v.type && v.type === CSS) { |
|
752 if (self.isCSSLoaded(v.name)) { |
|
753 Y.log('Found CSS module on page: ' + v.name, 'info', 'loader'); |
|
754 self.loaded[i] = true; |
|
755 } |
|
756 } |
|
757 } |
|
758 } |
|
759 for (i in ON_PAGE) { |
|
760 if (ON_PAGE.hasOwnProperty(i)) { |
|
761 v = ON_PAGE[i]; |
|
762 if (v.details) { |
|
763 m = self.moduleInfo[v.name]; |
|
764 req = v.details.requires; |
|
765 mr = m && m.requires; |
|
766 |
|
767 if (m) { |
|
768 if (!m._inspected && req && mr.length !== req.length) { |
|
769 // console.log('deleting ' + m.name); |
|
770 delete m.expanded; |
|
771 } |
|
772 } else { |
|
773 m = self.addModule(v.details, i); |
|
774 } |
|
775 m._inspected = true; |
|
776 } |
|
777 } |
|
778 } |
|
779 }, |
|
780 /* |
|
781 * returns true if b is not loaded, and is required directly or by means of modules it supersedes. |
|
782 * @private |
|
783 * @method _requires |
|
784 * @param {String} mod1 The first module to compare |
|
785 * @param {String} mod2 The second module to compare |
|
786 */ |
|
787 _requires: function(mod1, mod2) { |
|
788 |
|
789 var i, rm, after_map, s, |
|
790 info = this.moduleInfo, |
|
791 m = info[mod1], |
|
792 other = info[mod2]; |
|
793 |
|
794 if (!m || !other) { |
|
795 return false; |
|
796 } |
|
797 |
|
798 rm = m.expanded_map; |
|
799 after_map = m.after_map; |
|
800 |
|
801 // check if this module should be sorted after the other |
|
802 // do this first to short circut circular deps |
|
803 if (after_map && (mod2 in after_map)) { |
|
804 return true; |
|
805 } |
|
806 |
|
807 after_map = other.after_map; |
|
808 |
|
809 // and vis-versa |
|
810 if (after_map && (mod1 in after_map)) { |
|
811 return false; |
|
812 } |
|
813 |
|
814 // check if this module requires one the other supersedes |
|
815 s = info[mod2] && info[mod2].supersedes; |
|
816 if (s) { |
|
817 for (i = 0; i < s.length; i++) { |
|
818 if (this._requires(mod1, s[i])) { |
|
819 return true; |
|
820 } |
|
821 } |
|
822 } |
|
823 |
|
824 s = info[mod1] && info[mod1].supersedes; |
|
825 if (s) { |
|
826 for (i = 0; i < s.length; i++) { |
|
827 if (this._requires(mod2, s[i])) { |
|
828 return false; |
|
829 } |
|
830 } |
|
831 } |
|
832 |
|
833 // check if this module requires the other directly |
|
834 // if (r && yArray.indexOf(r, mod2) > -1) { |
|
835 if (rm && (mod2 in rm)) { |
|
836 return true; |
|
837 } |
|
838 |
|
839 // external css files should be sorted below yui css |
|
840 if (m.ext && m.type === CSS && !other.ext && other.type === CSS) { |
|
841 return true; |
|
842 } |
|
843 |
|
844 return false; |
|
845 }, |
|
846 /** |
|
847 * Apply a new config to the Loader instance |
|
848 * @method _config |
|
849 * @private |
|
850 * @param {Object} o The new configuration |
|
851 */ |
|
852 _config: function(o) { |
|
853 var i, j, val, a, f, group, groupName, self = this, |
|
854 mods = [], mod; |
|
855 // apply config values |
|
856 if (o) { |
|
857 for (i in o) { |
|
858 if (o.hasOwnProperty(i)) { |
|
859 val = o[i]; |
|
860 //TODO This should be a case |
|
861 if (i === 'require') { |
|
862 self.require(val); |
|
863 } else if (i === 'skin') { |
|
864 //If the config.skin is a string, format to the expected object |
|
865 if (typeof val === 'string') { |
|
866 self.skin.defaultSkin = o.skin; |
|
867 val = { |
|
868 defaultSkin: val |
|
869 }; |
|
870 } |
|
871 |
|
872 Y.mix(self.skin, val, true); |
|
873 } else if (i === 'groups') { |
|
874 for (j in val) { |
|
875 if (val.hasOwnProperty(j)) { |
|
876 // Y.log('group: ' + j); |
|
877 groupName = j; |
|
878 group = val[j]; |
|
879 self.addGroup(group, groupName); |
|
880 if (group.aliases) { |
|
881 for (a in group.aliases) { |
|
882 if (group.aliases.hasOwnProperty(a)) { |
|
883 self.addAlias(group.aliases[a], a); |
|
884 } |
|
885 } |
|
886 } |
|
887 } |
|
888 } |
|
889 |
|
890 } else if (i === 'modules') { |
|
891 // add a hash of module definitions |
|
892 for (j in val) { |
|
893 if (val.hasOwnProperty(j)) { |
|
894 self.addModule(val[j], j); |
|
895 } |
|
896 } |
|
897 } else if (i === 'aliases') { |
|
898 for (j in val) { |
|
899 if (val.hasOwnProperty(j)) { |
|
900 self.addAlias(val[j], j); |
|
901 } |
|
902 } |
|
903 } else if (i === 'gallery') { |
|
904 if (this.groups.gallery.update) { |
|
905 this.groups.gallery.update(val, o); |
|
906 } |
|
907 } else if (i === 'yui2' || i === '2in3') { |
|
908 if (this.groups.yui2.update) { |
|
909 this.groups.yui2.update(o['2in3'], o.yui2, o); |
|
910 } |
|
911 } else { |
|
912 self[i] = val; |
|
913 } |
|
914 } |
|
915 } |
|
916 } |
|
917 |
|
918 // fix filter |
|
919 f = self.filter; |
|
920 |
|
921 if (L.isString(f)) { |
|
922 f = f.toUpperCase(); |
|
923 self.filterName = f; |
|
924 self.filter = self.FILTER_DEFS[f]; |
|
925 if (f === 'DEBUG') { |
|
926 self.require('yui-log', 'dump'); |
|
927 } |
|
928 } |
|
929 |
|
930 if (self.filterName && self.coverage) { |
|
931 if (self.filterName === 'COVERAGE' && L.isArray(self.coverage) && self.coverage.length) { |
|
932 for (i = 0; i < self.coverage.length; i++) { |
|
933 mod = self.coverage[i]; |
|
934 if (self.moduleInfo[mod] && self.moduleInfo[mod].use) { |
|
935 mods = [].concat(mods, self.moduleInfo[mod].use); |
|
936 } else { |
|
937 mods.push(mod); |
|
938 } |
|
939 } |
|
940 self.filters = self.filters || {}; |
|
941 Y.Array.each(mods, function(mod) { |
|
942 self.filters[mod] = self.FILTER_DEFS.COVERAGE; |
|
943 }); |
|
944 self.filterName = 'RAW'; |
|
945 self.filter = self.FILTER_DEFS[self.filterName]; |
|
946 } |
|
947 } |
|
948 |
|
949 }, |
|
950 |
|
951 /** |
|
952 * Returns the skin module name for the specified skin name. If a |
|
953 * module name is supplied, the returned skin module name is |
|
954 * specific to the module passed in. |
|
955 * @method formatSkin |
|
956 * @param {string} skin the name of the skin. |
|
957 * @param {string} mod optional: the name of a module to skin. |
|
958 * @return {string} the full skin module name. |
|
959 */ |
|
960 formatSkin: function(skin, mod) { |
|
961 var s = SKIN_PREFIX + skin; |
|
962 if (mod) { |
|
963 s = s + '-' + mod; |
|
964 } |
|
965 |
|
966 return s; |
|
967 }, |
|
968 |
|
969 /** |
|
970 * Adds the skin def to the module info |
|
971 * @method _addSkin |
|
972 * @param {string} skin the name of the skin. |
|
973 * @param {string} mod the name of the module. |
|
974 * @param {string} parent parent module if this is a skin of a |
|
975 * submodule or plugin. |
|
976 * @return {string} the module name for the skin. |
|
977 * @private |
|
978 */ |
|
979 _addSkin: function(skin, mod, parent) { |
|
980 var mdef, pkg, name, nmod, |
|
981 info = this.moduleInfo, |
|
982 sinf = this.skin, |
|
983 ext = info[mod] && info[mod].ext; |
|
984 |
|
985 // Add a module definition for the module-specific skin css |
|
986 if (mod) { |
|
987 name = this.formatSkin(skin, mod); |
|
988 if (!info[name]) { |
|
989 mdef = info[mod]; |
|
990 pkg = mdef.pkg || mod; |
|
991 nmod = { |
|
992 skin: true, |
|
993 name: name, |
|
994 group: mdef.group, |
|
995 type: 'css', |
|
996 after: sinf.after, |
|
997 path: (parent || pkg) + '/' + sinf.base + skin + |
|
998 '/' + mod + '.css', |
|
999 ext: ext |
|
1000 }; |
|
1001 if (mdef.base) { |
|
1002 nmod.base = mdef.base; |
|
1003 } |
|
1004 if (mdef.configFn) { |
|
1005 nmod.configFn = mdef.configFn; |
|
1006 } |
|
1007 this.addModule(nmod, name); |
|
1008 |
|
1009 Y.log('Adding skin (' + name + '), ' + parent + ', ' + pkg + ', ' + info[name].path, 'info', 'loader'); |
|
1010 } |
|
1011 } |
|
1012 |
|
1013 return name; |
|
1014 }, |
|
1015 /** |
|
1016 * Adds an alias module to the system |
|
1017 * @method addAlias |
|
1018 * @param {Array} use An array of modules that makes up this alias |
|
1019 * @param {String} name The name of the alias |
|
1020 * @example |
|
1021 * var loader = new Y.Loader({}); |
|
1022 * loader.addAlias([ 'node', 'yql' ], 'davglass'); |
|
1023 * loader.require(['davglass']); |
|
1024 * var out = loader.resolve(true); |
|
1025 * |
|
1026 * //out.js will contain Node and YQL modules |
|
1027 */ |
|
1028 addAlias: function(use, name) { |
|
1029 YUI.Env.aliases[name] = use; |
|
1030 this.addModule({ |
|
1031 name: name, |
|
1032 use: use |
|
1033 }); |
|
1034 }, |
|
1035 /** |
|
1036 * Add a new module group |
|
1037 * @method addGroup |
|
1038 * @param {Object} config An object containing the group configuration data |
|
1039 * @param {String} config.name required, the group name |
|
1040 * @param {String} config.base The base directory for this module group |
|
1041 * @param {String} config.root The root path to add to each combo resource path |
|
1042 * @param {Boolean} config.combine Should the request be combined |
|
1043 * @param {String} config.comboBase Combo service base path |
|
1044 * @param {Object} config.modules The group of modules |
|
1045 * @param {String} name the group name. |
|
1046 * @example |
|
1047 * var loader = new Y.Loader({}); |
|
1048 * loader.addGroup({ |
|
1049 * name: 'davglass', |
|
1050 * combine: true, |
|
1051 * comboBase: '/combo?', |
|
1052 * root: '', |
|
1053 * modules: { |
|
1054 * //Module List here |
|
1055 * } |
|
1056 * }, 'davglass'); |
|
1057 */ |
|
1058 addGroup: function(o, name) { |
|
1059 var mods = o.modules, |
|
1060 self = this, i, v; |
|
1061 |
|
1062 name = name || o.name; |
|
1063 o.name = name; |
|
1064 self.groups[name] = o; |
|
1065 |
|
1066 if (o.patterns) { |
|
1067 for (i in o.patterns) { |
|
1068 if (o.patterns.hasOwnProperty(i)) { |
|
1069 o.patterns[i].group = name; |
|
1070 self.patterns[i] = o.patterns[i]; |
|
1071 } |
|
1072 } |
|
1073 } |
|
1074 |
|
1075 if (mods) { |
|
1076 for (i in mods) { |
|
1077 if (mods.hasOwnProperty(i)) { |
|
1078 v = mods[i]; |
|
1079 if (typeof v === 'string') { |
|
1080 v = { name: i, fullpath: v }; |
|
1081 } |
|
1082 v.group = name; |
|
1083 self.addModule(v, i); |
|
1084 } |
|
1085 } |
|
1086 } |
|
1087 }, |
|
1088 |
|
1089 /** |
|
1090 * Add a new module to the component metadata. |
|
1091 * @method addModule |
|
1092 * @param {Object} config An object containing the module data. |
|
1093 * @param {String} config.name Required, the component name |
|
1094 * @param {String} config.type Required, the component type (js or css) |
|
1095 * @param {String} config.path Required, the path to the script from `base` |
|
1096 * @param {Array} config.requires Array of modules required by this component |
|
1097 * @param {Array} [config.optional] Array of optional modules for this component |
|
1098 * @param {Array} [config.supersedes] Array of the modules this component replaces |
|
1099 * @param {Array} [config.after] Array of modules the components which, if present, should be sorted above this one |
|
1100 * @param {Object} [config.after_map] Faster alternative to 'after' -- supply a hash instead of an array |
|
1101 * @param {Number} [config.rollup] The number of superseded modules required for automatic rollup |
|
1102 * @param {String} [config.fullpath] If `fullpath` is specified, this is used instead of the configured `base + path` |
|
1103 * @param {Boolean} [config.skinnable] Flag to determine if skin assets should automatically be pulled in |
|
1104 * @param {Object} [config.submodules] Hash of submodules |
|
1105 * @param {String} [config.group] The group the module belongs to -- this is set automatically when it is added as part of a group configuration. |
|
1106 * @param {Array} [config.lang] Array of BCP 47 language tags of languages for which this module has localized resource bundles, e.g., `["en-GB", "zh-Hans-CN"]` |
|
1107 * @param {Object} [config.condition] Specifies that the module should be loaded automatically if a condition is met. This is an object with up to four fields: |
|
1108 * @param {String} [config.condition.trigger] The name of a module that can trigger the auto-load |
|
1109 * @param {Function} [config.condition.test] A function that returns true when the module is to be loaded. |
|
1110 * @param {String} [config.condition.ua] The UA name of <a href="UA.html">Y.UA</a> object that returns true when the module is to be loaded. e.g., `"ie"`, `"nodejs"`. |
|
1111 * @param {String} [config.condition.when] Specifies the load order of the conditional module |
|
1112 * with regard to the position of the trigger module. |
|
1113 * This should be one of three values: `before`, `after`, or `instead`. The default is `after`. |
|
1114 * @param {Object} [config.testresults] A hash of test results from `Y.Features.all()` |
|
1115 * @param {Function} [config.configFn] A function to exectute when configuring this module |
|
1116 * @param {Object} config.configFn.mod The module config, modifying this object will modify it's config. Returning false will delete the module's config. |
|
1117 * @param {String} [name] The module name, required if not in the module data. |
|
1118 * @return {Object} the module definition or null if the object passed in did not provide all required attributes. |
|
1119 */ |
|
1120 addModule: function(o, name) { |
|
1121 name = name || o.name; |
|
1122 |
|
1123 if (typeof o === 'string') { |
|
1124 o = { name: name, fullpath: o }; |
|
1125 } |
|
1126 |
|
1127 |
|
1128 var subs, i, l, t, sup, s, smod, plugins, plug, |
|
1129 j, langs, packName, supName, flatSup, flatLang, lang, ret, |
|
1130 overrides, skinname, when, g, p, |
|
1131 conditions = this.conditions, trigger; |
|
1132 |
|
1133 //Only merge this data if the temp flag is set |
|
1134 //from an earlier pass from a pattern or else |
|
1135 //an override module (YUI_config) can not be used to |
|
1136 //replace a default module. |
|
1137 if (this.moduleInfo[name] && this.moduleInfo[name].temp) { |
|
1138 //This catches temp modules loaded via a pattern |
|
1139 // The module will be added twice, once from the pattern and |
|
1140 // Once from the actual add call, this ensures that properties |
|
1141 // that were added to the module the first time around (group: gallery) |
|
1142 // are also added the second time around too. |
|
1143 o = Y.merge(this.moduleInfo[name], o); |
|
1144 } |
|
1145 |
|
1146 o.name = name; |
|
1147 |
|
1148 if (!o || !o.name) { |
|
1149 return null; |
|
1150 } |
|
1151 |
|
1152 if (!o.type) { |
|
1153 //Always assume it's javascript unless the CSS pattern is matched. |
|
1154 o.type = JS; |
|
1155 p = o.path || o.fullpath; |
|
1156 if (p && this.REGEX_CSS.test(p)) { |
|
1157 Y.log('Auto determined module type as CSS', 'warn', 'loader'); |
|
1158 o.type = CSS; |
|
1159 } |
|
1160 } |
|
1161 |
|
1162 if (!o.path && !o.fullpath) { |
|
1163 o.path = _path(name, name, o.type); |
|
1164 } |
|
1165 o.supersedes = o.supersedes || o.use; |
|
1166 |
|
1167 o.ext = ('ext' in o) ? o.ext : (this._internal) ? false : true; |
|
1168 |
|
1169 // Handle submodule logic |
|
1170 subs = o.submodules; |
|
1171 |
|
1172 this.moduleInfo[name] = o; |
|
1173 |
|
1174 o.requires = o.requires || []; |
|
1175 |
|
1176 /* |
|
1177 Only allowing the cascade of requires information, since |
|
1178 optional and supersedes are far more fine grained than |
|
1179 a blanket requires is. |
|
1180 */ |
|
1181 if (this.requires) { |
|
1182 for (i = 0; i < this.requires.length; i++) { |
|
1183 o.requires.push(this.requires[i]); |
|
1184 } |
|
1185 } |
|
1186 if (o.group && this.groups && this.groups[o.group]) { |
|
1187 g = this.groups[o.group]; |
|
1188 if (g.requires) { |
|
1189 for (i = 0; i < g.requires.length; i++) { |
|
1190 o.requires.push(g.requires[i]); |
|
1191 } |
|
1192 } |
|
1193 } |
|
1194 |
|
1195 |
|
1196 if (!o.defaults) { |
|
1197 o.defaults = { |
|
1198 requires: o.requires ? [].concat(o.requires) : null, |
|
1199 supersedes: o.supersedes ? [].concat(o.supersedes) : null, |
|
1200 optional: o.optional ? [].concat(o.optional) : null |
|
1201 }; |
|
1202 } |
|
1203 |
|
1204 if (o.skinnable && o.ext && o.temp) { |
|
1205 skinname = this._addSkin(this.skin.defaultSkin, name); |
|
1206 o.requires.unshift(skinname); |
|
1207 } |
|
1208 |
|
1209 if (o.requires.length) { |
|
1210 o.requires = this.filterRequires(o.requires) || []; |
|
1211 } |
|
1212 |
|
1213 if (!o.langPack && o.lang) { |
|
1214 langs = yArray(o.lang); |
|
1215 for (j = 0; j < langs.length; j++) { |
|
1216 lang = langs[j]; |
|
1217 packName = this.getLangPackName(lang, name); |
|
1218 smod = this.moduleInfo[packName]; |
|
1219 if (!smod) { |
|
1220 smod = this._addLangPack(lang, o, packName); |
|
1221 } |
|
1222 } |
|
1223 } |
|
1224 |
|
1225 |
|
1226 if (subs) { |
|
1227 sup = o.supersedes || []; |
|
1228 l = 0; |
|
1229 |
|
1230 for (i in subs) { |
|
1231 if (subs.hasOwnProperty(i)) { |
|
1232 s = subs[i]; |
|
1233 |
|
1234 s.path = s.path || _path(name, i, o.type); |
|
1235 s.pkg = name; |
|
1236 s.group = o.group; |
|
1237 |
|
1238 if (s.supersedes) { |
|
1239 sup = sup.concat(s.supersedes); |
|
1240 } |
|
1241 |
|
1242 smod = this.addModule(s, i); |
|
1243 sup.push(i); |
|
1244 |
|
1245 if (smod.skinnable) { |
|
1246 o.skinnable = true; |
|
1247 overrides = this.skin.overrides; |
|
1248 if (overrides && overrides[i]) { |
|
1249 for (j = 0; j < overrides[i].length; j++) { |
|
1250 skinname = this._addSkin(overrides[i][j], |
|
1251 i, name); |
|
1252 sup.push(skinname); |
|
1253 } |
|
1254 } |
|
1255 skinname = this._addSkin(this.skin.defaultSkin, |
|
1256 i, name); |
|
1257 sup.push(skinname); |
|
1258 } |
|
1259 |
|
1260 // looks like we are expected to work out the metadata |
|
1261 // for the parent module language packs from what is |
|
1262 // specified in the child modules. |
|
1263 if (s.lang && s.lang.length) { |
|
1264 |
|
1265 langs = yArray(s.lang); |
|
1266 for (j = 0; j < langs.length; j++) { |
|
1267 lang = langs[j]; |
|
1268 packName = this.getLangPackName(lang, name); |
|
1269 supName = this.getLangPackName(lang, i); |
|
1270 smod = this.moduleInfo[packName]; |
|
1271 |
|
1272 if (!smod) { |
|
1273 smod = this._addLangPack(lang, o, packName); |
|
1274 } |
|
1275 |
|
1276 flatSup = flatSup || yArray.hash(smod.supersedes); |
|
1277 |
|
1278 if (!(supName in flatSup)) { |
|
1279 smod.supersedes.push(supName); |
|
1280 } |
|
1281 |
|
1282 o.lang = o.lang || []; |
|
1283 |
|
1284 flatLang = flatLang || yArray.hash(o.lang); |
|
1285 |
|
1286 if (!(lang in flatLang)) { |
|
1287 o.lang.push(lang); |
|
1288 } |
|
1289 |
|
1290 // Y.log('pack ' + packName + ' should supersede ' + supName); |
|
1291 // Add rollup file, need to add to supersedes list too |
|
1292 |
|
1293 // default packages |
|
1294 packName = this.getLangPackName(ROOT_LANG, name); |
|
1295 supName = this.getLangPackName(ROOT_LANG, i); |
|
1296 |
|
1297 smod = this.moduleInfo[packName]; |
|
1298 |
|
1299 if (!smod) { |
|
1300 smod = this._addLangPack(lang, o, packName); |
|
1301 } |
|
1302 |
|
1303 if (!(supName in flatSup)) { |
|
1304 smod.supersedes.push(supName); |
|
1305 } |
|
1306 |
|
1307 // Y.log('pack ' + packName + ' should supersede ' + supName); |
|
1308 // Add rollup file, need to add to supersedes list too |
|
1309 |
|
1310 } |
|
1311 } |
|
1312 |
|
1313 l++; |
|
1314 } |
|
1315 } |
|
1316 //o.supersedes = YObject.keys(yArray.hash(sup)); |
|
1317 o.supersedes = yArray.dedupe(sup); |
|
1318 if (this.allowRollup) { |
|
1319 o.rollup = (l < 4) ? l : Math.min(l - 1, 4); |
|
1320 } |
|
1321 } |
|
1322 |
|
1323 plugins = o.plugins; |
|
1324 if (plugins) { |
|
1325 for (i in plugins) { |
|
1326 if (plugins.hasOwnProperty(i)) { |
|
1327 plug = plugins[i]; |
|
1328 plug.pkg = name; |
|
1329 plug.path = plug.path || _path(name, i, o.type); |
|
1330 plug.requires = plug.requires || []; |
|
1331 plug.group = o.group; |
|
1332 this.addModule(plug, i); |
|
1333 if (o.skinnable) { |
|
1334 this._addSkin(this.skin.defaultSkin, i, name); |
|
1335 } |
|
1336 |
|
1337 } |
|
1338 } |
|
1339 } |
|
1340 |
|
1341 if (o.condition) { |
|
1342 t = o.condition.trigger; |
|
1343 if (YUI.Env.aliases[t]) { |
|
1344 t = YUI.Env.aliases[t]; |
|
1345 } |
|
1346 if (!Y.Lang.isArray(t)) { |
|
1347 t = [t]; |
|
1348 } |
|
1349 |
|
1350 for (i = 0; i < t.length; i++) { |
|
1351 trigger = t[i]; |
|
1352 when = o.condition.when; |
|
1353 conditions[trigger] = conditions[trigger] || {}; |
|
1354 conditions[trigger][name] = o.condition; |
|
1355 // the 'when' attribute can be 'before', 'after', or 'instead' |
|
1356 // the default is after. |
|
1357 if (when && when !== 'after') { |
|
1358 if (when === 'instead') { // replace the trigger |
|
1359 o.supersedes = o.supersedes || []; |
|
1360 o.supersedes.push(trigger); |
|
1361 } |
|
1362 // before the trigger |
|
1363 // the trigger requires the conditional mod, |
|
1364 // so it should appear before the conditional |
|
1365 // mod if we do not intersede. |
|
1366 } else { // after the trigger |
|
1367 o.after = o.after || []; |
|
1368 o.after.push(trigger); |
|
1369 } |
|
1370 } |
|
1371 } |
|
1372 |
|
1373 if (o.supersedes) { |
|
1374 o.supersedes = this.filterRequires(o.supersedes); |
|
1375 } |
|
1376 |
|
1377 if (o.after) { |
|
1378 o.after = this.filterRequires(o.after); |
|
1379 o.after_map = yArray.hash(o.after); |
|
1380 } |
|
1381 |
|
1382 // this.dirty = true; |
|
1383 |
|
1384 if (o.configFn) { |
|
1385 ret = o.configFn(o); |
|
1386 if (ret === false) { |
|
1387 Y.log('Config function returned false for ' + name + ', skipping.', 'info', 'loader'); |
|
1388 delete this.moduleInfo[name]; |
|
1389 delete GLOBAL_ENV._renderedMods[name]; |
|
1390 o = null; |
|
1391 } |
|
1392 } |
|
1393 //Add to global cache |
|
1394 if (o) { |
|
1395 if (!GLOBAL_ENV._renderedMods) { |
|
1396 GLOBAL_ENV._renderedMods = {}; |
|
1397 } |
|
1398 GLOBAL_ENV._renderedMods[name] = Y.mix(GLOBAL_ENV._renderedMods[name] || {}, o); |
|
1399 GLOBAL_ENV._conditions = conditions; |
|
1400 } |
|
1401 |
|
1402 return o; |
|
1403 }, |
|
1404 |
|
1405 /** |
|
1406 * Add a requirement for one or more module |
|
1407 * @method require |
|
1408 * @param {string[] | string*} what the modules to load. |
|
1409 */ |
|
1410 require: function(what) { |
|
1411 var a = (typeof what === 'string') ? yArray(arguments) : what; |
|
1412 this.dirty = true; |
|
1413 this.required = Y.merge(this.required, yArray.hash(this.filterRequires(a))); |
|
1414 |
|
1415 this._explodeRollups(); |
|
1416 }, |
|
1417 /** |
|
1418 * Grab all the items that were asked for, check to see if the Loader |
|
1419 * meta-data contains a "use" array. If it doesm remove the asked item and replace it with |
|
1420 * the content of the "use". |
|
1421 * This will make asking for: "dd" |
|
1422 * Actually ask for: "dd-ddm-base,dd-ddm,dd-ddm-drop,dd-drag,dd-proxy,dd-constrain,dd-drop,dd-scroll,dd-drop-plugin" |
|
1423 * @private |
|
1424 * @method _explodeRollups |
|
1425 */ |
|
1426 _explodeRollups: function() { |
|
1427 var self = this, m, m2, i, a, v, len, len2, |
|
1428 r = self.required; |
|
1429 |
|
1430 if (!self.allowRollup) { |
|
1431 for (i in r) { |
|
1432 if (r.hasOwnProperty(i)) { |
|
1433 m = self.getModule(i); |
|
1434 if (m && m.use) { |
|
1435 len = m.use.length; |
|
1436 for (a = 0; a < len; a++) { |
|
1437 m2 = self.getModule(m.use[a]); |
|
1438 if (m2 && m2.use) { |
|
1439 len2 = m2.use.length; |
|
1440 for (v = 0; v < len2; v++) { |
|
1441 r[m2.use[v]] = true; |
|
1442 } |
|
1443 } else { |
|
1444 r[m.use[a]] = true; |
|
1445 } |
|
1446 } |
|
1447 } |
|
1448 } |
|
1449 } |
|
1450 self.required = r; |
|
1451 } |
|
1452 |
|
1453 }, |
|
1454 /** |
|
1455 * Explodes the required array to remove aliases and replace them with real modules |
|
1456 * @method filterRequires |
|
1457 * @param {Array} r The original requires array |
|
1458 * @return {Array} The new array of exploded requirements |
|
1459 */ |
|
1460 filterRequires: function(r) { |
|
1461 if (r) { |
|
1462 if (!Y.Lang.isArray(r)) { |
|
1463 r = [r]; |
|
1464 } |
|
1465 r = Y.Array(r); |
|
1466 var c = [], i, mod, o, m; |
|
1467 |
|
1468 for (i = 0; i < r.length; i++) { |
|
1469 mod = this.getModule(r[i]); |
|
1470 if (mod && mod.use) { |
|
1471 for (o = 0; o < mod.use.length; o++) { |
|
1472 //Must walk the other modules in case a module is a rollup of rollups (datatype) |
|
1473 m = this.getModule(mod.use[o]); |
|
1474 if (m && m.use && (m.name !== mod.name)) { |
|
1475 c = Y.Array.dedupe([].concat(c, this.filterRequires(m.use))); |
|
1476 } else { |
|
1477 c.push(mod.use[o]); |
|
1478 } |
|
1479 } |
|
1480 } else { |
|
1481 c.push(r[i]); |
|
1482 } |
|
1483 } |
|
1484 r = c; |
|
1485 } |
|
1486 return r; |
|
1487 }, |
|
1488 /** |
|
1489 * Returns an object containing properties for all modules required |
|
1490 * in order to load the requested module |
|
1491 * @method getRequires |
|
1492 * @param {object} mod The module definition from moduleInfo. |
|
1493 * @return {array} the expanded requirement list. |
|
1494 */ |
|
1495 getRequires: function(mod) { |
|
1496 |
|
1497 if (!mod) { |
|
1498 //console.log('returning no reqs for ' + mod.name); |
|
1499 return NO_REQUIREMENTS; |
|
1500 } |
|
1501 |
|
1502 if (mod._parsed) { |
|
1503 //console.log('returning requires for ' + mod.name, mod.requires); |
|
1504 return mod.expanded || NO_REQUIREMENTS; |
|
1505 } |
|
1506 |
|
1507 //TODO add modue cache here out of scope.. |
|
1508 |
|
1509 var i, m, j, add, packName, lang, testresults = this.testresults, |
|
1510 name = mod.name, cond, |
|
1511 adddef = ON_PAGE[name] && ON_PAGE[name].details, |
|
1512 d, go, def, |
|
1513 r, old_mod, |
|
1514 o, skinmod, skindef, skinpar, skinname, |
|
1515 intl = mod.lang || mod.intl, |
|
1516 info = this.moduleInfo, |
|
1517 ftests = Y.Features && Y.Features.tests.load, |
|
1518 hash, reparse; |
|
1519 |
|
1520 // console.log(name); |
|
1521 |
|
1522 // pattern match leaves module stub that needs to be filled out |
|
1523 if (mod.temp && adddef) { |
|
1524 old_mod = mod; |
|
1525 mod = this.addModule(adddef, name); |
|
1526 mod.group = old_mod.group; |
|
1527 mod.pkg = old_mod.pkg; |
|
1528 delete mod.expanded; |
|
1529 } |
|
1530 |
|
1531 // console.log('cache: ' + mod.langCache + ' == ' + this.lang); |
|
1532 |
|
1533 //If a skin or a lang is different, reparse.. |
|
1534 reparse = !((!this.lang || mod.langCache === this.lang) && (mod.skinCache === this.skin.defaultSkin)); |
|
1535 |
|
1536 if (mod.expanded && !reparse) { |
|
1537 //Y.log('Already expanded ' + name + ', ' + mod.expanded); |
|
1538 return mod.expanded; |
|
1539 } |
|
1540 |
|
1541 |
|
1542 d = []; |
|
1543 hash = {}; |
|
1544 r = this.filterRequires(mod.requires); |
|
1545 if (mod.lang) { |
|
1546 //If a module has a lang attribute, auto add the intl requirement. |
|
1547 d.unshift('intl'); |
|
1548 r.unshift('intl'); |
|
1549 intl = true; |
|
1550 } |
|
1551 o = this.filterRequires(mod.optional); |
|
1552 |
|
1553 // Y.log("getRequires: " + name + " (dirty:" + this.dirty + |
|
1554 // ", expanded:" + mod.expanded + ")"); |
|
1555 |
|
1556 mod._parsed = true; |
|
1557 mod.langCache = this.lang; |
|
1558 mod.skinCache = this.skin.defaultSkin; |
|
1559 |
|
1560 for (i = 0; i < r.length; i++) { |
|
1561 //Y.log(name + ' requiring ' + r[i], 'info', 'loader'); |
|
1562 if (!hash[r[i]]) { |
|
1563 d.push(r[i]); |
|
1564 hash[r[i]] = true; |
|
1565 m = this.getModule(r[i]); |
|
1566 if (m) { |
|
1567 add = this.getRequires(m); |
|
1568 intl = intl || (m.expanded_map && |
|
1569 (INTL in m.expanded_map)); |
|
1570 for (j = 0; j < add.length; j++) { |
|
1571 d.push(add[j]); |
|
1572 } |
|
1573 } |
|
1574 } |
|
1575 } |
|
1576 |
|
1577 // get the requirements from superseded modules, if any |
|
1578 r = this.filterRequires(mod.supersedes); |
|
1579 if (r) { |
|
1580 for (i = 0; i < r.length; i++) { |
|
1581 if (!hash[r[i]]) { |
|
1582 // if this module has submodules, the requirements list is |
|
1583 // expanded to include the submodules. This is so we can |
|
1584 // prevent dups when a submodule is already loaded and the |
|
1585 // parent is requested. |
|
1586 if (mod.submodules) { |
|
1587 d.push(r[i]); |
|
1588 } |
|
1589 |
|
1590 hash[r[i]] = true; |
|
1591 m = this.getModule(r[i]); |
|
1592 |
|
1593 if (m) { |
|
1594 add = this.getRequires(m); |
|
1595 intl = intl || (m.expanded_map && |
|
1596 (INTL in m.expanded_map)); |
|
1597 for (j = 0; j < add.length; j++) { |
|
1598 d.push(add[j]); |
|
1599 } |
|
1600 } |
|
1601 } |
|
1602 } |
|
1603 } |
|
1604 |
|
1605 if (o && this.loadOptional) { |
|
1606 for (i = 0; i < o.length; i++) { |
|
1607 if (!hash[o[i]]) { |
|
1608 d.push(o[i]); |
|
1609 hash[o[i]] = true; |
|
1610 m = info[o[i]]; |
|
1611 if (m) { |
|
1612 add = this.getRequires(m); |
|
1613 intl = intl || (m.expanded_map && |
|
1614 (INTL in m.expanded_map)); |
|
1615 for (j = 0; j < add.length; j++) { |
|
1616 d.push(add[j]); |
|
1617 } |
|
1618 } |
|
1619 } |
|
1620 } |
|
1621 } |
|
1622 |
|
1623 cond = this.conditions[name]; |
|
1624 |
|
1625 if (cond) { |
|
1626 //Set the module to not parsed since we have conditionals and this could change the dependency tree. |
|
1627 mod._parsed = false; |
|
1628 if (testresults && ftests) { |
|
1629 oeach(testresults, function(result, id) { |
|
1630 var condmod = ftests[id].name; |
|
1631 if (!hash[condmod] && ftests[id].trigger === name) { |
|
1632 if (result && ftests[id]) { |
|
1633 hash[condmod] = true; |
|
1634 d.push(condmod); |
|
1635 } |
|
1636 } |
|
1637 }); |
|
1638 } else { |
|
1639 for (i in cond) { |
|
1640 if (cond.hasOwnProperty(i)) { |
|
1641 if (!hash[i]) { |
|
1642 def = cond[i]; |
|
1643 //first see if they've specfied a ua check |
|
1644 //then see if they've got a test fn & if it returns true |
|
1645 //otherwise just having a condition block is enough |
|
1646 go = def && ((!def.ua && !def.test) || (def.ua && Y.UA[def.ua]) || |
|
1647 (def.test && def.test(Y, r))); |
|
1648 |
|
1649 if (go) { |
|
1650 hash[i] = true; |
|
1651 d.push(i); |
|
1652 m = this.getModule(i); |
|
1653 if (m) { |
|
1654 add = this.getRequires(m); |
|
1655 for (j = 0; j < add.length; j++) { |
|
1656 d.push(add[j]); |
|
1657 } |
|
1658 |
|
1659 } |
|
1660 } |
|
1661 } |
|
1662 } |
|
1663 } |
|
1664 } |
|
1665 } |
|
1666 |
|
1667 // Create skin modules |
|
1668 if (mod.skinnable) { |
|
1669 skindef = this.skin.overrides; |
|
1670 for (i in YUI.Env.aliases) { |
|
1671 if (YUI.Env.aliases.hasOwnProperty(i)) { |
|
1672 if (Y.Array.indexOf(YUI.Env.aliases[i], name) > -1) { |
|
1673 skinpar = i; |
|
1674 } |
|
1675 } |
|
1676 } |
|
1677 if (skindef && (skindef[name] || (skinpar && skindef[skinpar]))) { |
|
1678 skinname = name; |
|
1679 if (skindef[skinpar]) { |
|
1680 skinname = skinpar; |
|
1681 } |
|
1682 for (i = 0; i < skindef[skinname].length; i++) { |
|
1683 skinmod = this._addSkin(skindef[skinname][i], name); |
|
1684 if (!this.isCSSLoaded(skinmod, this._boot)) { |
|
1685 d.push(skinmod); |
|
1686 } |
|
1687 } |
|
1688 } else { |
|
1689 skinmod = this._addSkin(this.skin.defaultSkin, name); |
|
1690 if (!this.isCSSLoaded(skinmod, this._boot)) { |
|
1691 d.push(skinmod); |
|
1692 } |
|
1693 } |
|
1694 } |
|
1695 |
|
1696 mod._parsed = false; |
|
1697 |
|
1698 if (intl) { |
|
1699 |
|
1700 if (mod.lang && !mod.langPack && Y.Intl) { |
|
1701 lang = Y.Intl.lookupBestLang(this.lang || ROOT_LANG, mod.lang); |
|
1702 //Y.log('Best lang: ' + lang + ', this.lang: ' + this.lang + ', mod.lang: ' + mod.lang); |
|
1703 packName = this.getLangPackName(lang, name); |
|
1704 if (packName) { |
|
1705 d.unshift(packName); |
|
1706 } |
|
1707 } |
|
1708 d.unshift(INTL); |
|
1709 } |
|
1710 |
|
1711 mod.expanded_map = yArray.hash(d); |
|
1712 |
|
1713 mod.expanded = YObject.keys(mod.expanded_map); |
|
1714 |
|
1715 return mod.expanded; |
|
1716 }, |
|
1717 /** |
|
1718 * Check to see if named css module is already loaded on the page |
|
1719 * @method isCSSLoaded |
|
1720 * @param {String} name The name of the css file |
|
1721 * @return Boolean |
|
1722 */ |
|
1723 isCSSLoaded: function(name, skip) { |
|
1724 //TODO - Make this call a batching call with name being an array |
|
1725 if (!name || !YUI.Env.cssStampEl || (!skip && this.ignoreRegistered)) { |
|
1726 Y.log('isCSSLoaded was skipped for ' + name, 'warn', 'loader'); |
|
1727 return false; |
|
1728 } |
|
1729 var el = YUI.Env.cssStampEl, |
|
1730 ret = false, |
|
1731 mod = YUI.Env._cssLoaded[name], |
|
1732 style = el.currentStyle; //IE |
|
1733 |
|
1734 |
|
1735 if (mod !== undefined) { |
|
1736 //Y.log('isCSSLoaded was cached for ' + name, 'warn', 'loader'); |
|
1737 return mod; |
|
1738 } |
|
1739 |
|
1740 //Add the classname to the element |
|
1741 el.className = name; |
|
1742 |
|
1743 if (!style) { |
|
1744 style = Y.config.doc.defaultView.getComputedStyle(el, null); |
|
1745 } |
|
1746 |
|
1747 if (style && style.display === 'none') { |
|
1748 ret = true; |
|
1749 } |
|
1750 |
|
1751 Y.log('Has Skin? ' + name + ' : ' + ret, 'info', 'loader'); |
|
1752 |
|
1753 el.className = ''; //Reset the classname to '' |
|
1754 |
|
1755 YUI.Env._cssLoaded[name] = ret; |
|
1756 |
|
1757 return ret; |
|
1758 }, |
|
1759 |
|
1760 /** |
|
1761 * Returns a hash of module names the supplied module satisfies. |
|
1762 * @method getProvides |
|
1763 * @param {string} name The name of the module. |
|
1764 * @return {object} what this module provides. |
|
1765 */ |
|
1766 getProvides: function(name) { |
|
1767 var m = this.getModule(name), o, s; |
|
1768 // supmap = this.provides; |
|
1769 |
|
1770 if (!m) { |
|
1771 return NOT_FOUND; |
|
1772 } |
|
1773 |
|
1774 if (m && !m.provides) { |
|
1775 o = {}; |
|
1776 s = m.supersedes; |
|
1777 |
|
1778 if (s) { |
|
1779 yArray.each(s, function(v) { |
|
1780 Y.mix(o, this.getProvides(v)); |
|
1781 }, this); |
|
1782 } |
|
1783 |
|
1784 o[name] = true; |
|
1785 m.provides = o; |
|
1786 |
|
1787 } |
|
1788 |
|
1789 return m.provides; |
|
1790 }, |
|
1791 |
|
1792 /** |
|
1793 * Calculates the dependency tree, the result is stored in the sorted |
|
1794 * property. |
|
1795 * @method calculate |
|
1796 * @param {object} o optional options object. |
|
1797 * @param {string} type optional argument to prune modules. |
|
1798 */ |
|
1799 calculate: function(o, type) { |
|
1800 if (o || type || this.dirty) { |
|
1801 |
|
1802 if (o) { |
|
1803 this._config(o); |
|
1804 } |
|
1805 |
|
1806 if (!this._init) { |
|
1807 this._setup(); |
|
1808 } |
|
1809 |
|
1810 this._explode(); |
|
1811 |
|
1812 if (this.allowRollup) { |
|
1813 this._rollup(); |
|
1814 } else { |
|
1815 this._explodeRollups(); |
|
1816 } |
|
1817 this._reduce(); |
|
1818 this._sort(); |
|
1819 } |
|
1820 }, |
|
1821 /** |
|
1822 * Creates a "psuedo" package for languages provided in the lang array |
|
1823 * @method _addLangPack |
|
1824 * @private |
|
1825 * @param {String} lang The language to create |
|
1826 * @param {Object} m The module definition to create the language pack around |
|
1827 * @param {String} packName The name of the package (e.g: lang/datatype-date-en-US) |
|
1828 * @return {Object} The module definition |
|
1829 */ |
|
1830 _addLangPack: function(lang, m, packName) { |
|
1831 var name = m.name, |
|
1832 packPath, conf, |
|
1833 existing = this.moduleInfo[packName]; |
|
1834 |
|
1835 if (!existing) { |
|
1836 |
|
1837 packPath = _path((m.pkg || name), packName, JS, true); |
|
1838 |
|
1839 conf = { |
|
1840 path: packPath, |
|
1841 intl: true, |
|
1842 langPack: true, |
|
1843 ext: m.ext, |
|
1844 group: m.group, |
|
1845 supersedes: [] |
|
1846 }; |
|
1847 if (m.root) { |
|
1848 conf.root = m.root; |
|
1849 } |
|
1850 if (m.base) { |
|
1851 conf.base = m.base; |
|
1852 } |
|
1853 |
|
1854 if (m.configFn) { |
|
1855 conf.configFn = m.configFn; |
|
1856 } |
|
1857 |
|
1858 this.addModule(conf, packName); |
|
1859 |
|
1860 if (lang) { |
|
1861 Y.Env.lang = Y.Env.lang || {}; |
|
1862 Y.Env.lang[lang] = Y.Env.lang[lang] || {}; |
|
1863 Y.Env.lang[lang][name] = true; |
|
1864 } |
|
1865 } |
|
1866 |
|
1867 return this.moduleInfo[packName]; |
|
1868 }, |
|
1869 |
|
1870 /** |
|
1871 * Investigates the current YUI configuration on the page. By default, |
|
1872 * modules already detected will not be loaded again unless a force |
|
1873 * option is encountered. Called by calculate() |
|
1874 * @method _setup |
|
1875 * @private |
|
1876 */ |
|
1877 _setup: function() { |
|
1878 var info = this.moduleInfo, name, i, j, m, l, |
|
1879 packName; |
|
1880 |
|
1881 for (name in info) { |
|
1882 if (info.hasOwnProperty(name)) { |
|
1883 m = info[name]; |
|
1884 if (m) { |
|
1885 |
|
1886 // remove dups |
|
1887 //m.requires = YObject.keys(yArray.hash(m.requires)); |
|
1888 m.requires = yArray.dedupe(m.requires); |
|
1889 |
|
1890 // Create lang pack modules |
|
1891 //if (m.lang && m.lang.length) { |
|
1892 if (m.lang) { |
|
1893 // Setup root package if the module has lang defined, |
|
1894 // it needs to provide a root language pack |
|
1895 packName = this.getLangPackName(ROOT_LANG, name); |
|
1896 this._addLangPack(null, m, packName); |
|
1897 } |
|
1898 |
|
1899 } |
|
1900 } |
|
1901 } |
|
1902 |
|
1903 |
|
1904 //l = Y.merge(this.inserted); |
|
1905 l = {}; |
|
1906 |
|
1907 // available modules |
|
1908 if (!this.ignoreRegistered) { |
|
1909 Y.mix(l, GLOBAL_ENV.mods); |
|
1910 } |
|
1911 |
|
1912 // add the ignore list to the list of loaded packages |
|
1913 if (this.ignore) { |
|
1914 Y.mix(l, yArray.hash(this.ignore)); |
|
1915 } |
|
1916 |
|
1917 // expand the list to include superseded modules |
|
1918 for (j in l) { |
|
1919 if (l.hasOwnProperty(j)) { |
|
1920 Y.mix(l, this.getProvides(j)); |
|
1921 } |
|
1922 } |
|
1923 |
|
1924 // remove modules on the force list from the loaded list |
|
1925 if (this.force) { |
|
1926 for (i = 0; i < this.force.length; i++) { |
|
1927 if (this.force[i] in l) { |
|
1928 delete l[this.force[i]]; |
|
1929 } |
|
1930 } |
|
1931 } |
|
1932 |
|
1933 Y.mix(this.loaded, l); |
|
1934 |
|
1935 this._init = true; |
|
1936 }, |
|
1937 |
|
1938 /** |
|
1939 * Builds a module name for a language pack |
|
1940 * @method getLangPackName |
|
1941 * @param {string} lang the language code. |
|
1942 * @param {string} mname the module to build it for. |
|
1943 * @return {string} the language pack module name. |
|
1944 */ |
|
1945 getLangPackName: function(lang, mname) { |
|
1946 return ('lang/' + mname + ((lang) ? '_' + lang : '')); |
|
1947 }, |
|
1948 /** |
|
1949 * Inspects the required modules list looking for additional |
|
1950 * dependencies. Expands the required list to include all |
|
1951 * required modules. Called by calculate() |
|
1952 * @method _explode |
|
1953 * @private |
|
1954 */ |
|
1955 _explode: function() { |
|
1956 //TODO Move done out of scope |
|
1957 var r = this.required, m, reqs, done = {}, |
|
1958 self = this, name, expound; |
|
1959 |
|
1960 // the setup phase is over, all modules have been created |
|
1961 self.dirty = false; |
|
1962 |
|
1963 self._explodeRollups(); |
|
1964 r = self.required; |
|
1965 |
|
1966 for (name in r) { |
|
1967 if (r.hasOwnProperty(name)) { |
|
1968 if (!done[name]) { |
|
1969 done[name] = true; |
|
1970 m = self.getModule(name); |
|
1971 if (m) { |
|
1972 expound = m.expound; |
|
1973 |
|
1974 if (expound) { |
|
1975 r[expound] = self.getModule(expound); |
|
1976 reqs = self.getRequires(r[expound]); |
|
1977 Y.mix(r, yArray.hash(reqs)); |
|
1978 } |
|
1979 |
|
1980 reqs = self.getRequires(m); |
|
1981 Y.mix(r, yArray.hash(reqs)); |
|
1982 } |
|
1983 } |
|
1984 } |
|
1985 } |
|
1986 |
|
1987 // Y.log('After explode: ' + YObject.keys(r)); |
|
1988 }, |
|
1989 /** |
|
1990 * The default method used to test a module against a pattern |
|
1991 * @method _patternTest |
|
1992 * @private |
|
1993 * @param {String} mname The module being tested |
|
1994 * @param {String} pname The pattern to match |
|
1995 */ |
|
1996 _patternTest: function(mname, pname) { |
|
1997 return (mname.indexOf(pname) > -1); |
|
1998 }, |
|
1999 /** |
|
2000 * Get's the loader meta data for the requested module |
|
2001 * @method getModule |
|
2002 * @param {String} mname The module name to get |
|
2003 * @return {Object} The module metadata |
|
2004 */ |
|
2005 getModule: function(mname) { |
|
2006 //TODO: Remove name check - it's a quick hack to fix pattern WIP |
|
2007 if (!mname) { |
|
2008 return null; |
|
2009 } |
|
2010 |
|
2011 var p, found, pname, |
|
2012 m = this.moduleInfo[mname], |
|
2013 patterns = this.patterns; |
|
2014 |
|
2015 // check the patterns library to see if we should automatically add |
|
2016 // the module with defaults |
|
2017 if (!m || (m && m.ext)) { |
|
2018 // Y.log('testing patterns ' + YObject.keys(patterns)); |
|
2019 for (pname in patterns) { |
|
2020 if (patterns.hasOwnProperty(pname)) { |
|
2021 // Y.log('testing pattern ' + i); |
|
2022 p = patterns[pname]; |
|
2023 |
|
2024 //There is no test method, create a default one that tests |
|
2025 // the pattern against the mod name |
|
2026 if (!p.test) { |
|
2027 p.test = this._patternTest; |
|
2028 } |
|
2029 |
|
2030 if (p.test(mname, pname)) { |
|
2031 // use the metadata supplied for the pattern |
|
2032 // as the module definition. |
|
2033 found = p; |
|
2034 break; |
|
2035 } |
|
2036 } |
|
2037 } |
|
2038 } |
|
2039 |
|
2040 if (!m) { |
|
2041 if (found) { |
|
2042 if (p.action) { |
|
2043 // Y.log('executing pattern action: ' + pname); |
|
2044 p.action.call(this, mname, pname); |
|
2045 } else { |
|
2046 Y.log('Undefined module: ' + mname + ', matched a pattern: ' + |
|
2047 pname, 'info', 'loader'); |
|
2048 // ext true or false? |
|
2049 m = this.addModule(Y.merge(found), mname); |
|
2050 if (found.configFn) { |
|
2051 m.configFn = found.configFn; |
|
2052 } |
|
2053 m.temp = true; |
|
2054 } |
|
2055 } |
|
2056 } else { |
|
2057 if (found && m && found.configFn && !m.configFn) { |
|
2058 m.configFn = found.configFn; |
|
2059 m.configFn(m); |
|
2060 } |
|
2061 } |
|
2062 |
|
2063 return m; |
|
2064 }, |
|
2065 |
|
2066 // impl in rollup submodule |
|
2067 _rollup: function() { }, |
|
2068 |
|
2069 /** |
|
2070 * Remove superceded modules and loaded modules. Called by |
|
2071 * calculate() after we have the mega list of all dependencies |
|
2072 * @method _reduce |
|
2073 * @return {object} the reduced dependency hash. |
|
2074 * @private |
|
2075 */ |
|
2076 _reduce: function(r) { |
|
2077 |
|
2078 r = r || this.required; |
|
2079 |
|
2080 var i, j, s, m, type = this.loadType, |
|
2081 ignore = this.ignore ? yArray.hash(this.ignore) : false; |
|
2082 |
|
2083 for (i in r) { |
|
2084 if (r.hasOwnProperty(i)) { |
|
2085 m = this.getModule(i); |
|
2086 // remove if already loaded |
|
2087 if (((this.loaded[i] || ON_PAGE[i]) && |
|
2088 !this.forceMap[i] && !this.ignoreRegistered) || |
|
2089 (type && m && m.type !== type)) { |
|
2090 delete r[i]; |
|
2091 } |
|
2092 if (ignore && ignore[i]) { |
|
2093 delete r[i]; |
|
2094 } |
|
2095 // remove anything this module supersedes |
|
2096 s = m && m.supersedes; |
|
2097 if (s) { |
|
2098 for (j = 0; j < s.length; j++) { |
|
2099 if (s[j] in r) { |
|
2100 delete r[s[j]]; |
|
2101 } |
|
2102 } |
|
2103 } |
|
2104 } |
|
2105 } |
|
2106 |
|
2107 return r; |
|
2108 }, |
|
2109 /** |
|
2110 * Handles the queue when a module has been loaded for all cases |
|
2111 * @method _finish |
|
2112 * @private |
|
2113 * @param {String} msg The message from Loader |
|
2114 * @param {Boolean} success A boolean denoting success or failure |
|
2115 */ |
|
2116 _finish: function(msg, success) { |
|
2117 Y.log('loader finishing: ' + msg + ', ' + Y.id + ', ' + |
|
2118 this.data, 'info', 'loader'); |
|
2119 |
|
2120 _queue.running = false; |
|
2121 |
|
2122 var onEnd = this.onEnd; |
|
2123 if (onEnd) { |
|
2124 onEnd.call(this.context, { |
|
2125 msg: msg, |
|
2126 data: this.data, |
|
2127 success: success |
|
2128 }); |
|
2129 } |
|
2130 this._continue(); |
|
2131 }, |
|
2132 /** |
|
2133 * The default Loader onSuccess handler, calls this.onSuccess with a payload |
|
2134 * @method _onSuccess |
|
2135 * @private |
|
2136 */ |
|
2137 _onSuccess: function() { |
|
2138 var self = this, skipped = Y.merge(self.skipped), fn, |
|
2139 failed = [], rreg = self.requireRegistration, |
|
2140 success, msg, i, mod; |
|
2141 |
|
2142 for (i in skipped) { |
|
2143 if (skipped.hasOwnProperty(i)) { |
|
2144 delete self.inserted[i]; |
|
2145 } |
|
2146 } |
|
2147 |
|
2148 self.skipped = {}; |
|
2149 |
|
2150 for (i in self.inserted) { |
|
2151 if (self.inserted.hasOwnProperty(i)) { |
|
2152 mod = self.getModule(i); |
|
2153 if (mod && rreg && mod.type === JS && !(i in YUI.Env.mods)) { |
|
2154 failed.push(i); |
|
2155 } else { |
|
2156 Y.mix(self.loaded, self.getProvides(i)); |
|
2157 } |
|
2158 } |
|
2159 } |
|
2160 |
|
2161 fn = self.onSuccess; |
|
2162 msg = (failed.length) ? 'notregistered' : 'success'; |
|
2163 success = !(failed.length); |
|
2164 if (fn) { |
|
2165 fn.call(self.context, { |
|
2166 msg: msg, |
|
2167 data: self.data, |
|
2168 success: success, |
|
2169 failed: failed, |
|
2170 skipped: skipped |
|
2171 }); |
|
2172 } |
|
2173 self._finish(msg, success); |
|
2174 }, |
|
2175 /** |
|
2176 * The default Loader onProgress handler, calls this.onProgress with a payload |
|
2177 * @method _onProgress |
|
2178 * @private |
|
2179 */ |
|
2180 _onProgress: function(e) { |
|
2181 var self = this, i; |
|
2182 //set the internal cache to what just came in. |
|
2183 if (e.data && e.data.length) { |
|
2184 for (i = 0; i < e.data.length; i++) { |
|
2185 e.data[i] = self.getModule(e.data[i].name); |
|
2186 } |
|
2187 } |
|
2188 if (self.onProgress) { |
|
2189 self.onProgress.call(self.context, { |
|
2190 name: e.url, |
|
2191 data: e.data |
|
2192 }); |
|
2193 } |
|
2194 }, |
|
2195 /** |
|
2196 * The default Loader onFailure handler, calls this.onFailure with a payload |
|
2197 * @method _onFailure |
|
2198 * @private |
|
2199 */ |
|
2200 _onFailure: function(o) { |
|
2201 var f = this.onFailure, msg = [], i = 0, len = o.errors.length; |
|
2202 |
|
2203 for (i; i < len; i++) { |
|
2204 msg.push(o.errors[i].error); |
|
2205 } |
|
2206 |
|
2207 msg = msg.join(','); |
|
2208 |
|
2209 Y.log('load error: ' + msg + ', ' + Y.id, 'error', 'loader'); |
|
2210 |
|
2211 if (f) { |
|
2212 f.call(this.context, { |
|
2213 msg: msg, |
|
2214 data: this.data, |
|
2215 success: false |
|
2216 }); |
|
2217 } |
|
2218 |
|
2219 this._finish(msg, false); |
|
2220 |
|
2221 }, |
|
2222 |
|
2223 /** |
|
2224 * The default Loader onTimeout handler, calls this.onTimeout with a payload |
|
2225 * @method _onTimeout |
|
2226 * @param {Get.Transaction} transaction The Transaction object from `Y.Get` |
|
2227 * @private |
|
2228 */ |
|
2229 _onTimeout: function(transaction) { |
|
2230 Y.log('loader timeout: ' + Y.id, 'error', 'loader'); |
|
2231 var f = this.onTimeout; |
|
2232 if (f) { |
|
2233 f.call(this.context, { |
|
2234 msg: 'timeout', |
|
2235 data: this.data, |
|
2236 success: false, |
|
2237 transaction: transaction |
|
2238 }); |
|
2239 } |
|
2240 }, |
|
2241 |
|
2242 /** |
|
2243 * Sorts the dependency tree. The last step of calculate() |
|
2244 * @method _sort |
|
2245 * @private |
|
2246 */ |
|
2247 _sort: function() { |
|
2248 var name, |
|
2249 |
|
2250 // Object containing module names. |
|
2251 required = this.required, |
|
2252 |
|
2253 // Keep track of whether we've visited a module. |
|
2254 visited = {}; |
|
2255 |
|
2256 // Will contain modules names, in the correct order, |
|
2257 // according to dependencies. |
|
2258 this.sorted = []; |
|
2259 |
|
2260 for (name in required) { |
|
2261 if (!visited[name] && required.hasOwnProperty(name)) { |
|
2262 this._visit(name, visited); |
|
2263 } |
|
2264 } |
|
2265 }, |
|
2266 |
|
2267 /** |
|
2268 * Recursively visits the dependencies of the module name |
|
2269 * passed in, and appends each module name to the `sorted` property. |
|
2270 * @param {String} name The name of a module. |
|
2271 * @param {Object} visited Keeps track of whether a module was visited. |
|
2272 * @method _visit |
|
2273 * @private |
|
2274 */ |
|
2275 _visit: function (name, visited) { |
|
2276 var required, moduleInfo, dependency, dependencies, i, l; |
|
2277 |
|
2278 visited[name] = true; |
|
2279 required = this.required; |
|
2280 moduleInfo = this.moduleInfo[name]; |
|
2281 |
|
2282 if (moduleInfo) { |
|
2283 // Recurse on each dependency of this module, |
|
2284 // figuring out its dependencies, and so on. |
|
2285 dependencies = moduleInfo.requires; |
|
2286 for (i = 0, l = dependencies.length; i < l; ++i) { |
|
2287 dependency = dependencies[i]; |
|
2288 |
|
2289 // Is this module name in the required list of modules, |
|
2290 // and have we not already visited it? |
|
2291 if (required[dependency] && !visited[dependency]) { |
|
2292 this._visit(dependency, visited); |
|
2293 } |
|
2294 } |
|
2295 } |
|
2296 |
|
2297 this.sorted.push(name); |
|
2298 }, |
|
2299 |
|
2300 /** |
|
2301 * Handles the actual insertion of script/link tags |
|
2302 * @method _insert |
|
2303 * @private |
|
2304 * @param {Object} source The YUI instance the request came from |
|
2305 * @param {Object} o The metadata to include |
|
2306 * @param {String} type JS or CSS |
|
2307 * @param {Boolean} [skipcalc=false] Do a Loader.calculate on the meta |
|
2308 */ |
|
2309 _insert: function(source, o, type, skipcalc) { |
|
2310 |
|
2311 Y.log('private _insert() ' + (type || '') + ', ' + Y.id, "info", "loader"); |
|
2312 |
|
2313 // restore the state at the time of the request |
|
2314 if (source) { |
|
2315 this._config(source); |
|
2316 } |
|
2317 |
|
2318 // build the dependency list |
|
2319 // don't include type so we can process CSS and script in |
|
2320 // one pass when the type is not specified. |
|
2321 |
|
2322 var modules = this.resolve(!skipcalc), |
|
2323 self = this, comp = 0, actions = 0, |
|
2324 mods = {}, deps, complete; |
|
2325 |
|
2326 self._refetch = []; |
|
2327 |
|
2328 if (type) { |
|
2329 //Filter out the opposite type and reset the array so the checks later work |
|
2330 modules[((type === JS) ? CSS : JS)] = []; |
|
2331 } |
|
2332 if (!self.fetchCSS) { |
|
2333 modules.css = []; |
|
2334 } |
|
2335 if (modules.js.length) { |
|
2336 comp++; |
|
2337 } |
|
2338 if (modules.css.length) { |
|
2339 comp++; |
|
2340 } |
|
2341 |
|
2342 //console.log('Resolved Modules: ', modules); |
|
2343 |
|
2344 complete = function(d) { |
|
2345 actions++; |
|
2346 var errs = {}, i = 0, o = 0, u = '', fn, |
|
2347 modName, resMods; |
|
2348 |
|
2349 if (d && d.errors) { |
|
2350 for (i = 0; i < d.errors.length; i++) { |
|
2351 if (d.errors[i].request) { |
|
2352 u = d.errors[i].request.url; |
|
2353 } else { |
|
2354 u = d.errors[i]; |
|
2355 } |
|
2356 errs[u] = u; |
|
2357 } |
|
2358 } |
|
2359 |
|
2360 if (d && d.data && d.data.length && (d.type === 'success')) { |
|
2361 for (i = 0; i < d.data.length; i++) { |
|
2362 self.inserted[d.data[i].name] = true; |
|
2363 //If the external module has a skin or a lang, reprocess it |
|
2364 if (d.data[i].lang || d.data[i].skinnable) { |
|
2365 delete self.inserted[d.data[i].name]; |
|
2366 self._refetch.push(d.data[i].name); |
|
2367 } |
|
2368 } |
|
2369 } |
|
2370 |
|
2371 if (actions === comp) { |
|
2372 self._loading = null; |
|
2373 Y.log('Loader actions complete!', 'info', 'loader'); |
|
2374 if (self._refetch.length) { |
|
2375 //Get the deps for the new meta-data and reprocess |
|
2376 Y.log('Found potential modules to refetch', 'info', 'loader'); |
|
2377 for (i = 0; i < self._refetch.length; i++) { |
|
2378 deps = self.getRequires(self.getModule(self._refetch[i])); |
|
2379 for (o = 0; o < deps.length; o++) { |
|
2380 if (!self.inserted[deps[o]]) { |
|
2381 //We wouldn't be to this point without the module being here |
|
2382 mods[deps[o]] = deps[o]; |
|
2383 } |
|
2384 } |
|
2385 } |
|
2386 mods = Y.Object.keys(mods); |
|
2387 if (mods.length) { |
|
2388 Y.log('Refetching modules with new meta-data', 'info', 'loader'); |
|
2389 self.require(mods); |
|
2390 resMods = self.resolve(true); |
|
2391 if (resMods.cssMods.length) { |
|
2392 for (i=0; i < resMods.cssMods.length; i++) { |
|
2393 modName = resMods.cssMods[i].name; |
|
2394 delete YUI.Env._cssLoaded[modName]; |
|
2395 if (self.isCSSLoaded(modName)) { |
|
2396 self.inserted[modName] = true; |
|
2397 delete self.required[modName]; |
|
2398 } |
|
2399 } |
|
2400 self.sorted = []; |
|
2401 self._sort(); |
|
2402 } |
|
2403 d = null; //bail |
|
2404 self._insert(); //insert the new deps |
|
2405 } |
|
2406 } |
|
2407 if (d && d.fn) { |
|
2408 Y.log('Firing final Loader callback!', 'info', 'loader'); |
|
2409 fn = d.fn; |
|
2410 delete d.fn; |
|
2411 fn.call(self, d); |
|
2412 } |
|
2413 } |
|
2414 }; |
|
2415 |
|
2416 this._loading = true; |
|
2417 |
|
2418 if (!modules.js.length && !modules.css.length) { |
|
2419 Y.log('No modules resolved..', 'warn', 'loader'); |
|
2420 actions = -1; |
|
2421 complete({ |
|
2422 fn: self._onSuccess |
|
2423 }); |
|
2424 return; |
|
2425 } |
|
2426 |
|
2427 |
|
2428 if (modules.css.length) { //Load CSS first |
|
2429 Y.log('Loading CSS modules', 'info', 'loader'); |
|
2430 Y.Get.css(modules.css, { |
|
2431 data: modules.cssMods, |
|
2432 attributes: self.cssAttributes, |
|
2433 insertBefore: self.insertBefore, |
|
2434 charset: self.charset, |
|
2435 timeout: self.timeout, |
|
2436 context: self, |
|
2437 onProgress: function(e) { |
|
2438 self._onProgress.call(self, e); |
|
2439 }, |
|
2440 onTimeout: function(d) { |
|
2441 self._onTimeout.call(self, d); |
|
2442 }, |
|
2443 onSuccess: function(d) { |
|
2444 d.type = 'success'; |
|
2445 d.fn = self._onSuccess; |
|
2446 complete.call(self, d); |
|
2447 }, |
|
2448 onFailure: function(d) { |
|
2449 d.type = 'failure'; |
|
2450 d.fn = self._onFailure; |
|
2451 complete.call(self, d); |
|
2452 } |
|
2453 }); |
|
2454 } |
|
2455 |
|
2456 if (modules.js.length) { |
|
2457 Y.log('Loading JS modules', 'info', 'loader'); |
|
2458 Y.Get.js(modules.js, { |
|
2459 data: modules.jsMods, |
|
2460 insertBefore: self.insertBefore, |
|
2461 attributes: self.jsAttributes, |
|
2462 charset: self.charset, |
|
2463 timeout: self.timeout, |
|
2464 autopurge: false, |
|
2465 context: self, |
|
2466 async: self.async, |
|
2467 onProgress: function(e) { |
|
2468 self._onProgress.call(self, e); |
|
2469 }, |
|
2470 onTimeout: function(d) { |
|
2471 self._onTimeout.call(self, d); |
|
2472 }, |
|
2473 onSuccess: function(d) { |
|
2474 d.type = 'success'; |
|
2475 d.fn = self._onSuccess; |
|
2476 complete.call(self, d); |
|
2477 }, |
|
2478 onFailure: function(d) { |
|
2479 d.type = 'failure'; |
|
2480 d.fn = self._onFailure; |
|
2481 complete.call(self, d); |
|
2482 } |
|
2483 }); |
|
2484 } |
|
2485 }, |
|
2486 /** |
|
2487 * Once a loader operation is completely finished, process any additional queued items. |
|
2488 * @method _continue |
|
2489 * @private |
|
2490 */ |
|
2491 _continue: function() { |
|
2492 if (!(_queue.running) && _queue.size() > 0) { |
|
2493 _queue.running = true; |
|
2494 _queue.next()(); |
|
2495 } |
|
2496 }, |
|
2497 |
|
2498 /** |
|
2499 * inserts the requested modules and their dependencies. |
|
2500 * <code>type</code> can be "js" or "css". Both script and |
|
2501 * css are inserted if type is not provided. |
|
2502 * @method insert |
|
2503 * @param {object} o optional options object. |
|
2504 * @param {string} type the type of dependency to insert. |
|
2505 */ |
|
2506 insert: function(o, type, skipsort) { |
|
2507 Y.log('public insert() ' + (type || '') + ', ' + |
|
2508 Y.Object.keys(this.required), "info", "loader"); |
|
2509 var self = this, copy = Y.merge(this); |
|
2510 delete copy.require; |
|
2511 delete copy.dirty; |
|
2512 _queue.add(function() { |
|
2513 self._insert(copy, o, type, skipsort); |
|
2514 }); |
|
2515 this._continue(); |
|
2516 }, |
|
2517 |
|
2518 /** |
|
2519 * Executed every time a module is loaded, and if we are in a load |
|
2520 * cycle, we attempt to load the next script. Public so that it |
|
2521 * is possible to call this if using a method other than |
|
2522 * Y.register to determine when scripts are fully loaded |
|
2523 * @method loadNext |
|
2524 * @deprecated |
|
2525 * @param {string} mname optional the name of the module that has |
|
2526 * been loaded (which is usually why it is time to load the next |
|
2527 * one). |
|
2528 */ |
|
2529 loadNext: function() { |
|
2530 Y.log('loadNext was called..', 'error', 'loader'); |
|
2531 return; |
|
2532 }, |
|
2533 |
|
2534 /** |
|
2535 * Apply filter defined for this instance to a url/path |
|
2536 * @method _filter |
|
2537 * @param {string} u the string to filter. |
|
2538 * @param {string} name the name of the module, if we are processing |
|
2539 * a single module as opposed to a combined url. |
|
2540 * @return {string} the filtered string. |
|
2541 * @private |
|
2542 */ |
|
2543 _filter: function(u, name, group) { |
|
2544 var f = this.filter, |
|
2545 hasFilter = name && (name in this.filters), |
|
2546 modFilter = hasFilter && this.filters[name], |
|
2547 groupName = group || (this.moduleInfo[name] ? this.moduleInfo[name].group : null); |
|
2548 |
|
2549 if (groupName && this.groups[groupName] && this.groups[groupName].filter) { |
|
2550 modFilter = this.groups[groupName].filter; |
|
2551 hasFilter = true; |
|
2552 } |
|
2553 |
|
2554 if (u) { |
|
2555 if (hasFilter) { |
|
2556 f = (L.isString(modFilter)) ? this.FILTER_DEFS[modFilter.toUpperCase()] || null : modFilter; |
|
2557 } |
|
2558 if (f) { |
|
2559 u = u.replace(new RegExp(f.searchExp, 'g'), f.replaceStr); |
|
2560 } |
|
2561 } |
|
2562 return u; |
|
2563 }, |
|
2564 |
|
2565 /** |
|
2566 * Generates the full url for a module |
|
2567 * @method _url |
|
2568 * @param {string} path the path fragment. |
|
2569 * @param {String} name The name of the module |
|
2570 * @param {String} [base] The base url to use. Defaults to self.base |
|
2571 * @return {string} the full url. |
|
2572 * @private |
|
2573 */ |
|
2574 _url: function(path, name, base) { |
|
2575 return this._filter((base || this.base || '') + path, name); |
|
2576 }, |
|
2577 /** |
|
2578 * Returns an Object hash of file arrays built from `loader.sorted` or from an arbitrary list of sorted modules. |
|
2579 * @method resolve |
|
2580 * @param {Boolean} [calc=false] Perform a loader.calculate() before anything else |
|
2581 * @param {Array} [s] An override for the loader.sorted array. Defaults to |
|
2582 * `loader.sorted`. |
|
2583 * @return {Object} Object hash (js and css) of two arrays of file lists |
|
2584 * @example This method can be used as an off-line dep calculator |
|
2585 * |
|
2586 * var Y = YUI(); |
|
2587 * var loader = new Y.Loader({ |
|
2588 * filter: 'debug', |
|
2589 * base: '../../', |
|
2590 * root: 'build/', |
|
2591 * combine: true, |
|
2592 * require: ['node', 'dd', 'console'] |
|
2593 * }); |
|
2594 * var out = loader.resolve(true); |
|
2595 * |
|
2596 */ |
|
2597 resolve: function(calc, s) { |
|
2598 |
|
2599 var len, i, m, url, group, groupName, j, frag, |
|
2600 comboSource, comboSources, mods, comboBase, |
|
2601 base, urls, u = [], tmpBase, baseLen, resCombos = {}, |
|
2602 self = this, comboSep, maxURLLength, |
|
2603 inserted = (self.ignoreRegistered) ? {} : self.inserted, |
|
2604 resolved = { js: [], jsMods: [], css: [], cssMods: [] }, |
|
2605 type = self.loadType || 'js', addSingle; |
|
2606 |
|
2607 if (self.skin.overrides || self.skin.defaultSkin !== DEFAULT_SKIN || self.ignoreRegistered) { |
|
2608 self._resetModules(); |
|
2609 } |
|
2610 |
|
2611 if (calc) { |
|
2612 self.calculate(); |
|
2613 } |
|
2614 s = s || self.sorted; |
|
2615 |
|
2616 addSingle = function(m) { |
|
2617 |
|
2618 if (m) { |
|
2619 group = (m.group && self.groups[m.group]) || NOT_FOUND; |
|
2620 |
|
2621 //Always assume it's async |
|
2622 if (group.async === false) { |
|
2623 m.async = group.async; |
|
2624 } |
|
2625 |
|
2626 url = (m.fullpath) ? self._filter(m.fullpath, s[i]) : |
|
2627 self._url(m.path, s[i], group.base || m.base); |
|
2628 |
|
2629 if (m.attributes || m.async === false) { |
|
2630 url = { |
|
2631 url: url, |
|
2632 async: m.async |
|
2633 }; |
|
2634 if (m.attributes) { |
|
2635 url.attributes = m.attributes; |
|
2636 } |
|
2637 } |
|
2638 resolved[m.type].push(url); |
|
2639 resolved[m.type + 'Mods'].push(m); |
|
2640 } else { |
|
2641 Y.log('Undefined Module', 'warn', 'loader'); |
|
2642 } |
|
2643 |
|
2644 }; |
|
2645 |
|
2646 len = s.length; |
|
2647 |
|
2648 // the default combo base |
|
2649 comboBase = self.comboBase; |
|
2650 |
|
2651 url = comboBase; |
|
2652 |
|
2653 comboSources = {}; |
|
2654 |
|
2655 for (i = 0; i < len; i++) { |
|
2656 comboSource = comboBase; |
|
2657 m = self.getModule(s[i]); |
|
2658 groupName = m && m.group; |
|
2659 group = self.groups[groupName]; |
|
2660 if (groupName && group) { |
|
2661 |
|
2662 if (!group.combine || m.fullpath) { |
|
2663 //This is not a combo module, skip it and load it singly later. |
|
2664 addSingle(m); |
|
2665 continue; |
|
2666 } |
|
2667 m.combine = true; |
|
2668 if (group.comboBase) { |
|
2669 comboSource = group.comboBase; |
|
2670 } |
|
2671 |
|
2672 if ("root" in group && L.isValue(group.root)) { |
|
2673 m.root = group.root; |
|
2674 } |
|
2675 m.comboSep = group.comboSep || self.comboSep; |
|
2676 m.maxURLLength = group.maxURLLength || self.maxURLLength; |
|
2677 } else { |
|
2678 if (!self.combine) { |
|
2679 //This is not a combo module, skip it and load it singly later. |
|
2680 addSingle(m); |
|
2681 continue; |
|
2682 } |
|
2683 } |
|
2684 |
|
2685 comboSources[comboSource] = comboSources[comboSource] || []; |
|
2686 comboSources[comboSource].push(m); |
|
2687 } |
|
2688 |
|
2689 for (j in comboSources) { |
|
2690 if (comboSources.hasOwnProperty(j)) { |
|
2691 resCombos[j] = resCombos[j] || { js: [], jsMods: [], css: [], cssMods: [] }; |
|
2692 url = j; |
|
2693 mods = comboSources[j]; |
|
2694 len = mods.length; |
|
2695 |
|
2696 if (len) { |
|
2697 for (i = 0; i < len; i++) { |
|
2698 if (inserted[mods[i]]) { |
|
2699 continue; |
|
2700 } |
|
2701 m = mods[i]; |
|
2702 // Do not try to combine non-yui JS unless combo def |
|
2703 // is found |
|
2704 if (m && (m.combine || !m.ext)) { |
|
2705 resCombos[j].comboSep = m.comboSep; |
|
2706 resCombos[j].group = m.group; |
|
2707 resCombos[j].maxURLLength = m.maxURLLength; |
|
2708 frag = ((L.isValue(m.root)) ? m.root : self.root) + (m.path || m.fullpath); |
|
2709 frag = self._filter(frag, m.name); |
|
2710 resCombos[j][m.type].push(frag); |
|
2711 resCombos[j][m.type + 'Mods'].push(m); |
|
2712 } else { |
|
2713 //Add them to the next process.. |
|
2714 if (mods[i]) { |
|
2715 addSingle(mods[i]); |
|
2716 } |
|
2717 } |
|
2718 |
|
2719 } |
|
2720 } |
|
2721 } |
|
2722 } |
|
2723 |
|
2724 |
|
2725 for (j in resCombos) { |
|
2726 if (resCombos.hasOwnProperty(j)) { |
|
2727 base = j; |
|
2728 comboSep = resCombos[base].comboSep || self.comboSep; |
|
2729 maxURLLength = resCombos[base].maxURLLength || self.maxURLLength; |
|
2730 Y.log('Using maxURLLength of ' + maxURLLength, 'info', 'loader'); |
|
2731 for (type in resCombos[base]) { |
|
2732 if (type === JS || type === CSS) { |
|
2733 urls = resCombos[base][type]; |
|
2734 mods = resCombos[base][type + 'Mods']; |
|
2735 len = urls.length; |
|
2736 tmpBase = base + urls.join(comboSep); |
|
2737 baseLen = tmpBase.length; |
|
2738 if (maxURLLength <= base.length) { |
|
2739 Y.log('maxURLLength (' + maxURLLength + ') is lower than the comboBase length (' + base.length + '), resetting to default (' + MAX_URL_LENGTH + ')', 'error', 'loader'); |
|
2740 maxURLLength = MAX_URL_LENGTH; |
|
2741 } |
|
2742 |
|
2743 if (len) { |
|
2744 if (baseLen > maxURLLength) { |
|
2745 Y.log('Exceeded maxURLLength (' + maxURLLength + ') for ' + type + ', splitting', 'info', 'loader'); |
|
2746 u = []; |
|
2747 for (s = 0; s < len; s++) { |
|
2748 u.push(urls[s]); |
|
2749 tmpBase = base + u.join(comboSep); |
|
2750 |
|
2751 if (tmpBase.length > maxURLLength) { |
|
2752 m = u.pop(); |
|
2753 tmpBase = base + u.join(comboSep); |
|
2754 resolved[type].push(self._filter(tmpBase, null, resCombos[base].group)); |
|
2755 u = []; |
|
2756 if (m) { |
|
2757 u.push(m); |
|
2758 } |
|
2759 } |
|
2760 } |
|
2761 if (u.length) { |
|
2762 tmpBase = base + u.join(comboSep); |
|
2763 resolved[type].push(self._filter(tmpBase, null, resCombos[base].group)); |
|
2764 } |
|
2765 } else { |
|
2766 resolved[type].push(self._filter(tmpBase, null, resCombos[base].group)); |
|
2767 } |
|
2768 } |
|
2769 resolved[type + 'Mods'] = resolved[type + 'Mods'].concat(mods); |
|
2770 } |
|
2771 } |
|
2772 } |
|
2773 } |
|
2774 |
|
2775 resCombos = null; |
|
2776 |
|
2777 return resolved; |
|
2778 }, |
|
2779 /** |
|
2780 Shortcut to calculate, resolve and load all modules. |
|
2781 |
|
2782 var loader = new Y.Loader({ |
|
2783 ignoreRegistered: true, |
|
2784 modules: { |
|
2785 mod: { |
|
2786 path: 'mod.js' |
|
2787 } |
|
2788 }, |
|
2789 requires: [ 'mod' ] |
|
2790 }); |
|
2791 loader.load(function() { |
|
2792 console.log('All modules have loaded..'); |
|
2793 }); |
|
2794 |
|
2795 |
|
2796 @method load |
|
2797 @param {Function} cb Executed after all load operations are complete |
|
2798 */ |
|
2799 load: function(cb) { |
|
2800 if (!cb) { |
|
2801 Y.log('No callback supplied to load()', 'error', 'loader'); |
|
2802 return; |
|
2803 } |
|
2804 var self = this, |
|
2805 out = self.resolve(true); |
|
2806 |
|
2807 self.data = out; |
|
2808 |
|
2809 self.onEnd = function() { |
|
2810 cb.apply(self.context || self, arguments); |
|
2811 }; |
|
2812 |
|
2813 self.insert(); |
|
2814 } |
|
2815 }; |
|
2816 |
|
2817 |
|
2818 |
|
2819 }, '@VERSION@', {"requires": ["get", "features"]}); |