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