| 1 | | ## http://trac.openlayers.org/attachment/ticket/1882/ModifyFeature-delete.diff |
| 2 | | OpenLayers.Control.ModifyFeature.prototype.handleKeypress = function(evt) { |
| 3 | | var code = evt.keyCode; |
| 4 | | |
| 5 | | // check for delete key |
| 6 | | if(this.feature && |
| 7 | | !this.dragControl.handlers.drag.dragging && |
| 8 | | OpenLayers.Util.indexOf(this.deleteCodes, code) != -1) { |
| 9 | | var vertex = this.dragControl.feature; |
| 10 | | if(vertex && |
| 11 | | OpenLayers.Util.indexOf(this.vertices, vertex) != -1 && |
| 12 | | vertex.geometry.parent) { |
| 13 | | // remove the vertex |
| 14 | | vertex.geometry.parent.removeComponent(vertex.geometry); |
| 15 | | this.layer.drawFeature(this.feature, this.standalone ? |
| 16 | | undefined : |
| 17 | | this.selectControl.renderIntent); |
| 18 | | this.resetVertices(); |
| 19 | | this.setFeatureState(); |
| 20 | | this.onModification(this.feature); |
| 21 | | this.layer.events.triggerEvent("featuremodified", |
| 22 | | {feature: this.feature}); |
| 23 | | } else { |
| 24 | | // if not pointing to a vertex, remove the whole feature |
| 25 | | var feature = this.feature; |
| 26 | | |
| 27 | | var continueRemoving = this.layer.events.triggerEvent("beforefeatureremoved", |
| 28 | | {feature: feature}); |
| 29 | | if (continueRemoving === false) { |
| 30 | | return; |
| 31 | | } |
| 32 | | |
| 33 | | this.layer.removeFeatures([feature], {silent: true}); |
| 34 | | feature.state = OpenLayers.State.DELETE; |
| 35 | | this.layer.events.triggerEvent("featureremoved", |
| 36 | | {feature: feature}); |
| 37 | | this.unselectFeature(feature); |
| 38 | | } |
| | 1 | /** |
| | 2 | * Class: DeleteFeatureControl |
| | 3 | * Control to delete features. It is supposed to be used together |
| | 4 | * with a <OpenLayers.Control.ModifyFeature> control, which |
| | 5 | * must be set in the constructor. |
| | 6 | * The control is activated when a feature is selected with |
| | 7 | * the <OpenLayers.Control.ModifyFeature> control. Then |
| | 8 | * a click on the DeleteFeature control deletes the feature. |
| | 9 | * |
| | 10 | * Inherits From: |
| | 11 | * - <OpenLayers.Control> |
| | 12 | */ |
| | 13 | DeleteFeatureControl = OpenLayers.Class(OpenLayers.Control, { |
| | 14 | |
| | 15 | /** |
| | 16 | * Property: type |
| | 17 | * {String} The type of <OpenLayers.Control> -- When added to a |
| | 18 | * <Control.Panel>, 'type' is used by the panel to determine how to |
| | 19 | * handle our events. |
| | 20 | */ |
| | 21 | type: OpenLayers.Control.TYPE_BUTTON, |
| | 22 | |
| | 23 | /** |
| | 24 | * Constructor: DeleteFeatureControl |
| | 25 | * Creates a new delete feature control. |
| | 26 | * |
| | 27 | * Parameters: |
| | 28 | * modifyFeatureControl - {<OpenLayers.Control.ModifyFeature>} ModifyFeature control that |
| | 29 | * is used to select features. |
| | 30 | * options - {Object} Optional object whose properties will be set on the |
| | 31 | * control. |
| | 32 | */ |
| | 33 | initialize: function (modifyFeatureControl, options) { |
| | 34 | this.modifyFeatureControl = modifyFeatureControl; |
| | 35 | |
| | 36 | modifyFeatureControl.layer.events.register("beforefeaturemodified", |
| | 37 | this, this.handleFeatureSelected); |
| | 38 | modifyFeatureControl.layer.events.register("afterfeaturemodified", |
| | 39 | this, this.handleFeatureUnSelected); |
| | 40 | |
| | 41 | OpenLayers.Control.prototype.initialize.apply(this, [options]); |
| | 42 | }, |
| | 43 | |
| | 44 | /** |
| | 45 | * Method: handleFeatureSelected |
| | 46 | * Called before a feature is selected with the ModifyFeature control. Activates |
| | 47 | * the control by changing the 'displayClass' of the DIV. |
| | 48 | */ |
| | 49 | handleFeatureSelected : function () { |
| | 50 | this.panel_div.className = this.displayClass + 'ItemActive'; |
| | 51 | }, |
| | 52 | |
| | 53 | /** |
| | 54 | * Method: handleFeatureUnSelected |
| | 55 | * Called when a feature is unselected with the ModifyFeature control. Deactivates |
| | 56 | * the control by changing the 'displayClass' of the DIV. |
| | 57 | */ |
| | 58 | handleFeatureUnSelected : function() { |
| | 59 | this.panel_div.className = this.displayClass + 'ItemInactive'; |
| | 60 | }, |
| | 61 | |
| | 62 | /** |
| | 63 | * Method: trigger |
| | 64 | * Called when the control is clicked. Deletes the selected |
| | 65 | * feature. |
| | 66 | */ |
| | 67 | trigger: function () { |
| | 68 | if (this.modifyFeatureControl.feature) { |
| | 69 | var layer = this.modifyFeatureControl.layer; |
| | 70 | var feature = this.modifyFeatureControl.feature; |
| | 71 | |
| | 72 | var continueRemoving = layer.events.triggerEvent("beforefeatureremoved", |
| | 73 | {feature: feature}); |
| | 74 | |
| | 75 | if (continueRemoving === false) { |
| | 76 | return; |
| | 77 | } |
| | 78 | |
| | 79 | layer.removeFeatures([feature], {silent: true}); |
| | 80 | feature.state = OpenLayers.State.DELETE; |
| | 81 | layer.events.triggerEvent("featureremoved", |
| | 82 | {feature: feature}); |
| | 83 | this.modifyFeatureControl.unselectFeature(feature); |