oop-debug.js revision 23209f57fce338501bc1dc828a991d103732b92f
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncYUI.add('oop', function(Y) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync/**
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * Supplies object inheritance and manipulation utilities. This adds
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * additional functionaity to what is provided in yui-base, and the
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * methods are applied directly to the YUI instance. This module
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * is required for most YUI components.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @module oop
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
14d0566627e0dd5853cae37a1ae7b8e28a99e0f5vboxsync var L = Y.Lang,
14d0566627e0dd5853cae37a1ae7b8e28a99e0f5vboxsync A = Y.Array,
14d0566627e0dd5853cae37a1ae7b8e28a99e0f5vboxsync OP = Object.prototype,
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync CLONE_MARKER = "_~yuim~_";
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // dispatch = function(o, f, c, proto, action) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // if (o[action] && o.item) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // return o[action].call(o, f, c);
2e6dc32bcc9c3a3e70c957764c033b1f402bc617vboxsync // } else {
2e6dc32bcc9c3a3e70c957764c033b1f402bc617vboxsync // switch (A.test(o)) {
c12885bf3de22fa504a7b9ddc41473b485d9ab25vboxsync // case 1:
2e6dc32bcc9c3a3e70c957764c033b1f402bc617vboxsync // return A[action](o, f, c);
2e6dc32bcc9c3a3e70c957764c033b1f402bc617vboxsync // case 2:
a7ba3d5f31ca70d04a3933e570374e5ec5eff84avboxsync // return A[action](Y.Array(o, 0, true), f, c);
a7ba3d5f31ca70d04a3933e570374e5ec5eff84avboxsync // default:
a7ba3d5f31ca70d04a3933e570374e5ec5eff84avboxsync // return Y.Object[action](o, f, c, proto);
a7ba3d5f31ca70d04a3933e570374e5ec5eff84avboxsync // }
a7ba3d5f31ca70d04a3933e570374e5ec5eff84avboxsync // }
8cfe2efff2058bd07777056112155ea5353dcfbavboxsync // };
8cfe2efff2058bd07777056112155ea5353dcfbavboxsync
8cfe2efff2058bd07777056112155ea5353dcfbavboxsync /**
8cfe2efff2058bd07777056112155ea5353dcfbavboxsync * The following methods are added to the YUI instance
8cfe2efff2058bd07777056112155ea5353dcfbavboxsync * @class YUI~oop
8cfe2efff2058bd07777056112155ea5353dcfbavboxsync */
ea1cc8df95dba6fca9c36c94f565ef95c7802a36vboxsync
ea1cc8df95dba6fca9c36c94f565ef95c7802a36vboxsync /**
8cfe2efff2058bd07777056112155ea5353dcfbavboxsync * Applies prototype properties from the supplier to the receiver.
8cfe2efff2058bd07777056112155ea5353dcfbavboxsync * The receiver can be a constructor or an instance.
8cfe2efff2058bd07777056112155ea5353dcfbavboxsync * @method augment
8cfe2efff2058bd07777056112155ea5353dcfbavboxsync * @param {Function} r the object to receive the augmentation
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @param {Function} s the object that supplies the properties to augment
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @param ov {boolean} if true, properties already on the receiver
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * will be overwritten if found on the supplier.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @param wl {string[]} a whitelist. If supplied, only properties in
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * this list will be applied to the receiver.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @param args {Array | Any} arg or arguments to apply to the supplier
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * constructor when initializing.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @return {object} the augmented object
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync *
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @todo constructor optional?
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @todo understanding what an instance is augmented with
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @TODO best practices for overriding sequestered methods.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync Y.augment = function(r, s, ov, wl, args) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync var sProto = s.prototype,
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync newProto = null,
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync construct = s,
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync a = (args) ? Y.Array(args) : [],
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync rProto = r.prototype,
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync target = rProto || r,
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync applyConstructor = false,
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync sequestered, replacements, i;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // working on a class, so apply constructor infrastructure
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync if (rProto && construct) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync sequestered = {};
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync replacements = {};
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync newProto = {};
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // sequester all of the functions in the supplier and replace with
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // one that will restore all of them.
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync Y.each(sProto, function(v, k) {
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync replacements[k] = function() {
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync
a36941c0c575a6116528b213066e4f08381feb69vboxsync// Y.log('sequestered function "' + k + '" executed. Initializing EventTarget');
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync// overwrite the prototype with all of the sequestered functions,
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync// but only if it hasn't been overridden
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync for (i in sequestered) {
a36941c0c575a6116528b213066e4f08381feb69vboxsync if (sequestered.hasOwnProperty(i) && (this[i] === replacements[i])) {
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // Y.log('... restoring ' + k);
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync this[i] = sequestered[i];
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync }
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync }
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // apply the constructor
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync construct.apply(this, a);
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // apply the original sequestered function
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync return sequestered[k].apply(this, arguments);
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync };
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync if ((!wl || (k in wl)) && (ov || !(k in this))) {
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // Y.log('augment: ' + k);
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync if (L.isFunction(v)) {
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // sequester the function
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync sequestered[k] = v;
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync// replace the sequestered function with a function that will
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync// restore all sequestered functions and exectue the constructor.
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync this[k] = replacements[k];
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync } else {
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // Y.log('augment() applying non-function: ' + k);
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync this[k] = v;
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync }
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync }
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync }, newProto, true);
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // augmenting an instance, so apply the constructor immediately
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync } else {
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync applyConstructor = true;
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync }
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync Y.mix(target, newProto || sProto, ov, wl);
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync if (applyConstructor) {
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync s.apply(target, a);
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync }
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync return r;
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync };
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync /**
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * Applies object properties from the supplier to the receiver. If
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * the target has the property, and the property is an object, the target
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * object will be augmented with the supplier's value. If the property
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * is an array, the suppliers value will be appended to the target.
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * @method aggregate
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * @param {Function} r the object to receive the augmentation
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * @param {Function} s the object that supplies the properties to augment
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * @param ov {boolean} if true, properties already on the receiver
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * will be overwritten if found on the supplier.
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * @param wl {string[]} a whitelist. If supplied, only properties in
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * this list will be applied to the receiver.
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * @return {object} the extended object
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync */
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync Y.aggregate = function(r, s, ov, wl) {
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync return Y.mix(r, s, ov, wl, 0, true);
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync };
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync /**
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * Utility to set up the prototype, constructor and superclass properties to
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * support an inheritance strategy that can chain constructors and methods.
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * Static members will not be inherited.
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync *
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * @method extend
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * @param {Function} r the object to modify
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * @param {Function} s the object to inherit
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * @param {Object} px prototype properties to add/override
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * @param {Object} sx static properties to add/override
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * @return {YUI} the YUI instance
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync */
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync Y.extend = function(r, s, px, sx) {
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync if (!s||!r) {
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // @TODO error symbols
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync Y.error("extend failed, verify dependencies");
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync }
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync var sp = s.prototype, rp=Y.Object(sp);
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync r.prototype=rp;
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync rp.constructor=r;
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync r.superclass=sp;
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // assign constructor property
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync if (s != Object && sp.constructor == OP.constructor) {
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync sp.constructor=s;
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync }
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // add prototype overrides
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync if (px) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync Y.mix(rp, px, true);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync }
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // add object overrides
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync if (sx) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync Y.mix(r, sx, true);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync }
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync return r;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync };
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync /**
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * Executes the supplied function for each item in
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync * a collection. Supports arrays, objects, and
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * Y.NodeLists
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @method each
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @param o the object to iterate
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @param f the function to execute. This function
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * receives the value, key, and object as parameters
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @param proto if true, prototype properties are
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * iterated on objects
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @return {YUI} the YUI instance
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync Y.each = function(o, f, c, proto) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync if (o.each && o.item) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync return o.each.call(o, f, c);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync } else {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync switch (A.test(o)) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync case 1:
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync return A.each(o, f, c);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync case 2:
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync return A.each(Y.Array(o, 0, true), f, c);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync default:
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync return Y.Object.each(o, f, c, proto);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync }
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync }
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // return Y.Object.each(o, f, c);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync };
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // Y.each = function(o, f, c, proto) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // return dispatch(o, f, c, proto, 'each');
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // };
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync /*
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * Executes the supplied function for each item in
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * a collection. The operation stops if the function
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * returns true. Supports arrays, objects, and
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * Y.NodeLists.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @method some
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @param o the object to iterate
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @param f the function to execute. This function
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * receives the value, key, and object as parameters
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @param proto if true, prototype properties are
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * iterated on objects
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @return {boolean} true if the function ever returns true, false otherwise
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // Y.some = function(o, f, c, proto) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // return dispatch(o, f, c, proto, 'some');
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // };
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync /**
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * Deep obj/array copy. Functions are cloned with Y.bind.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * Array-like objects are treated as arrays.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * Primitives are returned untouched. Optionally, a
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * function can be provided to handle other data types,
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * filter keys, validate values, etc.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync *
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @method clone
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @param o what to clone
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @param safe {boolean} if true, objects will not have prototype
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * items from the source. If false, they will. In this case, the
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * original is initially protected, but the clone is not completely immune
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * from changes to the source object prototype. Also, cloned prototype
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * items that are deleted from the clone will result in the value
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * of the source prototype being exposed. If operating on a non-safe
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * clone, items should be nulled out rather than deleted.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @TODO review
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @param f optional function to apply to each item in a collection;
* it will be executed prior to applying the value to
* the new object. Return false to prevent the copy.
* @param c optional execution context for f
* @param owner Owner object passed when clone is iterating an
* object. Used to set up context for cloned functions.
* @return {Array|Object} the cloned object
*/
Y.clone = function(o, safe, f, c, owner, cloned) {
if (!L.isObject(o)) {
return o;
}
var o2, marked = cloned || {}, stamp;
switch (L.type(o)) {
case 'date':
return new Date(o);
case 'regexp':
return new RegExp(o.source);
case 'function':
o2 = Y.bind(o, owner);
break;
case 'array':
o2 = [];
break;
default:
// #2528250 only one clone of a given object should be created.
if (o[CLONE_MARKER]) {
return marked[o[CLONE_MARKER]];
}
stamp = Y.guid();
o2 = (safe) ? {} : Y.Object(o);
o[CLONE_MARKER] = stamp;
marked[stamp] = o;
}
// #2528250 don't try to clone element properties
if (!o.addEventListener && !o.attachEvent) {
Y.each(o, function(v, k) {
if (!f || (f.call(c || this, v, k, this, o) !== false)) {
if (k !== CLONE_MARKER) {
this[k] = Y.clone(v, safe, f, c, owner || o, marked);
}
}
}, o2);
}
if (!cloned) {
Y.each(marked, function(v, k) {
delete v[CLONE_MARKER];
});
marked = null;
}
return o2;
};
/**
* Returns a function that will execute the supplied function in the
* supplied object's context, optionally adding any additional
* supplied parameters to the beginning of the arguments collection the
* supplied to the function.
*
* @method bind
* @param f {Function|String} the function to bind, or a function name
* to execute on the context object
* @param c the execution context
* @param args* 0..n arguments to include before the arguments the
* function is executed with.
* @return {function} the wrapped function
*/
Y.bind = function(f, c) {
var xargs = arguments.length > 2 ? Y.Array(arguments, 2, true) : null;
return function () {
var fn = L.isString(f) ? c[f] : f,
args = (xargs) ? xargs.concat(Y.Array(arguments, 0, true)) : arguments;
return fn.apply(c || fn, args);
};
};
/**
* Returns a function that will execute the supplied function in the
* supplied object's context, optionally adding any additional
* supplied parameters to the end of the arguments the function
* is executed with.
*
* @method rbind
* @param f {Function|String} the function to bind, or a function name
* to execute on the context object
* @param c the execution context
* @param args* 0..n arguments to append to the end of arguments collection
* supplied to the function
* @return {function} the wrapped function
*/
Y.rbind = function(f, c) {
var xargs = arguments.length > 2 ? Y.Array(arguments, 2, true) : null;
return function () {
var fn = L.isString(f) ? c[f] : f,
args = (xargs) ? Y.Array(arguments, 0, true).concat(xargs) : arguments;
return fn.apply(c || fn, args);
};
};
}, '@VERSION@' );