From ed5420b67877dc063b0b214da31a4598cb81f32f Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Tue, 7 Apr 2009 02:03:57 +0000 Subject: [PATCH] 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 --- examples/measure.html | 21 +++++---- lib/OpenLayers/Control/Measure.js | 24 ++++++++++ lib/OpenLayers/Handler/Path.js | 4 +- lib/OpenLayers/Handler/Point.js | 7 +-- lib/OpenLayers/Handler/Polygon.js | 4 +- tests/Control/Measure.html | 75 +++++++++++++++++++++++++++++++ tests/list-tests.html | 1 + 7 files changed, 120 insertions(+), 16 deletions(-) create mode 100644 tests/Control/Measure.html diff --git a/examples/measure.html b/examples/measure.html index b309d84b3a..9a4285f5ee 100644 --- a/examples/measure.html +++ b/examples/measure.html @@ -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} + } + } ) }; diff --git a/lib/OpenLayers/Control/Measure.js b/lib/OpenLayers/Control/Measure.js index fee9c9f3b5..88b712be7d 100644 --- a/lib/OpenLayers/Control/Measure.js +++ b/lib/OpenLayers/Control/Measure.js @@ -67,6 +67,15 @@ OpenLayers.Control.Measure = OpenLayers.Class(OpenLayers.Control, { english: ['mi', 'ft', 'in'], 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 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 is true, the temporary + * sketch will be erased. + */ + cancel: function() { + this.handler.cancel(); + }, + /** * Method: updateHandler * diff --git a/lib/OpenLayers/Handler/Path.js b/lib/OpenLayers/Handler/Path.js index 6b080b4ee2..e610b752f9 100644 --- a/lib/OpenLayers/Handler/Path.js +++ b/lib/OpenLayers/Handler/Path.js @@ -185,8 +185,8 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, { * {} */ 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; diff --git a/lib/OpenLayers/Handler/Point.js b/lib/OpenLayers/Handler/Point.js index c70124abce..2d41a15d31 100644 --- a/lib/OpenLayers/Handler/Point.js +++ b/lib/OpenLayers/Handler/Point.js @@ -269,8 +269,8 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, { * {} */ 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, { * {} */ geometryClone: function() { - return this.getGeometry().clone(); + var geom = this.getGeometry(); + return geom && geom.clone(); }, /** diff --git a/lib/OpenLayers/Handler/Polygon.js b/lib/OpenLayers/Handler/Polygon.js index 468622e240..5e4db7746c 100644 --- a/lib/OpenLayers/Handler/Polygon.js +++ b/lib/OpenLayers/Handler/Polygon.js @@ -109,8 +109,8 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, { * {} */ 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; diff --git a/tests/Control/Measure.html b/tests/Control/Measure.html new file mode 100644 index 0000000000..279269ae72 --- /dev/null +++ b/tests/Control/Measure.html @@ -0,0 +1,75 @@ + + + + + + +
+ + diff --git a/tests/list-tests.html b/tests/list-tests.html index c2aa182a01..88b3ed79b0 100644 --- a/tests/list-tests.html +++ b/tests/list-tests.html @@ -17,6 +17,7 @@
  • Control/GetFeature.html
  • Control/KeyboardDefaults.html
  • Control/LayerSwitcher.html
  • +
  • Control/Measure.html
  • Control/ModifyFeature.html
  • Control/MousePosition.html
  • Control/MouseToolbar.html