yui.js revision f6d14b3bf6e08e09949f6a1e7d5431b7829f3550
(function() {
var _instances = {},
// @TODO: this needs to be created at build time from module metadata
'io.xdrReady': 1,
'io.start': 1,
'io.success': 1,
'io.failure': 1,
'io.abort': 1
};
/**
* The YUI global namespace object. If YUI is already defined, the
* existing YUI object will not be overwritten so that defined
* namespaces are preserved.
* @class YUI
* @constructor
* @global
* @uses Event.Target
* @param o configuration object
*/
/*global YUI*/
YUI = function(o) {
var Y = this;
// Allow var yui = YUI() instead of var yui = new YUI()
if (Y == window) {
return new YUI(o);
} else {
// set up the core environment
Y._init(o);
// bind the specified additional modules for this instance
Y._setup();
}
};
}
// The prototype contains the functions that are required to allow the external
// modules to be registered and for the instance to be initialized.
/**
* Initialize this YUI instance
* @param o config options
* @private
*/
_init: function(o) {
// @todo
// loadcfg {
// base
// securebase
// filter
// win
// doc
// debug
// useConsole
// logInclude
// logExclude
// throwFail
// pollInterval
// core
// }
o = o || {};
// find targeted window and @TODO create facades
o.win = w;
// add a reference to o for anything that needs it
// before _setup is called.
this.config = o;
this.Env = {
// @todo expand the new module metadata
mods: {},
_idx: 0,
_pre: 'yuid',
_used: {},
_yidx: 0,
_uidx: 0
};
_instances[this.id] = this;
}
this.constructor = YUI;
},
/**
* Finishes the instance setup. Attaches whatever modules were defined
* when the yui modules was registered.
* @method _setup
* @private
*/
_setup: function(o) {
this.use("yui");
// make a shallow copy of the config. This won't fix nested configs
// so we need to determine if we only allow one level (probably) or
// if we make clone create references for functions and elements.
// this.mix(c, {
// debug: true,
// useConsole: true
// // , throwFail: false
// });
// this.config = c;
// this.publish('yui:load');
// this.publish('yui:log', {
// silent: true
// });
},
/**
* Executes a method on a YUI instance with
* the specified id.
* @method applyTo
* @param id {string} the YUI instance id
* @param method {string} the name of the method to exectute.
* Ex: 'Object.keys'
* @param args {Array} the arguments to apply to the method
* @return the return value from the applied method or null
*/
if (!(method in _APPLY_TO_WHITE_LIST)) {
return null;
}
if (instance) {
m = m[nest[i]];
if (!m) {
}
}
}
return null;
},
/**
* Register a module
* @method add
* @param name {string} module name
* @param namespace {string} name space for the module
* @param fn {Function} entry point into the module that
* is used to bind module to the YUI instance
* @param version {string} version string
* @return {YUI} the YUI instance
*
* requires - features that should be present before loading
* optional - optional features that should be present if load optional defined
* use - features that should be attached automatically
* skinnable -
* rollup
* omit - features that should not be loaded if this module is present
*/
// this.log('Adding a new component ' + name);
// @todo expand this to include version mapping
// @todo allow requires/supersedes
// @todo may want to restore the build property
// @todo fire moduleAvailable event
var m = {
};
return this; // chain support
},
/**
* Bind a module to a YUI instance
* @param modules* {string} 1-n modules to bind (uses arguments array)
* @param *callback {function} callback function executed when
* the instance has the required functionality. If included, it
* must be the last parameter.
*
* @TODO
* Implement versioning? loader can load different versions?
* Should sub-modules/plugins be normal modules, or do
* we add syntax for specifying these?
*
* YUI().use('dragdrop')
* YUI().use('dragdrop:2.4.0'); // specific version
* YUI().use('dragdrop:2.4.0-'); // at least this version
* YUI().use('dragdrop:2.4.0-2.9999.9999'); // version range
* YUI().use('*'); // use all available modules
* YUI().use('lang+dump+substitute'); // use lang and some plugins
* YUI().use('lang+*'); // use lang and all known plugins
*
*
* @return {YUI} the YUI instance
*/
use: function() {
var Y = this,
firstArg = a[0],
dynamic = false,
sorted = false,
// The last argument supplied to use can be a load complete callback
if (typeof callback === 'function') {
a.pop();
} else {
callback = null;
}
// YUI().use('*'); // bind everything available
if (firstArg === "*") {
a = [];
for (var k in mods) {
if (mods.hasOwnProperty(k)) {
a.push(k);
}
}
if (callback) {
}
// Accept a loader instance with a pre-sorted list of dependencies
sorted = true;
}
// Y.log('loader before: ' + a.join(','));
// use loader to optimize and sort the requirements if it
// is available.
dynamic = true;
loader.ignoreRegistered = true;
}
// Y.log('loader after: ' + a.join(','));
// only attach a module once
// Y.log(name + ' already used');
return;
}
if (m) {
if (dynamic) {
// Y.mix(l, YUI.Env.mods);
m.fn(Y);
}
// Y.log('found ' + name);
} else {
}
// make sure requirements are attached
if (req) {
f(req);
} else {
f(req[j]);
}
}
}
// add this module to full list of things to attach
// Y.log('using ' + name);
// auto-attach sub-modules
if (use) {
f(req);
} else {
f(use[j]);
}
}
}
};
// process each requirement and any additional requirements
// the module metadata specifies
f(a[i]);
}
// Y.log('all reqs: ' + r + ' --- missing: ' + missing);
var attach = function(fromLoader) {
// Y.log('ATTACH ' + arguments[0]);
if (!fromLoader) {
var m = mods[r[i]];
if (m) {
m.fn(Y);
}
}
}
if (callback) {
callback(Y);
}
if (Y.fire) {
Y.fire('yui:load', Y);
}
};
// dynamic load
// loader calls use to automatically attach when finished
// but we still need to execute the callback.
// loader.subscribe('failure', function() {
// Y.log('asdf');
// });
} else {
attach();
}
return Y; // chain support var yui = YUI().use('dragdrop');
},
/**
* Returns the namespace specified and creates it if it doesn't exist
* <pre>
* YUI.namespace("property.package");
* YUI.namespace("YUI.property.package");
* </pre>
* Either of the above would create YUI.property, then
* YUI.property.package
*
* Be careful when naming packages. Reserved words may work in some browsers
* and not others. For instance, the following will fail in Safari:
* <pre>
* YUI.namespace("really.long.nested.namespace");
* </pre>
* This fails because "long" is a future reserved word in ECMAScript
*
* @method namespace
* @static
* @param {String*} arguments 1-n namespaces to create
* @return {Object} A reference to the last namespace object created
*/
namespace: function() {
var a=arguments, o=null, i, j, d;
d = a[i].split(".");
o = this;
o[d[j]] = o[d[j]] || {};
o = o[d[j]];
}
}
return o;
},
// this is replaced if the log module is included
log: function() {
},
/**
* Report an error. The reporting mechanism is controled by
* the 'throwFail' configuration attribute. If throwFail is
* not specified, the message is written to the logger, otherwise
* a JS error is thrown
* @method fail
* @param msg {string} the failure message
* @param e {Error} Optional JS error that was caught. If supplied
* and throwFail is specified, this error will be re-thrown.
* @return {YUI} this YUI instance
*/
}
return this;
},
/**
* Generate an id that is unique among all YUI instances
* @method guid
* @param pre {string} optional guid prefix
* @return {string} the guid
*/
},
/**
* Stamps an object with a guid. If the object already
* has one, a new one is not created
* @method stamp
* @param o The object to stamp
* @return {string} The object's guid
*/
stamp: function(o) {
if (!o) {
return o;
}
if (!uid) {
}
return uid;
}
};
// Give the YUI global the same properties as an instance.
// This makes it so that the YUI global can be used like the YAHOO
// global was used prior to 3.x. More importantly, the YUI global
// provides global metadata, so env needs to be configured.
// @TODO review
// inheritance utilities are not available yet
for (i in p) {
if (true) { // hasOwnProperty not available yet and not needed
Y[i] = p[i];
}
}
// set up the environment
Y._init();
})();