1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
* full list of contributors). Published under the 2-clause BSD license.
* See license.txt in the OpenLayers distribution or repository for the
* full text of the license. */
/**
* @requires OpenLayers/Control.js
* @requires OpenLayers/Feature/Vector.js
*/
/**
* Class: OpenLayers.Control.DrawFeature
* The DrawFeature control draws point, line or polygon features on a vector
* layer when active.
*
* Inherits from:
* - <OpenLayers.Control>
*/
OpenLayers.Control.DrawFeature = OpenLayers.Class(OpenLayers.Control, {
/**
* Property: layer
* {<OpenLayers.Layer.Vector>}
*/
layer: null,
/**
* Property: callbacks
* {Object} The functions that are sent to the handler for callback
*/
callbacks: null,
/**
* APIProperty: events
* {<OpenLayers.Events>} Events instance for listeners and triggering
* control specific events.
*
* Register a listener for a particular event with the following syntax:
* (code)
* control.events.register(type, obj, listener);
* (end)
*
* Supported event types (in addition to those from <OpenLayers.Control.events>):
* featureadded - Triggered when a feature is added
*/
/**
* APIProperty: multi
* {Boolean} Cast features to multi-part geometries before passing to the
* layer. Default is false.
*/
multi: false,
/**
* APIProperty: featureAdded
* {Function} Called after each feature is added
*/
featureAdded: function() {},
/**
* APIProperty: handlerOptions
* {Object} Used to set non-default properties on the control's handler
*/
/**
* Constructor: OpenLayers.Control.DrawFeature
*
* Parameters:
* layer - {<OpenLayers.Layer.Vector>}
* handler - {<OpenLayers.Handler>}
* options - {Object}
*/
initialize: function(layer, handler, options) {
OpenLayers.Control.prototype.initialize.apply(this, [options]);
this.callbacks = OpenLayers.Util.extend(
{
done: this.drawFeature,
modify: function(vertex, feature) {
this.layer.events.triggerEvent(
"sketchmodified", {vertex: vertex, feature: feature}
);
},
create: function(vertex, feature) {
this.layer.events.triggerEvent(
"sketchstarted", {vertex: vertex, feature: feature}
);
}
},
this.callbacks
);
this.layer = layer;
this.handlerOptions = this.handlerOptions || {};
this.handlerOptions.layerOptions = OpenLayers.Util.applyDefaults(
this.handlerOptions.layerOptions, {
renderers: layer.renderers, rendererOptions: layer.rendererOptions
}
);
if (!("multi" in this.handlerOptions)) {
this.handlerOptions.multi = this.multi;
}
var sketchStyle = this.layer.styleMap && this.layer.styleMap.styles.temporary;
if(sketchStyle) {
this.handlerOptions.layerOptions = OpenLayers.Util.applyDefaults(
this.handlerOptions.layerOptions,
{styleMap: new OpenLayers.StyleMap({"default": sketchStyle})}
);
}
this.handler = new handler(this, this.callbacks, this.handlerOptions);
},
/**
* Method: drawFeature
*/
drawFeature: function(geometry) {
var feature = new OpenLayers.Feature.Vector(geometry);
var proceed = this.layer.events.triggerEvent(
"sketchcomplete", {feature: feature}
);
if(proceed !== false) {
feature.state = OpenLayers.State.INSERT;
this.layer.addFeatures([feature]);
this.featureAdded(feature);
this.events.triggerEvent("featureadded",{feature : feature});
}
},
/**
* APIMethod: insertXY
* Insert a point in the current sketch given x & y coordinates.
*
* Parameters:
* x - {Number} The x-coordinate of the point.
* y - {Number} The y-coordinate of the point.
*/
insertXY: function(x, y) {
if (this.handler && this.handler.line) {
this.handler.insertXY(x, y);
}
},
/**
* APIMethod: insertDeltaXY
* Insert a point given offsets from the previously inserted point.
*
* Parameters:
* dx - {Number} The x-coordinate offset of the point.
* dy - {Number} The y-coordinate offset of the point.
*/
insertDeltaXY: function(dx, dy) {
if (this.handler && this.handler.line) {
this.handler.insertDeltaXY(dx, dy);
}
},
/**
* APIMethod: insertDirectionLength
* Insert a point in the current sketch given a direction and a length.
*
* Parameters:
* direction - {Number} Degrees clockwise from the positive x-axis.
* length - {Number} Distance from the previously drawn point.
*/
insertDirectionLength: function(direction, length) {
if (this.handler && this.handler.line) {
this.handler.insertDirectionLength(direction, length);
}
},
/**
* APIMethod: insertDeflectionLength
* Insert a point in the current sketch given a deflection and a length.
* The deflection should be degrees clockwise from the previously
* digitized segment.
*
* Parameters:
* deflection - {Number} Degrees clockwise from the previous segment.
* length - {Number} Distance from the previously drawn point.
*/
insertDeflectionLength: function(deflection, length) {
if (this.handler && this.handler.line) {
this.handler.insertDeflectionLength(deflection, length);
}
},
/**
* APIMethod: undo
* Remove the most recently added point in the current sketch geometry.
*
* Returns:
* {Boolean} An edit was undone.
*/
undo: function() {
return this.handler.undo && this.handler.undo();
},
/**
* APIMethod: redo
* Reinsert the most recently removed point resulting from an <undo> call.
* The undo stack is deleted whenever a point is added by other means.
*
* Returns:
* {Boolean} An edit was redone.
*/
redo: function() {
return this.handler.redo && this.handler.redo();
},
/**
* APIMethod: finishSketch
* Finishes the sketch without including the currently drawn point.
* This method can be called to terminate drawing programmatically
* instead of waiting for the user to end the sketch.
*/
finishSketch: function() {
this.handler.finishGeometry();
},
/**
* APIMethod: cancel
* Cancel the current sketch. This removes the current sketch and keeps
* the drawing control active.
*/
cancel: function() {
this.handler.cancel();
},
CLASS_NAME: "OpenLayers.Control.DrawFeature"
});
|