From 3f2a02c0595727bc3d5d704a1aec38340dd8ce98 Mon Sep 17 00:00:00 2001 From: mourner Date: Thu, 2 Sep 2010 13:40:34 +0300 Subject: [PATCH] Added some environment code and Class implementation --- src/env.js | 7 ++++++ src/util/Class.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/env.js create mode 100644 src/util/Class.js diff --git a/src/env.js b/src/env.js new file mode 100644 index 00000000..83589354 --- /dev/null +++ b/src/env.js @@ -0,0 +1,7 @@ +var originalL = L, + L = {}; + +L.noConflict = function() { + window.L = originalL; + return this; +}; \ No newline at end of file diff --git a/src/util/Class.js b/src/util/Class.js new file mode 100644 index 00000000..e49b9cdd --- /dev/null +++ b/src/util/Class.js @@ -0,0 +1,61 @@ +/* + * Class powers the OOP facilities of the library. + */ + +L.Class = function() {}; + +L.Class.extend = function(props) { + var _super = this.prototype, statics; + + // instantiate class without calling constructor + L.Class._prototyping = true; + var proto = new this(); + L.Class._prototyping = false; + + // mix includes into the prototype + if (props.includes) { + L.Util.extend.apply(null, [proto].concat(props.includes)); + delete props.includes; + } + + // callParent method + if (this != L.Class) { + proto.callParent = function(fnName) { + _super[fnName].apply(this, Array.prototype.slice.call(arguments, 1)); + }; + } + + // save static properties + if (props.statics) { + statics = props.statics; + delete props.statics; + } + + // mix given properties into the prototype + L.Util.extend(proto, props); + + // extended class with the new prototype + function NewClass() { + if (!L.Class._prototyping && this.initialize) { + this.initialize.apply(this, arguments); + } + } + proto.constructor = NewClass; + NewClass.prototype = proto; + + NewClass.extend = arguments.callee; + + // method for adding properties to prototype + NewClass.include = function(props) { + L.Util.extend(this.prototype, props); + }; + + // mix static properties into the class + if (statics) { + NewClass.include(statics); + } + + //TODO inherit parent's statics? + + return NewClass; +}; \ No newline at end of file