2 Copyright (c) 2008, 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
93 * @param {String*} arguments 1-n namespaces to create
94 * @return {Object} A reference to the last namespace object created
96 YAHOO
.namespace = function() {
97 var a
=arguments
, o
=null, i
, j
, d
;
98 for (i
=0; i
<a
.length
; i
=i
+1) {
102 // YAHOO is implied, so it is ignored if it is included
103 for (j
=(d
[0] == "YAHOO") ? 1 : 0; j
<d
.length
; j
=j
+1) {
104 o
[d
[j
]]=o
[d
[j
]] || {};
113 * Uses YAHOO.widget.Logger to output a log message, if the widget is
118 * @param {String} msg The message to log.
119 * @param {String} cat The log category for the message. Default
120 * categories are "info", "warn", "error", time".
121 * Custom categories can be used as well. (opt)
122 * @param {String} src The source of the the message (opt)
123 * @return {Boolean} True if the log operation was successful.
125 YAHOO
.log = function(msg
, cat
, src
) {
126 var l
=YAHOO
.widget
.Logger
;
128 return l
.log(msg
, cat
, src
);
135 * Registers a module with the YAHOO object
138 * @param {String} name the name of the module (event, slider, etc)
139 * @param {Function} mainClass a reference to class in the module. This
140 * class will be tagged with the version info
141 * so that it will be possible to identify the
142 * version that is in use when multiple versions
144 * @param {Object} data metadata object for the module. Currently it
145 * is expected to contain a "version" property
146 * and a "build" property at minimum.
148 YAHOO
.register = function(name
, mainClass
, data
) {
149 var mods
= YAHOO
.env
.modules
;
151 mods
[name
] = { versions
:[], builds
:[] };
153 var m
=mods
[name
],v
=data
.version
,b
=data
.build
,ls
=YAHOO
.env
.listeners
;
159 m
.mainClass
= mainClass
;
160 // fire the module load listeners
161 for (var i
=0;i
<ls
.length
;i
=i
+1) {
164 // label the main class
166 mainClass
.VERSION
= v
;
169 YAHOO
.log("mainClass is undefined for module " + name
, "warn");
174 * YAHOO.env is used to keep track of what is known about the YUI library and
175 * the browsing environment
179 YAHOO
.env
= YAHOO
.env
|| {
182 * Keeps the version info for all YUI modules that have reported themselves
189 * List of functions that should be executed every time a YUI module
191 * @property listeners
198 * Returns the version data for the specified module:
200 * <dt>name:</dt> <dd>The name of the module</dd>
201 * <dt>version:</dt> <dd>The version in use</dd>
202 * <dt>build:</dt> <dd>The build number in use</dd>
203 * <dt>versions:</dt> <dd>All versions that were registered</dd>
204 * <dt>builds:</dt> <dd>All builds that were registered.</dd>
205 * <dt>mainClass:</dt> <dd>An object that was was stamped with the
206 * current version and build. If
207 * mainClass.VERSION != version or mainClass.BUILD != build,
208 * multiple versions of pieces of the library have been
209 * loaded, potentially causing issues.</dd>
214 * @param {String} name the name of the module (event, slider, etc)
215 * @return {Object} The version info
217 YAHOO
.env
.getVersion = function(name
) {
218 return YAHOO
.env
.modules
[name
] || null;
222 * Do not fork for a browser if it can be avoided. Use feature detection when
223 * you can. Use the user agent as a last resort. YAHOO.env.ua stores a version
224 * number for the browser engine, 0 otherwise. This value may or may not map
225 * to the version number of the browser using the engine. The value is
226 * presented as a float so that it can easily be used for boolean evaluation
227 * as well as for looking for a particular range of versions. Because of this,
228 * some of the granularity of the version info may be lost (e.g., Gecko 1.8.0.9
230 * @class YAHOO.env.ua
233 YAHOO
.env
.ua = function() {
237 * Internet Explorer version number or 0. Example: 6
244 * Opera version number or 0. Example: 9.2
251 * Gecko engine revision number. Will evaluate to 1 if Gecko
252 * is detected but the revision could not be found. Other browsers
253 * will be 0. Example: 1.8
255 * Firefox 1.0.0.4: 1.7.8 <-- Reports 1.7
256 * Firefox 1.5.0.9: 1.8.0.9 <-- Reports 1.8
257 * Firefox 2.0.0.3: 1.8.1.3 <-- Reports 1.8
258 * Firefox 3 alpha: 1.9a4 <-- Reports 1.9
266 * AppleWebKit version. KHTML browsers that are not WebKit browsers
267 * will evaluate to 1, other browsers 0. Example: 418.9.1
269 * Safari 1.3.2 (312.6): 312.8.1 <-- Reports 312.8 -- currently the
270 * latest available for Mac OSX 10.3.
271 * Safari 2.0.2: 416 <-- hasOwnProperty introduced
272 * Safari 2.0.4: 418 <-- preventDefault fixed
273 * Safari 2.0.4 (419.3): 418.9.1 <-- One version of Safari may run
274 * different versions of webkit
275 * Safari 2.0.4 (419.3): 419 <-- Tiger installations that have been
276 * updated, but not updated
277 * to the latest patch.
278 * Webkit 212 nightly: 522+ <-- Safari 3.0 precursor (with native SVG
279 * and many major issues fixed).
280 * 3.x yahoo.com, flickr:422 <-- Safari 3.x hacks the user agent
281 * string when hitting yahoo.com and
283 * Safari 3.0.4 (523.12):523.12 <-- First Tiger release - automatic update
284 * from 2.x via the 10.4.11 OS patch
285 * Webkit nightly 1/2008:525+ <-- Supports DOMContentLoaded event.
286 * yahoo.com user agent hack removed.
289 * http://developer.apple.com/internet/safari/uamatrix.html
296 * The mobile property will be set to a string containing any relevant
297 * user agent information when a modern mobile browser is detected.
298 * Currently limited to Safari on the iPhone/iPod Touch, Nokia N-series
299 * devices with the WebKit-based browser, and Opera Mini.
306 * Adobe AIR version number or 0. Only populated if webkit is detected.
315 var ua
=navigator
.userAgent
, m
;
317 // Modern KHTML browsers should qualify as Safari X-Grade
318 if ((/KHTML/).test(ua
)) {
321 // Modern WebKit browsers are at least X-Grade
322 m
=ua
.match(/AppleWebKit\/([^\s]*)/);
324 o
.webkit
=parseFloat(m
[1]);
326 // Mobile browser check
327 if (/ Mobile
\//.test(ua)) {
328 o
.mobile
= "Apple"; // iPhone or iPod Touch
330 m
=ua
.match(/NokiaN[^\/]*/);
332 o
.mobile
= m
[0]; // Nokia N-series, ex: NokiaN95
336 m
=ua
.match(/AdobeAIR\/([^\s]*)/);
338 o
.air
= m
[0]; // Adobe AIR 1.0 or better
343 if (!o
.webkit
) { // not webkit
344 // @todo check Opera/8.01 (J2ME/MIDP; Opera Mini/2.0.4509/1316; fi; U; ssr)
345 m
=ua
.match(/Opera[\s\/]([^\s]*)/);
347 o
.opera
=parseFloat(m
[1]);
348 m
=ua
.match(/Opera Mini[^;]*/);
350 o
.mobile
= m
[0]; // ex: Opera Mini/2.0.4509/1316
352 } else { // not opera or webkit
353 m
=ua
.match(/MSIE\s([^;]*)/);
355 o
.ie
=parseFloat(m
[1]);
356 } else { // not opera, webkit, or ie
357 m
=ua
.match(/Gecko\/([^\s]*)/);
359 o
.gecko
=1; // Gecko detected, look for revision
360 m
=ua
.match(/rv:([^\s\)]*)/);
362 o
.gecko
=parseFloat(m
[1]);
373 * Initializes the global by creating the default namespaces and applying
374 * any new configuration information that is detected. This is the setup
381 YAHOO
.namespace("util", "widget", "example");
382 if ("undefined" !== typeof YAHOO_config
) {
383 var l
=YAHOO_config
.listener
,ls
=YAHOO
.env
.listeners
,unique
=true,i
;
385 // if YAHOO is loaded multiple times we need to check to see if
386 // this is a new config object. If it is, add the new component
387 // load listener to the stack
388 for (i
=0;i
<ls
.length
;i
=i
+1) {
401 * Provides the language utilites and extensions used by the library
404 YAHOO
.lang
= YAHOO
.lang
|| {};
410 // ADD = ["toString", "valueOf", "hasOwnProperty"],
411 ADD
= ["toString", "valueOf"],
416 * Determines whether or not the provided object is an array.
417 * Testing typeof/instanceof/constructor of arrays across frame
418 * boundaries isn't possible in Safari unless you have a reference
419 * to the other frame to test against its Array prototype. To
420 * handle this case, we test well-known array properties instead.
423 * @param {any} o The object being testing
424 * @return {boolean} the result
426 isArray: function(o
) {
428 return L
.isNumber(o
.length
) && L
.isFunction(o
.splice
);
434 * Determines whether or not the provided object is a boolean
436 * @param {any} o The object being testing
437 * @return {boolean} the result
439 isBoolean: function(o
) {
440 return typeof o
=== 'boolean';
444 * Determines whether or not the provided object is a function
446 * @param {any} o The object being testing
447 * @return {boolean} the result
449 isFunction: function(o
) {
450 return typeof o
=== 'function';
454 * Determines whether or not the provided object is null
456 * @param {any} o The object being testing
457 * @return {boolean} the result
459 isNull: function(o
) {
464 * Determines whether or not the provided object is a legal number
466 * @param {any} o The object being testing
467 * @return {boolean} the result
469 isNumber: function(o
) {
470 return typeof o
=== 'number' && isFinite(o
);
474 * Determines whether or not the provided object is of type object
477 * @param {any} o The object being testing
478 * @return {boolean} the result
480 isObject: function(o
) {
481 return (o
&& (typeof o
=== 'object' || L
.isFunction(o
))) || false;
485 * Determines whether or not the provided object is a string
487 * @param {any} o The object being testing
488 * @return {boolean} the result
490 isString: function(o
) {
491 return typeof o
=== 'string';
495 * Determines whether or not the provided object is undefined
496 * @method isUndefined
497 * @param {any} o The object being testing
498 * @return {boolean} the result
500 isUndefined: function(o
) {
501 return typeof o
=== 'undefined';
506 * IE will not enumerate native functions in a derived object even if the
507 * function was overridden. This is a workaround for specific functions
508 * we care about on the Object prototype.
509 * @property _IEEnumFix
510 * @param {Function} r the object to receive the augmentation
511 * @param {Function} s the object that supplies the properties to augment
515 _IEEnumFix
: (YAHOO
.env
.ua
.ie
) ? function(r
, s
) {
516 for (var i
=0;i
<ADD
.length
;i
=i
+1) {
517 var fname
=ADD
[i
],f
=s
[fname
];
518 if (L
.isFunction(f
) && f
!=Object
.prototype[fname
]) {
525 * Utility to set up the prototype, constructor and superclass properties to
526 * support an inheritance strategy that can chain constructors and methods.
527 * Static members will not be inherited.
531 * @param {Function} subc the object to modify
532 * @param {Function} superc the object to inherit
533 * @param {Object} overrides additional properties/methods to add to the
534 * subclass prototype. These will override the
535 * matching items obtained from the superclass
538 extend: function(subc
, superc
, overrides
) {
539 if (!superc
||!subc
) {
540 throw new Error("extend failed, please check that " +
541 "all dependencies are included.");
543 var F = function() {};
544 F
.prototype=superc
.prototype;
545 subc
.prototype=new F();
546 subc
.prototype.constructor=subc
;
547 subc
.superclass
=superc
.prototype;
548 if (superc
.prototype.constructor == Object
.prototype.constructor) {
549 superc
.prototype.constructor=superc
;
553 for (var i
in overrides
) {
554 if (L
.hasOwnProperty(overrides
, i
)) {
555 subc
.prototype[i
]=overrides
[i
];
559 L
._IEEnumFix(subc
.prototype, overrides
);
564 * Applies all properties in the supplier to the receiver if the
565 * receiver does not have these properties yet. Optionally, one or
566 * more methods/properties can be specified (as additional
567 * parameters). This option will overwrite the property if receiver
568 * has it already. If true is passed as the third parameter, all
569 * properties will be applied and _will_ overwrite properties in
572 * @method augmentObject
575 * @param {Function} r the object to receive the augmentation
576 * @param {Function} s the object that supplies the properties to augment
577 * @param {String*|boolean} arguments zero or more properties methods
578 * to augment the receiver with. If none specified, everything
579 * in the supplier will be used unless it would
580 * overwrite an existing property in the receiver. If true
581 * is specified as the third parameter, all properties will
582 * be applied and will overwrite an existing property in
585 augmentObject: function(r
, s
) {
587 throw new Error("Absorb failed, verify dependencies.");
589 var a
=arguments
, i
, p
, override
=a
[2];
590 if (override
&& override
!==true) { // only absorb the specified properties
591 for (i
=2; i
<a
.length
; i
=i
+1) {
594 } else { // take everything, overwriting only if the third parameter is true
596 if (override
|| !(p
in r
)) {
606 * Same as YAHOO.lang.augmentObject, except it only applies prototype properties
607 * @see YAHOO.lang.augmentObject
608 * @method augmentProto
610 * @param {Function} r the object to receive the augmentation
611 * @param {Function} s the object that supplies the properties to augment
612 * @param {String*|boolean} arguments zero or more properties methods
613 * to augment the receiver with. If none specified, everything
614 * in the supplier will be used unless it would overwrite an existing
615 * property in the receiver. if true is specified as the third
616 * parameter, all properties will be applied and will overwrite an
617 * existing property in the receiver
619 augmentProto: function(r
, s
) {
621 throw new Error("Augment failed, verify dependencies.");
623 //var a=[].concat(arguments);
624 var a
=[r
.prototype,s
.prototype];
625 for (var i
=2;i
<arguments
.length
;i
=i
+1) {
626 a
.push(arguments
[i
]);
628 L
.augmentObject
.apply(this, a
);
633 * Returns a simple string representation of the object or array.
634 * Other types of objects will be returned unprocessed. Arrays
635 * are expected to be indexed. Use object notation for
636 * associative arrays.
639 * @param o {Object} The object to dump
640 * @param d {int} How deep to recurse child objects, default 3
641 * @return {String} the dump result
643 dump: function(o
, d
) {
644 var i
,len
,s
=[],OBJ
="{...}",FUN
="f(){...}",
645 COMMA
=', ', ARROW
=' => ';
647 // Cast non-objects to string
648 // Skip dates because the std toString is what we want
649 // Skip HTMLElement-like objects because trying to dump
650 // an element will cause an unhandled exception in FF 2.x
651 if (!L
.isObject(o
)) {
653 } else if (o
instanceof Date
|| ("nodeType" in o
&& "tagName" in o
)) {
655 } else if (L
.isFunction(o
)) {
659 // dig into child objects the depth specifed. Default 3
660 d
= (L
.isNumber(d
)) ? d
: 3;
665 for (i
=0,len
=o
.length
;i
<len
;i
=i
+1) {
666 if (L
.isObject(o
[i
])) {
667 s
.push((d
> 0) ? L
.dump(o
[i
], d
-1) : OBJ
);
677 // objects {k1 => v1, k2 => v2}
681 if (L
.hasOwnProperty(o
, i
)) {
683 if (L
.isObject(o
[i
])) {
684 s
.push((d
> 0) ? L
.dump(o
[i
], d
-1) : OBJ
);
701 * Does variable substitution on a string. It scans through the string
702 * looking for expressions enclosed in { } braces. If an expression
703 * is found, it is used a key on the object. If there is a space in
704 * the key, the first word is used for the key and the rest is provided
705 * to an optional function to be used to programatically determine the
706 * value (the extra information might be used for this decision). If
707 * the value for the key in the object, or what is returned from the
708 * function has a string value, number value, or object value, it is
709 * substituted for the bracket expression and it repeats. If this
710 * value is an object, it uses the Object's toString() if this has
711 * been overridden, otherwise it does a shallow dump of the key/value
715 * @param s {String} The string that will be modified.
716 * @param o {Object} An object containing the replacement values
717 * @param f {Function} An optional function that can be used to
718 * process each match. It receives the key,
719 * value, and any extra metadata included with
720 * the key inside of the braces.
721 * @return {String} the substituted string
723 substitute: function (s
, o
, f
) {
724 var i
, j
, k
, key
, v
, meta
, saved
=[], token
,
725 DUMP
='dump', SPACE
=' ', LBRACE
='{', RBRACE
='}';
729 i
= s
.lastIndexOf(LBRACE
);
733 j
= s
.indexOf(RBRACE
, i
);
738 //Extract key and meta info
739 token
= s
.substring(i
+ 1, j
);
742 k
= key
.indexOf(SPACE
);
744 meta
= key
.substring(k
+ 1);
745 key
= key
.substring(0, k
);
751 // if a substitution function was provided, execute it
758 v
= L
.dump(v
, parseInt(meta
, 10));
762 // look for the keyword 'dump', if found force obj dump
763 var dump
= meta
.indexOf(DUMP
);
765 meta
= meta
.substring(4);
768 // use the toString if it is not the Object toString
769 // and the 'dump' meta info was not found
770 if (v
.toString
===Object
.prototype.toString
||dump
>-1) {
771 v
= L
.dump(v
, parseInt(meta
, 10));
776 } else if (!L
.isString(v
) && !L
.isNumber(v
)) {
777 // This {block} has no replace string. Save it for later.
778 v
= "~-" + saved
.length
+ "-~";
779 saved
[saved
.length
] = token
;
784 s
= s
.substring(0, i
) + v
+ s
.substring(j
+ 1);
789 // restore saved {block}s
790 for (i
=saved
.length
-1; i
>=0; i
=i
-1) {
791 s
= s
.replace(new RegExp("~-" + i
+ "-~"), "{" + saved
[i
] + "}", "g");
799 * Returns a string without any leading or trailing whitespace. If
800 * the input is not a string, the input will be returned untouched.
803 * @param s {string} the string to trim
804 * @return {string} the trimmed string
808 return s
.replace(/^\s+|\s+$/g, "");
815 * Returns a new object containing all of the properties of
816 * all the supplied objects. The properties from later objects
817 * will overwrite those in earlier objects.
820 * @param arguments {Object*} the objects to merge
821 * @return the new merged object
824 var o
={}, a
=arguments
;
825 for (var i
=0, l
=a
.length
; i
<l
; i
=i
+1) {
826 L
.augmentObject(o
, a
[i
], true);
832 * Executes the supplied function in the context of the supplied
833 * object 'when' milliseconds later. Executes the function a
834 * single time unless periodic is set to true.
837 * @param when {int} the number of milliseconds to wait until the fn
839 * @param o the context object
840 * @param fn {Function|String} the function to execute or the name of
841 * the method in the 'o' object to execute
842 * @param data [Array] data that is provided to the function. This accepts
843 * either a single item or an array. If an array is provided, the
844 * function is executed with one parameter for each array item. If
845 * you need to pass a single array parameter, it needs to be wrapped in
847 * @param periodic {boolean} if true, executes continuously at supplied
848 * interval until canceled
849 * @return a timer object. Call the cancel() method on this object to
852 later: function(when
, o
, fn
, data
, periodic
) {
855 var m
=fn
, d
=data
, f
, r
;
857 if (L
.isString(fn
)) {
862 throw new TypeError("method undefined");
873 r
= (periodic
) ? setInterval(f
, when
) : setTimeout(f
, when
);
888 * A convenience method for detecting a legitimate non-null value.
889 * Returns false for null/undefined/NaN, true for other values,
890 * including 0/false/''
893 * @param o {any} the item to test
894 * @return {boolean} true if it is not null/undefined/NaN || false
896 isValue: function(o
) {
897 // return (o || o === false || o === 0 || o === ''); // Infinity fails
898 return (L
.isObject(o
) || L
.isString(o
) || L
.isNumber(o
) || L
.isBoolean(o
));
904 * Determines whether or not the property was added
905 * to the object instance. Returns false if the property is not present
906 * in the object, or was inherited from the prototype.
907 * This abstraction is provided to enable hasOwnProperty for Safari 1.3.x.
908 * There is a discrepancy between YAHOO.lang.hasOwnProperty and
909 * Object.prototype.hasOwnProperty when the property is a primitive added to
910 * both the instance AND prototype with the same value:
912 * var A = function() {};
913 * A.prototype.foo = 'foo';
916 * alert(a.hasOwnProperty('foo')); // true
917 * alert(YAHOO.lang.hasOwnProperty(a, 'foo')); // false when using fallback
919 * @method hasOwnProperty
920 * @param {any} o The object being testing
921 * @param prop {string} the name of the property to test
922 * @return {boolean} the result
924 L
.hasOwnProperty
= (Object
.prototype.hasOwnProperty
) ?
926 return o
&& o
.hasOwnProperty(prop
);
927 } : function(o
, prop
) {
928 return !L
.isUndefined(o
[prop
]) &&
929 o
.constructor.prototype[prop
] !== o
[prop
];
933 OB
.augmentObject(L
, OB
, true);
936 * An alias for <a href="YAHOO.lang.html">YAHOO.lang</a>
937 * @class YAHOO.util.Lang
942 * Same as YAHOO.lang.augmentObject, except it only applies prototype
943 * properties. This is an alias for augmentProto.
944 * @see YAHOO.lang.augmentObject
947 * @param {Function} r the object to receive the augmentation
948 * @param {Function} s the object that supplies the properties to augment
949 * @param {String*|boolean} arguments zero or more properties methods to
950 * augment the receiver with. If none specified, everything
951 * in the supplier will be used unless it would
952 * overwrite an existing property in the receiver. if true
953 * is specified as the third parameter, all properties will
954 * be applied and will overwrite an existing property in
957 L
.augment
= L
.augmentProto
;
960 * An alias for <a href="YAHOO.lang.html#augment">YAHOO.lang.augment</a>
964 * @param {Function} r the object to receive the augmentation
965 * @param {Function} s the object that supplies the properties to augment
966 * @param {String*} arguments zero or more properties methods to
967 * augment the receiver with. If none specified, everything
968 * in the supplier will be used unless it would
969 * overwrite an existing property in the receiver
971 YAHOO
.augment
= L
.augmentProto
;
974 * An alias for <a href="YAHOO.lang.html#extend">YAHOO.lang.extend</a>
977 * @param {Function} subc the object to modify
978 * @param {Function} superc the object to inherit
979 * @param {Object} overrides additional properties/methods to add to the
980 * subclass prototype. These will override the
981 * matching items obtained from the superclass if present.
983 YAHOO
.extend
= L
.extend
;
986 YAHOO
.register("yahoo", YAHOO
, {version
: "2.6.0", build
: "1321"});