2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 * The YAHOO object is the single global object used by YUI Library. It
9 * contains utility function for setting up namespaces, inheritance, and
10 * logging. YAHOO.util, YAHOO.widget, and YAHOO.example are namespaces
11 * created automatically for and used by the library.
17 * YAHOO_config is not included as part of the library. Instead it is an
18 * object that can be defined by the implementer immediately before
19 * including the YUI library. The properties included in this object
20 * will be used to configure global properties needed as soon as the
21 * library begins to load.
27 * A reference to a function that will be executed every time a YAHOO module
28 * is loaded. As parameter, this function will receive the version
29 * information for the module. See <a href="YAHOO.env.html#getVersion">
30 * YAHOO.env.getVersion</a> for the description of the version data structure.
38 * Set to true if the library will be dynamically loaded after window.onload.
47 * Instructs the yuiloader component to dynamically load yui components and
48 * their dependencies. See the yuiloader documentation for more information
49 * about dynamic loading
57 * Forces the use of the supplied locale where applicable in the library
64 if (typeof YAHOO == "undefined" || !YAHOO) {
66 * The YAHOO global namespace object. If YAHOO is already defined, the
67 * existing YAHOO object will not be overwritten so that defined
68 * namespaces are preserved.
76 * Returns the namespace specified and creates it if it doesn't exist
78 * YAHOO.namespace("property.package");
79 * YAHOO.namespace("YAHOO.property.package");
81 * Either of the above would create YAHOO.property, then
82 * YAHOO.property.package
84 * Be careful when naming packages. Reserved words may work in some browsers
85 * and not others. For instance, the following will fail in Safari:
87 * YAHOO.namespace("really.long.nested.namespace");
89 * This fails because "long" is a future reserved word in ECMAScript
91 * For implementation code that uses YUI, do not create your components
92 * in the namespaces created by the library. defined by YUI -- create
93 * your own (YAHOO.util, YAHOO.widget, YAHOO.lang, YAHOO.env)
97 * @param {String*} arguments 1-n namespaces to create
98 * @return {Object} A reference to the last namespace object created
100 YAHOO.namespace = function() {
101 var a=arguments, o=null, i, j, d;
102 for (i=0; i<a.length; i=i+1) {
103 d=(""+a[i]).split(".");
106 // YAHOO is implied, so it is ignored if it is included
107 for (j=(d[0] == "YAHOO") ? 1 : 0; j<d.length; j=j+1) {
108 o[d[j]]=o[d[j]] || {};
117 * Uses YAHOO.widget.Logger to output a log message, if the widget is
122 * @param {String} msg The message to log.
123 * @param {String} cat The log category for the message. Default
124 * categories are "info", "warn", "error", time".
125 * Custom categories can be used as well. (opt)
126 * @param {String} src The source of the the message (opt)
127 * @return {Boolean} True if the log operation was successful.
129 YAHOO.log = function(msg, cat, src) {
130 var l=YAHOO.widget.Logger;
132 return l.log(msg, cat, src);
139 * Registers a module with the YAHOO object
142 * @param {String} name the name of the module (event, slider, etc)
143 * @param {Function} mainClass a reference to class in the module. This
144 * class will be tagged with the version info
145 * so that it will be possible to identify the
146 * version that is in use when multiple versions
148 * @param {Object} data metadata object for the module. Currently it
149 * is expected to contain a "version" property
150 * and a "build" property at minimum.
152 YAHOO.register = function(name, mainClass, data) {
153 var mods = YAHOO.env.modules, m, v, b, ls, i;
165 ls = YAHOO.env.listeners;
172 m.mainClass = mainClass;
174 // fire the module load listeners
175 for (i=0;i<ls.length;i=i+1) {
178 // label the main class
180 mainClass.VERSION = v;
183 YAHOO.log("mainClass is undefined for module " + name, "warn");
188 * YAHOO.env is used to keep track of what is known about the YUI library and
189 * the browsing environment
193 YAHOO.env = YAHOO.env || {
196 * Keeps the version info for all YUI modules that have reported themselves
203 * List of functions that should be executed every time a YUI module
205 * @property listeners
212 * Returns the version data for the specified module:
214 * <dt>name:</dt> <dd>The name of the module</dd>
215 * <dt>version:</dt> <dd>The version in use</dd>
216 * <dt>build:</dt> <dd>The build number in use</dd>
217 * <dt>versions:</dt> <dd>All versions that were registered</dd>
218 * <dt>builds:</dt> <dd>All builds that were registered.</dd>
219 * <dt>mainClass:</dt> <dd>An object that was was stamped with the
220 * current version and build. If
221 * mainClass.VERSION != version or mainClass.BUILD != build,
222 * multiple versions of pieces of the library have been
223 * loaded, potentially causing issues.</dd>
228 * @param {String} name the name of the module (event, slider, etc)
229 * @return {Object} The version info
231 YAHOO.env.getVersion = function(name) {
232 return YAHOO.env.modules[name] || null;
236 * Do not fork for a browser if it can be avoided. Use feature detection when
237 * you can. Use the user agent as a last resort. YAHOO.env.ua stores a version
238 * number for the browser engine, 0 otherwise. This value may or may not map
239 * to the version number of the browser using the engine. The value is
240 * presented as a float so that it can easily be used for boolean evaluation
241 * as well as for looking for a particular range of versions. Because of this,
242 * some of the granularity of the version info may be lost (e.g., Gecko 1.8.0.9
244 * @class YAHOO.env.ua
247 YAHOO.env.ua = function() {
251 * Internet Explorer version number or 0. Example: 6
258 * Opera version number or 0. Example: 9.2
265 * Gecko engine revision number. Will evaluate to 1 if Gecko
266 * is detected but the revision could not be found. Other browsers
267 * will be 0. Example: 1.8
269 * Firefox 1.0.0.4: 1.7.8 <-- Reports 1.7
270 * Firefox 1.5.0.9: 1.8.0.9 <-- Reports 1.8
271 * Firefox 2.0.0.3: 1.8.1.3 <-- Reports 1.8
272 * Firefox 3 alpha: 1.9a4 <-- Reports 1.9
280 * AppleWebKit version. KHTML browsers that are not WebKit browsers
281 * will evaluate to 1, other browsers 0. Example: 418.9.1
283 * Safari 1.3.2 (312.6): 312.8.1 <-- Reports 312.8 -- currently the
284 * latest available for Mac OSX 10.3.
285 * Safari 2.0.2: 416 <-- hasOwnProperty introduced
286 * Safari 2.0.4: 418 <-- preventDefault fixed
287 * Safari 2.0.4 (419.3): 418.9.1 <-- One version of Safari may run
288 * different versions of webkit
289 * Safari 2.0.4 (419.3): 419 <-- Tiger installations that have been
290 * updated, but not updated
291 * to the latest patch.
292 * Webkit 212 nightly: 522+ <-- Safari 3.0 precursor (with native SVG
293 * and many major issues fixed).
294 * 3.x yahoo.com, flickr:422 <-- Safari 3.x hacks the user agent
295 * string when hitting yahoo.com and
297 * Safari 3.0.4 (523.12):523.12 <-- First Tiger release - automatic update
298 * from 2.x via the 10.4.11 OS patch
299 * Webkit nightly 1/2008:525+ <-- Supports DOMContentLoaded event.
300 * yahoo.com user agent hack removed.
303 * http://developer.apple.com/internet/safari/uamatrix.html
310 * The mobile property will be set to a string containing any relevant
311 * user agent information when a modern mobile browser is detected.
312 * Currently limited to Safari on the iPhone/iPod Touch, Nokia N-series
313 * devices with the WebKit-based browser, and Opera Mini.
320 * Adobe AIR version number or 0. Only populated if webkit is detected.
328 * Google Caja version number or 0.
336 ua = navigator.userAgent,
340 // Modern KHTML browsers should qualify as Safari X-Grade
341 if ((/KHTML/).test(ua)) {
344 // Modern WebKit browsers are at least X-Grade
345 m=ua.match(/AppleWebKit\/([^\s]*)/);
347 o.webkit=parseFloat(m[1]);
349 // Mobile browser check
350 if (/ Mobile\//.test(ua)) {
351 o.mobile = "Apple"; // iPhone or iPod Touch
353 m=ua.match(/NokiaN[^\/]*/);
355 o.mobile = m[0]; // Nokia N-series, ex: NokiaN95
359 m=ua.match(/AdobeAIR\/([^\s]*)/);
361 o.air = m[0]; // Adobe AIR 1.0 or better
366 if (!o.webkit) { // not webkit
367 // @todo check Opera/8.01 (J2ME/MIDP; Opera Mini/2.0.4509/1316; fi; U; ssr)
368 m=ua.match(/Opera[\s\/]([^\s]*)/);
370 o.opera=parseFloat(m[1]);
371 m=ua.match(/Opera Mini[^;]*/);
373 o.mobile = m[0]; // ex: Opera Mini/2.0.4509/1316
375 } else { // not opera or webkit
376 m=ua.match(/MSIE\s([^;]*)/);
378 o.ie=parseFloat(m[1]);
379 } else { // not opera, webkit, or ie
380 m=ua.match(/Gecko\/([^\s]*)/);
382 o.gecko=1; // Gecko detected, look for revision
383 m=ua.match(/rv:([^\s\)]*)/);
385 o.gecko=parseFloat(m[1]);
392 m=ua.match(/Caja\/([^\s]*)/);
394 o.caja=parseFloat(m[1]);
401 * Initializes the global by creating the default namespaces and applying
402 * any new configuration information that is detected. This is the setup
409 YAHOO.namespace("util", "widget", "example");
410 /*global YAHOO_config*/
411 if ("undefined" !== typeof YAHOO_config) {
412 var l=YAHOO_config.listener,ls=YAHOO.env.listeners,unique=true,i;
414 // if YAHOO is loaded multiple times we need to check to see if
415 // this is a new config object. If it is, add the new component
416 // load listener to the stack
417 for (i=0;i<ls.length;i=i+1) {
430 * Provides the language utilites and extensions used by the library
433 YAHOO.lang = YAHOO.lang || {};
440 ARRAY_TOSTRING = '[object Array]',
441 FUNCTION_TOSTRING = '[object Function]',
442 OP = Object.prototype,
444 // ADD = ["toString", "valueOf", "hasOwnProperty"],
445 ADD = ["toString", "valueOf"],
450 * Determines wheather or not the provided object is an array.
452 * @param {any} o The object being testing
453 * @return {boolean} the result
455 isArray: function(o) {
456 return OP.toString.apply(o) === ARRAY_TOSTRING;
460 * Determines whether or not the provided object is a boolean
462 * @param {any} o The object being testing
463 * @return {boolean} the result
465 isBoolean: function(o) {
466 return typeof o === 'boolean';
470 * Determines whether or not the provided object is a function.
471 * Note: Internet Explorer thinks certain functions are objects:
473 * var obj = document.createElement("object");
474 * YAHOO.lang.isFunction(obj.getAttribute) // reports false in IE
476 * var input = document.createElement("input"); // append to body
477 * YAHOO.lang.isFunction(input.focus) // reports false in IE
479 * You will have to implement additional tests if these functions
483 * @param {any} o The object being testing
484 * @return {boolean} the result
486 isFunction: function(o) {
487 return OP.toString.apply(o) === FUNCTION_TOSTRING;
491 * Determines whether or not the provided object is null
493 * @param {any} o The object being testing
494 * @return {boolean} the result
496 isNull: function(o) {
501 * Determines whether or not the provided object is a legal number
503 * @param {any} o The object being testing
504 * @return {boolean} the result
506 isNumber: function(o) {
507 return typeof o === 'number' && isFinite(o);
511 * Determines whether or not the provided object is of type object
514 * @param {any} o The object being testing
515 * @return {boolean} the result
517 isObject: function(o) {
518 return (o && (typeof o === 'object' || L.isFunction(o))) || false;
522 * Determines whether or not the provided object is a string
524 * @param {any} o The object being testing
525 * @return {boolean} the result
527 isString: function(o) {
528 return typeof o === 'string';
532 * Determines whether or not the provided object is undefined
533 * @method isUndefined
534 * @param {any} o The object being testing
535 * @return {boolean} the result
537 isUndefined: function(o) {
538 return typeof o === 'undefined';
543 * IE will not enumerate native functions in a derived object even if the
544 * function was overridden. This is a workaround for specific functions
545 * we care about on the Object prototype.
546 * @property _IEEnumFix
547 * @param {Function} r the object to receive the augmentation
548 * @param {Function} s the object that supplies the properties to augment
552 _IEEnumFix: (YAHOO.env.ua.ie) ? function(r, s) {
554 for (i=0;i<ADD.length;i=i+1) {
559 if (L.isFunction(f) && f!=OP[fname]) {
566 * Utility to set up the prototype, constructor and superclass properties to
567 * support an inheritance strategy that can chain constructors and methods.
568 * Static members will not be inherited.
572 * @param {Function} subc the object to modify
573 * @param {Function} superc the object to inherit
574 * @param {Object} overrides additional properties/methods to add to the
575 * subclass prototype. These will override the
576 * matching items obtained from the superclass
579 extend: function(subc, superc, overrides) {
580 if (!superc||!subc) {
581 throw new Error("extend failed, please check that " +
582 "all dependencies are included.");
584 var F = function() {}, i;
585 F.prototype=superc.prototype;
586 subc.prototype=new F();
587 subc.prototype.constructor=subc;
588 subc.superclass=superc.prototype;
589 if (superc.prototype.constructor == OP.constructor) {
590 superc.prototype.constructor=superc;
594 for (i in overrides) {
595 if (L.hasOwnProperty(overrides, i)) {
596 subc.prototype[i]=overrides[i];
600 L._IEEnumFix(subc.prototype, overrides);
605 * Applies all properties in the supplier to the receiver if the
606 * receiver does not have these properties yet. Optionally, one or
607 * more methods/properties can be specified (as additional
608 * parameters). This option will overwrite the property if receiver
609 * has it already. If true is passed as the third parameter, all
610 * properties will be applied and _will_ overwrite properties in
613 * @method augmentObject
616 * @param {Function} r the object to receive the augmentation
617 * @param {Function} s the object that supplies the properties to augment
618 * @param {String*|boolean} arguments zero or more properties methods
619 * to augment the receiver with. If none specified, everything
620 * in the supplier will be used unless it would
621 * overwrite an existing property in the receiver. If true
622 * is specified as the third parameter, all properties will
623 * be applied and will overwrite an existing property in
626 augmentObject: function(r, s) {
628 throw new Error("Absorb failed, verify dependencies.");
630 var a=arguments, i, p, overrideList=a[2];
631 if (overrideList && overrideList!==true) { // only absorb the specified properties
632 for (i=2; i<a.length; i=i+1) {
635 } else { // take everything, overwriting only if the third parameter is true
637 if (overrideList || !(p in r)) {
647 * Same as YAHOO.lang.augmentObject, except it only applies prototype properties
648 * @see YAHOO.lang.augmentObject
649 * @method augmentProto
651 * @param {Function} r the object to receive the augmentation
652 * @param {Function} s the object that supplies the properties to augment
653 * @param {String*|boolean} arguments zero or more properties methods
654 * to augment the receiver with. If none specified, everything
655 * in the supplier will be used unless it would overwrite an existing
656 * property in the receiver. if true is specified as the third
657 * parameter, all properties will be applied and will overwrite an
658 * existing property in the receiver
660 augmentProto: function(r, s) {
662 throw new Error("Augment failed, verify dependencies.");
664 //var a=[].concat(arguments);
665 var a=[r.prototype,s.prototype], i;
666 for (i=2;i<arguments.length;i=i+1) {
667 a.push(arguments[i]);
669 L.augmentObject.apply(this, a);
674 * Returns a simple string representation of the object or array.
675 * Other types of objects will be returned unprocessed. Arrays
676 * are expected to be indexed. Use object notation for
677 * associative arrays.
680 * @param o {Object} The object to dump
681 * @param d {int} How deep to recurse child objects, default 3
682 * @return {String} the dump result
684 dump: function(o, d) {
685 var i,len,s=[],OBJ="{...}",FUN="f(){...}",
686 COMMA=', ', ARROW=' => ';
688 // Cast non-objects to string
689 // Skip dates because the std toString is what we want
690 // Skip HTMLElement-like objects because trying to dump
691 // an element will cause an unhandled exception in FF 2.x
692 if (!L.isObject(o)) {
694 } else if (o instanceof Date || ("nodeType" in o && "tagName" in o)) {
696 } else if (L.isFunction(o)) {
700 // dig into child objects the depth specifed. Default 3
701 d = (L.isNumber(d)) ? d : 3;
706 for (i=0,len=o.length;i<len;i=i+1) {
707 if (L.isObject(o[i])) {
708 s.push((d > 0) ? L.dump(o[i], d-1) : OBJ);
718 // objects {k1 => v1, k2 => v2}
722 if (L.hasOwnProperty(o, i)) {
724 if (L.isObject(o[i])) {
725 s.push((d > 0) ? L.dump(o[i], d-1) : OBJ);
742 * Does variable substitution on a string. It scans through the string
743 * looking for expressions enclosed in { } braces. If an expression
744 * is found, it is used a key on the object. If there is a space in
745 * the key, the first word is used for the key and the rest is provided
746 * to an optional function to be used to programatically determine the
747 * value (the extra information might be used for this decision). If
748 * the value for the key in the object, or what is returned from the
749 * function has a string value, number value, or object value, it is
750 * substituted for the bracket expression and it repeats. If this
751 * value is an object, it uses the Object's toString() if this has
752 * been overridden, otherwise it does a shallow dump of the key/value
756 * @param s {String} The string that will be modified.
757 * @param o {Object} An object containing the replacement values
758 * @param f {Function} An optional function that can be used to
759 * process each match. It receives the key,
760 * value, and any extra metadata included with
761 * the key inside of the braces.
762 * @return {String} the substituted string
764 substitute: function (s, o, f) {
765 var i, j, k, key, v, meta, saved=[], token,
766 DUMP='dump', SPACE=' ', LBRACE='{', RBRACE='}',
771 i = s.lastIndexOf(LBRACE);
775 j = s.indexOf(RBRACE, i);
780 //Extract key and meta info
781 token = s.substring(i + 1, j);
784 k = key.indexOf(SPACE);
786 meta = key.substring(k + 1);
787 key = key.substring(0, k);
793 // if a substitution function was provided, execute it
800 v = L.dump(v, parseInt(meta, 10));
804 // look for the keyword 'dump', if found force obj dump
805 dump = meta.indexOf(DUMP);
807 meta = meta.substring(4);
810 // use the toString if it is not the Object toString
811 // and the 'dump' meta info was not found
812 if (v.toString===OP.toString || dump>-1) {
813 v = L.dump(v, parseInt(meta, 10));
818 } else if (!L.isString(v) && !L.isNumber(v)) {
819 // This {block} has no replace string. Save it for later.
820 v = "~-" + saved.length + "-~";
821 saved[saved.length] = token;
826 s = s.substring(0, i) + v + s.substring(j + 1);
831 // restore saved {block}s
832 for (i=saved.length-1; i>=0; i=i-1) {
833 s = s.replace(new RegExp("~-" + i + "-~"), "{" + saved[i] + "}", "g");
841 * Returns a string without any leading or trailing whitespace. If
842 * the input is not a string, the input will be returned untouched.
845 * @param s {string} the string to trim
846 * @return {string} the trimmed string
850 return s.replace(/^\s+|\s+$/g, "");
857 * Returns a new object containing all of the properties of
858 * all the supplied objects. The properties from later objects
859 * will overwrite those in earlier objects.
862 * @param arguments {Object*} the objects to merge
863 * @return the new merged object
866 var o={}, a=arguments, l=a.length, i;
867 for (i=0; i<l; i=i+1) {
868 L.augmentObject(o, a[i], true);
874 * Executes the supplied function in the context of the supplied
875 * object 'when' milliseconds later. Executes the function a
876 * single time unless periodic is set to true.
879 * @param when {int} the number of milliseconds to wait until the fn
881 * @param o the context object
882 * @param fn {Function|String} the function to execute or the name of
883 * the method in the 'o' object to execute
884 * @param data [Array] data that is provided to the function. This accepts
885 * either a single item or an array. If an array is provided, the
886 * function is executed with one parameter for each array item. If
887 * you need to pass a single array parameter, it needs to be wrapped in
889 * @param periodic {boolean} if true, executes continuously at supplied
890 * interval until canceled
891 * @return a timer object. Call the cancel() method on this object to
894 later: function(when, o, fn, data, periodic) {
897 var m=fn, d=data, f, r;
899 if (L.isString(fn)) {
904 throw new TypeError("method undefined");
915 r = (periodic) ? setInterval(f, when) : setTimeout(f, when);
930 * A convenience method for detecting a legitimate non-null value.
931 * Returns false for null/undefined/NaN, true for other values,
932 * including 0/false/''
935 * @param o {any} the item to test
936 * @return {boolean} true if it is not null/undefined/NaN || false
938 isValue: function(o) {
939 // return (o || o === false || o === 0 || o === ''); // Infinity fails
940 return (L.isObject(o) || L.isString(o) || L.isNumber(o) || L.isBoolean(o));
946 * Determines whether or not the property was added
947 * to the object instance. Returns false if the property is not present
948 * in the object, or was inherited from the prototype.
949 * This abstraction is provided to enable hasOwnProperty for Safari 1.3.x.
950 * There is a discrepancy between YAHOO.lang.hasOwnProperty and
951 * Object.prototype.hasOwnProperty when the property is a primitive added to
952 * both the instance AND prototype with the same value:
954 * var A = function() {};
955 * A.prototype.foo = 'foo';
958 * alert(a.hasOwnProperty('foo')); // true
959 * alert(YAHOO.lang.hasOwnProperty(a, 'foo')); // false when using fallback
961 * @method hasOwnProperty
962 * @param {any} o The object being testing
963 * @param prop {string} the name of the property to test
964 * @return {boolean} the result
966 L.hasOwnProperty = (OP.hasOwnProperty) ?
968 return o && o.hasOwnProperty(prop);
969 } : function(o, prop) {
970 return !L.isUndefined(o[prop]) &&
971 o.constructor.prototype[prop] !== o[prop];
975 OB.augmentObject(L, OB, true);
978 * An alias for <a href="YAHOO.lang.html">YAHOO.lang</a>
979 * @class YAHOO.util.Lang
984 * Same as YAHOO.lang.augmentObject, except it only applies prototype
985 * properties. This is an alias for augmentProto.
986 * @see YAHOO.lang.augmentObject
989 * @param {Function} r the object to receive the augmentation
990 * @param {Function} s the object that supplies the properties to augment
991 * @param {String*|boolean} arguments zero or more properties methods to
992 * augment the receiver with. If none specified, everything
993 * in the supplier will be used unless it would
994 * overwrite an existing property in the receiver. if true
995 * is specified as the third parameter, all properties will
996 * be applied and will overwrite an existing property in
999 L.augment = L.augmentProto;
1002 * An alias for <a href="YAHOO.lang.html#augment">YAHOO.lang.augment</a>
1006 * @param {Function} r the object to receive the augmentation
1007 * @param {Function} s the object that supplies the properties to augment
1008 * @param {String*} arguments zero or more properties methods to
1009 * augment the receiver with. If none specified, everything
1010 * in the supplier will be used unless it would
1011 * overwrite an existing property in the receiver
1013 YAHOO.augment = L.augmentProto;
1016 * An alias for <a href="YAHOO.lang.html#extend">YAHOO.lang.extend</a>
1019 * @param {Function} subc the object to modify
1020 * @param {Function} superc the object to inherit
1021 * @param {Object} overrides additional properties/methods to add to the
1022 * subclass prototype. These will override the
1023 * matching items obtained from the superclass if present.
1025 YAHOO.extend = L.extend;
1028 YAHOO.register("yahoo", YAHOO, {version: "2.7.0", build: "1799"});