From 33a8466913805f4235f1f4dc47cf5b136f9684e2 Mon Sep 17 00:00:00 2001 From: Thomas Chandelle Date: Tue, 21 Feb 2017 14:34:42 +0100 Subject: [PATCH] Add API method abortDrawing and dispatch a DRAWABORT event --- src/ol/interaction/Draw.js | 27 ++++++-- test/spec/ol/interaction/draw.test.js | 96 +++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 5 deletions(-) diff --git a/src/ol/interaction/Draw.js b/src/ol/interaction/Draw.js index 5848ab2f37..9b43feae21 100644 --- a/src/ol/interaction/Draw.js +++ b/src/ol/interaction/Draw.js @@ -143,7 +143,13 @@ const DrawEventType = { * @event DrawEvent#drawend * @api */ - DRAWEND: 'drawend' + DRAWEND: 'drawend', + /** + * Triggered upon feature draw abortion + * @event DrawEvent#drawabort + * @api + */ + DRAWABORT: 'drawabort' }; @@ -584,8 +590,7 @@ class Draw extends PointerInteraction { } pass = false; } else if (this.freehand_) { - this.finishCoordinate_ = null; - this.abortDrawing_(); + this.abortDrawing(); } if (!pass && this.stopClick_) { event.stopPropagation(); @@ -834,7 +839,7 @@ class Draw extends PointerInteraction { } if (coordinates.length === 0) { - this.finishCoordinate_ = null; + this.abortDrawing(); } this.updateSketchFeatures_(); @@ -901,6 +906,18 @@ class Draw extends PointerInteraction { return sketchFeature; } + /** + * Stop drawing without adding the sketch feature to the target layer. + * @api + */ + abortDrawing() { + const sketchFeature = this.abortDrawing_(); + if (sketchFeature) { + this.dispatchEvent(new DrawEvent(DrawEventType.DRAWABORT, sketchFeature)); + } + } + + /** * Append coordinates to the end of the geometry that is currently being drawn. * This can be used when drawing LineStrings or Polygons. Coordinates will @@ -982,7 +999,7 @@ class Draw extends PointerInteraction { const map = this.getMap(); const active = this.getActive(); if (!map || !active) { - this.abortDrawing_(); + this.abortDrawing(); } this.overlay_.setMap(active ? map : null); } diff --git a/test/spec/ol/interaction/draw.test.js b/test/spec/ol/interaction/draw.test.js index 7ad50aea49..30a42c3a23 100644 --- a/test/spec/ol/interaction/draw.test.js +++ b/test/spec/ol/interaction/draw.test.js @@ -215,16 +215,20 @@ describe('ol.interaction.Draw', function() { it('triggers draw events', function() { const ds = sinon.spy(); const de = sinon.spy(); + const da = sinon.spy(); listen(draw, 'drawstart', ds); listen(draw, 'drawend', de); + listen(draw, 'drawabort', da); simulateEvent('pointermove', 10, 20); simulateEvent('pointerdown', 10, 20); simulateEvent('pointerup', 10, 20); expect(ds.called).to.be(true); expect(de.called).to.be(true); + expect(da.called).to.be(false); simulateEvent('pointermove', 20, 20); expect(ds.callCount).to.be(1); expect(de.callCount).to.be(1); + expect(da.callCount).to.be(0); }); it('triggers drawend event before inserting the feature', function() { @@ -463,8 +467,10 @@ describe('ol.interaction.Draw', function() { it('triggers draw events', function() { const ds = sinon.spy(); const de = sinon.spy(); + const da = sinon.spy(); listen(draw, 'drawstart', ds); listen(draw, 'drawend', de); + listen(draw, 'drawabort', da); // first point simulateEvent('pointermove', 10, 20); @@ -485,6 +491,8 @@ describe('ol.interaction.Draw', function() { expect(ds.callCount).to.be(1); expect(de.called).to.be(true); expect(de.callCount).to.be(1); + expect(da.called).to.be(false); + expect(da.callCount).to.be(0); }); it('works if finishDrawing is called when the sketch feature is not defined', function() { @@ -781,8 +789,10 @@ describe('ol.interaction.Draw', function() { it('triggers draw events', function() { const ds = sinon.spy(); const de = sinon.spy(); + const da = sinon.spy(); listen(draw, 'drawstart', ds); listen(draw, 'drawend', de); + listen(draw, 'drawabort', da); // first point simulateEvent('pointermove', 10, 20); @@ -808,6 +818,8 @@ describe('ol.interaction.Draw', function() { expect(ds.callCount).to.be(1); expect(de.called).to.be(true); expect(de.callCount).to.be(1); + expect(da.called).to.be(false); + expect(da.callCount).to.be(0); }); it('works if finishDrawing is called when the sketch feature is not defined', function() { @@ -1020,8 +1032,10 @@ describe('ol.interaction.Draw', function() { it('triggers draw events', function() { const ds = sinon.spy(); const de = sinon.spy(); + const da = sinon.spy(); listen(draw, 'drawstart', ds); listen(draw, 'drawend', de); + listen(draw, 'drawabort', da); // first point simulateEvent('pointermove', 10, 20); @@ -1037,6 +1051,88 @@ describe('ol.interaction.Draw', function() { expect(ds.callCount).to.be(1); expect(de.called).to.be(true); expect(de.callCount).to.be(1); + expect(da.called).to.be(false); + expect(da.callCount).to.be(0); + }); + + }); + + describe('#abortDrawing()', function() { + let draw; + + beforeEach(function() { + draw = new Draw({ + source: source, + type: 'LineString' + }); + map.addInteraction(draw); + }); + + it('aborts the current drawing', function() { + // first point + simulateEvent('pointermove', 10, 20); + simulateEvent('pointerdown', 10, 20); + simulateEvent('pointerup', 10, 20); + + // second point + simulateEvent('pointermove', 30, 20); + simulateEvent('pointerdown', 30, 20); + simulateEvent('pointerup', 30, 20); + + draw.abortDrawing(); + + expect(source.getFeatures()).to.have.length(0); + expect(draw.sketchFeature_).to.be(null); + }); + + it('triggers draw events', function() { + const ds = sinon.spy(); + const de = sinon.spy(); + const da = sinon.spy(); + listen(draw, 'drawstart', ds); + listen(draw, 'drawend', de); + listen(draw, 'drawabort', da); + + // first point + simulateEvent('pointermove', 10, 20); + simulateEvent('pointerdown', 10, 20); + simulateEvent('pointerup', 10, 20); + + // second point + simulateEvent('pointermove', 30, 20); + simulateEvent('pointerdown', 30, 20); + simulateEvent('pointerup', 30, 20); + + draw.abortDrawing(); + + expect(ds.called).to.be(true); + expect(ds.callCount).to.be(1); + expect(de.called).to.be(false); + expect(de.callCount).to.be(0); + expect(da.called).to.be(true); + expect(da.callCount).to.be(1); + + + // first point + simulateEvent('pointermove', 10, 20); + simulateEvent('pointerdown', 10, 20); + simulateEvent('pointerup', 10, 20); + + // second point + simulateEvent('pointermove', 30, 20); + simulateEvent('pointerdown', 30, 20); + simulateEvent('pointerup', 30, 20); + + draw.removeLastPoint(); + draw.removeLastPoint(); + draw.removeLastPoint(); + + expect(ds.called).to.be(true); + expect(ds.callCount).to.be(2); + expect(de.called).to.be(false); + expect(de.callCount).to.be(0); + expect(da.called).to.be(true); + expect(da.callCount).to.be(2); }); it('works if finishDrawing is called when the sketch feature is not defined', function() {