From d49bcdeedc50240f58dafa8ce986932bb8cd521e Mon Sep 17 00:00:00 2001 From: giohappy Date: Thu, 28 Apr 2016 12:47:46 +0200 Subject: [PATCH] Expose removeVertex_ as removePoint --- src/ol/interaction/modifyinteraction.js | 30 ++++++++++++--- .../ol/interaction/modifyinteraction.test.js | 37 +++++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/ol/interaction/modifyinteraction.js b/src/ol/interaction/modifyinteraction.js index 317ed58150..e8fef033ca 100644 --- a/src/ol/interaction/modifyinteraction.js +++ b/src/ol/interaction/modifyinteraction.js @@ -240,6 +240,12 @@ ol.interaction.Modify = function(options) { ol.events.listen(this.features_, ol.CollectionEventType.REMOVE, this.handleFeatureRemove_, this); + /** + * @type {ol.MapBrowserPointerEvent} + * @private + */ + this.lastPointerEvent_ = null; + }; goog.inherits(ol.interaction.Modify, ol.interaction.Pointer); @@ -696,6 +702,7 @@ ol.interaction.Modify.handleEvent = function(mapBrowserEvent) { if (!(mapBrowserEvent instanceof ol.MapBrowserPointerEvent)) { return true; } + this.lastPointerEvent_ = mapBrowserEvent; var handled; if (!mapBrowserEvent.map.getView().getHints()[ol.ViewHint.INTERACTING] && @@ -709,11 +716,7 @@ ol.interaction.Modify.handleEvent = function(mapBrowserEvent) { var geometry = this.vertexFeature_.getGeometry(); goog.asserts.assertInstanceof(geometry, ol.geom.Point, 'geometry should be an ol.geom.Point'); - this.willModifyFeatures_(mapBrowserEvent); - handled = this.removeVertex_(); - this.dispatchEvent(new ol.interaction.ModifyEvent( - ol.ModifyEventType.MODIFYEND, this.features_, mapBrowserEvent)); - this.modified_ = false; + handled = this.removePoint(); } else { handled = true; } @@ -879,6 +882,23 @@ ol.interaction.Modify.prototype.insertVertex_ = function(segmentData, vertex) { this.ignoreNextSingleClick_ = true; }; +/** + * Removes the vertex currently being pointed. + * @return {boolean} True when a vertex was removed. + * @api + */ +ol.interaction.Modify.prototype.removePoint = function() { + var handled = false; + if (this.lastPointerEvent_) { + var evt = this.lastPointerEvent_; + this.willModifyFeatures_(evt); + handled = this.removeVertex_(); + this.dispatchEvent(new ol.interaction.ModifyEvent( + ol.ModifyEventType.MODIFYEND, this.features_, evt)); + this.modified_ = false; + } + return handled; +}; /** * Removes a vertex from all matching features. diff --git a/test/spec/ol/interaction/modifyinteraction.test.js b/test/spec/ol/interaction/modifyinteraction.test.js index af473bc3a3..970e9358af 100644 --- a/test/spec/ol/interaction/modifyinteraction.test.js +++ b/test/spec/ol/interaction/modifyinteraction.test.js @@ -252,6 +252,43 @@ describe('ol.interaction.Modify', function() { validateEvents(events, features); }); + it('deletes vertex of a LineString programmatically', function() { + var lineFeature = new ol.Feature({ + geometry: new ol.geom.LineString( + [[0, 0], [10, 20], [0, 40], [40, 40], [40, 0]] + ) + }); + features.length = 0; + features.push(lineFeature); + features.push(lineFeature.clone()); + + var first = features[0]; + var firstRevision = first.getGeometry().getRevision(); + + var modify = new ol.interaction.Modify({ + features: new ol.Collection(features) + }); + map.addInteraction(modify); + + var events = trackEvents(first, modify); + + expect(first.getGeometry().getRevision()).to.equal(firstRevision); + expect(first.getGeometry().getCoordinates()).to.have.length(5); + + simulateEvent('pointerdown', 40, 0, false, 0); + simulateEvent('pointerup', 40, 0, false, 0); + + modify.removePoint(); + + expect(first.getGeometry().getRevision()).to.equal(firstRevision + 1); + expect(first.getGeometry().getCoordinates()).to.have.length(4); + expect(first.getGeometry().getCoordinates()[3][0]).to.equal(40); + expect(first.getGeometry().getCoordinates()[3][1]).to.equal(40); + + validateEvents(events, features); + }); + + }); describe('boundary modification', function() {