yui-core.js revision 63898dadaf6dd411a13dd8ba0e89f6edbc715363
5911N/A(function() {
5911N/A
5911N/Avar L = Y.Lang,
5911N/ADELIMITER = '__',
5911N/A// FROZEN = {
5911N/A// 'prototype': 1,
5911N/A// '_yuid': 1
5911N/A// },
5911N/A
5911N/A/*
5911N/A * IE will not enumerate native functions in a derived object even if the
5911N/A * function was overridden. This is a workaround for specific functions
5911N/A * we care about on the Object prototype.
5911N/A * @property _iefix
5911N/A * @param {Function} r the object to receive the augmentation
5911N/A * @param {Function} s the object that supplies the properties to augment
5911N/A * @private
5911N/A * @for YUI
5911N/A */
5911N/A_iefix = function(r, s) {
5911N/A var fn = s.toString;
5911N/A if (L.isFunction(fn) && fn != Object.prototype.toString) {
5911N/A r.toString = fn;
5911N/A }
5911N/A};
5911N/A
5911N/A
5911N/A/**
5911N/A * Returns a new object containing all of the properties of
5911N/A * all the supplied objects. The properties from later objects
5911N/A * will overwrite those in earlier objects. Passing in a
5911N/A * single object will create a shallow copy of it. For a deep
5911N/A * copy, use clone.
5911N/A * @method merge
7391N/A * @param arguments {Object*} the objects to merge
7391N/A * @return {object} the new merged object
5911N/A */
5911N/AY.merge = function() {
7391N/A var a = arguments, o = {}, i, l = a.length;
6532N/A for (i=0; i<l; i=i+1) {
5911N/A Y.mix(o, a[i], true);
5911N/A }
5911N/A return o;
7391N/A};
5911N/A
6552N/A/**
6552N/A * Applies the supplier's properties to the receiver. By default
6552N/A * all prototype and static propertes on the supplier are applied
6552N/A * to the corresponding spot on the receiver. By default all
6552N/A * properties are applied, and a property that is already on the
5911N/A * reciever will not be overwritten. The default behavior can
5911N/A * be modified by supplying the appropriate parameters.
5911N/A *
5911N/A * @TODO add constants for the modes
6552N/A *
5911N/A * @method mix
5911N/A * @param {Function} r the object to receive the augmentation
5911N/A * @param {Function} s the object that supplies the properties to augment
5911N/A * @param ov {boolean} if true, properties already on the receiver
5911N/A * will be overwritten if found on the supplier.
5911N/A * @param wl {string[]} a whitelist. If supplied, only properties in
5911N/A * this list will be applied to the receiver.
5911N/A * @param {int} mode what should be copies, and to where
5911N/A * default(0): object to object
6532N/A * 1: prototype to prototype (old augment)
6532N/A * 2: prototype to prototype and object props (new augment)
6532N/A * 3: prototype to object
5911N/A * 4: object to prototype
5911N/A * @param merge {boolean} merge objects instead of overwriting/ignoring
5911N/A * Used by Y.aggregate
6539N/A * @return {object} the augmented object
5911N/A */
5911N/AY.mix = function(r, s, ov, wl, mode, merge) {
5911N/A
5911N/A if (!s||!r) {
5911N/A return r || Y;
5911N/A }
5911N/A
5911N/A if (mode) {
5911N/A switch (mode) {
5911N/A case 1: // proto to proto
5911N/A return Y.mix(r.prototype, s.prototype);
5911N/A case 2: // object to object and proto to proto
5911N/A Y.mix(r.prototype, s.prototype);
5911N/A break; // pass through
5911N/A case 3: // proto to static
5911N/A return Y.mix(r, s.prototype);
5911N/A case 4: // static to proto
5911N/A return Y.mix(r.prototype, s);
5911N/A default: // object to object is what happens below
5911N/A }
5911N/A }
5911N/A
5911N/A // Maybe don't even need this wl && wl.length check anymore??
5911N/A var arr = merge && L.isArray(r), i, l, p;
5911N/A
5911N/A if (wl && wl.length) {
5911N/A for (i = 0, l = wl.length; i < l; ++i) {
5911N/A p = wl[i];
5911N/A if (p in s) {
5911N/A if (merge && L.isObject(r[p], true)) {
5911N/A Y.mix(r[p], s[p]);
5911N/A } else if (!arr && (ov || !(p in r))) {
5911N/A r[p] = s[p];
5911N/A } else if (arr) {
5911N/A r.push(s[p]);
5911N/A }
5911N/A }
6539N/A }
5911N/A } else {
5911N/A for (i in s) {
5911N/A // if (s.hasOwnProperty(i) && !(i in FROZEN)) {
5911N/A // check white list if it was supplied
5911N/A // if the receiver has this property, it is an object,
5911N/A // and merge is specified, merge the two objects.
5911N/A if (merge && L.isObject(r[i], true)) {
5911N/A Y.mix(r[i], s[i]); // recursive
5911N/A // otherwise apply the property only if overwrite
5911N/A // is specified or the receiver doesn't have one.
5911N/A } else if (!arr && (ov || !(i in r))) {
5911N/A r[i] = s[i];
5911N/A // if merge is specified and the receiver is an array,
5911N/A // append the array item
5911N/A } else if (arr) {
5911N/A r.push(s[i]);
5911N/A }
5911N/A // }
5911N/A }
5911N/A
5911N/A if (Y.UA.ie) {
5911N/A _iefix(r, s);
5911N/A }
5911N/A }
5911N/A
5911N/A return r;
5911N/A};
6532N/A
5911N/A/**
5911N/A * Returns a wrapper for a function which caches the
6539N/A * return value of that function, keyed off of the combined
6539N/A * argument values.
5911N/A * @function cached
5911N/A * @param source {function} the function to memoize
5911N/A * @param cache an optional cache seed
6539N/A * @return {Function} the wrapped function
6539N/A */
5911N/AY.cached = function(source, cache){
5911N/A cache = cache || {};
5911N/A
5911N/A return function(arg1, arg2) {
6539N/A var a = arguments,
5911N/A // key = arg2 ? Y.Array(a, 0, true).join(DELIMITER) : arg1;
5911N/A key = arg2 ? Array.prototype.join.call(a, DELIMITER) : arg1;
5911N/A
5911N/A if (!(key in cache)) {
5911N/A cache[key] = source.apply(source, a);
5911N/A }
5911N/A
5911N/A return cache[key];
5911N/A };
5911N/A
5911N/A};
5911N/A
5911N/A})();
5911N/A
5911N/A