From 8fdd4231045c437c0847a20082a6df3519a7dd46 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Mon, 27 Jul 2009 18:00:36 +0000 Subject: [PATCH] provide a standalone mode for the ModifyFeature control. Thanks tschaub for the fix in deactivate() and the tests. r=tschaub (closes #2199) git-svn-id: http://svn.openlayers.org/trunk/openlayers@9591 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Control/ModifyFeature.js | 53 ++++++++++++++------ tests/Control/ModifyFeature.html | 64 +++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 15 deletions(-) diff --git a/lib/OpenLayers/Control/ModifyFeature.js b/lib/OpenLayers/Control/ModifyFeature.js index cccccf0bb0..30eb7b5d16 100644 --- a/lib/OpenLayers/Control/ModifyFeature.js +++ b/lib/OpenLayers/Control/ModifyFeature.js @@ -43,6 +43,17 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { * Default is true. */ toggle: true, + + /** + * APIProperty: standalone + * {Boolean} Set to true to create a control without SelectFeature + * capabilities. Default is false. If standalone is true, to modify + * a feature, call the method with the target feature. + * Note that you must call the method to finish + * feature modification in standalone mode (before starting to modify + * another feature). + */ + standalone: false, /** * Property: layer @@ -207,9 +218,11 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { onUnselect: this.unselectFeature, scope: this }; - this.selectControl = new OpenLayers.Control.SelectFeature( - layer, selectOptions - ); + if(this.standalone === false) { + this.selectControl = new OpenLayers.Control.SelectFeature( + layer, selectOptions + ); + } // configure the drag control var dragOptions = { @@ -244,7 +257,7 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { */ destroy: function() { this.layer = null; - this.selectControl.destroy(); + this.standalone || this.selectControl.destroy(); this.dragControl.destroy(); OpenLayers.Control.prototype.destroy.apply(this, []); }, @@ -257,7 +270,7 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { * {Boolean} Successfully activated the control. */ activate: function() { - return (this.selectControl.activate() && + return ((this.standalone || this.selectControl.activate()) && this.handlers.keyboard.activate() && OpenLayers.Control.prototype.activate.apply(this, arguments)); }, @@ -277,11 +290,19 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { this.layer.removeFeatures(this.virtualVertices, {silent: true}); this.vertices = []; this.dragControl.deactivate(); - if(this.feature && this.feature.geometry && this.feature.layer) { - this.selectControl.unselect.apply(this.selectControl, - [this.feature]); + var feature = this.feature; + var valid = feature && feature.geometry && feature.layer; + if(this.standalone === false) { + if(valid) { + this.selectControl.unselect.apply(this.selectControl, + [feature]); + } + this.selectControl.deactivate(); + } else { + if(valid) { + this.unselectFeature(feature); + } } - this.selectControl.deactivate(); this.handlers.keyboard.deactivate(); deactivated = true; } @@ -364,7 +385,7 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { // only change behavior if the feature is not in the vertices array if(feature != this.feature && !feature.geometry.parent && feature != this.dragHandle && feature != this.radiusHandle) { - if(this.feature) { + if(this.standalone === false && this.feature) { // unselect the currently selected feature this.selectControl.clickFeature.apply(this.selectControl, [this.feature]); @@ -374,8 +395,8 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { OpenLayers.Util.indexOf(this.geometryTypes, feature.geometry.CLASS_NAME) != -1) { // select the point - this.selectControl.clickFeature.apply(this.selectControl, - [feature]); + this.standalone || this.selectControl.clickFeature.apply( + this.selectControl, [feature]); /** * TBD: These lines improve workflow by letting the user * immediately start dragging after the mouse down. @@ -452,7 +473,8 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { this.layer.destroyFeatures(this.virtualVertices, {silent: true}); this.virtualVertices = []; } - this.layer.drawFeature(this.feature, this.selectControl.renderIntent); + this.layer.drawFeature(this.feature, this.standalone ? undefined : + this.selectControl.renderIntent); } // keep the vertex on top so it gets the mouseout after dragging // this should be removed in favor of an option to draw under or @@ -558,7 +580,8 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { vertex.geometry.parent) { // remove the vertex vertex.geometry.parent.removeComponent(vertex.geometry); - this.layer.drawFeature(this.feature, + this.layer.drawFeature(this.feature, this.standalone ? + undefined : this.selectControl.renderIntent); this.resetVertices(); this.setFeatureState(); @@ -708,7 +731,7 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { * map - {} The control's map. */ setMap: function(map) { - this.selectControl.setMap(map); + this.standalone || this.selectControl.setMap(map); this.dragControl.setMap(map); OpenLayers.Control.prototype.setMap.apply(this, arguments); }, diff --git a/tests/Control/ModifyFeature.html b/tests/Control/ModifyFeature.html index af4a5e2c00..8d04fd32ce 100644 --- a/tests/Control/ModifyFeature.html +++ b/tests/Control/ModifyFeature.html @@ -609,6 +609,70 @@ map.destroy(); } + + function test_standalone(t) { + + t.plan(13); + var map = new OpenLayers.Map("map"); + var layer = new OpenLayers.Layer.Vector(); + + var f1 = new OpenLayers.Feature.Vector( + OpenLayers.Geometry.fromWKT("LINESTRING(3 4,10 50,20 25)") + ); + var f2 = new OpenLayers.Feature.Vector( + OpenLayers.Geometry.fromWKT("POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2, 3 2, 3 3, 2 3,2 2))") + ); + layer.addFeatures([f1, f2]); + + map.addLayer(layer); + var control = new OpenLayers.Control.ModifyFeature(layer, {standalone: true}); + map.addControl(control); + + var log = []; + layer.events.on({ + beforefeaturemodified: function(evt) { + log.push(evt); + }, + featuremodified: function(evt) { + log.push(evt); + }, + afterfeaturemodified: function(evt) { + log.push(evt); + } + }); + + // activate control + control.activate(); + t.eq(control.active, true, "[activate] control activated"); + t.eq(control.selectControl, null, "[activate] no select control"); + + // manually select feature for editing + control.selectFeature(f1); + t.ok(control.feature === f1, "[select f1] control.feature set to f1"); + + // manually unselect feature for editing + control.unselectFeature(f1); + t.eq(control.feature, null, "[unselect f1] control.feature set to null"); + t.eq(log.length, 1, "[unselect f1] event logged"); + t.eq(log[0].type, "afterfeaturemodified", "[unselect f1] afterfeaturemodified triggered"); + t.ok(log[0].feature === f1, "[unselect f1] correct feature"); + t.eq(log[0].modified, false, "[unselect f1] feature not actually modified"); + + // clear log and select new feature for editing + log = []; + control.selectFeature(f2); + t.ok(control.feature === f2, "[select f2] control.feature set to f2"); + + // deactivate control and confirm feature is unselected + control.deactivate(); + t.eq(log.length, 1, "[deactivate] event logged"); + t.eq(log[0].type, "afterfeaturemodified", "[deactivate] afterfeaturemodified triggered"); + t.ok(log[0].feature === f2, "[deactivate] correct feature"); + t.eq(log[0].modified, false, "[deactivate] feature not actually modified"); + + map.destroy(); + + }