loader.js revision f6d14b3bf6e08e09949f6a1e7d5431b7829f3550
/**
* Provides dynamic loading for the YUI library. It includes the dependency
* info for the library, and will automatically pull in dependencies for
* the modules requested. It supports rollup files (such as utilities.js
* and yahoo-dom-event.js), and will automatically use these when
* appropriate in order to minimize the number of http connections
* required to load all of the dependencies.
*/
/**
* YUILoader provides dynamic loading for YUI.
* @class Loader
* @TODO version management
*/
var BASE = 'base',
CSS = 'css',
JS = 'js',
PKG = 'pkg',
ANIMATION = 'animation',
RESET = 'reset',
FONTS = 'fonts',
GRIDS = 'grids',
DRAGDROP = 'dragdrop',
JSON = 'json',
VERSION = '@VERSION@',
// skin: {
// defaultSkin: 'sam',
// path: 'skin.css',
// after: [RESET, FONTS, GRIDS, BASE],
// rollup: 3
// },
modules: {
basecss: {
},
fonts: {
},
grids: {
},
log: {
path: 'log/logreader-min.js',
skinnable: 1
},
reset: {
},
'reset-fonts-grids': {
rollup: 4
},
'reset-fonts': {
rollup: 2
},
// node, dom, event included in the yui dist, so we are not including the metadata for PR1
animation: {
requires: ['base']
},
attribute: { },
base: {
requires: ['attribute']
},
classnamemanager: { },
compat: { },
cookie: { },
css: { },
'dd-ddm-base': {
path: 'dd/dd-ddm-base-min.js',
},
'dd-ddm':{
path: 'dd/dd-ddm-min.js',
requires: ['dd-ddm-base']
},
'dd-ddm-drop':{
path: 'dd/dd-ddm-drop-min.js',
requires: ['dd-ddm']
},
'dd-drag':{
path: 'dd/dd-drag-min.js',
requires: ['dd-ddm-base']
},
'dd-drop':{
path: 'dd/dd-drop-min.js',
requires: ['dd-ddm-drop']
},
'dd-proxy':{
path: 'dd/dd-proxy-min.js',
requires: ['dd-drag']
},
'dd-constrain':{
},
'dd-plugin':{
path: 'dd/dd-plugin-min.js',
requires: ['dd-drag'],
},
'dd-drop-plugin':{
requires: ['dd-drop']
},
'dd-drag-all':{
path: 'dd/dd-drag-all-min.js',
supersedes: ['dd-ddm-base', 'dd-ddm', 'dd-drag', 'dd-proxy', 'dd-constrain', 'dd-plugin', 'dd-drag-core', 'dd-drag-proxy']
},
'dd-dragdrop-all':{
supersedes: ['dd-ddm-base', 'dd-ddm', 'dd-ddm-drop', 'dd-drag', 'dd-proxy', 'dd-constrain', 'dd-plugin', 'dd-drop', 'dd-drop-plugin', 'dd-drag-core', 'dd-drag-proxy']
},
'dd-drop-core':{
},
'dd-drag-core':{
},
'dd-drag-proxy':{
},
dump: { },
io: { },
'json-parse': {
},
'json-stringify': {
},
json: {
},
logreader: {
requires: ['css']
},
profiler: { },
queue: { },
substitute: {
optional: ['dump']
},
yuitestcore: {
},
yuitest: {
requires: ['log'],
// skinnable: 1,
supersedes: ['yuitestcore']
}
}
};
REQ = "expanded";
var _Y = {
// dupsAllowed: {'yahoo': true, 'get': true},
dupsAllowed: {},
/*
* The library metadata for the current release
* @property YUIInfo
* @static
*/
// info: '@yuiinfo@',
};
Y.Loader = function(o) {
/**
* Internal callback to handle multiple internal insert() calls
* so that css is inserted prior to js
* @property _internalCallback
* @private
*/
this._internalCallback = null;
/**
* Use the YUI environment listener to detect script load. This
* is only switched on for Safari 2.x and below.
* @property _useYahooListener
* @private
*/
this._useYahooListener = false;
/*
* Callback that will be executed when the loader is finished
* with an insert
* @method onSuccess
* @type function
*/
// this.onSuccess = null;
/*
* Callback that will be executed if there is a failure
* @method onFailure
* @type function
*/
// this.onFailure = Y.log;
/*
* Callback that will be executed each time a new module is loaded
* @method onProgress
* @type function
*/
// this.onProgress = null;
/**
* The execution scope for all callbacks
* @property scope
* @default this
*/
this.scope = this;
/**
* Data that is passed to all callbacks
* @property data
*/
this.data = null;
/**
* Node reference or id where new nodes should be inserted before
* @property insertBefore
* @type string|HTMLElement
*/
this.insertBefore = null;
/**
* The charset attribute for inserted nodes
* @property charset
* @type string
* @default utf-8
*/
this.charset = null;
/**
* The name of a script node
* (for external script support in Safari 2.x and earlier)
* to reference when the load is complete. If this variable
* is not available in the specified scripts, the operation will
* fail.
* @property varName
* @type string
*/
this.varName = null;
/**
* The base directory.
* @property base
* @type string
* @default http://yui.yahooapis.com/[YUI VERSION]/build/
*/
/**
* Base path for the combo service
* @property comboBase
* @type string
* @default http://yui.yahooapis.com/combo?
*/
/**
* If configured, YUI JS resources will use the combo
* handler
* @property combine
* @type boolean
* @default false
*/
this.combine = false;
/**
* Ignore modules registered on the YUI global
* @property ignoreRegistered
* @default false
*/
this.ignoreRegistered = false;
/**
* Root path to prepend to module path for the combo
* service
* @property root
* @type string
* @default [YUI VERSION]/build/
*/
/**
* Timeout value in milliseconds. If set, this value will be used by
* the get utility. the timeout event will fire if
* a timeout occurs.
* @property timeout
* @type int
*/
this.timeout = 0;
/**
* A list of modules that should not be loaded, even if
* they turn up in the dependency tree
* @property ignore
* @type string[]
*/
this.ignore = null;
/**
* A list of modules that should always be loaded, even
* if they have already been inserted into the page.
* @property force
* @type string[]
*/
this.force = null;
/**
* Should we allow rollups
* @property allowRollup
* @type boolean
* @default true
*/
this.allowRollup = true;
/**
* A filter to apply to result urls. This filter will modify the default
* path for all modules. The default path for the YUI library is the
* minified version of the files (e.g., event-min.js). The filter property
* can be a predefined filter or a custom filter. The valid predefined
* filters are:
* <dl>
* <dt>DEBUG</dt>
* <dd>Selects the debug versions of the library (e.g., event-debug.js).
* This option will automatically include the logger widget</dd>
* <dt>RAW</dt>
* <dd>Selects the non-minified version of the library (e.g., event.js).
* </dl>
* You can also define a custom filter, which must be an object literal
* containing a search expression and a replace string:
* <pre>
* myFilter: {
* 'searchExp': "-min\\.js",
* 'replaceStr': "-debug.js"
* }
* </pre>
* @property filter
* @type string|{searchExp: string, replaceStr: string}
*/
this.filter = null;
/**
* The list of requested modules
* @property required
* @type {string: boolean}
*/
this.required = {};
/**
* The library metadata
* @property moduleInfo
*/
// this.moduleInfo = Y.merge(_Y.info.moduleInfo);
this.moduleInfo = {};
for (var i in defaults) {
if (defaults.hasOwnProperty(i)) {
this._internal = true;
this._internal = false;
}
}
/**
* List of rollup files found in the library metadata
* @property rollups
*/
this.rollups = null;
/**
* Whether or not to load optional dependencies for
* the requested modules
* @property loadOptional
* @type boolean
* @default false
*/
this.loadOptional = false;
/**
* All of the derived dependencies in sorted order, which
* will be populated when either calculate() or insert()
* is called
* @property sorted
* @type string[]
*/
this.sorted = [];
/**
* Set when beginning to compute the dependency tree.
* Composed of what YUI reports to be loaded combined
* with what has been loaded by the tool
* @propery loaded
* @type {string: boolean}
*/
this.loaded = {};
/**
* Flag to indicate the dependency tree needs to be recomputed
* if insert is called again.
* @property dirty
* @type boolean
* @default true
*/
this.dirty = true;
/**
* List of modules inserted by the utility
* @property inserted
* @type {string: boolean}
*/
this.inserted = {};
// Y.on('yui:load', this.loadNext, this);
this._config(o);
};
FILTERS: {
RAW: {
'searchExp': "-min\\.js",
'replaceStr': ".js"
},
DEBUG: {
'searchExp': "-min\\.js",
'replaceStr': "-debug.js"
}
},
_config: function(o) {
// apply config values
if (o) {
for (var i in o) {
if (Y.Object.owns(o, i)) {
if (i == "require") {
this.require(o[i]);
// support the old callback syntax
} else {
this[i] = o[i];
}
}
}
}
// fix filter
var f = this.filter;
if (L.isString(f)) {
f = f.toUpperCase();
// the logger must be available in order to use the debug
// versions of the library
if (f === "DEBUG") {
this.require("log");
}
// hack to handle a a bug where LogWriter is being instantiated
// at load time, and the loader has no way to sort above it
// at the moment.
if (!Y.LogWriter) {
Y.LogWriter = function() {
return Y;
};
}
}
},
/** Add a new module to the component metadata.
* <dl>
* <dt>name:</dt> <dd>required, the component name</dd>
* <dt>type:</dt> <dd>required, the component type (js or css)</dd>
* <dt>path:</dt> <dd>required, the path to the script from "base"</dd>
* <dt>requires:</dt> <dd>array of modules required by this component</dd>
* <dt>optional:</dt> <dd>array of optional modules for this component</dd>
* <dt>supersedes:</dt> <dd>array of the modules this component replaces</dd>
* <dt>after:</dt> <dd>array of modules the components which, if present, should be sorted above this one</dd>
* <dt>rollup:</dt> <dd>the number of superseded modules required for automatic rollup</dd>
* <dt>fullpath:</dt> <dd>If fullpath is specified, this is used instead of the configured base + path</dd>
* <dt>skinnable:</dt> <dd>flag to determine if skin assets should automatically be pulled in</dd>
* </dl>
* @method addModule
* @param o An object containing the module data
* @param name the module name (optional), required if not in the module data
* @return {boolean} true if the module was added, false if
* the object passed in did not provide all required attributes
*/
if (!o || !o.name) {
return false;
}
if (!o.type) {
}
}
this.moduleInfo[name] = o;
this.dirty = true;
//Y.log('New module ' + name);
return o;
},
/**
* Add a requirement for one or more module
* @method require
* @param what {string[] | string*} the modules to load
*/
this.dirty = true;
//OU.appendArray(this.required, a);
},
/**
* Returns an object containing properties for all modules required
* in order to load the requested module
* @method getRequires
* @param mod The module definition from moduleInfo
*/
getRequires: function(mod) {
if (!mod) {
// Y.log('getRequires, no module');
return [];
}
// Y.log('already expanded');
}
// Y.log(mod.name + ' requiring ' + r[i]);
d.push(r[i]);
m = this.getModule(r[i]);
// AU.appendArray(d, this.getRequires(m));
// d.concat(this.getRequires(m));
// Y.log(d);
add = this.getRequires(m);
}
}
// get the requirements from superseded modules, if any
r=mod.supersedes;
if (r) {
// Y.log(mod.name + ' requiring ' + r[i]);
d.push(r[i]);
m = this.getModule(r[i]);
// AU.appendArray(d, this.getRequires(m));
// d.concat(this.getRequires(m));
// Y.log(d);
add = this.getRequires(m);
}
}
}
if (o && this.loadOptional) {
d.push(o[i]);
// AU.appendArray(d, this.getRequires(info[o[i]]));
// d.concat(this.getRequires(info[o[i]]));
}
}
}
// Y.log(d);
// mod.expanded = AU.uniq(d);
// Y.log(mod.expanded);
},
/**
* Returns an object literal of the modules the supplied module satisfies
* @method getProvides
* @param name{string} The name of the module
* @param notMe {string} don't add this module name, only include superseded modules
* @return what this module provides
*/
if (!m) {
return o;
}
if (m[ckey]) {
// Y.log('cached: ' + name + ' ' + ckey + ' ' + L.dump(this.moduleInfo[name][ckey], 0));
return m[ckey];
}
// use worker to break cycles
// Y.log(name + ' provides worker trying: ' + mm);
// we always want the return value normal behavior
// (provides) for superseded modules.
}
// else {
// Y.log(name + ' provides worker skipping done: ' + mm);
// }
};
// calculate superseded modules
if (s) {
add(s[i]);
}
}
// supersedes cache
m[SUPER] = o;
// provides cache
// Y.log(name + " supersedes " + L.dump(m[SUPER], 0));
// Y.log(name + " provides " + L.dump(m[PROV], 0));
return m[ckey];
},
/**
* Calculates the dependency tree, the result is stored in the sorted
* property
* @method calculate
* @param o optional options object
*/
calculate: function(o) {
if (o || this.dirty) {
this._config(o);
this._setup();
this._explode();
if (this.allowRollup) {
this._rollup();
}
this._reduce();
this._sort();
// Y.log("after calculate: " + this.sorted);
this.dirty = false;
}
},
/**
* Investigates the current YUI configuration on the page. By default,
* modules already detected will not be loaded again unless a force
* option is encountered. Called by calculate()
* @method _setup
* @private
*/
_setup: function() {
// available modules
if (!this.ignoreRegistered) {
}
// Y.log("Already loaded stuff: " + L.dump(l, 0));
// add the ignore list to the list of loaded packages
if (this.ignore) {
// OU.appendArray(l, this.ignore);
}
// remove modules on the force list from the loaded list
if (this.force) {
if (this.force[i] in l) {
delete l[this.force[i]];
}
}
}
// expand the list to include superseded modules
for (j in l) {
// Y.log("expanding: " + j);
if (Y.Object.owns(l, j)) {
Y.mix(l, this.getProvides(j));
}
}
// Y.log("loaded expanded: " + L.dump(l, 0));
this.loaded = l;
},
/**
* Inspects the required modules list looking for additional
* dependencies. Expands the required list to include all
* required modules. Called by calculate()
* @method _explode
* @private
*/
_explode: function() {
for (i in r) {
if (r.hasOwnProperty(i)) {
// Y.log('exploding ' + i);
if (req) {
// Y.log('via explode: ' + req);
}
}
}
},
var m = this.moduleInfo[name];
// create the default module
// if (!m) {
// Y.log('Module does not exist: ' + name + ', creating with defaults');
// m = this.addModule({ext: false}, name);
// }
return m;
},
/**
* Look for rollup packages to determine if all of the modules a
* rollup supersedes are required. If so, include the rollup to
* help reduce the total number of connections required. Called
* by calculate()
* @method _rollup
* @private
*/
_rollup: function() {
info = this.moduleInfo;
// find and cache rollup modules
for (i in info) {
if (info.hasOwnProperty(i)) {
m = this.getModule(i);
// if (m && m.rollup && m.supersedes) {
if (m && m.rollup) {
rollups[i] = m;
}
}
}
}
// make as many passes as needed to pick up rollup rollups
for (;;) {
var rolled = false;
// go through the rollup candidates
for (i in rollups) {
// there can be only one
if (!r[i] && !this.loaded[i]) {
if (!m.rollup) {
continue;
}
// check the threshold
// if the superseded module is loaded, we can't load the rollup
roll = false;
break;
// increment the counter if this module is required. if we are
// beyond the rollup threshold, we will use the rollup module
} else if (r[s[j]]) {
c++;
if (roll) {
// Y.log("over thresh " + c + ", " + L.dump(r));
break;
}
}
}
if (roll) {
// Y.log("rollup: " + i + ", " + L.dump(this, 1));
// add the rollup
r[i] = true;
rolled = true;
// expand the rollup's dependencies
this.getRequires(m);
}
}
}
// if we made it here w/o rolling up something, we are done
if (!rolled) {
break;
}
}
},
/**
* Remove superceded modules and loaded modules. Called by
* calculate() after we have the mega list of all dependencies
* @method _reduce
* @private
*/
_reduce: function() {
var i, j, s, m, r=this.required;
for (i in r) {
// remove if already loaded
if (i in this.loaded) {
delete r[i];
// remove anything this module supersedes
} else {
m = this.getModule(i);
s = m && m.supersedes;
if (s) {
if (s[j] in r) {
delete r[s[j]];
}
}
}
}
}
},
_onFailure: function(msg) {
this.fire('failure', {
});
},
_onTimeout: function(msg) {
this.fire('timeout', {
});
},
/**
* Sorts the dependency tree. The last step of calculate()
* @method _sort
* @private
*/
_sort: function() {
// create an indexed list
me = this;
// returns true if b is not loaded, and is required
// directly or by means of modules it supersedes.
return false;
}
// check if this module requires the other directly
return true;
}
// check if this module should be sorted after the other
return true;
}
// check if this module requires one the other supersedes
if (ss) {
return true;
}
}
}
// external css files should be sorted below yui css
return true;
}
return false;
};
// pointer to the first unsorted item
var p=0;
// keep going until we make a pass without moving anything
for (;;) {
// start the loop after items that are already sorted
for (j=p; j<l; j=j+1) {
// check the next module on the list to see if its
// dependencies have been met
a = s[j];
// check everything below current item and move if we
// find a requirement for the current item
for (k=j+1; k<l; k=k+1) {
if (requires(a, s[k])) {
// extract the dependency so we can move it up
b = s.splice(k, 1);
// insert the dependency above the item that
// requires it
moved = true;
break;
}
}
// jump out of loop if we moved something
if (moved) {
break;
// this item is sorted, move our pointer and keep going
} else {
p = p + 1;
}
}
// when we make it here and moved is false, we are
// finished sorting
if (!moved) {
break;
}
}
this.sorted = s;
},
/**
* inserts the requested modules and their dependencies.
* <code>type</code> can be "js" or "css". Both script and
* css are inserted if type is not provided.
* @method insert
* @param o optional options object
* @param type {string} the type of dependency to insert
*/
// if (o) {
// Y.log("insert: " + L.dump(o, 1) + ", " + type);
// } else {
// Y.log("insert: " + this.toString() + ", " + type);
// }
// build the dependency list
this.calculate(o);
if (!type) {
// Y.log("trying to load css first");
var self = this;
this._internalCallback = function() {
self._internalCallback = null;
};
return;
}
// set a flag to indicate the load has started
this._loading = true;
// flag to indicate we are done with the combo service
// and any additional files will need to be loaded
// individually
this._combineComplete = false;
// keep the loadType (js, css or undefined) cached
// start the load
this.loadNext();
},
/**
* Executed every time a module is loaded, and if we are in a load
* cycle, we attempt to load the next script. Public so that it
* is possible to call this if using a method other than
* Y.register to determine when scripts are fully loaded
* @method loadNext
* @param mname {string} optional the name of the module that has
* been loaded (which is usually why it is time to load the next
* one)
*/
// It is possible that this function is executed due to something
// else one the page loading a YUI module. Only react when we
// are actively loading something
if (!this._loading) {
return;
}
if (this.combine && !this._combineComplete) {
this._combining = [];
m = this.getModule(s[i]);
// @TODO we can't combine CSS yet until we deliver files with absolute paths to the assets
// Do not try to combine non-yui JS
if (i < len) {
url += '&';
}
this._combining.push(s[i]);
}
}
if (this._combining.length) {
var self=this,
c=function(o) {
self._combineComplete = true;
}
};
// @TODO get rid of the redundant Get code
data: s[i],
onSuccess: c,
onFailure: this._onFailure,
onTimeout: this._onTimeout,
insertBefore: this.insertBefore,
});
return;
} else {
this._combineComplete = true;
}
}
if (mname) {
// if the module that was just loaded isn't what we were expecting,
// continue to wait
return;
}
// The global handler that is called when each module is loaded
// will pass that module name to this function. Storing this
// data to avoid loading the same module multiple times
this.fire('progress', {
});
}
// this.inserted keeps track of what the loader has loaded.
// move on if this item is done.
if (s[i] in this.inserted) {
// Y.log(s[i] + " alread loaded ");
continue;
}
// Because rollups will cause multiple load notifications
// from Y, loadNext may be called multiple times for
// the same module when loading a rollup. We can safely
// skip the subsequent requests
if (s[i] === this._loading) {
return;
}
// log("inserting " + s[i]);
m = this.getModule(s[i]);
if (!m) {
var msg = "undefined module " + m;
this.fire('failure', {
});
return;
}
// The load type is stored to offer the possibility to load
// the css separately from the script.
this._loading = s[i];
c=function(o) {
// Y.log('loading next, just loaded' + o.data);
};
data: s[i],
onSuccess: c,
insertBefore: this.insertBefore,
onFailure: this._onFailure,
onTimeout: this._onTimeout,
});
return;
}
}
// we are finished
this._loading = null;
// internal callback for loading css first
if (this._internalCallback) {
var f = this._internalCallback;
this._internalCallback = null;
f.call(this);
// } else if (this.onSuccess) {
} else {
this._pushEvents();
// call Y.use passing this instance. Y will use the sorted
// dependency list.
Y.use(this);
this.fire('success', {
});
}
},
/**
* In IE, the onAvailable/onDOMReady events need help when Event is
* loaded dynamically
* @method _pushEvents
* @param {Function} optional function reference
* @private
*/
_pushEvents: function(ref) {
var r = ref || Y;
if (r.Event) {
}
},
/**
* Generates the full url for a module
* method _url
* @param path {string} the path fragment
* @return {string} the full url
* @private
*/
u = u + path;
if (f) {
// Y.log("filter: " + f + ", " + f.searchExp +
// ", " + f.replaceStr);
}
// Y.log(u);
return u;
}
};
}, "@VERSION@");