[OpenLayers-Commits] r10866 - sandbox/camptocamp/geobretagne/lib/OpenLayers/BaseTypes

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Tue Nov 2 13:32:07 EDT 2010


Author: fvanderbiest
Date: 2010-11-02 10:32:07 -0700 (Tue, 02 Nov 2010)
New Revision: 10866

Modified:
   sandbox/camptocamp/geobretagne/lib/OpenLayers/BaseTypes/Class.js
Log:
camptocamp/geobretagne sandbox: use of new OpenLayers.Class

Modified: sandbox/camptocamp/geobretagne/lib/OpenLayers/BaseTypes/Class.js
===================================================================
--- sandbox/camptocamp/geobretagne/lib/OpenLayers/BaseTypes/Class.js	2010-11-02 17:13:19 UTC (rev 10865)
+++ sandbox/camptocamp/geobretagne/lib/OpenLayers/BaseTypes/Class.js	2010-11-02 17:32:07 UTC (rev 10866)
@@ -1,5 +1,6 @@
-/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
- * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
+/* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for 
+ * full list of contributors). Published under the Clear BSD license.  
+ * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
  * full text of the license. */
 
 /**
@@ -22,48 +23,22 @@
  *
  */
 OpenLayers.Class = function() {
-    var Class = function() {
-        /**
-         * This following condition can be removed at 3.0 - this is only for
-         * backwards compatibility while the Class.inherit method is still
-         * in use.  So at 3.0, the following three lines would be replaced with
-         * simply:
-         * this.initialize.apply(this, arguments);
-         */
-        if (arguments && arguments[0] != OpenLayers.Class.isPrototype) {
-            this.initialize.apply(this, arguments);
-        }
-    };
-    var extended = {};
-    var parent, initialize;
-    for(var i=0, len=arguments.length; i<len; ++i) {
-        if(typeof arguments[i] == "function") {
-            // make the class passed as the first argument the superclass
-            if(i == 0 && len > 1) {
-                initialize = arguments[i].prototype.initialize;
-                // replace the initialize method with an empty function,
-                // because we do not want to create a real instance here
-                arguments[i].prototype.initialize = function() {};
-                // the line below makes sure that the new class has a
-                // superclass
-                extended = new arguments[i];
-                // restore the original initialize method
-                if(initialize === undefined) {
-                    delete arguments[i].prototype.initialize;
-                } else {
-                    arguments[i].prototype.initialize = initialize;
-                }
-            }
-            // get the prototype of the superclass
-            parent = arguments[i].prototype;
-        } else {
-            // in this case we're extending with the prototype
-            parent = arguments[i];
-        }
-        OpenLayers.Util.extend(extended, parent);
+    var len = arguments.length;
+    var P = arguments[0];
+    var F = arguments[len-1];
+
+    var C = typeof F.initialize == "function" ?
+        F.initialize :
+        function(){ P.apply(this, arguments); };
+
+    if (len > 1) {
+        var newArgs = [C, P].concat(
+                Array.prototype.slice.call(arguments).slice(1, len-1), F);
+        OpenLayers.inherit.apply(null, newArgs);
+    } else {
+        C.prototype = F;
     }
-    Class.prototype = extended;
-    return Class;
+    return C;
 };
 
 /**
@@ -88,7 +63,6 @@
     };
 };
 
-
 /**
  * APIFunction: inherit
  * *Deprecated*.  Old method to inherit from one or more OpenLayers style
@@ -100,15 +74,35 @@
  * Returns:
  * An object prototype
  */
-OpenLayers.Class.inherit = function () {
-    var superClass = arguments[0];
-    var proto = new superClass(OpenLayers.Class.isPrototype);
-    for (var i=1, len=arguments.length; i<len; i++) {
-        if (typeof arguments[i] == "function") {
-            var mixin = arguments[i];
-            arguments[i] = new mixin(OpenLayers.Class.isPrototype);
-        }
-        OpenLayers.Util.extend(proto, arguments[i]);
-    }
-    return proto;
+OpenLayers.Class.inherit = function (P) {
+    var C = function() {
+       P.call(this);
+    };
+    var newArgs = [C].concat(Array.prototype.slice.call(arguments));
+    OpenLayers.inherit.apply(null, newArgs);
+    return C.prototype;
 };
+
+/**
+ * Function: OpenLayers.inherit
+ *
+ * Parameters:
+ * C - {Object} the class that inherits
+ * P - {Object} the superclass to inherit from
+ *
+ * In addition to the mandatory C and P parameters, an arbitrary number of
+ * objects can be passed, which will extend C.
+ */
+OpenLayers.inherit = function(C, P) {
+   var F = function() {};
+   F.prototype = P.prototype;
+   C.prototype = new F;
+   var i, l, o;
+   for(i=2, l=arguments.length; i<l; i++) {
+       o = arguments[i];
+       if(typeof o === "function") {
+           o = o.prototype;
+       }
+       OpenLayers.Util.extend(C.prototype, o);
+   }
+};



More information about the Commits mailing list