[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/Control.js |
---|
| 9 | * @requires OpenLayers/Feature/Vector.js |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | /** |
---|
| 13 | * Class: OpenLayers.Control.DrawFeature |
---|
| 14 | * The DrawFeature control draws point, line or polygon features on a vector |
---|
| 15 | * layer when active. |
---|
| 16 | * |
---|
| 17 | * Inherits from: |
---|
| 18 | * - <OpenLayers.Control> |
---|
| 19 | */ |
---|
| 20 | OpenLayers.Control.DrawFeature = OpenLayers.Class(OpenLayers.Control, { |
---|
| 21 | |
---|
| 22 | /** |
---|
| 23 | * Property: layer |
---|
| 24 | * {<OpenLayers.Layer.Vector>} |
---|
| 25 | */ |
---|
| 26 | layer: null, |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * Property: callbacks |
---|
| 30 | * {Object} The functions that are sent to the handler for callback |
---|
| 31 | */ |
---|
| 32 | callbacks: null, |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * Constant: EVENT_TYPES |
---|
| 36 | * |
---|
| 37 | * Supported event types: |
---|
| 38 | * featureadded - Triggered when a feature is added |
---|
| 39 | */ |
---|
| 40 | EVENT_TYPES: ["featureadded"], |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * APIProperty: multi |
---|
| 44 | * {Boolean} Cast features to multi-part geometries before passing to the |
---|
| 45 | * layer. Default is false. |
---|
| 46 | */ |
---|
| 47 | multi: false, |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * APIProperty: featureAdded |
---|
| 51 | * {Function} Called after each feature is added |
---|
| 52 | */ |
---|
| 53 | featureAdded: function() {}, |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | * APIProperty: handlerOptions |
---|
| 57 | * {Object} Used to set non-default properties on the control's handler |
---|
| 58 | */ |
---|
| 59 | handlerOptions: null, |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * Constructor: OpenLayers.Control.DrawFeature |
---|
| 63 | * |
---|
| 64 | * Parameters: |
---|
| 65 | * layer - {<OpenLayers.Layer.Vector>} |
---|
| 66 | * handler - {<OpenLayers.Handler>} |
---|
| 67 | * options - {Object} |
---|
| 68 | */ |
---|
| 69 | initialize: function(layer, handler, options) { |
---|
| 70 | |
---|
| 71 | // concatenate events specific to vector with those from the base |
---|
| 72 | this.EVENT_TYPES = |
---|
| 73 | OpenLayers.Control.DrawFeature.prototype.EVENT_TYPES.concat( |
---|
| 74 | OpenLayers.Control.prototype.EVENT_TYPES |
---|
| 75 | ); |
---|
| 76 | |
---|
| 77 | OpenLayers.Control.prototype.initialize.apply(this, [options]); |
---|
| 78 | this.callbacks = OpenLayers.Util.extend( |
---|
| 79 | { |
---|
| 80 | done: this.drawFeature, |
---|
| 81 | modify: function(vertex, feature) { |
---|
| 82 | this.layer.events.triggerEvent( |
---|
| 83 | "sketchmodified", {vertex: vertex, feature: feature} |
---|
| 84 | ); |
---|
| 85 | }, |
---|
| 86 | create: function(vertex, feature) { |
---|
| 87 | this.layer.events.triggerEvent( |
---|
| 88 | "sketchstarted", {vertex: vertex, feature: feature} |
---|
| 89 | ); |
---|
| 90 | } |
---|
| 91 | }, |
---|
| 92 | this.callbacks |
---|
| 93 | ); |
---|
| 94 | this.layer = layer; |
---|
| 95 | this.handlerOptions = this.handlerOptions || {}; |
---|
| 96 | if (!("multi" in this.handlerOptions)) { |
---|
| 97 | this.handlerOptions.multi = this.multi; |
---|
| 98 | } |
---|
| 99 | var sketchStyle = this.layer.styleMap && this.layer.styleMap.styles.temporary; |
---|
| 100 | if(sketchStyle) { |
---|
| 101 | this.handlerOptions.layerOptions = OpenLayers.Util.applyDefaults( |
---|
| 102 | this.handlerOptions.layerOptions, |
---|
| 103 | {styleMap: new OpenLayers.StyleMap({"default": sketchStyle})} |
---|
| 104 | ); |
---|
| 105 | } |
---|
| 106 | this.handler = new handler(this, this.callbacks, this.handlerOptions); |
---|
| 107 | }, |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | * Method: drawFeature |
---|
| 111 | */ |
---|
| 112 | drawFeature: function(geometry) { |
---|
| 113 | var feature = new OpenLayers.Feature.Vector(geometry); |
---|
| 114 | var proceed = this.layer.events.triggerEvent( |
---|
| 115 | "sketchcomplete", {feature: feature} |
---|
| 116 | ); |
---|
| 117 | if(proceed !== false) { |
---|
| 118 | feature.state = OpenLayers.State.INSERT; |
---|
| 119 | this.layer.addFeatures([feature]); |
---|
| 120 | this.featureAdded(feature); |
---|
| 121 | this.events.triggerEvent("featureadded",{feature : feature}); |
---|
| 122 | } |
---|
| 123 | }, |
---|
| 124 | |
---|
| 125 | CLASS_NAME: "OpenLayers.Control.DrawFeature" |
---|
| 126 | }); |
---|