]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/BaseTypes/Class.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / BaseTypes / Class.js
1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2  * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3  * full text of the license. */
4
5 /**
6  * Constructor: OpenLayers.Class
7  * Base class used to construct all other classes. Includes support for 
8  *     multiple inheritance. 
9  *     
10  * This constructor is new in OpenLayers 2.5.  At OpenLayers 3.0, the old 
11  *     syntax for creating classes and dealing with inheritance 
12  *     will be removed.
13  * 
14  * To create a new OpenLayers-style class, use the following syntax:
15  * > var MyClass = OpenLayers.Class(prototype);
16  *
17  * To create a new OpenLayers-style class with multiple inheritance, use the
18  *     following syntax:
19  * > var MyClass = OpenLayers.Class(Class1, Class2, prototype);
20  * Note that instanceof reflection will only reveil Class1 as superclass.
21  * Class2 ff are mixins.
22  *
23  */
24 OpenLayers.Class = function() {
25     var Class = function() {
26         /**
27          * This following condition can be removed at 3.0 - this is only for
28          * backwards compatibility while the Class.inherit method is still
29          * in use.  So at 3.0, the following three lines would be replaced with
30          * simply:
31          * this.initialize.apply(this, arguments);
32          */
33         if (arguments && arguments[0] != OpenLayers.Class.isPrototype) {
34             this.initialize.apply(this, arguments);
35         }
36     };
37     var extended = {};
38     var parent, initialize;
39     for(var i=0, len=arguments.length; i<len; ++i) {
40         if(typeof arguments[i] == "function") {
41             // make the class passed as the first argument the superclass
42             if(i == 0 && len > 1) {
43                 initialize = arguments[i].prototype.initialize;
44                 // replace the initialize method with an empty function,
45                 // because we do not want to create a real instance here
46                 arguments[i].prototype.initialize = function() {};
47                 // the line below makes sure that the new class has a
48                 // superclass
49                 extended = new arguments[i];
50                 // restore the original initialize method
51                 if(initialize === undefined) {
52                     delete arguments[i].prototype.initialize;
53                 } else {
54                     arguments[i].prototype.initialize = initialize;
55                 }
56             }
57             // get the prototype of the superclass
58             parent = arguments[i].prototype;
59         } else {
60             // in this case we're extending with the prototype
61             parent = arguments[i];
62         }
63         OpenLayers.Util.extend(extended, parent);
64     }
65     Class.prototype = extended;
66     return Class;
67 };
68
69 /**
70  * Property: isPrototype
71  * *Deprecated*.  This is no longer needed and will be removed at 3.0.
72  */
73 OpenLayers.Class.isPrototype = function () {};
74
75 /**
76  * APIFunction: OpenLayers.create
77  * *Deprecated*.  Old method to create an OpenLayers style class.  Use the
78  *     <OpenLayers.Class> constructor instead.
79  *
80  * Returns:
81  * An OpenLayers class
82  */
83 OpenLayers.Class.create = function() {
84     return function() {
85         if (arguments && arguments[0] != OpenLayers.Class.isPrototype) {
86             this.initialize.apply(this, arguments);
87         }
88     };
89 };
90
91
92 /**
93  * APIFunction: inherit
94  * *Deprecated*.  Old method to inherit from one or more OpenLayers style
95  *     classes.  Use the <OpenLayers.Class> constructor instead.
96  *
97  * Parameters:
98  * class - One or more classes can be provided as arguments
99  *
100  * Returns:
101  * An object prototype
102  */
103 OpenLayers.Class.inherit = function () {
104     var superClass = arguments[0];
105     var proto = new superClass(OpenLayers.Class.isPrototype);
106     for (var i=1, len=arguments.length; i<len; i++) {
107         if (typeof arguments[i] == "function") {
108             var mixin = arguments[i];
109             arguments[i] = new mixin(OpenLayers.Class.isPrototype);
110         }
111         OpenLayers.Util.extend(proto, arguments[i]);
112     }
113     return proto;
114 };