[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 | * @requires OpenLayers/Layer.js |
---|
| 8 | * @requires OpenLayers/Renderer.js |
---|
| 9 | * @requires OpenLayers/StyleMap.js |
---|
| 10 | * @requires OpenLayers/Feature/Vector.js |
---|
| 11 | * @requires OpenLayers/Console.js |
---|
| 12 | */ |
---|
| 13 | |
---|
| 14 | /** |
---|
| 15 | * Class: OpenLayers.Layer.Vector |
---|
| 16 | * Instances of OpenLayers.Layer.Vector are used to render vector data from |
---|
| 17 | * a variety of sources. Create a new vector layer with the |
---|
| 18 | * <OpenLayers.Layer.Vector> constructor. |
---|
| 19 | * |
---|
| 20 | * Inherits from: |
---|
| 21 | * - <OpenLayers.Layer> |
---|
| 22 | */ |
---|
| 23 | OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, { |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * Constant: EVENT_TYPES |
---|
| 27 | * {Array(String)} Supported application event types. Register a listener |
---|
| 28 | * for a particular event with the following syntax: |
---|
| 29 | * (code) |
---|
| 30 | * layer.events.register(type, obj, listener); |
---|
| 31 | * (end) |
---|
| 32 | * |
---|
| 33 | * Listeners will be called with a reference to an event object. The |
---|
| 34 | * properties of this event depends on exactly what happened. |
---|
| 35 | * |
---|
| 36 | * All event objects have at least the following properties: |
---|
| 37 | * object - {Object} A reference to layer.events.object. |
---|
| 38 | * element - {DOMElement} A reference to layer.events.element. |
---|
| 39 | * |
---|
| 40 | * Supported map event types (in addition to those from <OpenLayers.Layer>): |
---|
| 41 | * beforefeatureadded - Triggered before a feature is added. Listeners |
---|
| 42 | * will receive an object with a *feature* property referencing the |
---|
| 43 | * feature to be added. To stop the feature from being added, a |
---|
| 44 | * listener should return false. |
---|
| 45 | * beforefeaturesadded - Triggered before an array of features is added. |
---|
| 46 | * Listeners will receive an object with a *features* property |
---|
| 47 | * referencing the feature to be added. To stop the features from |
---|
| 48 | * being added, a listener should return false. |
---|
| 49 | * featureadded - Triggered after a feature is added. The event |
---|
| 50 | * object passed to listeners will have a *feature* property with a |
---|
| 51 | * reference to the added feature. |
---|
| 52 | * featuresadded - Triggered after features are added. The event |
---|
| 53 | * object passed to listeners will have a *features* property with a |
---|
| 54 | * reference to an array of added features. |
---|
| 55 | * beforefeatureremoved - Triggered before a feature is removed. Listeners |
---|
| 56 | * will receive an object with a *feature* property referencing the |
---|
| 57 | * feature to be removed. |
---|
| 58 | * beforefeaturesremoved - Triggered before multiple features are removed. |
---|
| 59 | * Listeners will receive an object with a *features* property |
---|
| 60 | * referencing the features to be removed. |
---|
| 61 | * featureremoved - Triggerd after a feature is removed. The event |
---|
| 62 | * object passed to listeners will have a *feature* property with a |
---|
| 63 | * reference to the removed feature. |
---|
| 64 | * featuresremoved - Triggered after features are removed. The event |
---|
| 65 | * object passed to listeners will have a *features* property with a |
---|
| 66 | * reference to an array of removed features. |
---|
| 67 | * featureselected - Triggered after a feature is selected. Listeners |
---|
| 68 | * will receive an object with a *feature* property referencing the |
---|
| 69 | * selected feature. |
---|
| 70 | * featureunselected - Triggered after a feature is unselected. |
---|
| 71 | * Listeners will receive an object with a *feature* property |
---|
| 72 | * referencing the unselected feature. |
---|
| 73 | * beforefeaturemodified - Triggered when a feature is selected to |
---|
| 74 | * be modified. Listeners will receive an object with a *feature* |
---|
| 75 | * property referencing the selected feature. |
---|
| 76 | * featuremodified - Triggered when a feature has been modified. |
---|
| 77 | * Listeners will receive an object with a *feature* property referencing |
---|
| 78 | * the modified feature. |
---|
| 79 | * afterfeaturemodified - Triggered when a feature is finished being modified. |
---|
| 80 | * Listeners will receive an object with a *feature* property referencing |
---|
| 81 | * the modified feature. |
---|
| 82 | * vertexmodified - Triggered when a vertex within any feature geometry |
---|
| 83 | * has been modified. Listeners will receive an object with a |
---|
| 84 | * *feature* property referencing the modified feature, a *vertex* |
---|
| 85 | * property referencing the vertex modified (always a point geometry), |
---|
| 86 | * and a *pixel* property referencing the pixel location of the |
---|
| 87 | * modification. |
---|
| 88 | * sketchstarted - Triggered when a feature sketch bound for this layer |
---|
| 89 | * is started. Listeners will receive an object with a *feature* |
---|
| 90 | * property referencing the new sketch feature and a *vertex* property |
---|
| 91 | * referencing the creation point. |
---|
| 92 | * sketchmodified - Triggered when a feature sketch bound for this layer |
---|
| 93 | * is modified. Listeners will receive an object with a *vertex* |
---|
| 94 | * property referencing the modified vertex and a *feature* property |
---|
| 95 | * referencing the sketch feature. |
---|
| 96 | * sketchcomplete - Triggered when a feature sketch bound for this layer |
---|
| 97 | * is complete. Listeners will receive an object with a *feature* |
---|
| 98 | * property referencing the sketch feature. By returning false, a |
---|
| 99 | * listener can stop the sketch feature from being added to the layer. |
---|
| 100 | * refresh - Triggered when something wants a strategy to ask the protocol |
---|
| 101 | * for a new set of features. |
---|
| 102 | */ |
---|
| 103 | EVENT_TYPES: ["beforefeatureadded", "beforefeaturesadded", |
---|
| 104 | "featureadded", "featuresadded", "beforefeatureremoved", |
---|
| 105 | "beforefeaturesremoved", "featureremoved", "featuresremoved", |
---|
| 106 | "beforefeatureselected", "featureselected", "featureunselected", |
---|
| 107 | "beforefeaturemodified", "featuremodified", "afterfeaturemodified", |
---|
| 108 | "vertexmodified", "sketchstarted", "sketchmodified", |
---|
| 109 | "sketchcomplete", "refresh"], |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * APIProperty: isBaseLayer |
---|
| 113 | * {Boolean} The layer is a base layer. Default is false. Set this property |
---|
| 114 | * in the layer options. |
---|
| 115 | */ |
---|
| 116 | isBaseLayer: false, |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * APIProperty: isFixed |
---|
| 120 | * {Boolean} Whether the layer remains in one place while dragging the |
---|
| 121 | * map. |
---|
| 122 | */ |
---|
| 123 | isFixed: false, |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | * APIProperty: isVector |
---|
| 127 | * {Boolean} Whether the layer is a vector layer. |
---|
| 128 | */ |
---|
| 129 | isVector: true, |
---|
| 130 | |
---|
| 131 | /** |
---|
| 132 | * APIProperty: features |
---|
| 133 | * {Array(<OpenLayers.Feature.Vector>)} |
---|
| 134 | */ |
---|
| 135 | features: null, |
---|
| 136 | |
---|
| 137 | /** |
---|
| 138 | * Property: filter |
---|
| 139 | * {<OpenLayers.Filter>} The filter set in this layer, |
---|
| 140 | * a strategy launching read requests can combined |
---|
| 141 | * this filter with its own filter. |
---|
| 142 | */ |
---|
| 143 | filter: null, |
---|
| 144 | |
---|
| 145 | /** |
---|
| 146 | * Property: selectedFeatures |
---|
| 147 | * {Array(<OpenLayers.Feature.Vector>)} |
---|
| 148 | */ |
---|
| 149 | selectedFeatures: null, |
---|
| 150 | |
---|
| 151 | /** |
---|
| 152 | * Property: unrenderedFeatures |
---|
| 153 | * {Object} hash of features, keyed by feature.id, that the renderer |
---|
| 154 | * failed to draw |
---|
| 155 | */ |
---|
| 156 | unrenderedFeatures: null, |
---|
| 157 | |
---|
| 158 | /** |
---|
| 159 | * APIProperty: reportError |
---|
| 160 | * {Boolean} report friendly error message when loading of renderer |
---|
| 161 | * fails. |
---|
| 162 | */ |
---|
| 163 | reportError: true, |
---|
| 164 | |
---|
| 165 | /** |
---|
| 166 | * APIProperty: style |
---|
| 167 | * {Object} Default style for the layer |
---|
| 168 | */ |
---|
| 169 | style: null, |
---|
| 170 | |
---|
| 171 | /** |
---|
| 172 | * Property: styleMap |
---|
| 173 | * {<OpenLayers.StyleMap>} |
---|
| 174 | */ |
---|
| 175 | styleMap: null, |
---|
| 176 | |
---|
| 177 | /** |
---|
| 178 | * Property: strategies |
---|
| 179 | * {Array(<OpenLayers.Strategy>})} Optional list of strategies for the layer. |
---|
| 180 | */ |
---|
| 181 | strategies: null, |
---|
| 182 | |
---|
| 183 | /** |
---|
| 184 | * Property: protocol |
---|
| 185 | * {<OpenLayers.Protocol>} Optional protocol for the layer. |
---|
| 186 | */ |
---|
| 187 | protocol: null, |
---|
| 188 | |
---|
| 189 | /** |
---|
| 190 | * Property: renderers |
---|
| 191 | * {Array(String)} List of supported Renderer classes. Add to this list to |
---|
| 192 | * add support for additional renderers. This list is ordered: |
---|
| 193 | * the first renderer which returns true for the 'supported()' |
---|
| 194 | * method will be used, if not defined in the 'renderer' option. |
---|
| 195 | */ |
---|
| 196 | renderers: ['SVG', 'VML', 'Canvas'], |
---|
| 197 | |
---|
| 198 | /** |
---|
| 199 | * Property: renderer |
---|
| 200 | * {<OpenLayers.Renderer>} |
---|
| 201 | */ |
---|
| 202 | renderer: null, |
---|
| 203 | |
---|
| 204 | /** |
---|
| 205 | * APIProperty: rendererOptions |
---|
| 206 | * {Object} Options for the renderer. See {<OpenLayers.Renderer>} for |
---|
| 207 | * supported options. |
---|
| 208 | */ |
---|
| 209 | rendererOptions: null, |
---|
| 210 | |
---|
| 211 | /** |
---|
| 212 | * APIProperty: geometryType |
---|
| 213 | * {String} geometryType allows you to limit the types of geometries this |
---|
| 214 | * layer supports. This should be set to something like |
---|
| 215 | * "OpenLayers.Geometry.Point" to limit types. |
---|
| 216 | */ |
---|
| 217 | geometryType: null, |
---|
| 218 | |
---|
| 219 | /** |
---|
| 220 | * Property: drawn |
---|
| 221 | * {Boolean} Whether the Vector Layer features have been drawn yet. |
---|
| 222 | */ |
---|
| 223 | drawn: false, |
---|
| 224 | |
---|
| 225 | /** |
---|
| 226 | * Constructor: OpenLayers.Layer.Vector |
---|
| 227 | * Create a new vector layer |
---|
| 228 | * |
---|
| 229 | * Parameters: |
---|
| 230 | * name - {String} A name for the layer |
---|
| 231 | * options - {Object} Optional object with non-default properties to set on |
---|
| 232 | * the layer. |
---|
| 233 | * |
---|
| 234 | * Returns: |
---|
| 235 | * {<OpenLayers.Layer.Vector>} A new vector layer |
---|
| 236 | */ |
---|
| 237 | initialize: function(name, options) { |
---|
| 238 | |
---|
| 239 | // concatenate events specific to vector with those from the base |
---|
| 240 | this.EVENT_TYPES = |
---|
| 241 | OpenLayers.Layer.Vector.prototype.EVENT_TYPES.concat( |
---|
| 242 | OpenLayers.Layer.prototype.EVENT_TYPES |
---|
| 243 | ); |
---|
| 244 | |
---|
| 245 | OpenLayers.Layer.prototype.initialize.apply(this, arguments); |
---|
| 246 | |
---|
| 247 | // allow user-set renderer, otherwise assign one |
---|
| 248 | if (!this.renderer || !this.renderer.supported()) { |
---|
| 249 | this.assignRenderer(); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | // if no valid renderer found, display error |
---|
| 253 | if (!this.renderer || !this.renderer.supported()) { |
---|
| 254 | this.renderer = null; |
---|
| 255 | this.displayError(); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | if (!this.styleMap) { |
---|
| 259 | this.styleMap = new OpenLayers.StyleMap(); |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | this.features = []; |
---|
| 263 | this.selectedFeatures = []; |
---|
| 264 | this.unrenderedFeatures = {}; |
---|
| 265 | |
---|
| 266 | // Allow for custom layer behavior |
---|
| 267 | if(this.strategies){ |
---|
| 268 | for(var i=0, len=this.strategies.length; i<len; i++) { |
---|
| 269 | this.strategies[i].setLayer(this); |
---|
| 270 | } |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | }, |
---|
| 274 | |
---|
| 275 | /** |
---|
| 276 | * APIMethod: destroy |
---|
| 277 | * Destroy this layer |
---|
| 278 | */ |
---|
| 279 | destroy: function() { |
---|
| 280 | if (this.strategies) { |
---|
| 281 | var strategy, i, len; |
---|
| 282 | for(i=0, len=this.strategies.length; i<len; i++) { |
---|
| 283 | strategy = this.strategies[i]; |
---|
| 284 | if(strategy.autoDestroy) { |
---|
| 285 | strategy.destroy(); |
---|
| 286 | } |
---|
| 287 | } |
---|
| 288 | this.strategies = null; |
---|
| 289 | } |
---|
| 290 | if (this.protocol) { |
---|
| 291 | if(this.protocol.autoDestroy) { |
---|
| 292 | this.protocol.destroy(); |
---|
| 293 | } |
---|
| 294 | this.protocol = null; |
---|
| 295 | } |
---|
| 296 | this.destroyFeatures(); |
---|
| 297 | this.features = null; |
---|
| 298 | this.selectedFeatures = null; |
---|
| 299 | this.unrenderedFeatures = null; |
---|
| 300 | if (this.renderer) { |
---|
| 301 | this.renderer.destroy(); |
---|
| 302 | } |
---|
| 303 | this.renderer = null; |
---|
| 304 | this.geometryType = null; |
---|
| 305 | this.drawn = null; |
---|
| 306 | OpenLayers.Layer.prototype.destroy.apply(this, arguments); |
---|
| 307 | }, |
---|
| 308 | |
---|
| 309 | /** |
---|
| 310 | * Method: clone |
---|
| 311 | * Create a clone of this layer. |
---|
| 312 | * |
---|
| 313 | * Note: Features of the layer are also cloned. |
---|
| 314 | * |
---|
| 315 | * Returns: |
---|
| 316 | * {<OpenLayers.Layer.Vector>} An exact clone of this layer |
---|
| 317 | */ |
---|
| 318 | clone: function (obj) { |
---|
| 319 | |
---|
| 320 | if (obj == null) { |
---|
| 321 | obj = new OpenLayers.Layer.Vector(this.name, this.getOptions()); |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | //get all additions from superclasses |
---|
| 325 | obj = OpenLayers.Layer.prototype.clone.apply(this, [obj]); |
---|
| 326 | |
---|
| 327 | // copy/set any non-init, non-simple values here |
---|
| 328 | var features = this.features; |
---|
| 329 | var len = features.length; |
---|
| 330 | var clonedFeatures = new Array(len); |
---|
| 331 | for(var i=0; i<len; ++i) { |
---|
| 332 | clonedFeatures[i] = features[i].clone(); |
---|
| 333 | } |
---|
| 334 | obj.features = clonedFeatures; |
---|
| 335 | |
---|
| 336 | return obj; |
---|
| 337 | }, |
---|
| 338 | |
---|
| 339 | /** |
---|
| 340 | * Method: refresh |
---|
| 341 | * Ask the layer to request features again and redraw them. Triggers |
---|
| 342 | * the refresh event if the layer is in range and visible. |
---|
| 343 | * |
---|
| 344 | * Parameters: |
---|
| 345 | * obj - {Object} Optional object with properties for any listener of |
---|
| 346 | * the refresh event. |
---|
| 347 | */ |
---|
| 348 | refresh: function(obj) { |
---|
| 349 | if(this.calculateInRange() && this.visibility) { |
---|
| 350 | this.events.triggerEvent("refresh", obj); |
---|
| 351 | } |
---|
| 352 | }, |
---|
| 353 | |
---|
| 354 | /** |
---|
| 355 | * Method: assignRenderer |
---|
| 356 | * Iterates through the available renderer implementations and selects |
---|
| 357 | * and assigns the first one whose "supported()" function returns true. |
---|
| 358 | */ |
---|
| 359 | assignRenderer: function() { |
---|
| 360 | for (var i=0, len=this.renderers.length; i<len; i++) { |
---|
| 361 | var rendererClass = this.renderers[i]; |
---|
| 362 | var renderer = (typeof rendererClass == "function") ? |
---|
| 363 | rendererClass : |
---|
| 364 | OpenLayers.Renderer[rendererClass]; |
---|
| 365 | if (renderer && renderer.prototype.supported()) { |
---|
| 366 | this.renderer = new renderer(this.div, this.rendererOptions); |
---|
| 367 | break; |
---|
| 368 | } |
---|
| 369 | } |
---|
| 370 | }, |
---|
| 371 | |
---|
| 372 | /** |
---|
| 373 | * Method: displayError |
---|
| 374 | * Let the user know their browser isn't supported. |
---|
| 375 | */ |
---|
| 376 | displayError: function() { |
---|
| 377 | if (this.reportError) { |
---|
| 378 | OpenLayers.Console.userError(OpenLayers.i18n("browserNotSupported", |
---|
| 379 | {'renderers':this.renderers.join("\n")})); |
---|
| 380 | } |
---|
| 381 | }, |
---|
| 382 | |
---|
| 383 | /** |
---|
| 384 | * Method: setMap |
---|
| 385 | * The layer has been added to the map. |
---|
| 386 | * |
---|
| 387 | * If there is no renderer set, the layer can't be used. Remove it. |
---|
| 388 | * Otherwise, give the renderer a reference to the map and set its size. |
---|
| 389 | * |
---|
| 390 | * Parameters: |
---|
| 391 | * map - {<OpenLayers.Map>} |
---|
| 392 | */ |
---|
| 393 | setMap: function(map) { |
---|
| 394 | OpenLayers.Layer.prototype.setMap.apply(this, arguments); |
---|
| 395 | |
---|
| 396 | if (!this.renderer) { |
---|
| 397 | this.map.removeLayer(this); |
---|
| 398 | } else { |
---|
| 399 | this.renderer.map = this.map; |
---|
| 400 | this.renderer.setSize(this.map.getSize()); |
---|
| 401 | } |
---|
| 402 | }, |
---|
| 403 | |
---|
| 404 | /** |
---|
| 405 | * Method: afterAdd |
---|
| 406 | * Called at the end of the map.addLayer sequence. At this point, the map |
---|
| 407 | * will have a base layer. Any autoActivate strategies will be |
---|
| 408 | * activated here. |
---|
| 409 | */ |
---|
| 410 | afterAdd: function() { |
---|
| 411 | if(this.strategies) { |
---|
| 412 | var strategy, i, len; |
---|
| 413 | for(i=0, len=this.strategies.length; i<len; i++) { |
---|
| 414 | strategy = this.strategies[i]; |
---|
| 415 | if(strategy.autoActivate) { |
---|
| 416 | strategy.activate(); |
---|
| 417 | } |
---|
| 418 | } |
---|
| 419 | } |
---|
| 420 | }, |
---|
| 421 | |
---|
| 422 | /** |
---|
| 423 | * Method: removeMap |
---|
| 424 | * The layer has been removed from the map. |
---|
| 425 | * |
---|
| 426 | * Parameters: |
---|
| 427 | * map - {<OpenLayers.Map>} |
---|
| 428 | */ |
---|
| 429 | removeMap: function(map) { |
---|
| 430 | this.drawn = false; |
---|
| 431 | if(this.strategies) { |
---|
| 432 | var strategy, i, len; |
---|
| 433 | for(i=0, len=this.strategies.length; i<len; i++) { |
---|
| 434 | strategy = this.strategies[i]; |
---|
| 435 | if(strategy.autoActivate) { |
---|
| 436 | strategy.deactivate(); |
---|
| 437 | } |
---|
| 438 | } |
---|
| 439 | } |
---|
| 440 | }, |
---|
| 441 | |
---|
| 442 | /** |
---|
| 443 | * Method: onMapResize |
---|
| 444 | * Notify the renderer of the change in size. |
---|
| 445 | * |
---|
| 446 | */ |
---|
| 447 | onMapResize: function() { |
---|
| 448 | OpenLayers.Layer.prototype.onMapResize.apply(this, arguments); |
---|
| 449 | this.renderer.setSize(this.map.getSize()); |
---|
| 450 | }, |
---|
| 451 | |
---|
| 452 | /** |
---|
| 453 | * Method: moveTo |
---|
| 454 | * Reset the vector layer's div so that it once again is lined up with |
---|
| 455 | * the map. Notify the renderer of the change of extent, and in the |
---|
| 456 | * case of a change of zoom level (resolution), have the |
---|
| 457 | * renderer redraw features. |
---|
| 458 | * |
---|
| 459 | * If the layer has not yet been drawn, cycle through the layer's |
---|
| 460 | * features and draw each one. |
---|
| 461 | * |
---|
| 462 | * Parameters: |
---|
| 463 | * bounds - {<OpenLayers.Bounds>} |
---|
| 464 | * zoomChanged - {Boolean} |
---|
| 465 | * dragging - {Boolean} |
---|
| 466 | */ |
---|
| 467 | moveTo: function(bounds, zoomChanged, dragging) { |
---|
| 468 | OpenLayers.Layer.prototype.moveTo.apply(this, arguments); |
---|
| 469 | |
---|
| 470 | var coordSysUnchanged = true; |
---|
| 471 | |
---|
| 472 | if (!dragging) { |
---|
| 473 | this.renderer.root.style.visibility = "hidden"; |
---|
| 474 | |
---|
| 475 | this.div.style.left = -parseInt(this.map.layerContainerDiv.style.left) + "px"; |
---|
| 476 | this.div.style.top = -parseInt(this.map.layerContainerDiv.style.top) + "px"; |
---|
| 477 | var extent = this.map.getExtent(); |
---|
| 478 | coordSysUnchanged = this.renderer.setExtent(extent, zoomChanged); |
---|
| 479 | |
---|
| 480 | this.renderer.root.style.visibility = "visible"; |
---|
| 481 | |
---|
| 482 | // Force a reflow on gecko based browsers to prevent jump/flicker. |
---|
| 483 | // This seems to happen on only certain configurations; it was originally |
---|
| 484 | // noticed in FF 2.0 and Linux. |
---|
| 485 | if (navigator.userAgent.toLowerCase().indexOf("gecko") != -1) { |
---|
| 486 | this.div.scrollLeft = this.div.scrollLeft; |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | if(!zoomChanged && coordSysUnchanged) { |
---|
| 490 | for(var i in this.unrenderedFeatures) { |
---|
| 491 | var feature = this.unrenderedFeatures[i]; |
---|
| 492 | this.drawFeature(feature); |
---|
| 493 | } |
---|
| 494 | } |
---|
| 495 | } |
---|
| 496 | |
---|
| 497 | if (!this.drawn || zoomChanged || !coordSysUnchanged) { |
---|
| 498 | this.drawn = true; |
---|
| 499 | var feature; |
---|
| 500 | for(var i=0, len=this.features.length; i<len; i++) { |
---|
| 501 | this.renderer.locked = (i !== (len - 1)); |
---|
| 502 | feature = this.features[i]; |
---|
| 503 | this.drawFeature(feature); |
---|
| 504 | } |
---|
| 505 | } |
---|
| 506 | }, |
---|
| 507 | |
---|
| 508 | /** |
---|
| 509 | * APIMethod: display |
---|
| 510 | * Hide or show the Layer |
---|
| 511 | * |
---|
| 512 | * Parameters: |
---|
| 513 | * display - {Boolean} |
---|
| 514 | */ |
---|
| 515 | display: function(display) { |
---|
| 516 | OpenLayers.Layer.prototype.display.apply(this, arguments); |
---|
| 517 | // we need to set the display style of the root in case it is attached |
---|
| 518 | // to a foreign layer |
---|
| 519 | var currentDisplay = this.div.style.display; |
---|
| 520 | if(currentDisplay != this.renderer.root.style.display) { |
---|
| 521 | this.renderer.root.style.display = currentDisplay; |
---|
| 522 | } |
---|
| 523 | }, |
---|
| 524 | |
---|
| 525 | /** |
---|
| 526 | * APIMethod: addFeatures |
---|
| 527 | * Add Features to the layer. |
---|
| 528 | * |
---|
| 529 | * Parameters: |
---|
| 530 | * features - {Array(<OpenLayers.Feature.Vector>)} |
---|
| 531 | * options - {Object} |
---|
| 532 | */ |
---|
| 533 | addFeatures: function(features, options) { |
---|
| 534 | if (!(features instanceof Array)) { |
---|
| 535 | features = [features]; |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | var notify = !options || !options.silent; |
---|
| 539 | if(notify) { |
---|
| 540 | var event = {features: features}; |
---|
| 541 | var ret = this.events.triggerEvent("beforefeaturesadded", event); |
---|
| 542 | if(ret === false) { |
---|
| 543 | return; |
---|
| 544 | } |
---|
| 545 | features = event.features; |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | // Track successfully added features for featuresadded event, since |
---|
| 549 | // beforefeatureadded can veto single features. |
---|
| 550 | var featuresAdded = []; |
---|
| 551 | for (var i=0, len=features.length; i<len; i++) { |
---|
| 552 | if (i != (features.length - 1)) { |
---|
| 553 | this.renderer.locked = true; |
---|
| 554 | } else { |
---|
| 555 | this.renderer.locked = false; |
---|
| 556 | } |
---|
| 557 | var feature = features[i]; |
---|
| 558 | |
---|
| 559 | if (this.geometryType && |
---|
| 560 | !(feature.geometry instanceof this.geometryType)) { |
---|
| 561 | var throwStr = OpenLayers.i18n('componentShouldBe', |
---|
| 562 | {'geomType':this.geometryType.prototype.CLASS_NAME}); |
---|
| 563 | throw throwStr; |
---|
| 564 | } |
---|
| 565 | |
---|
| 566 | //give feature reference to its layer |
---|
| 567 | feature.layer = this; |
---|
| 568 | |
---|
| 569 | if (!feature.style && this.style) { |
---|
| 570 | feature.style = OpenLayers.Util.extend({}, this.style); |
---|
| 571 | } |
---|
| 572 | |
---|
| 573 | if (notify) { |
---|
| 574 | if(this.events.triggerEvent("beforefeatureadded", |
---|
| 575 | {feature: feature}) === false) { |
---|
| 576 | continue; |
---|
| 577 | }; |
---|
| 578 | this.preFeatureInsert(feature); |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | featuresAdded.push(feature); |
---|
| 582 | this.features.push(feature); |
---|
| 583 | this.drawFeature(feature); |
---|
| 584 | |
---|
| 585 | if (notify) { |
---|
| 586 | this.events.triggerEvent("featureadded", { |
---|
| 587 | feature: feature |
---|
| 588 | }); |
---|
| 589 | this.onFeatureInsert(feature); |
---|
| 590 | } |
---|
| 591 | } |
---|
| 592 | |
---|
| 593 | if(notify) { |
---|
| 594 | this.events.triggerEvent("featuresadded", {features: featuresAdded}); |
---|
| 595 | } |
---|
| 596 | }, |
---|
| 597 | |
---|
| 598 | |
---|
| 599 | /** |
---|
| 600 | * APIMethod: removeFeatures |
---|
| 601 | * Remove features from the layer. This erases any drawn features and |
---|
| 602 | * removes them from the layer's control. The beforefeatureremoved |
---|
| 603 | * and featureremoved events will be triggered for each feature. The |
---|
| 604 | * featuresremoved event will be triggered after all features have |
---|
| 605 | * been removed. To supress event triggering, use the silent option. |
---|
| 606 | * |
---|
| 607 | * Parameters: |
---|
| 608 | * features - {Array(<OpenLayers.Feature.Vector>)} List of features to be |
---|
| 609 | * removed. |
---|
| 610 | * options - {Object} Optional properties for changing behavior of the |
---|
| 611 | * removal. |
---|
| 612 | * |
---|
| 613 | * Valid options: |
---|
| 614 | * silent - {Boolean} Supress event triggering. Default is false. |
---|
| 615 | */ |
---|
| 616 | removeFeatures: function(features, options) { |
---|
| 617 | if(!features || features.length === 0) { |
---|
| 618 | return; |
---|
| 619 | } |
---|
| 620 | if (features === this.features) { |
---|
| 621 | return this.removeAllFeatures(options); |
---|
| 622 | } |
---|
| 623 | if (!(features instanceof Array)) { |
---|
| 624 | features = [features]; |
---|
| 625 | } |
---|
| 626 | if (features === this.selectedFeatures) { |
---|
| 627 | features = features.slice(); |
---|
| 628 | } |
---|
| 629 | |
---|
| 630 | var notify = !options || !options.silent; |
---|
| 631 | |
---|
| 632 | if (notify) { |
---|
| 633 | this.events.triggerEvent( |
---|
| 634 | "beforefeaturesremoved", {features: features} |
---|
| 635 | ); |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | for (var i = features.length - 1; i >= 0; i--) { |
---|
| 639 | // We remain locked so long as we're not at 0 |
---|
| 640 | // and the 'next' feature has a geometry. We do the geometry check |
---|
| 641 | // because if all the features after the current one are 'null', we |
---|
| 642 | // won't call eraseGeometry, so we break the 'renderer functions |
---|
| 643 | // will always be called with locked=false *last*' rule. The end result |
---|
| 644 | // is a possible gratiutious unlocking to save a loop through the rest |
---|
| 645 | // of the list checking the remaining features every time. So long as |
---|
| 646 | // null geoms are rare, this is probably okay. |
---|
| 647 | if (i != 0 && features[i-1].geometry) { |
---|
| 648 | this.renderer.locked = true; |
---|
| 649 | } else { |
---|
| 650 | this.renderer.locked = false; |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | var feature = features[i]; |
---|
| 654 | delete this.unrenderedFeatures[feature.id]; |
---|
| 655 | |
---|
| 656 | if (notify) { |
---|
| 657 | this.events.triggerEvent("beforefeatureremoved", { |
---|
| 658 | feature: feature |
---|
| 659 | }); |
---|
| 660 | } |
---|
| 661 | |
---|
| 662 | this.features = OpenLayers.Util.removeItem(this.features, feature); |
---|
| 663 | // feature has no layer at this point |
---|
| 664 | feature.layer = null; |
---|
| 665 | |
---|
| 666 | if (feature.geometry) { |
---|
| 667 | this.renderer.eraseFeatures(feature); |
---|
| 668 | } |
---|
| 669 | |
---|
| 670 | //in the case that this feature is one of the selected features, |
---|
| 671 | // remove it from that array as well. |
---|
| 672 | if (OpenLayers.Util.indexOf(this.selectedFeatures, feature) != -1){ |
---|
| 673 | OpenLayers.Util.removeItem(this.selectedFeatures, feature); |
---|
| 674 | } |
---|
| 675 | |
---|
| 676 | if (notify) { |
---|
| 677 | this.events.triggerEvent("featureremoved", { |
---|
| 678 | feature: feature |
---|
| 679 | }); |
---|
| 680 | } |
---|
| 681 | } |
---|
| 682 | |
---|
| 683 | if (notify) { |
---|
| 684 | this.events.triggerEvent("featuresremoved", {features: features}); |
---|
| 685 | } |
---|
| 686 | }, |
---|
| 687 | |
---|
| 688 | /** |
---|
| 689 | * APIMethod: removeAllFeatures |
---|
| 690 | * Remove all features from the layer. |
---|
| 691 | * |
---|
| 692 | * Parameters: |
---|
| 693 | * options - {Object} Optional properties for changing behavior of the |
---|
| 694 | * removal. |
---|
| 695 | * |
---|
| 696 | * Valid options: |
---|
| 697 | * silent - {Boolean} Supress event triggering. Default is false. |
---|
| 698 | */ |
---|
| 699 | removeAllFeatures: function(options) { |
---|
| 700 | var notify = !options || !options.silent; |
---|
| 701 | var features = this.features; |
---|
| 702 | if (notify) { |
---|
| 703 | this.events.triggerEvent( |
---|
| 704 | "beforefeaturesremoved", {features: features} |
---|
| 705 | ); |
---|
| 706 | } |
---|
| 707 | var feature; |
---|
| 708 | for (var i = features.length-1; i >= 0; i--) { |
---|
| 709 | feature = features[i]; |
---|
| 710 | if (notify) { |
---|
| 711 | this.events.triggerEvent("beforefeatureremoved", { |
---|
| 712 | feature: feature |
---|
| 713 | }); |
---|
| 714 | } |
---|
| 715 | feature.layer = null; |
---|
| 716 | if (notify) { |
---|
| 717 | this.events.triggerEvent("featureremoved", { |
---|
| 718 | feature: feature |
---|
| 719 | }); |
---|
| 720 | } |
---|
| 721 | } |
---|
| 722 | this.renderer.clear(); |
---|
| 723 | this.features = []; |
---|
| 724 | this.unrenderedFeatures = {}; |
---|
| 725 | this.selectedFeatures = []; |
---|
| 726 | if (notify) { |
---|
| 727 | this.events.triggerEvent("featuresremoved", {features: features}); |
---|
| 728 | } |
---|
| 729 | }, |
---|
| 730 | |
---|
| 731 | /** |
---|
| 732 | * APIMethod: destroyFeatures |
---|
| 733 | * Erase and destroy features on the layer. |
---|
| 734 | * |
---|
| 735 | * Parameters: |
---|
| 736 | * features - {Array(<OpenLayers.Feature.Vector>)} An optional array of |
---|
| 737 | * features to destroy. If not supplied, all features on the layer |
---|
| 738 | * will be destroyed. |
---|
| 739 | * options - {Object} |
---|
| 740 | */ |
---|
| 741 | destroyFeatures: function(features, options) { |
---|
| 742 | var all = (features == undefined); // evaluates to true if |
---|
| 743 | // features is null |
---|
| 744 | if(all) { |
---|
| 745 | features = this.features; |
---|
| 746 | } |
---|
| 747 | if(features) { |
---|
| 748 | this.removeFeatures(features, options); |
---|
| 749 | for(var i=features.length-1; i>=0; i--) { |
---|
| 750 | features[i].destroy(); |
---|
| 751 | } |
---|
| 752 | } |
---|
| 753 | }, |
---|
| 754 | |
---|
| 755 | /** |
---|
| 756 | * APIMethod: drawFeature |
---|
| 757 | * Draw (or redraw) a feature on the layer. If the optional style argument |
---|
| 758 | * is included, this style will be used. If no style is included, the |
---|
| 759 | * feature's style will be used. If the feature doesn't have a style, |
---|
| 760 | * the layer's style will be used. |
---|
| 761 | * |
---|
| 762 | * This function is not designed to be used when adding features to |
---|
| 763 | * the layer (use addFeatures instead). It is meant to be used when |
---|
| 764 | * the style of a feature has changed, or in some other way needs to |
---|
| 765 | * visually updated *after* it has already been added to a layer. You |
---|
| 766 | * must add the feature to the layer for most layer-related events to |
---|
| 767 | * happen. |
---|
| 768 | * |
---|
| 769 | * Parameters: |
---|
| 770 | * feature - {<OpenLayers.Feature.Vector>} |
---|
| 771 | * style - {String | Object} Named render intent or full symbolizer object. |
---|
| 772 | */ |
---|
| 773 | drawFeature: function(feature, style) { |
---|
| 774 | // don't try to draw the feature with the renderer if the layer is not |
---|
| 775 | // drawn itself |
---|
| 776 | if (!this.drawn) { |
---|
| 777 | return |
---|
| 778 | } |
---|
| 779 | if (typeof style != "object") { |
---|
| 780 | if(!style && feature.state === OpenLayers.State.DELETE) { |
---|
| 781 | style = "delete"; |
---|
| 782 | } |
---|
| 783 | var renderIntent = style || feature.renderIntent; |
---|
| 784 | style = feature.style || this.style; |
---|
| 785 | if (!style) { |
---|
| 786 | style = this.styleMap.createSymbolizer(feature, renderIntent); |
---|
| 787 | } |
---|
| 788 | } |
---|
| 789 | |
---|
| 790 | if (!this.renderer.drawFeature(feature, style)) { |
---|
| 791 | this.unrenderedFeatures[feature.id] = feature; |
---|
| 792 | } else { |
---|
| 793 | delete this.unrenderedFeatures[feature.id]; |
---|
| 794 | }; |
---|
| 795 | }, |
---|
| 796 | |
---|
| 797 | /** |
---|
| 798 | * Method: eraseFeatures |
---|
| 799 | * Erase features from the layer. |
---|
| 800 | * |
---|
| 801 | * Parameters: |
---|
| 802 | * features - {Array(<OpenLayers.Feature.Vector>)} |
---|
| 803 | */ |
---|
| 804 | eraseFeatures: function(features) { |
---|
| 805 | this.renderer.eraseFeatures(features); |
---|
| 806 | }, |
---|
| 807 | |
---|
| 808 | /** |
---|
| 809 | * Method: getFeatureFromEvent |
---|
| 810 | * Given an event, return a feature if the event occurred over one. |
---|
| 811 | * Otherwise, return null. |
---|
| 812 | * |
---|
| 813 | * Parameters: |
---|
| 814 | * evt - {Event} |
---|
| 815 | * |
---|
| 816 | * Returns: |
---|
| 817 | * {<OpenLayers.Feature.Vector>} A feature if one was under the event. |
---|
| 818 | */ |
---|
| 819 | getFeatureFromEvent: function(evt) { |
---|
| 820 | if (!this.renderer) { |
---|
| 821 | OpenLayers.Console.error(OpenLayers.i18n("getFeatureError")); |
---|
| 822 | return null; |
---|
| 823 | } |
---|
| 824 | var featureId = this.renderer.getFeatureIdFromEvent(evt); |
---|
| 825 | return this.getFeatureById(featureId); |
---|
| 826 | }, |
---|
| 827 | |
---|
| 828 | /** |
---|
| 829 | * APIMethod: getFeatureBy |
---|
| 830 | * Given a property value, return the feature if it exists in the features array |
---|
| 831 | * |
---|
| 832 | * Parameters: |
---|
| 833 | * property - {String} |
---|
| 834 | * value - {String} |
---|
| 835 | * |
---|
| 836 | * Returns: |
---|
| 837 | * {<OpenLayers.Feature.Vector>} A feature corresponding to the given |
---|
| 838 | * property value or null if there is no such feature. |
---|
| 839 | */ |
---|
| 840 | getFeatureBy: function(property, value) { |
---|
| 841 | //TBD - would it be more efficient to use a hash for this.features? |
---|
| 842 | var feature = null; |
---|
| 843 | for(var i=0, len=this.features.length; i<len; ++i) { |
---|
| 844 | if(this.features[i][property] == value) { |
---|
| 845 | feature = this.features[i]; |
---|
| 846 | break; |
---|
| 847 | } |
---|
| 848 | } |
---|
| 849 | return feature; |
---|
| 850 | }, |
---|
| 851 | |
---|
| 852 | /** |
---|
| 853 | * APIMethod: getFeatureById |
---|
| 854 | * Given a feature id, return the feature if it exists in the features array |
---|
| 855 | * |
---|
| 856 | * Parameters: |
---|
| 857 | * featureId - {String} |
---|
| 858 | * |
---|
| 859 | * Returns: |
---|
| 860 | * {<OpenLayers.Feature.Vector>} A feature corresponding to the given |
---|
| 861 | * featureId or null if there is no such feature. |
---|
| 862 | */ |
---|
| 863 | getFeatureById: function(featureId) { |
---|
| 864 | return this.getFeatureBy('id', featureId); |
---|
| 865 | }, |
---|
| 866 | |
---|
| 867 | /** |
---|
| 868 | * APIMethod: getFeatureByFid |
---|
| 869 | * Given a feature fid, return the feature if it exists in the features array |
---|
| 870 | * |
---|
| 871 | * Parameters: |
---|
| 872 | * featureFid - {String} |
---|
| 873 | * |
---|
| 874 | * Returns: |
---|
| 875 | * {<OpenLayers.Feature.Vector>} A feature corresponding to the given |
---|
| 876 | * featureFid or null if there is no such feature. |
---|
| 877 | */ |
---|
| 878 | getFeatureByFid: function(featureFid) { |
---|
| 879 | return this.getFeatureBy('fid', featureFid); |
---|
| 880 | }, |
---|
| 881 | |
---|
| 882 | /** |
---|
| 883 | * Unselect the selected features |
---|
| 884 | * i.e. clears the featureSelection array |
---|
| 885 | * change the style back |
---|
| 886 | clearSelection: function() { |
---|
| 887 | |
---|
| 888 | var vectorLayer = this.map.vectorLayer; |
---|
| 889 | for (var i = 0; i < this.map.featureSelection.length; i++) { |
---|
| 890 | var featureSelection = this.map.featureSelection[i]; |
---|
| 891 | vectorLayer.drawFeature(featureSelection, vectorLayer.style); |
---|
| 892 | } |
---|
| 893 | this.map.featureSelection = []; |
---|
| 894 | }, |
---|
| 895 | */ |
---|
| 896 | |
---|
| 897 | |
---|
| 898 | /** |
---|
| 899 | * APIMethod: onFeatureInsert |
---|
| 900 | * method called after a feature is inserted. |
---|
| 901 | * Does nothing by default. Override this if you |
---|
| 902 | * need to do something on feature updates. |
---|
| 903 | * |
---|
| 904 | * Paarameters: |
---|
| 905 | * feature - {<OpenLayers.Feature.Vector>} |
---|
| 906 | */ |
---|
| 907 | onFeatureInsert: function(feature) { |
---|
| 908 | }, |
---|
| 909 | |
---|
| 910 | /** |
---|
| 911 | * APIMethod: preFeatureInsert |
---|
| 912 | * method called before a feature is inserted. |
---|
| 913 | * Does nothing by default. Override this if you |
---|
| 914 | * need to do something when features are first added to the |
---|
| 915 | * layer, but before they are drawn, such as adjust the style. |
---|
| 916 | * |
---|
| 917 | * Parameters: |
---|
| 918 | * feature - {<OpenLayers.Feature.Vector>} |
---|
| 919 | */ |
---|
| 920 | preFeatureInsert: function(feature) { |
---|
| 921 | }, |
---|
| 922 | |
---|
| 923 | /** |
---|
| 924 | * APIMethod: getDataExtent |
---|
| 925 | * Calculates the max extent which includes all of the features. |
---|
| 926 | * |
---|
| 927 | * Returns: |
---|
| 928 | * {<OpenLayers.Bounds>} |
---|
| 929 | */ |
---|
| 930 | getDataExtent: function () { |
---|
| 931 | var maxExtent = null; |
---|
| 932 | var features = this.features; |
---|
| 933 | if(features && (features.length > 0)) { |
---|
| 934 | maxExtent = new OpenLayers.Bounds(); |
---|
| 935 | var geometry = null; |
---|
| 936 | for(var i=0, len=features.length; i<len; i++) { |
---|
| 937 | geometry = features[i].geometry; |
---|
| 938 | if (geometry) { |
---|
| 939 | maxExtent.extend(geometry.getBounds()); |
---|
| 940 | } |
---|
| 941 | } |
---|
| 942 | } |
---|
| 943 | return maxExtent; |
---|
| 944 | }, |
---|
| 945 | |
---|
| 946 | CLASS_NAME: "OpenLayers.Layer.Vector" |
---|
| 947 | }); |
---|