2010-06-17 14:00:47 +00:00
|
|
|
(function() {
|
2012-06-07 14:23:06 +00:00
|
|
|
|
2011-10-28 00:06:19 +00:00
|
|
|
var slice = Array.prototype.slice, emptyFunction = function() { };
|
2012-06-07 14:23:06 +00:00
|
|
|
|
2010-06-17 14:00:47 +00:00
|
|
|
var IS_DONTENUM_BUGGY = (function(){
|
|
|
|
|
for (var p in { toString: 1 }) {
|
|
|
|
|
if (p === 'toString') return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
})();
|
2012-06-07 14:23:06 +00:00
|
|
|
|
|
|
|
|
/** @ignore */
|
|
|
|
|
var addMethods = function(klass, source, parent) {
|
|
|
|
|
for (var property in source) {
|
|
|
|
|
|
2013-01-02 22:25:13 +00:00
|
|
|
if (property in klass.prototype &&
|
|
|
|
|
typeof klass.prototype[property] === 'function' &&
|
|
|
|
|
(source[property] + '').indexOf('callSuper') > -1) {
|
2012-06-07 14:23:06 +00:00
|
|
|
|
|
|
|
|
klass.prototype[property] = (function(property) {
|
|
|
|
|
return function() {
|
|
|
|
|
|
|
|
|
|
var superclass = this.constructor.superclass;
|
|
|
|
|
this.constructor.superclass = parent;
|
|
|
|
|
var returnValue = source[property].apply(this, arguments);
|
|
|
|
|
this.constructor.superclass = superclass;
|
|
|
|
|
|
|
|
|
|
if (property !== 'initialize') {
|
|
|
|
|
return returnValue;
|
|
|
|
|
}
|
2012-10-14 00:53:12 +00:00
|
|
|
};
|
2012-06-07 14:23:06 +00:00
|
|
|
})(property);
|
2010-06-17 14:00:47 +00:00
|
|
|
}
|
2012-06-07 14:23:06 +00:00
|
|
|
else {
|
2010-06-17 14:00:47 +00:00
|
|
|
klass.prototype[property] = source[property];
|
|
|
|
|
}
|
2012-06-07 14:23:06 +00:00
|
|
|
|
|
|
|
|
if (IS_DONTENUM_BUGGY) {
|
|
|
|
|
if (source.toString !== Object.prototype.toString) {
|
|
|
|
|
klass.prototype.toString = source.toString;
|
|
|
|
|
}
|
|
|
|
|
if (source.valueOf !== Object.prototype.valueOf) {
|
|
|
|
|
klass.prototype.valueOf = source.valueOf;
|
|
|
|
|
}
|
2010-06-17 14:00:47 +00:00
|
|
|
}
|
2012-06-07 14:23:06 +00:00
|
|
|
}
|
|
|
|
|
};
|
2010-06-17 14:00:47 +00:00
|
|
|
|
2012-10-14 00:53:12 +00:00
|
|
|
function Subclass() { }
|
2012-06-07 14:23:06 +00:00
|
|
|
|
2012-11-23 12:39:50 +00:00
|
|
|
function callSuper(methodName) {
|
|
|
|
|
var fn = this.constructor.superclass.prototype[methodName];
|
|
|
|
|
return (arguments.length > 1)
|
|
|
|
|
? fn.apply(this, slice.call(arguments, 1))
|
|
|
|
|
: fn.call(this);
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-15 02:16:24 +00:00
|
|
|
/**
|
2012-11-23 12:39:50 +00:00
|
|
|
* Helper for creation of "classes". Note that pr
|
|
|
|
|
* @param parent optional "Class" to inherit from
|
|
|
|
|
* @param properties Properties shared by all instances of this class
|
|
|
|
|
* (be careful modifying objects defined here as this would affect all instances)
|
2010-10-15 02:16:24 +00:00
|
|
|
* @memberOf fabric.util
|
|
|
|
|
*/
|
2010-06-17 14:00:47 +00:00
|
|
|
function createClass() {
|
2012-06-07 14:23:06 +00:00
|
|
|
var parent = null,
|
2010-06-17 14:00:47 +00:00
|
|
|
properties = slice.call(arguments, 0);
|
2012-06-07 14:23:06 +00:00
|
|
|
|
2010-06-17 14:00:47 +00:00
|
|
|
if (typeof properties[0] === 'function') {
|
|
|
|
|
parent = properties.shift();
|
|
|
|
|
}
|
|
|
|
|
function klass() {
|
|
|
|
|
this.initialize.apply(this, arguments);
|
|
|
|
|
}
|
2012-06-07 14:23:06 +00:00
|
|
|
|
2010-06-17 14:00:47 +00:00
|
|
|
klass.superclass = parent;
|
|
|
|
|
klass.subclasses = [ ];
|
|
|
|
|
|
|
|
|
|
if (parent) {
|
2012-10-14 00:53:12 +00:00
|
|
|
Subclass.prototype = parent.prototype;
|
|
|
|
|
klass.prototype = new Subclass();
|
2010-06-17 14:00:47 +00:00
|
|
|
parent.subclasses.push(klass);
|
|
|
|
|
}
|
|
|
|
|
for (var i = 0, length = properties.length; i < length; i++) {
|
2012-06-07 14:23:06 +00:00
|
|
|
addMethods(klass, properties[i], parent);
|
2010-06-17 14:00:47 +00:00
|
|
|
}
|
|
|
|
|
if (!klass.prototype.initialize) {
|
|
|
|
|
klass.prototype.initialize = emptyFunction;
|
|
|
|
|
}
|
|
|
|
|
klass.prototype.constructor = klass;
|
2012-11-23 12:39:50 +00:00
|
|
|
klass.prototype.callSuper = callSuper;
|
2010-06-17 14:00:47 +00:00
|
|
|
return klass;
|
|
|
|
|
}
|
2012-06-07 14:23:06 +00:00
|
|
|
|
2010-07-10 01:50:13 +00:00
|
|
|
fabric.util.createClass = createClass;
|
2013-04-24 16:58:04 +00:00
|
|
|
})();
|