yui-base.js revision 173310d032abe522e8645dd148cc28591cd128ea
0fdefaa9ca017edfb76b736c825b34186f33045aTripp * The YUI module contains the components required for building the YUI seed
0fdefaa9ca017edfb76b736c825b34186f33045aTripp * file. This includes the script loading mechanism, a simple queue, and
0fdefaa9ca017edfb76b736c825b34186f33045aTripp * the core utilities for the library.
0fdefaa9ca017edfb76b736c825b34186f33045aTripp * @module yui
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @submodule yui-base
a89ad754cce3cfc8aee71760e10217b54020360dTripp * The YUI global namespace object. If YUI is already defined, the
a89ad754cce3cfc8aee71760e10217b54020360dTripp * existing YUI object will not be overwritten so that defined
a89ad754cce3cfc8aee71760e10217b54020360dTripp * namespaces are preserved. It is the constructor for the object
a89ad754cce3cfc8aee71760e10217b54020360dTripp * the end user interacts with. As indicated below, each instance
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * has full custom event support, but only if the event system
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * is available.
c093c1aed867e18aa4778708592e1ceb45d18cffTripp * @class YUI
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @constructor
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @uses EventTarget
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @param o* {object} 0..n optional configuration objects. these values
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * are store in Y.config. See config for the list of supported
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * properties.
c093c1aed867e18aa4778708592e1ceb45d18cffTripp /*global YUI*/
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp /*global YUI_config*/
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var YUI = function() {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp return (o && o.hasOwnProperty && (o instanceof type));
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp gconf = (typeof YUI_config !== 'undefined') && YUI_config;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y = new YUI();
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp // set up the core environment
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp // YUI.GlobalConfig is a master configuration that might span
a89ad754cce3cfc8aee71760e10217b54020360dTripp // multiple contexts in a non-browser environment. It is applied
a89ad754cce3cfc8aee71760e10217b54020360dTripp // first to all instances in all contexts.
a89ad754cce3cfc8aee71760e10217b54020360dTripp // YUI_Config is a page-level config. It is applied to all
a89ad754cce3cfc8aee71760e10217b54020360dTripp // instances created on the page. This is applied after
a89ad754cce3cfc8aee71760e10217b54020360dTripp // YUI.GlobalConfig, and before the instance level configuration
a89ad754cce3cfc8aee71760e10217b54020360dTripp // objects.
a89ad754cce3cfc8aee71760e10217b54020360dTripp // bind the specified additional modules for this instance
a89ad754cce3cfc8aee71760e10217b54020360dTripp // Each instance can accept one or more configuration objects.
a89ad754cce3cfc8aee71760e10217b54020360dTripp // These are applied after YUI.GlobalConfig and YUI_Config,
7947db4b7d8682ea81598e3a4283e659a8103be6Tripp // overriding values set in those config files if there is a '
a89ad754cce3cfc8aee71760e10217b54020360dTripp // matching property.
a89ad754cce3cfc8aee71760e10217b54020360dTripp for (; i < l; i++) {
a89ad754cce3cfc8aee71760e10217b54020360dTripp(function() {
a89ad754cce3cfc8aee71760e10217b54020360dTripp NOOP = function() {},
a89ad754cce3cfc8aee71760e10217b54020360dTripp APPLY_TO_AUTH = { 'io.xdrReady': 1, // the functions applyTo
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp // this can throw an uncaught exception in FF
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp } catch (ex) {}
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp handleLoad = function() {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp getLoader = function(Y, o) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp clobber = function(r, s) {
c093c1aed867e18aa4778708592e1ceb45d18cffTripp for (var i in s) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp r[i] = s[i];
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp// Stamp the documentElement (HTML) with a class of "yui-loaded" to
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp// enable styles that need to key off of JS being enabled.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Applies a new configuration object to the YUI instance config.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * This will merge new group/module definitions, and will also
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * update the loader cache if necessary. Updating Y.config directly
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * will not update the cache.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method applyConfig
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @param {object} the configuration object.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @since 3.2.0
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp applyConfig: function(o) {
c093c1aed867e18aa4778708592e1ceb45d18cffTripp for (name in o) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp // preserve the guid
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp _config: function(o) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Initialize this YUI instance
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp _init: function() {
a89ad754cce3cfc8aee71760e10217b54020360dTripp * The version number of the YUI instance.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property version
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type string
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp // bootstrapped: false,
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp // get from querystring
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp nodes = (doc && doc.getElementsByTagName('script')) || [];
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // this is to set up the path to the loader. The file
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp // filter for loader should match the yui include.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // extract correct path for mixed combo urls
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp // use CDN default
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp // configuration defaults
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * Finishes the instance setup. Attaches whatever modules were defined
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * when the yui modules was registered.
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @method _setup
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp _setup: function(o) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var i, Y = this,
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp 'intl-base',
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp 'yui-later',
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp 'yui-throttle'];
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Executes a method on a YUI instance with
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * the specified id if the specified method is whitelisted.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method applyTo
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @param id {string} the YUI instance id.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @param method {string} the name of the method to exectute.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Ex: 'Object.keys'.
117557654069e56f5003be755819b76fe0f77107Tripp * @param args {Array} the arguments to apply to the method.
117557654069e56f5003be755819b76fe0f77107Tripp * @return {object} the return value from the applied method or null.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp this.log(method + ': applyTo not allowed', 'warn', 'yui');
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp return null;
a89ad754cce3cfc8aee71760e10217b54020360dTripp this.log('applyTo not found: ' + method, 'warn', 'yui');
a89ad754cce3cfc8aee71760e10217b54020360dTripp return null;
a89ad754cce3cfc8aee71760e10217b54020360dTripp * Registers a module with the YUI global. The easiest way to create a
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * first-class YUI module is to use the YUI component build tool.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * The build system will produce the YUI.add wrapper for you module, along
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * with any configuration info required for the module.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method add
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param name {string} module name.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param fn {Function} entry point into the module that
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * is used to bind module to the YUI instance.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param version {string} version string.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param details {object} optional config data:
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * requires: features that must be present before this module can be
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * attached.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * optional: optional features that should be present if loadOptional
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * is defined. Note: modules are not often loaded this way in YUI 3,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * but this field is still useful to inform the user that certain
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * features in the component will require additional dependencies.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * use: features that are included within this module which need to
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * be attached automatically when this module is attached. This
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * supports the YUI 3 rollup system -- a module with submodules
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * defined will need to have the submodules listed in the 'use'
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * config. The YUI component build tool does this for you.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @return {YUI} the YUI instance.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp for (i in instances) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp return this;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Executes the function associated with each required
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * module, binding the module to the YUI instance.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method _attach
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp Y = this, j,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (!done[r[i]]) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp return false;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp } catch (e) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp return false;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp return false;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp return true;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Attaches one or more modules to the YUI instance. When this
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * is executed, the requirements are analyzed, and one of
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * several things can happen:
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * - All requirements are available on the page -- The modules
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * are attached to the instance. If supplied, the use callback
cec703a844d9691646231634fe709f4ea41d278aTripp * is executed synchronously.
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * - Modules are missing, the Get utility is not available OR
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp * the 'bootstrap' config is false -- A warning is issued about
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp * the missing modules and all available modules are attached.
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp * - Modules are missing, the Loader is not available but the Get
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp * utility is and boostrap is not false -- The loader is bootstrapped
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp * before doing the following....
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp * - Modules are missing and the Loader is available -- The loader
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp * expands the dependency tree and fetches missing modules. When
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp * the loader is finshed the callback supplied to use is executed
6c4ec9d420df654d019b936fd06bef6f769db4cbTripp * asynchronously.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param modules* {string} 1-n modules to bind (uses arguments array).
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @param *callback {function} callback function executed when
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * the instance has the required functionality. If included, it
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * must be the last parameter.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * // loads and attaches drag and drop and its dependencies
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * YUI().use('dd', function(Y) {});
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * // attaches all modules that are available on the page
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * YUI().use('*', function(Y) {});
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * // intrinsic YUI gallery support (since 3.1.0)
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * YUI().use('gallery-yql', function(Y) {});
bbd1285cbb2183b7f89010412ad97ae1680b4b5eTripp * // intrinsic YUI 2in3 support (since 3.1.0)
bbd1285cbb2183b7f89010412ad97ae1680b4b5eTripp * YUI().use('yui2-datatable', function(Y) {});.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @return {YUI} the YUI instance.
9eaaa502227248d304ac9170902697d02158c1d9Tripp use: function() {
4ef2f7e4cb7c7d255be077c47d542199f7bf8607Tripp // The last argument supplied to use can be a load complete callback
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp } catch (e) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if (!this.Array) {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp // add this module to full list of things to attach
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp // only attach a module once
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // CSS files don't register themselves, see if it has
a89ad754cce3cfc8aee71760e10217b54020360dTripp // been loaded
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp // make sure requirements are attached
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp // make sure we grab the submodule dependencies too
Y._loading = false;
if (data) {
missing = [];
if (redo) {
redo = false;
Y._loading = false;
if (data) {
if (ret) {
if (ret) {
handleLoader();
if (len) {
Y._loading = true;
onEnd: function(o) {
handleLoader(o);
Y._loading = true;
handleBoot = function() {
Y._loading = false;
if (ret) {
handleLoader();
namespace: function() {
for (; i < a.length; i++) {
arg = a[i];
var Y = this, ret;
var uid;
if (!uid) {
if (!readOnly) {
uid = null;
return uid;
destroy: function() {
if (Y.Event) {
delete Y.Env;
delete Y.config;
if (hasWin) {
handleLoad();
* The YUI combo service base dir. Ex: http://yui.yahooapis.com/combo?
* minified version of the files (e.g., event-min.js). The filter property
* <dd>Selects the debug versions of the library (e.g., event-debug.js).
* <dd>Selects the non-minified version of the library (e.g., event.js).</dd>
* 'replaceStr': "-debug.js"
* fullpath: 'http://myserver.mydomain.com/mymod1/mymod1.js'
* fullpath: 'http://myserver.mydomain.com/mymod2/mymod2.js'
* base: 'http://yui.yahooapis.com/2.8.0r4/build/',
* comboBase: 'http://yui.yahooapis.com/combo?',
* path: "yahoo-dom-event/yahoo-dom-event.js"
* path: "animation/animation.js",
* @default loader/loader-min.js
var L = Y.Lang,
TYPES = {
TRIMREGEX = /^\s+|\s+$/g,
SUBREGEX = /\{\s*([^\|\}]+?)\s*(?:\|([^\}]*))?\s*\}/g;
L.isArray = function(o) {
L.isBoolean = function(o) {
return typeof o === BOOLEAN;
L.isFunction = function(o) {
L.isDate = function(o) {
L.isNull = function(o) {
L.isNumber = function(o) {
return (o && (t === OBJECT ||
L.isString = function(o) {
return typeof o === STRING;
L.isUndefined = function(o) {
return typeof o === UNDEFINED;
L.trim = function(s) {
L.isValue = function(o) {
var t = L.type(o);
case NUMBER:
return isFinite(o);
case NULL:
case UNDEFINED:
L.type = function(o) {
L.sub = function(s, o) {
l = o.length;
Y.Array = YArray;
f.call(o || Y, a[i], i, a);
function(a, val) {
function(a, val) {
if (a[i] === val) {
var l = a.length, i;
if (f.call(o, a[i], i, a)) {
function Queue() {
this._init();
_init: function() {
this._q = [];
next: function() {
last: function() {
add: function() {
size: function() {
_iefix = function(r, s) {
Y.merge = function() {
Y.mix(o, a[i], true);
* @param merge {boolean/int} merge objects instead of overwriting/ignoring.
if (mode) {
switch (mode) {
var i, l, p, type;
p = wl[i];
if (s.hasOwnProperty(p)) {
Y.mix(r[p], s[p]);
} else if (ov || !(p in r)) {
if (s.hasOwnProperty(i)) {
} else if (ov || !(i in r)) {
_iefix(r, s);
return function(arg1) {
return cache[k];
O = Object.create || function(o) {
F.prototype = o;
owns = function(o, k) {
if (owns(o, i)) {
if (count) {
out++;
return out;
O.keys = function(o) {
return _extract(o);
O.values = function(o) {
O.size = function(o) {
O.hasValue = function(o, v) {
f.call(s, o[i], i, o);
if (f.call(s, o[i], i, o)) {
return UNDEF;
p = Y.Array(path),
l = p.length;
p = Y.Array(path),
ref = o;
return UNDEF;
O.isEmpty = function(o) {
if (owns(o, i)) {
var numberify = function(s) {
mobile: null,
ios: null,
secure: false,
os: null
if (ua) {