2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
11 _startTime = new Date().getTime(),
16 if (window.addEventListener) {
17 return function(el, type, fn, capture) {
18 el.addEventListener(type, fn, (!!capture));
20 } else if (window.attachEvent) {
21 return function(el, type, fn) {
22 el.attachEvent("on" + type, fn);
30 if (window.removeEventListener) {
31 return function (el, type, fn, capture) {
32 el.removeEventListener(type, fn, !!capture);
34 } else if (window.detachEvent) {
35 return function (el, type, fn) {
36 el.detachEvent("on" + type, fn);
43 globalListener = function() {
44 YUI.Env.windowLoaded = true;
45 YUI.Env.DOMReady = true;
46 remove(window, 'load', globalListener);
49 // @TODO: this needs to be created at build time from module metadata
51 _APPLY_TO_WHITE_LIST = {
58 SLICE = Array.prototype.slice;
60 // reduce to one or the other
61 if (typeof YUI === 'undefined' || !YUI) {
64 * The YUI global namespace object. If YUI is already defined, the
65 * existing YUI object will not be overwritten so that defined
66 * namespaces are preserved.
72 * @param o* Up to five optional configuration objects. This object is stored
73 * in YUI.config. See config for the list of supported properties.
77 // Make a function, disallow direct instantiation
78 YUI = function(o1, o2, o3, o4, o5) {
80 var Y = this, a = arguments, i, l = a.length;
82 // Allow instantiation without the new operator
83 if (!(Y instanceof YUI)) {
84 return new YUI(o1, o2, o3, o4, o5);
86 // set up the core environment
93 // bind the specified additional modules for this instance
101 // The prototype contains the functions that are required to allow the external
102 // modules to be registered and for the instance to be initialized.
105 _config: function(o) {
109 var c = this.config, i, j, m, mods;
113 if (mods && i == 'modules') {
116 if (m.hasOwnProperty(j)) {
120 } else if (i == 'win') {
121 c[i] = o[i].contentWindow || o[i];
122 c.doc = c[i].document;
130 * Initialize this YUI instance
131 * @param o config options
136 // find targeted window/frame
137 // @TODO create facades
138 var v = '3.0.0', Y = this;
140 if (v.indexOf('@') > -1) {
147 // @todo expand the new module metadata
149 cdn: 'http://yui.yahooapis.com/' + v + '/build/',
159 Y.Env._loaded[v] = {};
162 Y.Env._yidx = (++YUI.Env._yidx);
163 Y.Env._guidp = ('yui_' + v + '-' + Y.Env._yidx + '-' + _startTime).replace(/\./g, '_');
165 _instances[Y.id] = Y;
170 // configuration defaults
176 useBrowserConsole: true,
180 var b, nodes, i, match;
182 // get from querystring
183 nodes = document.getElementsByTagName('script');
185 for (i=0; i<nodes.length; i=i+1) {
186 match = nodes[i].src.match(/^(.*)yui\/yui[\.\-].*js(\?.*)?$/);
187 b = match && match[1];
194 return b || this.Env.cdn;
198 loaderPath: 'loader/loader-min.js'
204 * Finishes the instance setup. Attaches whatever modules were defined
205 * when the yui modules was registered.
209 _setup: function(o) {
210 this.use("yui-base");
214 * Executes a method on a YUI instance with
215 * the specified id if the specified method is whitelisted.
217 * @param id {string} the YUI instance id
218 * @param method {string} the name of the method to exectute.
220 * @param args {Array} the arguments to apply to the method
221 * @return {object} the return value from the applied method or null
223 applyTo: function(id, method, args) {
225 if (!(method in _APPLY_TO_WHITE_LIST)) {
226 this.error(method + ': applyTo not allowed');
230 var instance = _instances[id], nest, m, i;
234 nest = method.split('.');
237 for (i=0; i<nest.length; i=i+1) {
242 this.error('applyTo not found: ' + method);
246 return m.apply(instance, args);
255 * @param name {string} module name
256 * @param fn {Function} entry point into the module that
257 * is used to bind module to the YUI instance
258 * @param version {string} version string
259 * @param details optional config data:
260 * requires - features that should be present before loading
261 * optional - optional features that should be present if load optional defined
262 * use - features that should be attached automatically
265 * omit - features that should not be loaded if this module is present
266 * @return {YUI} the YUI instance
269 add: function(name, fn, version, details) {
270 // this.log('Adding a new component ' + name);
271 // @todo expand this to include version mapping
272 // @todo may want to restore the build property
273 // @todo fire moduleAvailable event
275 YUI.Env.mods[name] = {
279 details: details || {}
282 return this; // chain support
285 _attach: function(r, fromLoader) {
287 var mods = YUI.Env.mods,
288 attached = this.Env._attached,
289 i, l = r.length, name, m, d, req, use;
291 for (i=0; i<l; i=i+1) {
296 if (!attached[name] && m) {
298 attached[name] = true;
305 this._attach(this.Array(req));
308 // this.log('attaching ' + name, 'info', 'yui');
315 this._attach(this.Array(use));
323 * Bind a module to a YUI instance
324 * @param modules* {string} 1-n modules to bind (uses arguments array)
325 * @param *callback {function} callback function executed when
326 * the instance has the required functionality. If included, it
327 * must be the last parameter.
330 * Implement versioning? loader can load different versions?
331 * Should sub-modules/plugins be normal modules, or do
332 * we add syntax for specifying these?
334 * YUI().use('dragdrop')
335 * YUI().use('dragdrop:2.4.0'); // specific version
336 * YUI().use('dragdrop:2.4.0-'); // at least this version
337 * YUI().use('dragdrop:2.4.0-2.9999.9999'); // version range
338 * YUI().use('*'); // use all available modules
339 * YUI().use('lang+dump+substitute'); // use lang and some plugins
340 * YUI().use('lang+*'); // use lang and all known plugins
343 * @return {YUI} the YUI instance
348 this._useQueue = this._useQueue || new this.Queue();
349 this._useQueue.add(SLICE.call(arguments, 0));
354 a=SLICE.call(arguments, 0),
360 callback = a[a.length-1],
361 k, i, l, missing = [],
365 // only attach a module once
370 var m = mods[name], j, req, use;
377 req = m.details.requires;
381 // CSS files don't register themselves, see if it has been loaded
382 if (!YUI.Env._loaded[Y.version][name]) {
390 // make sure requirements are attached
392 if (Y.Lang.isString(req)) {
395 for (j = 0; j < req.length; j = j + 1) {
401 // add this module to full list of things to attach
406 onComplete = function(fromLoader) {
409 fromLoader = fromLoader || {
414 if (Y.Env._callback) {
416 var cb = Y.Env._callback;
417 Y.Env._callback = null;
422 Y.fire('yui:load', Y, fromLoader);
425 // process queued use requests as long until done
426 // or dynamic load happens again.
428 while (Y._useQueue && Y._useQueue.size() && !Y._loading) {
429 Y.use.apply(Y, Y._useQueue.next());
434 // The last argument supplied to use can be a load complete callback
435 if (typeof callback === 'function') {
437 Y.Env._callback = callback;
442 // YUI().use('*'); // bind everything available
443 if (firstArg === "*") {
446 if (mods.hasOwnProperty(k)) {
451 return Y.use.apply(Y, a);
455 // use loader to expand dependencies and sort the
456 // requirements if it is available.
459 loader = new Y.Loader(Y.config);
461 loader.ignoreRegistered = true;
462 loader.allowRollup = false;
470 // process each requirement and any additional requirements
471 // the module metadata specifies
472 for (i=0; i<l; i=i+1) {
478 if (Y.Loader && missing.length) {
480 loader = new Y.Loader(Y.config);
481 loader.onSuccess = onComplete;
482 loader.onFailure = onComplete;
483 loader.onTimeout = onComplete;
485 loader.attaching = a;
486 loader.require(missing);
488 } else if (Y.Get && missing.length && !Y.Env.bootstrapped) {
491 a = Y.Array(arguments, 0, true);
492 // a.unshift('loader');
494 Y.Get.script(Y.config.base + Y.config.loaderPath, {
497 Y.Env.bootstrapped = true;
498 Y._attach(['loader']);
510 return Y; // chain support var yui = YUI().use('dragdrop');
515 * Returns the namespace specified and creates it if it doesn't exist
517 * YUI.namespace("property.package");
518 * YUI.namespace("YAHOO.property.package");
520 * Either of the above would create YUI.property, then
521 * YUI.property.package (YAHOO is scrubbed out, this is
522 * to remain compatible with YUI2)
524 * Be careful when naming packages. Reserved words may work in some browsers
525 * and not others. For instance, the following will fail in Safari:
527 * YUI.namespace("really.long.nested.namespace");
529 * This fails because "long" is a future reserved word in ECMAScript
532 * @param {string*} arguments 1-n namespaces to create
533 * @return {object} A reference to the last namespace object created
535 namespace: function() {
536 var a=arguments, o=null, i, j, d;
537 for (i=0; i<a.length; i=i+1) {
538 d = ("" + a[i]).split(".");
540 for (j=(d[0] == "YAHOO") ? 1 : 0; j<d.length; j=j+1) {
541 o[d[j]] = o[d[j]] || {};
548 // this is replaced if the log module is included
554 * Report an error. The reporting mechanism is controled by
555 * the 'throwFail' configuration attribute. If throwFail is
556 * not specified, the message is written to the Logger, otherwise
557 * a JS error is thrown
559 * @param msg {string} the error message
560 * @param e {Error} Optional JS error that was caught. If supplied
561 * and throwFail is specified, this error will be re-thrown.
562 * @return {YUI} this YUI instance
564 error: function(msg, e) {
565 if (this.config.throwFail) {
566 throw (e || new Error(msg));
568 this.message(msg, "error"); // don't scrub this one
575 * Generate an id that is unique among all YUI instances
577 * @param pre {string} optional guid prefix
578 * @return {string} the guid
580 guid: function(pre) {
581 var id = this.Env._guidp + (++this.Env._uidx);
582 return (pre) ? (pre + id) : id;
586 * Returns a guid associated with an object. If the object
587 * does not have one, a new one is created unless readOnly
590 * @param o The object to stamp
591 * @param readOnly {boolean} if true, a valid guid will only
592 * be returned if the object has one assigned to it.
593 * @return {string} The object's guid or null
595 stamp: function(o, readOnly) {
601 var uid = (typeof o === 'string') ? o : o._yuid;
618 // Give the YUI global the same properties as an instance.
619 // This makes it so that the YUI global can be used like the YAHOO
620 // global was used prior to 3.x. More importantly, the YUI global
621 // provides global metadata, so env needs to be configured.
626 // inheritance utilities are not available yet
628 // if (1) { // intenionally ignoring hasOwnProperty check
633 // set up the environment
636 // add a window load event at load time so we can capture
637 // the case where it fires before dynamic loading is
639 add(window, 'load', globalListener);
642 YUI.Env.remove = remove;
645 * Subscribe to an event. The signature differs depending on the
646 * type of event you are attaching to.
648 * @param type {string|function|object} The type of the event. If
649 * this is a function, this is dispatched to the aop system. If an
650 * object, it is parsed for multiple subsription definitions
651 * @param fn {Function} The callback
652 * @param elspec {any} DOM element(s), selector string(s), and or
653 * Node ref(s) to attach DOM related events to (only applies to
656 * @return the event target or a detach handle per 'chain' config
662 * The config object contains all of the configuration options for
663 * the YUI instance. This object is supplied by the implementer
664 * when instantiating a YUI instance. Some properties have default
665 * values if they are not supplied by the implementer.
672 * Turn debug statements on or off.
680 * Log to the browser console if debug is on and the browser has a
683 * @property useBrowserConsole
689 * A hash of log sources that should be logged. If specified, only log messages from these sources will be logged.
691 * @property logInclude
696 * A hash of log sources that should be not be logged. If specified, all sources are logged if not on this list.
698 * @property logExclude
703 * Set to true if the yui seed file was dynamically loaded in
704 * order to bootstrap components relying on the window load event
705 * and the 'domready' custom event.
712 * If throwFail is set, Y.fail will generate or re-throw a JS Error. Otherwise the failure is logged.
714 * @property throwFail
720 * The window/frame that this instance should operate in.
724 * @default the window hosting YUI
728 * The document associated with the 'win' configuration.
732 * @default the document hosting YUI
736 * A list of modules that defines the YUI core (overrides the default).
743 * The default date format
745 * @property dateFormat
757 * The default interval when polling in milliseconds.
759 * @property pollInterval
765 * The number of dynamic nodes to insert by default before
766 * automatically removing them. This applies to script nodes
767 * because remove the node will not make the evaluated script
768 * unavailable. Dynamic CSS is not auto purged, because removing
769 * a linked style sheet will also remove the style definitions.
771 * @property purgethreshold
777 * The default interval when polling in milliseconds.
779 * @property windowResizeDelay
785 * Base directory for dynamic loading
792 * The secure base dir (not implemented)
794 * For dynamic loading.
796 * @property secureBase
801 * The YUI combo service base dir. Ex: http://yui.yahooapis.com/combo?
803 * For dynamic loading.
805 * @property comboBase
810 * The root path to prepend to module names for the combo service. Ex: 3.0.0b1/build/
812 * For dynamic loading.
819 * A filter to apply to result urls. This filter will modify the default
820 * path for all modules. The default path for the YUI library is the
821 * minified version of the files (e.g., event-min.js). The filter property
822 * can be a predefined filter or a custom filter. The valid predefined
826 * <dd>Selects the debug versions of the library (e.g., event-debug.js).
827 * This option will automatically include the Logger widget</dd>
829 * <dd>Selects the non-minified version of the library (e.g., event.js).</dd>
831 * You can also define a custom filter, which must be an object literal
832 * containing a search expression and a replace string:
835 * 'searchExp': "-min\\.js",
836 * 'replaceStr': "-debug.js"
840 * For dynamic loading.
843 * @type string|object
847 * Hash of per-component filter specification. If specified for a given component,
848 * this overrides the filter config
850 * For dynamic loading.
857 * Use the YUI combo service to reduce the number of http connections
858 * required to load your dependencies.
860 * For dynamic loading.
864 * @default true if 'base' is not supplied, false if it is.
868 * A list of modules that should never be dynamically loaded
875 * A list of modules that should always be loaded when required, even if already
876 * present on the page.
883 * Node or id for a node that should be used as the insertion point for new nodes
884 * For dynamic loading.
886 * @property insertBefore
891 * charset for dynamic nodes
895 * @deprecated use jsAttributes cssAttributes
899 * Object literal containing attributes to add to dynamically loaded script nodes.
901 * @property jsAttributes
906 * Object literal containing attributes to add to dynamically loaded link nodes.
908 * @property cssAttributes
913 * Number of milliseconds before a timeout occurs when dynamically
914 * loading nodes. If not set, there is no timeout.
921 * Callback for the 'CSSComplete' event. When dynamically loading YUI
922 * components with CSS, this property fires when the CSS is finished
923 * loading but script loading is still ongoing. This provides an
924 * opportunity to enhance the presentation of a loading page a little
925 * bit before the entire loading process is done.
932 * A list of module definitions to add to the list of YUI components.
933 * These components can then be dynamically loaded side by side with
934 * YUI via the use() method.See Loader.addModule for the supported
942 * The loader 'path' attribute to the loader itself. This is combined
943 * with the 'base' attribute to dynamically load the loader component
944 * when boostrapping with the get utility alone.
946 * @property loaderPath
947 * @default loader/loader-min.js
949 YUI.add('yui-base', function(Y) {
954 * @submodule yui-base
959 LOGEVENT = 'yui:log',
960 UNDEFINED = 'undefined',
961 LEVELS = { debug: 1, info: 1, warn: 1, error: 1 },
965 * If the 'debug' config is true, a 'yui:log' event will be
966 * dispatched, which the Console widget and anything else
967 * can consume. If the 'useBrowserConsole' config is true, it will
968 * write to the browser console if available. YUI-specific log
969 * messages will only be present in the -debug versions of the
970 * JS files. The build system is supposed to remove log statements
971 * from the raw and minified versions of the files.
975 * @param {String} msg The message to log.
976 * @param {String} cat The log category for the message. Default
977 * categories are "info", "warn", "error", time".
978 * Custom categories can be used as well. (opt)
979 * @param {String} src The source of the the message (opt)
980 * @param {boolean} silent If true, the log event won't fire
981 * @return {YUI} YUI instance
983 INSTANCE.log = function(msg, cat, src, silent) {
984 var Y = INSTANCE, c = Y.config, bail = false, excl, incl, m, f;
985 // suppress log message if the config is off or the event stack
986 // or the event call stack contains a consumer of the yui:log event
988 // apply source filters
994 if (incl && !(src in incl)) {
996 } else if (excl && (src in excl)) {
1003 if (c.useBrowserConsole) {
1004 m = (src) ? src + ': ' + msg : msg;
1005 if (typeof console != UNDEFINED && console.log) {
1006 f = (cat && console[cat] && (cat in LEVELS)) ? cat : 'log';
1008 } else if (typeof opera != UNDEFINED) {
1013 if (Y.fire && !bail && !silent) {
1015 Y.publish(LOGEVENT, {
1036 * Write a system message. This message will be preserved in the
1037 * minified and raw versions of the YUI files, unlike log statements.
1040 * @param {String} msg The message to log.
1041 * @param {String} cat The log category for the message. Default
1042 * categories are "info", "warn", "error", time".
1043 * Custom categories can be used as well. (opt)
1044 * @param {String} src The source of the the message (opt)
1045 * @param {boolean} silent If true, the log event won't fire
1046 * @return {YUI} YUI instance
1048 INSTANCE.message = function() {
1049 return INSTANCE.log.apply(INSTANCE, arguments);
1055 * Provides the language utilites and extensions used by the library
1059 Y.Lang = Y.Lang || {};
1064 BOOLEAN = 'boolean',
1067 FUNCTION = 'function',
1073 TOSTRING = Object.prototype.toString,
1074 UNDEFINED = 'undefined',
1077 'undefined' : UNDEFINED,
1079 'boolean' : BOOLEAN,
1081 '[object Function]' : FUNCTION,
1082 '[object RegExp]' : REGEX,
1083 '[object Array]' : ARRAY,
1084 '[object Date]' : DATE,
1085 '[object Error]' : ERROR
1088 TRIMREGEX = /^\s+|\s+$/g,
1092 * Determines whether or not the provided item is an array.
1093 * Returns false for array-like collections such as the
1094 * function arguments collection or HTMLElement collection
1095 * will return false. You can use @see Array.test if you
1099 * @param o The object to test
1100 * @return {boolean} true if o is an array
1102 L.isArray = function(o) {
1103 return L.type(o) === ARRAY;
1107 * Determines whether or not the provided item is a boolean
1110 * @param o The object to test
1111 * @return {boolean} true if o is a boolean
1113 L.isBoolean = function(o) {
1114 return typeof o === BOOLEAN;
1118 * Determines whether or not the provided item is a function
1119 * Note: Internet Explorer thinks certain functions are objects:
1121 * var obj = document.createElement("object");
1122 * Y.Lang.isFunction(obj.getAttribute) // reports false in IE
1124 * var input = document.createElement("input"); // append to body
1125 * Y.Lang.isFunction(input.focus) // reports false in IE
1127 * You will have to implement additional tests if these functions
1130 * @method isFunction
1132 * @param o The object to test
1133 * @return {boolean} true if o is a function
1135 L.isFunction = function(o) {
1136 return L.type(o) === FUNCTION;
1140 * Determines whether or not the supplied item is a date instance
1143 * @param o The object to test
1144 * @return {boolean} true if o is a date
1146 L.isDate = function(o) {
1147 // return o instanceof Date;
1148 return L.type(o) === DATE;
1152 * Determines whether or not the provided item is null
1155 * @param o The object to test
1156 * @return {boolean} true if o is null
1158 L.isNull = function(o) {
1163 * Determines whether or not the provided item is a legal number
1166 * @param o The object to test
1167 * @return {boolean} true if o is a number
1169 L.isNumber = function(o) {
1170 return typeof o === NUMBER && isFinite(o);
1174 * Determines whether or not the provided item is of type object
1178 * @param o The object to test
1179 * @param failfn {boolean} fail if the input is a function
1180 * @return {boolean} true if o is an object
1182 L.isObject = function(o, failfn) {
1183 return (o && (typeof o === OBJECT || (!failfn && L.isFunction(o)))) || false;
1187 * Determines whether or not the provided item is a string
1190 * @param o The object to test
1191 * @return {boolean} true if o is a string
1193 L.isString = function(o) {
1194 return typeof o === STRING;
1198 * Determines whether or not the provided item is undefined
1199 * @method isUndefined
1201 * @param o The object to test
1202 * @return {boolean} true if o is undefined
1204 L.isUndefined = function(o) {
1205 return typeof o === UNDEFINED;
1209 * Returns a string without any leading or trailing whitespace. If
1210 * the input is not a string, the input will be returned untouched.
1213 * @param s {string} the string to trim
1214 * @return {string} the trimmed string
1216 L.trim = function(s){
1218 return s.replace(TRIMREGEX, EMPTYSTRING);
1225 * A convenience method for detecting a legitimate non-null value.
1226 * Returns false for null/undefined/NaN, true for other values,
1227 * including 0/false/''
1230 * @param o The item to test
1231 * @return {boolean} true if it is not null/undefined/NaN || false
1233 L.isValue = function(o) {
1247 * Returns a string representing the type of the item passed in.
1249 * @param o the item to test
1250 * @return {string} the detected type
1252 L.type = function (o) {
1253 return TYPES[typeof o] || TYPES[TOSTRING.call(o)] || (o ? OBJECT : NULL);
1259 * Provides information about the environment hosting YUI
1266 var L = Y.Lang, Native = Array.prototype,
1269 * Adds the following array utilities to the YUI instance. Additional
1270 * array helpers can be found in the collection component.
1275 * Y.Array(o) returns an array:
1276 * - Arrays are return unmodified unless the start position is specified.
1277 * - "Array-like" collections (@see Array.test) are converted to arrays
1278 * - For everything else, a new array is created with the input as the sole item
1279 * - The start position is used if the input is or is like an array to return
1280 * a subset of the collection.
1282 * @TODO this will not automatically convert elements that are also collections
1283 * such as forms and selects. Passing true as the third param will
1284 * force a conversion.
1288 * @param o the item to arrayify
1289 * @param i {int} if an array or array-like, this is the start index
1290 * @param al {boolean} if true, it forces the array-like fork. This
1291 * can be used to avoid multiple array.test calls.
1292 * @return {Array} the resulting array
1294 YArray = function(o, startIdx, al) {
1295 var t = (al) ? 2 : Y.Array.test(o), i, l, a;
1299 // // return (startIdx) ? o.slice(startIdx) : o;
1301 // return Native.slice.call(o, startIdx || 0);
1308 return Native.slice.call(o, startIdx || 0);
1309 // IE errors when trying to slice element collections
1312 for (i=0, l=o.length; i<l; i=i+1) {
1326 * Evaluates the input to determine if it is an array, array-like, or
1327 * something else. This is used to handle the arguments collection
1328 * available within functions, and HTMLElement collections
1333 * @todo current implementation (intenionally) will not implicitly
1334 * handle html elements that are array-like (forms, selects, etc).
1336 * @return {int} a number indicating the results:
1337 * 0: Not an array or an array-like collection
1339 * 2: array-like collection.
1341 YArray.test = function(o) {
1343 if (L.isObject(o)) {
1348 // indexed, but no tagName (element) or alert (window)
1349 if ("length" in o && !("tagName" in o) && !("alert" in o) &&
1350 (!Y.Lang.isFunction(o.size) || o.size() > 1)) {
1361 * Executes the supplied function on each item in the array.
1363 * @param a {Array} the array to iterate
1364 * @param f {Function} the function to execute on each item
1365 * @param o Optional context object
1367 * @return {YUI} the YUI instance
1369 YArray.each = (Native.forEach) ?
1370 function (a, f, o) {
1371 Native.forEach.call(a || [], f, o || Y);
1374 function (a, f, o) {
1375 var l = (a && a.length) || 0, i;
1376 for (i = 0; i < l; i=i+1) {
1377 f.call(o || Y, a[i], i, a);
1383 * Returns an object using the first array as keys, and
1384 * the second as values. If the second array is not
1385 * provided the value is set to true for each.
1388 * @param k {Array} keyset
1389 * @param v {Array} optional valueset
1390 * @return {object} the hash
1392 YArray.hash = function(k, v) {
1393 var o = {}, l = k.length, vl = v && v.length, i;
1394 for (i=0; i<l; i=i+1) {
1395 o[k[i]] = (vl && vl > i) ? v[i] : true;
1402 * Returns the index of the first item in the array
1403 * that contains the specified value, -1 if the
1404 * value isn't found.
1407 * @param a {Array} the array to search
1408 * @param val the value to search for
1409 * @return {int} the index of the item that contains the value or -1
1411 YArray.indexOf = (Native.indexOf) ?
1413 return Native.indexOf.call(a, val);
1416 for (var i=0; i<a.length; i=i+1) {
1426 * Numeric sort convenience function.
1427 * Y.ArrayAssert.itemsAreEqual([1, 2, 3], [3, 1, 2].sort(Y.Array.numericSort));
1428 * @method numericSort
1430 YArray.numericSort = function(a, b) {
1435 * Executes the supplied function on each item in the array.
1436 * Returning true from the processing function will stop the
1437 * processing of the remaining
1440 * @param a {Array} the array to iterate
1441 * @param f {Function} the function to execute on each item
1442 * @param o Optional context object
1444 * @return {boolean} true if the function returns true on
1445 * any of the items in the array
1447 YArray.some = (Native.some) ?
1448 function (a, f, o) {
1449 return Native.some.call(a, f, o);
1451 function (a, f, o) {
1452 var l = a.length, i;
1453 for (i=0; i<l; i=i+1) {
1454 if (f.call(o, a[i], i, a)) {
1472 * IE will not enumerate native functions in a derived object even if the
1473 * function was overridden. This is a workaround for specific functions
1474 * we care about on the Object prototype.
1477 * @param {Function} r the object to receive the augmentation
1478 * @param {Function} s the object that supplies the properties to augment
1481 _iefix = function(r, s) {
1482 var fn = s.toString;
1483 if (L.isFunction(fn) && fn != Object.prototype.toString) {
1490 * Returns a new object containing all of the properties of
1491 * all the supplied objects. The properties from later objects
1492 * will overwrite those in earlier objects. Passing in a
1493 * single object will create a shallow copy of it. For a deep
1497 * @param arguments {Object*} the objects to merge
1498 * @return {object} the new merged object
1500 Y.merge = function() {
1501 var a = arguments, o = {}, i, l = a.length;
1502 for (i=0; i<l; i=i+1) {
1503 Y.mix(o, a[i], true);
1509 * Applies the supplier's properties to the receiver. By default
1510 * all prototype and static propertes on the supplier are applied
1511 * to the corresponding spot on the receiver. By default all
1512 * properties are applied, and a property that is already on the
1513 * reciever will not be overwritten. The default behavior can
1514 * be modified by supplying the appropriate parameters.
1516 * @TODO add constants for the modes
1519 * @param {Function} r the object to receive the augmentation
1520 * @param {Function} s the object that supplies the properties to augment
1521 * @param ov {boolean} if true, properties already on the receiver
1522 * will be overwritten if found on the supplier.
1523 * @param wl {string[]} a whitelist. If supplied, only properties in
1524 * this list will be applied to the receiver.
1525 * @param {int} mode what should be copies, and to where
1526 * default(0): object to object
1527 * 1: prototype to prototype (old augment)
1528 * 2: prototype to prototype and object props (new augment)
1529 * 3: prototype to object
1530 * 4: object to prototype
1531 * @param merge {boolean} merge objects instead of overwriting/ignoring
1532 * Used by Y.aggregate
1533 * @return {object} the augmented object
1535 Y.mix = function(r, s, ov, wl, mode, merge) {
1543 case 1: // proto to proto
1544 return Y.mix(r.prototype, s.prototype, ov, wl, 0, merge);
1545 case 2: // object to object and proto to proto
1546 Y.mix(r.prototype, s.prototype, ov, wl, 0, merge);
1547 break; // pass through
1548 case 3: // proto to static
1549 return Y.mix(r, s.prototype, ov, wl, 0, merge);
1550 case 4: // static to proto
1551 return Y.mix(r.prototype, s, ov, wl, 0, merge);
1552 default: // object to object is what happens below
1556 // Maybe don't even need this wl && wl.length check anymore??
1557 var arr = merge && L.isArray(r), i, l, p;
1559 if (wl && wl.length) {
1560 for (i = 0, l = wl.length; i < l; ++i) {
1563 if (merge && L.isObject(r[p], true)) {
1565 } else if (!arr && (ov || !(p in r))) {
1574 // if (s.hasOwnProperty(i) && !(i in FROZEN)) {
1575 // check white list if it was supplied
1576 // if the receiver has this property, it is an object,
1577 // and merge is specified, merge the two objects.
1578 if (merge && L.isObject(r[i], true)) {
1579 Y.mix(r[i], s[i]); // recursive
1580 // otherwise apply the property only if overwrite
1581 // is specified or the receiver doesn't have one.
1582 } else if (!arr && (ov || !(i in r))) {
1584 // if merge is specified and the receiver is an array,
1585 // append the array item
1601 * Returns a wrapper for a function which caches the
1602 * return value of that function, keyed off of the combined
1605 * @param source {function} the function to memoize
1606 * @param cache an optional cache seed
1607 * @param refetch if supplied, this value is tested against the cached
1608 * value. If the values are equal, the wrapped function is executed again.
1609 * @return {Function} the wrapped function
1611 Y.cached = function(source, cache, refetch){
1612 cache = cache || {};
1614 return function(arg1, arg2) {
1616 var k = (arg2) ? Array.prototype.join.call(arguments, DELIMITER) : arg1,
1619 if (!(k in cache) || (refetch && cache[k] == refetch)) {
1620 cache[k] = source.apply(source, arguments);
1633 * Adds the following Object utilities to the YUI instance
1638 * Y.Object(o) returns a new object based upon the supplied object.
1639 * @TODO Use native Object.create() when available
1642 * @param o the supplier object
1643 * @return {Object} the new object
1645 Y.Object = function(o) {
1646 var F = function() {};
1653 UNDEFINED = undefined,
1656 * Extracts the keys, values, or size from an object
1659 * @param o the object
1660 * @param what what to extract (0: keys, 1: values, 2: size)
1661 * @return {boolean|Array} the extracted info
1665 _extract = function(o, what) {
1666 var count = (what === 2), out = (count) ? 0 : [], i;
1672 if (o.hasOwnProperty(i)) {
1673 out.push((what) ? o[i] : i);
1682 * Returns an array containing the object's keys
1683 * @TODO use native Object.keys() if available
1686 * @param o an object
1687 * @return {string[]} the keys
1689 O.keys = function(o) {
1694 * Returns an array containing the object's values
1695 * @TODO use native Object.values() if available
1698 * @param o an object
1699 * @return {Array} the values
1701 O.values = function(o) {
1702 return _extract(o, 1);
1706 * Returns the size of an object
1707 * @TODO use native Object.size() if available
1710 * @param o an object
1711 * @return {int} the size
1713 O.size = function(o) {
1714 return _extract(o, 2);
1718 * Returns true if the object contains a given key
1721 * @param o an object
1722 * @param k the key to query
1723 * @return {boolean} true if the object contains the key
1725 O.hasKey = function(o, k) {
1726 // return (o.hasOwnProperty(k));
1731 * Returns true if the object contains a given value
1734 * @param o an object
1735 * @param v the value to query
1736 * @return {boolean} true if the object contains the value
1738 O.hasValue = function(o, v) {
1739 return (Y.Array.indexOf(O.values(o), v) > -1);
1743 * Determines whether or not the property was added
1744 * to the object instance. Returns false if the property is not present
1745 * in the object, or was inherited from the prototype.
1747 * @deprecated Safari 1.x support has been removed, so this is simply a
1748 * wrapper for the native implementation. Use the native implementation
1751 * @TODO Remove in B1
1755 * @param o {any} The object being testing
1756 * @param p {string} the property to look for
1757 * @return {boolean} true if the object has the property on the instance
1759 O.owns = function(o, k) {
1760 return (o.hasOwnProperty(k));
1764 * Executes a function on each item. The function
1765 * receives the value, the key, and the object
1766 * as paramters (in that order).
1769 * @param o the object to iterate
1770 * @param f {function} the function to execute
1771 * @param c the execution context
1772 * @param proto {boolean} include proto
1773 * @return {YUI} the YUI instance
1775 O.each = function (o, f, c, proto) {
1779 if (proto || o.hasOwnProperty(i)) {
1780 f.call(s, o[i], i, o);
1787 * Retrieves the sub value at the provided path,
1788 * from the value object provided.
1791 * @param o The object from which to extract the property value
1792 * @param path {Array} A path array, specifying the object traversal path
1793 * from which to obtain the sub value.
1794 * @return {Any} The value stored in the path, undefined if not found.
1795 * Returns the source object if an empty path is provided.
1797 O.getValue = function (o, path) {
1798 var p=Y.Array(path), l=p.length, i;
1800 for (i=0; o !== UNDEFINED && i < l; i=i+1) {
1808 * Sets the sub-attribute value at the provided path on the
1809 * value object. Returns the modified value object, or
1810 * undefined if the path is invalid.
1813 * @param o The object on which to set the sub value.
1814 * @param path {Array} A path array, specifying the object traversal path
1815 * at which to set the sub value.
1816 * @param val {Any} The new value for the sub-attribute.
1817 * @return {Object} The modified object, with the new sub value set, or
1818 * undefined, if the path was invalid.
1820 O.setValue = function(o, path, val) {
1822 var p=Y.Array(path), leafIdx=p.length-1, i, ref=o;
1825 for (i=0; ref !== UNDEFINED && i < leafIdx; i=i+1) {
1829 if (ref !== UNDEFINED) {
1843 * Provides information about the environment hosting YUI
1848 * YUI user agent detection.
1849 * Do not fork for a browser if it can be avoided. Use feature detection when
1850 * you can. Use the user agent as a last resort. UA stores a version
1851 * number for the browser engine, 0 otherwise. This value may or may not map
1852 * to the version number of the browser using the engine. The value is
1853 * presented as a float so that it can easily be used for boolean evaluation
1854 * as well as for looking for a particular range of versions. Because of this,
1855 * some of the granularity of the version info may be lost (e.g., Gecko 1.8.0.9
1862 var numberfy = function(s) {
1864 return parseFloat(s.replace(/\./g, function() {
1865 return (c++ == 1) ? '' : '.';
1874 * Internet Explorer version number or 0. Example: 6
1882 * Opera version number or 0. Example: 9.2
1890 * Gecko engine revision number. Will evaluate to 1 if Gecko
1891 * is detected but the revision could not be found. Other browsers
1892 * will be 0. Example: 1.8
1894 * Firefox 1.0.0.4: 1.7.8 <-- Reports 1.7
1895 * Firefox 1.5.0.9: 1.8.0.9 <-- Reports 1.8
1896 * Firefox 2.0.0.3: 1.8.1.3 <-- Reports 1.8
1897 * Firefox 3 alpha: 1.9a4 <-- Reports 1.9
1906 * AppleWebKit version. KHTML browsers that are not WebKit browsers
1907 * will evaluate to 1, other browsers 0. Example: 418.9
1909 * Safari 1.3.2 (312.6): 312.8.1 <-- Reports 312.8 -- currently the
1910 * latest available for Mac OSX 10.3.
1911 * Safari 2.0.2: 416 <-- hasOwnProperty introduced
1912 * Safari 2.0.4: 418 <-- preventDefault fixed
1913 * Safari 2.0.4 (419.3): 418.9.1 <-- One version of Safari may run
1914 * different versions of webkit
1915 * Safari 2.0.4 (419.3): 419 <-- Tiger installations that have been
1916 * updated, but not updated
1917 * to the latest patch.
1918 * Webkit 212 nightly: 522+ <-- Safari 3.0 precursor (with native SVG
1919 * and many major issues fixed).
1920 * Safari 3.0.4 (523.12) 523.12 <-- First Tiger release - automatic update
1921 * from 2.x via the 10.4.11 OS patch
1922 * Webkit nightly 1/2008:525+ <-- Supports DOMContentLoaded event.
1923 * yahoo.com user agent hack removed.
1925 * http://en.wikipedia.org/wiki/Safari_(web_browser)#Version_history
1933 * The mobile property will be set to a string containing any relevant
1934 * user agent information when a modern mobile browser is detected.
1935 * Currently limited to Safari on the iPhone/iPod Touch, Nokia N-series
1936 * devices with the WebKit-based browser, and Opera Mini.
1944 * Adobe AIR version number or 0. Only populated if webkit is detected.
1952 * Google Caja version number or 0.
1956 caja: nav.cajaVersion,
1959 * Set to true if the page appears to be in SSL
1967 * The operating system. Currently only detecting windows or macintosh
1976 ua = nav && nav.userAgent,
1978 loc = Y.config.win.location,
1980 href = loc && loc.href,
1984 o.secure = href && (href.toLowerCase().indexOf("https") === 0);
1988 if ((/windows|win32/i).test(ua)) {
1990 } else if ((/macintosh/i).test(ua)) {
1994 // Modern KHTML browsers should qualify as Safari X-Grade
1995 if ((/KHTML/).test(ua)) {
1998 // Modern WebKit browsers are at least X-Grade
1999 m=ua.match(/AppleWebKit\/([^\s]*)/);
2001 o.webkit=numberfy(m[1]);
2003 // Mobile browser check
2004 if (/ Mobile\//.test(ua)) {
2005 o.mobile = "Apple"; // iPhone or iPod Touch
2007 m=ua.match(/NokiaN[^\/]*|Android \d\.\d|webOS\/\d\.\d/);
2009 o.mobile = m[0]; // Nokia N-series, Android, webOS, ex: NokiaN95
2013 m=ua.match(/AdobeAIR\/([^\s]*)/);
2015 o.air = m[0]; // Adobe AIR 1.0 or better
2020 if (!o.webkit) { // not webkit
2021 // @todo check Opera/8.01 (J2ME/MIDP; Opera Mini/2.0.4509/1316; fi; U; ssr)
2022 m=ua.match(/Opera[\s\/]([^\s]*)/);
2024 o.opera=numberfy(m[1]);
2025 m=ua.match(/Opera Mini[^;]*/);
2027 o.mobile = m[0]; // ex: Opera Mini/2.0.4509/1316
2029 } else { // not opera or webkit
2030 m=ua.match(/MSIE\s([^;]*)/);
2032 o.ie=numberfy(m[1]);
2033 } else { // not opera, webkit, or ie
2034 m=ua.match(/Gecko\/([^\s]*)/);
2036 o.gecko=1; // Gecko detected, look for revision
2037 m=ua.match(/rv:([^\s\)]*)/);
2039 o.gecko=numberfy(m[1]);
2053 * Executes the supplied function in the context of the supplied
2054 * object 'when' milliseconds later. Executes the function a
2055 * single time unless periodic is set to true.
2058 * @param when {int} the number of milliseconds to wait until the fn
2060 * @param o the context object.
2061 * @param fn {Function|String} the function to execute or the name of
2062 * the method in the 'o' object to execute.
2063 * @param data [Array] data that is provided to the function. This accepts
2064 * either a single item or an array. If an array is provided, the
2065 * function is executed with one parameter for each array item. If
2066 * you need to pass a single array parameter, it needs to be wrapped in
2067 * an array [myarray].
2068 * @param periodic {boolean} if true, executes continuously at supplied
2069 * interval until canceled.
2070 * @return {object} a timer object. Call the cancel() method on this object to
2073 later = function(when, o, fn, data, periodic) {
2076 var m=fn, d=data, f, r;
2078 if (L.isString(fn)) {
2083 Y.error("method undefined");
2086 if (!L.isArray(d)) {
2094 r = (periodic) ? setInterval(f, when) : setTimeout(f, when);
2099 cancel: function() {
2100 if (this.interval) {
2115 var min = ['yui-base'], core, C = Y.config, LOADER='loader';
2117 // apply the minimal required functionality
2118 Y.use.apply(Y, min);
2124 core = ['queue-base', 'get'];
2125 if (YUI.Env.mods[LOADER]) {
2130 Y.use.apply(Y, core);