Adding a persist option to the measure control. This passes the same to the sketch handler. The cancel method on the control calls the same on the handler. Patch from dwins. Tests from me. r=me (closes #2029)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@9225 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -62,18 +62,21 @@
|
||||
]);
|
||||
var styleMap = new OpenLayers.StyleMap({"default": style});
|
||||
|
||||
var options = {
|
||||
handlerOptions: {
|
||||
layerOptions: {styleMap: styleMap},
|
||||
persist: true
|
||||
}
|
||||
};
|
||||
measureControls = {
|
||||
line: new OpenLayers.Control.Measure(
|
||||
OpenLayers.Handler.Path, options
|
||||
),
|
||||
OpenLayers.Handler.Path, {
|
||||
persist: true,
|
||||
handlerOptions: {
|
||||
layerOptions: {styleMap: styleMap},
|
||||
}
|
||||
}),
|
||||
polygon: new OpenLayers.Control.Measure(
|
||||
OpenLayers.Handler.Polygon, options
|
||||
OpenLayers.Handler.Polygon, {
|
||||
persist: true,
|
||||
handlerOptions: {
|
||||
layerOptions: {styleMap: styleMap}
|
||||
}
|
||||
}
|
||||
)
|
||||
};
|
||||
|
||||
|
||||
@@ -68,6 +68,15 @@ OpenLayers.Control.Measure = OpenLayers.Class(OpenLayers.Control, {
|
||||
metric: ['km', 'm']
|
||||
},
|
||||
|
||||
/**
|
||||
* APIProperty: persist
|
||||
* {Boolean} Keep the temporary measurement sketch drawn after the
|
||||
* measurement is complete. The geometry will persist until a new
|
||||
* measurement is started, the control is deactivated, or <cancel> is
|
||||
* called.
|
||||
*/
|
||||
persist: false,
|
||||
|
||||
/**
|
||||
* Constructor: OpenLayers.Control.Measure
|
||||
*
|
||||
@@ -86,9 +95,24 @@ OpenLayers.Control.Measure = OpenLayers.Class(OpenLayers.Control, {
|
||||
{done: this.measureComplete, point: this.measurePartial},
|
||||
this.callbacks
|
||||
);
|
||||
|
||||
// let the handler options override, so old code that passes 'persist'
|
||||
// directly to the handler does not need an update
|
||||
this.handlerOptions = OpenLayers.Util.extend(
|
||||
{persist: this.persist}, this.handlerOptions
|
||||
);
|
||||
this.handler = new handler(this, this.callbacks, this.handlerOptions);
|
||||
},
|
||||
|
||||
/**
|
||||
* APIMethod: cancel
|
||||
* Stop the control from measuring. If <persist> is true, the temporary
|
||||
* sketch will be erased.
|
||||
*/
|
||||
cancel: function() {
|
||||
this.handler.cancel();
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: updateHandler
|
||||
*
|
||||
|
||||
@@ -185,8 +185,8 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
|
||||
* {<OpenLayers.Geometry.LineString>}
|
||||
*/
|
||||
getGeometry: function() {
|
||||
var geometry = this.line.geometry;
|
||||
if(this.multi) {
|
||||
var geometry = this.line && this.line.geometry;
|
||||
if(geometry && this.multi) {
|
||||
geometry = new OpenLayers.Geometry.MultiLineString([geometry]);
|
||||
}
|
||||
return geometry;
|
||||
|
||||
@@ -269,8 +269,8 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
|
||||
* {<OpenLayers.Geometry.Point>}
|
||||
*/
|
||||
getGeometry: function() {
|
||||
var geometry = this.point.geometry;
|
||||
if(this.multi) {
|
||||
var geometry = this.point && this.point.geometry;
|
||||
if(geometry && this.multi) {
|
||||
geometry = new OpenLayers.Geometry.MultiPoint([geometry]);
|
||||
}
|
||||
return geometry;
|
||||
@@ -284,7 +284,8 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
|
||||
* {<OpenLayers.Geometry>}
|
||||
*/
|
||||
geometryClone: function() {
|
||||
return this.getGeometry().clone();
|
||||
var geom = this.getGeometry();
|
||||
return geom && geom.clone();
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
@@ -109,8 +109,8 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
|
||||
* {<OpenLayers.Geometry.Polygon>}
|
||||
*/
|
||||
getGeometry: function() {
|
||||
var geometry = this.polygon.geometry;
|
||||
if(this.multi) {
|
||||
var geometry = this.polygon && this.polygon.geometry;
|
||||
if(geometry && this.multi) {
|
||||
geometry = new OpenLayers.Geometry.MultiPolygon([geometry]);
|
||||
}
|
||||
return geometry;
|
||||
|
||||
75
tests/Control/Measure.html
Normal file
75
tests/Control/Measure.html
Normal file
@@ -0,0 +1,75 @@
|
||||
<html>
|
||||
<head>
|
||||
<script src="../../lib/OpenLayers.js"></script>
|
||||
<script type="text/javascript">
|
||||
|
||||
function test_initialze(t) {
|
||||
|
||||
t.plan(1);
|
||||
|
||||
var map = new OpenLayers.Map("map");
|
||||
var control = new OpenLayers.Control.Measure(
|
||||
OpenLayers.Handler.Path, {persist: true}
|
||||
);
|
||||
map.addControl(control);
|
||||
|
||||
t.eq(control.persist, true, "passing persist to constructor sets persist on handler");
|
||||
|
||||
map.destroy();
|
||||
|
||||
}
|
||||
|
||||
function test_cancel(t) {
|
||||
|
||||
t.plan(4);
|
||||
|
||||
var map = new OpenLayers.Map("map");
|
||||
var layer = new OpenLayers.Layer(null, {
|
||||
isBaseLayer: true
|
||||
});
|
||||
map.addLayer(layer);
|
||||
map.zoomToMaxExtent();
|
||||
|
||||
var control = new OpenLayers.Control.Measure(
|
||||
OpenLayers.Handler.Path, {persist: true}
|
||||
);
|
||||
map.addControl(control);
|
||||
|
||||
control.activate();
|
||||
|
||||
try {
|
||||
control.cancel();
|
||||
t.ok(true, "calling cancel before drawing works");
|
||||
} catch(err) {
|
||||
t.fail("calling cancel before drawing causes trouble: " + err);
|
||||
}
|
||||
t.eq(control.active, true, "control remains active after cancel");
|
||||
|
||||
// create a simple measurement
|
||||
function trigger(type, x, y) {
|
||||
map.events.triggerEvent(type, {
|
||||
xy: new OpenLayers.Pixel(x, y)
|
||||
})
|
||||
};
|
||||
trigger("mousedown", 0, 0);
|
||||
trigger("mouseup", 0, 0);
|
||||
trigger("mousemove", 10, 10);
|
||||
trigger("mousedown", 10, 10);
|
||||
trigger("mouseup", 10, 10);
|
||||
|
||||
// confirm that the sketch persists
|
||||
t.eq(control.handler.layer.features.length, 1, "feature persists");
|
||||
// cancel and see that sketch is gone
|
||||
control.cancel();
|
||||
t.eq(control.handler.layer.features.length, 0, "feature is gone after cancel");
|
||||
|
||||
map.destroy();
|
||||
|
||||
}
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="map" style="width: 512px; height: 256px;"></div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -17,6 +17,7 @@
|
||||
<li>Control/GetFeature.html</li>
|
||||
<li>Control/KeyboardDefaults.html</li>
|
||||
<li>Control/LayerSwitcher.html</li>
|
||||
<li>Control/Measure.html</li>
|
||||
<li>Control/ModifyFeature.html</li>
|
||||
<li>Control/MousePosition.html</li>
|
||||
<li>Control/MouseToolbar.html</li>
|
||||
|
||||
Reference in New Issue
Block a user