oop-debug.js revision 23209f57fce338501bc1dc828a991d103732b92f
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
14d0566627e0dd5853cae37a1ae7b8e28a99e0f5vboxsync A = Y.Array,
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)) {
2e6dc32bcc9c3a3e70c957764c033b1f402bc617vboxsync // return A[action](o, f, c);
a7ba3d5f31ca70d04a3933e570374e5ec5eff84avboxsync // return A[action](Y.Array(o, 0, true), f, c);
a7ba3d5f31ca70d04a3933e570374e5ec5eff84avboxsync // default:
a7ba3d5f31ca70d04a3933e570374e5ec5eff84avboxsync // return Y.Object[action](o, f, c, proto);
8cfe2efff2058bd07777056112155ea5353dcfbavboxsync * The following methods are added to the YUI instance
8cfe2efff2058bd07777056112155ea5353dcfbavboxsync * @class YUI~oop
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 * @todo constructor optional?
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @todo understanding what an instance is augmented with
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * @TODO best practices for overriding sequestered methods.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // working on a class, so apply constructor infrastructure
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // sequester all of the functions in the supplier and replace with
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // one that will restore all of them.
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync replacements[k] = function() {
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
a36941c0c575a6116528b213066e4f08381feb69vboxsync if (sequestered.hasOwnProperty(i) && (this[i] === replacements[i])) {
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // Y.log('... restoring ' + k);
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync this[i] = sequestered[i];
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // apply the constructor
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // apply the original sequestered function
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // Y.log('augment: ' + k);
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // sequester the function
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 // Y.log('augment() applying non-function: ' + k);
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync this[k] = v;
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // augmenting an instance, so apply the constructor immediately
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 * 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 * @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 if (!s||!r) {
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // @TODO error symbols
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync // assign constructor property
b0d3e712c0bf759a686c718a55896f19804cfa75vboxsync if (s != Object && sp.constructor == OP.constructor) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // add prototype overrides
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // add object overrides
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 switch (A.test(o)) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync return A.each(o, f, c);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // return Y.Object.each(o, f, c);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // Y.each = function(o, f, c, proto) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // return dispatch(o, f, c, proto, 'each');
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 // Y.some = function(o, f, c, proto) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync // return dispatch(o, f, c, proto, 'some');
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 * @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;
if (!L.isObject(o)) {
switch (L.type(o)) {
return new RegExp(o.source);
o2 = [];
if (o[CLONE_MARKER]) {
Y.each(o, function(v, k) {
if (!f || (f.call(c || this, v, k, this, o) !== false)) {
if (k !== CLONE_MARKER) {
}, o2);
if (!cloned) {
delete v[CLONE_MARKER];
marked = null;
return o2;
Y.bind = function(f, c) {
Y.rbind = function(f, c) {