2010-11-10 17:41:12 -05:00
|
|
|
/**
|
|
|
|
* Contains basic inheritance mechanism
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Mike Gerwitz
|
|
|
|
*
|
2010-11-10 22:07:03 -05:00
|
|
|
* This file is part of ease.js.
|
2010-11-10 17:41:12 -05:00
|
|
|
*
|
2010-11-10 22:07:03 -05:00
|
|
|
* ease.js is free software: you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
* any later version.
|
2010-11-10 17:41:12 -05:00
|
|
|
*
|
2010-11-10 22:07:03 -05:00
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2010-11-10 17:41:12 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @author Mike Gerwitz
|
|
|
|
* @package core
|
|
|
|
*/
|
|
|
|
|
2010-11-10 23:28:20 -05:00
|
|
|
// whether getters/setters are supported
|
|
|
|
var getset = ( Object.prototype.__defineGetter__ === undefined )
|
|
|
|
? false
|
|
|
|
: true
|
|
|
|
;
|
|
|
|
|
2010-11-10 17:41:12 -05:00
|
|
|
|
2010-11-10 23:30:30 -05:00
|
|
|
/**
|
|
|
|
* Creates a class, inheriting either from the provided base class or the
|
|
|
|
* default base class
|
|
|
|
*
|
|
|
|
* @param {Object} base object to extend (extends Class by default)
|
|
|
|
*
|
|
|
|
* @return {Object} extended class
|
|
|
|
*/
|
|
|
|
exports.extend = function( base )
|
|
|
|
{
|
|
|
|
return extend.apply( this, arguments );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-14 00:47:27 -05:00
|
|
|
/**
|
|
|
|
* Creates an abstract method
|
|
|
|
*
|
|
|
|
* Abstract methods must be implemented by a subclass and cannot be called
|
|
|
|
* directly. If a class contains a single abstract method, the class itself is
|
|
|
|
* considered to be abstract and cannot be instantiated. It may only be
|
|
|
|
* extended.
|
|
|
|
*
|
|
|
|
* @param {Function} definition function definition that concrete
|
|
|
|
* implementations must follow
|
|
|
|
*
|
|
|
|
* @return {Function}
|
|
|
|
*/
|
|
|
|
exports.abstractMethod = function( definition )
|
|
|
|
{
|
|
|
|
return function()
|
|
|
|
{
|
|
|
|
throw new Error( "Cannot call abstract method" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-10 17:41:12 -05:00
|
|
|
/**
|
|
|
|
* Default class implementation
|
|
|
|
*
|
|
|
|
* @return undefined
|
|
|
|
*/
|
2010-11-10 23:35:57 -05:00
|
|
|
function Class() {};
|
2010-11-10 17:41:12 -05:00
|
|
|
|
|
|
|
|
2010-11-10 20:45:33 -05:00
|
|
|
/**
|
|
|
|
* Copies properties to the destination object
|
|
|
|
*
|
2010-11-10 23:31:58 -05:00
|
|
|
* If the method already exists, it will be overridden and accessible via either
|
|
|
|
* the parent prototype or by invoking this.__super().
|
|
|
|
*
|
2010-11-10 20:45:33 -05:00
|
|
|
* The destination object is directly modified.
|
|
|
|
*
|
2010-11-14 00:56:54 -05:00
|
|
|
* The result data will be populated with information from the copy that may be
|
|
|
|
* useful to the creation of the class (e.g. list of the abstract methods).
|
|
|
|
*
|
|
|
|
* @param {Object} props properties to copy
|
|
|
|
* @param {Object} dest destination object
|
|
|
|
* @param {Object} result_data object to store data regarding the copy in
|
2010-11-10 20:45:33 -05:00
|
|
|
*
|
|
|
|
* @return undefined
|
|
|
|
*/
|
2010-11-14 00:56:54 -05:00
|
|
|
function prop_copy( props, dest, result_data )
|
2010-11-10 19:19:02 -05:00
|
|
|
{
|
2010-11-14 00:56:54 -05:00
|
|
|
result_data = result_data || {};
|
|
|
|
|
2010-11-10 19:19:02 -05:00
|
|
|
// copy each of the properties to the destination object
|
|
|
|
for ( property in props )
|
|
|
|
{
|
2010-11-10 22:49:27 -05:00
|
|
|
// if the property already exists, then it's being overridden (we only
|
|
|
|
// care about methods - properties will simply have their values
|
|
|
|
// overwritten)
|
2010-11-10 23:19:46 -05:00
|
|
|
var pre = dest[ property ],
|
2010-11-10 23:42:26 -05:00
|
|
|
prop = props[ property ],
|
2010-11-10 23:28:20 -05:00
|
|
|
getter = ( ( getset ) ? props.__lookupGetter__( property ) : null ),
|
|
|
|
setter = ( ( getset ) ? props.__lookupSetter__( property ) : null );
|
2010-11-10 23:19:46 -05:00
|
|
|
|
|
|
|
// check for getter/setter overrides
|
|
|
|
if ( getter || setter )
|
|
|
|
{
|
|
|
|
if ( getter )
|
|
|
|
{
|
|
|
|
dest.__defineGetter__( property, getter );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( setter )
|
|
|
|
{
|
|
|
|
dest.__defineSetter__( property, setter );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// check for method overrides
|
|
|
|
else if ( ( pre !== undefined ) && ( pre instanceof Function ) )
|
2010-11-10 22:49:27 -05:00
|
|
|
{
|
2010-11-10 23:42:26 -05:00
|
|
|
// ensure we're overriding the method with another method
|
|
|
|
if ( !( prop instanceof Function ) )
|
|
|
|
{
|
|
|
|
throw new TypeError( "Cannot override method with non-method" );
|
|
|
|
}
|
|
|
|
|
2010-11-10 22:49:27 -05:00
|
|
|
dest[ property ] = ( function( method, super_method )
|
|
|
|
{
|
|
|
|
// this is the method that will be invoked when the requested
|
|
|
|
// method is called, so note that in the context of this
|
|
|
|
// function, `this` will represent the current class instance
|
|
|
|
return function()
|
|
|
|
{
|
|
|
|
var tmp = this.__super;
|
|
|
|
|
|
|
|
// assign _super temporarily for the method invocation so
|
|
|
|
// that the method can call the parent method
|
|
|
|
this.__super = super_method;
|
|
|
|
var retval = method.apply( this, arguments );
|
|
|
|
this.__super = tmp;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
2010-11-10 23:42:26 -05:00
|
|
|
})( prop, dest[ property ] );
|
2010-11-10 22:49:27 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-11-10 23:42:26 -05:00
|
|
|
dest[ property ] = prop;
|
2010-11-10 22:49:27 -05:00
|
|
|
}
|
2010-11-10 19:19:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-10 18:40:36 -05:00
|
|
|
/**
|
|
|
|
* Mimics class inheritance
|
|
|
|
*
|
|
|
|
* This method will mimic inheritance by setting up the prototype with the
|
|
|
|
* provided base class (or, by default, Class) and copying the additional
|
|
|
|
* properties atop of it.
|
|
|
|
*
|
|
|
|
* The class to inherit from (the first argument) is optional. If omitted, the
|
|
|
|
* first argument will be considered to be the properties list.
|
|
|
|
*
|
|
|
|
* @return {Object} extended class
|
|
|
|
*/
|
2010-11-10 23:35:57 -05:00
|
|
|
function extend()
|
2010-11-10 18:40:36 -05:00
|
|
|
{
|
|
|
|
var args = Array.prototype.slice.call( arguments ),
|
|
|
|
props = args.pop() || {},
|
|
|
|
base = args.pop() || Class,
|
|
|
|
|
|
|
|
prototype = new base(),
|
2010-11-10 19:02:52 -05:00
|
|
|
new_class = function()
|
|
|
|
{
|
|
|
|
if ( this.__construct instanceof Function )
|
|
|
|
{
|
|
|
|
this.__construct.apply( this, arguments );
|
|
|
|
}
|
|
|
|
};
|
2010-11-10 18:40:36 -05:00
|
|
|
|
2010-11-10 19:19:02 -05:00
|
|
|
// copy the given properties into the new prototype
|
|
|
|
prop_copy( props, prototype );
|
|
|
|
|
2010-11-10 22:54:24 -05:00
|
|
|
// reference to the parent prototype (for more experienced users)
|
|
|
|
prototype.parent = base.prototype;
|
|
|
|
|
2010-11-10 19:19:02 -05:00
|
|
|
// set up the new class
|
2010-11-14 00:49:52 -05:00
|
|
|
setup_props( new_class );
|
2010-11-10 19:02:52 -05:00
|
|
|
new_class.prototype = prototype;
|
|
|
|
new_class.constructor = new_class;
|
2010-11-10 18:40:36 -05:00
|
|
|
|
|
|
|
return new_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-10 23:29:28 -05:00
|
|
|
/**
|
2010-11-14 00:49:52 -05:00
|
|
|
* Sets up common properties for the provided function (class)
|
2010-11-10 23:29:28 -05:00
|
|
|
*
|
2010-11-14 00:49:52 -05:00
|
|
|
* @param {Function} func function (class) to attach properties to
|
2010-11-10 23:29:28 -05:00
|
|
|
*
|
2010-11-14 00:49:52 -05:00
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function setup_props( func )
|
|
|
|
{
|
|
|
|
attach_extend( func );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attaches extend method to the given function (class)
|
|
|
|
*
|
|
|
|
* @param {Function} func function (class) to attach method to
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
2010-11-10 23:29:28 -05:00
|
|
|
*/
|
2010-11-10 23:35:57 -05:00
|
|
|
function attach_extend( func )
|
2010-11-10 17:41:12 -05:00
|
|
|
{
|
2010-11-10 21:10:31 -05:00
|
|
|
/**
|
|
|
|
* Shorthand for extending classes
|
|
|
|
*
|
|
|
|
* This method can be invoked on the object, rater than having to call
|
|
|
|
* Class.extend( this ).
|
|
|
|
*
|
|
|
|
* @param {Object} props properties to add to extended class
|
|
|
|
*
|
|
|
|
* @return {Object} extended class
|
|
|
|
*/
|
2010-11-10 23:28:20 -05:00
|
|
|
var ext_method = function( props )
|
|
|
|
{
|
|
|
|
return extend( this, props );
|
|
|
|
};
|
|
|
|
|
|
|
|
// if defineProperty is unsupported, do it the old fashioned way (it's just
|
|
|
|
// less restrictive)
|
|
|
|
if ( Object.defineProperty === undefined )
|
2010-11-10 17:41:12 -05:00
|
|
|
{
|
2010-11-10 23:28:20 -05:00
|
|
|
func.extend = ext_method;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Object.defineProperty( func, 'extend',
|
2010-11-10 21:10:31 -05:00
|
|
|
{
|
2010-11-10 23:28:20 -05:00
|
|
|
value: ext_method,
|
2010-11-10 17:41:12 -05:00
|
|
|
|
2010-11-10 23:28:20 -05:00
|
|
|
enumerable: false,
|
|
|
|
writable: false,
|
|
|
|
configurable: false,
|
|
|
|
} );
|
|
|
|
}
|
2010-11-10 21:10:31 -05:00
|
|
|
}
|