Adding a featureadded event to the draw feature control event types. First tests ever for the draw feature control. Thanks sbenthall for the patch. r=me (closes #1508)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@7601 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2008-07-30 03:41:22 +00:00
parent b837fea7a2
commit 309877a48c
3 changed files with 55 additions and 0 deletions

View File

@@ -29,6 +29,14 @@ OpenLayers.Control.DrawFeature = OpenLayers.Class(OpenLayers.Control, {
*/
callbacks: null,
/**
* Constant: EVENT_TYPES
*
* Supported event types:
* - *featureadded* Triggered when a feature is added
*/
EVENT_TYPES: ["featureadded"],
/**
* APIProperty: featureAdded
* {Function} Called after each feature is added
@@ -50,6 +58,13 @@ OpenLayers.Control.DrawFeature = OpenLayers.Class(OpenLayers.Control, {
* options - {Object}
*/
initialize: function(layer, handler, options) {
// concatenate events specific to vector with those from the base
this.EVENT_TYPES =
OpenLayers.Control.DrawFeature.prototype.EVENT_TYPES.concat(
OpenLayers.Control.prototype.EVENT_TYPES
);
OpenLayers.Control.prototype.initialize.apply(this, [options]);
this.callbacks = OpenLayers.Util.extend({done: this.drawFeature},
this.callbacks);
@@ -64,6 +79,7 @@ OpenLayers.Control.DrawFeature = OpenLayers.Class(OpenLayers.Control, {
var feature = new OpenLayers.Feature.Vector(geometry);
this.layer.addFeatures([feature]);
this.featureAdded(feature);
this.events.triggerEvent("featureadded",{feature : feature});
},
CLASS_NAME: "OpenLayers.Control.DrawFeature"

View File

@@ -0,0 +1,38 @@
<html>
<head>
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_initialize(t) {
t.plan(2);
var control = new OpenLayers.Control.DrawFeature("foo", function() {});
t.ok(control instanceof OpenLayers.Control.DrawFeature,
"constructor returns an instance");
t.ok(OpenLayers.Util.indexOf(control.EVENT_TYPES, "featureadded") > -1,
"featureadded event in EVENT_TYPES");
}
function test_drawFeature(t) {
t.plan(2);
var layer = new OpenLayers.Layer.Vector();
var control = new OpenLayers.Control.DrawFeature(layer, function() {});
var geom = {};
layer.addFeatures = function(features) {
t.ok(features[0].geometry == geom, "layer.addFeatures called");
};
function test_featureadded(event) {
t.ok(event.feature.geometry == geom, "featureadded triggered");
}
control.events.on({"featureadded": test_featureadded});
control.drawFeature(geom);
control.events.un({"featureadded": test_featureadded});
}
</script>
</head>
<body>
<div id="map" style="width: 400px; height: 250px;"/>
</body>
</html>

View File

@@ -90,6 +90,7 @@
<li>Control/Button.html</li>
<li>Control/DragFeature.html</li>
<li>Control/DragPan.html</li>
<li>Control/DrawFeature.html</li>
<li>Control/LayerSwitcher.html</li>
<li>Control/ModifyFeature.html</li>
<li>Control/MousePosition.html</li>