[76] | 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 | /** |
---|
| 8 | * @requires OpenLayers/Util.js |
---|
| 9 | * @requires OpenLayers/Marker.js |
---|
| 10 | * @requires OpenLayers/Popup/AnchoredBubble.js |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | /** |
---|
| 14 | * Class: OpenLayers.Feature |
---|
| 15 | * Features are combinations of geography and attributes. The OpenLayers.Feature |
---|
| 16 | * class specifically combines a marker and a lonlat. |
---|
| 17 | */ |
---|
| 18 | OpenLayers.Feature = OpenLayers.Class({ |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * Property: layer |
---|
| 22 | * {<OpenLayers.Layer>} |
---|
| 23 | */ |
---|
| 24 | layer: null, |
---|
| 25 | |
---|
| 26 | /** |
---|
| 27 | * Property: id |
---|
| 28 | * {String} |
---|
| 29 | */ |
---|
| 30 | id: null, |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * Property: lonlat |
---|
| 34 | * {<OpenLayers.LonLat>} |
---|
| 35 | */ |
---|
| 36 | lonlat: null, |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * Property: data |
---|
| 40 | * {Object} |
---|
| 41 | */ |
---|
| 42 | data: null, |
---|
| 43 | |
---|
| 44 | /** |
---|
| 45 | * Property: marker |
---|
| 46 | * {<OpenLayers.Marker>} |
---|
| 47 | */ |
---|
| 48 | marker: null, |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * APIProperty: popupClass |
---|
| 52 | * {<OpenLayers.Class>} The class which will be used to instantiate |
---|
| 53 | * a new Popup. Default is <OpenLayers.Popup.AnchoredBubble>. |
---|
| 54 | */ |
---|
| 55 | popupClass: OpenLayers.Popup.AnchoredBubble, |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | * Property: popup |
---|
| 59 | * {<OpenLayers.Popup>} |
---|
| 60 | */ |
---|
| 61 | popup: null, |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * Constructor: OpenLayers.Feature |
---|
| 65 | * Constructor for features. |
---|
| 66 | * |
---|
| 67 | * Parameters: |
---|
| 68 | * layer - {<OpenLayers.Layer>} |
---|
| 69 | * lonlat - {<OpenLayers.LonLat>} |
---|
| 70 | * data - {Object} |
---|
| 71 | * |
---|
| 72 | * Returns: |
---|
| 73 | * {<OpenLayers.Feature>} |
---|
| 74 | */ |
---|
| 75 | initialize: function(layer, lonlat, data) { |
---|
| 76 | this.layer = layer; |
---|
| 77 | this.lonlat = lonlat; |
---|
| 78 | this.data = (data != null) ? data : {}; |
---|
| 79 | this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_"); |
---|
| 80 | }, |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * Method: destroy |
---|
| 84 | * nullify references to prevent circular references and memory leaks |
---|
| 85 | */ |
---|
| 86 | destroy: function() { |
---|
| 87 | |
---|
| 88 | //remove the popup from the map |
---|
| 89 | if ((this.layer != null) && (this.layer.map != null)) { |
---|
| 90 | if (this.popup != null) { |
---|
| 91 | this.layer.map.removePopup(this.popup); |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | // remove the marker from the layer |
---|
| 95 | if (this.layer != null && this.marker != null) { |
---|
| 96 | this.layer.removeMarker(this.marker); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | this.layer = null; |
---|
| 100 | this.id = null; |
---|
| 101 | this.lonlat = null; |
---|
| 102 | this.data = null; |
---|
| 103 | if (this.marker != null) { |
---|
| 104 | this.destroyMarker(this.marker); |
---|
| 105 | this.marker = null; |
---|
| 106 | } |
---|
| 107 | if (this.popup != null) { |
---|
| 108 | this.destroyPopup(this.popup); |
---|
| 109 | this.popup = null; |
---|
| 110 | } |
---|
| 111 | }, |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * Method: onScreen |
---|
| 115 | * |
---|
| 116 | * Returns: |
---|
| 117 | * {Boolean} Whether or not the feature is currently visible on screen |
---|
| 118 | * (based on its 'lonlat' property) |
---|
| 119 | */ |
---|
| 120 | onScreen:function() { |
---|
| 121 | |
---|
| 122 | var onScreen = false; |
---|
| 123 | if ((this.layer != null) && (this.layer.map != null)) { |
---|
| 124 | var screenBounds = this.layer.map.getExtent(); |
---|
| 125 | onScreen = screenBounds.containsLonLat(this.lonlat); |
---|
| 126 | } |
---|
| 127 | return onScreen; |
---|
| 128 | }, |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | /** |
---|
| 132 | * Method: createMarker |
---|
| 133 | * Based on the data associated with the Feature, create and return a marker object. |
---|
| 134 | * |
---|
| 135 | * Returns: |
---|
| 136 | * {<OpenLayers.Marker>} A Marker Object created from the 'lonlat' and 'icon' properties |
---|
| 137 | * set in this.data. If no 'lonlat' is set, returns null. If no |
---|
| 138 | * 'icon' is set, OpenLayers.Marker() will load the default image. |
---|
| 139 | * |
---|
| 140 | * Note - this.marker is set to return value |
---|
| 141 | * |
---|
| 142 | */ |
---|
| 143 | createMarker: function() { |
---|
| 144 | |
---|
| 145 | if (this.lonlat != null) { |
---|
| 146 | this.marker = new OpenLayers.Marker(this.lonlat, this.data.icon); |
---|
| 147 | } |
---|
| 148 | return this.marker; |
---|
| 149 | }, |
---|
| 150 | |
---|
| 151 | /** |
---|
| 152 | * Method: destroyMarker |
---|
| 153 | * Destroys marker. |
---|
| 154 | * If user overrides the createMarker() function, s/he should be able |
---|
| 155 | * to also specify an alternative function for destroying it |
---|
| 156 | */ |
---|
| 157 | destroyMarker: function() { |
---|
| 158 | this.marker.destroy(); |
---|
| 159 | }, |
---|
| 160 | |
---|
| 161 | /** |
---|
| 162 | * Method: createPopup |
---|
| 163 | * Creates a popup object created from the 'lonlat', 'popupSize', |
---|
| 164 | * and 'popupContentHTML' properties set in this.data. It uses |
---|
| 165 | * this.marker.icon as default anchor. |
---|
| 166 | * |
---|
| 167 | * If no 'lonlat' is set, returns null. |
---|
| 168 | * If no this.marker has been created, no anchor is sent. |
---|
| 169 | * |
---|
| 170 | * Note - the returned popup object is 'owned' by the feature, so you |
---|
| 171 | * cannot use the popup's destroy method to discard the popup. |
---|
| 172 | * Instead, you must use the feature's destroyPopup |
---|
| 173 | * |
---|
| 174 | * Note - this.popup is set to return value |
---|
| 175 | * |
---|
| 176 | * Parameters: |
---|
| 177 | * closeBox - {Boolean} create popup with closebox or not |
---|
| 178 | * |
---|
| 179 | * Returns: |
---|
| 180 | * {<OpenLayers.Popup>} Returns the created popup, which is also set |
---|
| 181 | * as 'popup' property of this feature. Will be of whatever type |
---|
| 182 | * specified by this feature's 'popupClass' property, but must be |
---|
| 183 | * of type <OpenLayers.Popup>. |
---|
| 184 | * |
---|
| 185 | */ |
---|
| 186 | createPopup: function(closeBox) { |
---|
| 187 | |
---|
| 188 | if (this.lonlat != null) { |
---|
| 189 | |
---|
| 190 | var id = this.id + "_popup"; |
---|
| 191 | var anchor = (this.marker) ? this.marker.icon : null; |
---|
| 192 | |
---|
| 193 | if (!this.popup) { |
---|
| 194 | this.popup = new this.popupClass(id, |
---|
| 195 | this.lonlat, |
---|
| 196 | this.data.popupSize, |
---|
| 197 | this.data.popupContentHTML, |
---|
| 198 | anchor, |
---|
| 199 | closeBox); |
---|
| 200 | } |
---|
| 201 | if (this.data.overflow != null) { |
---|
| 202 | this.popup.contentDiv.style.overflow = this.data.overflow; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | this.popup.feature = this; |
---|
| 206 | } |
---|
| 207 | return this.popup; |
---|
| 208 | }, |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | /** |
---|
| 212 | * Method: destroyPopup |
---|
| 213 | * Destroys the popup created via createPopup. |
---|
| 214 | * |
---|
| 215 | * As with the marker, if user overrides the createPopup() function, s/he |
---|
| 216 | * should also be able to override the destruction |
---|
| 217 | */ |
---|
| 218 | destroyPopup: function() { |
---|
| 219 | if (this.popup) { |
---|
| 220 | this.popup.feature = null; |
---|
| 221 | this.popup.destroy(); |
---|
| 222 | this.popup = null; |
---|
| 223 | } |
---|
| 224 | }, |
---|
| 225 | |
---|
| 226 | CLASS_NAME: "OpenLayers.Feature" |
---|
| 227 | }); |
---|