Add API method abortDrawing and dispatch a DRAWABORT event

This commit is contained in:
Thomas Chandelle
2017-02-21 14:34:42 +01:00
committed by Frederic Junod
parent 8750cb0b1a
commit 33a8466913
2 changed files with 118 additions and 5 deletions

View File

@@ -143,7 +143,13 @@ const DrawEventType = {
* @event DrawEvent#drawend * @event DrawEvent#drawend
* @api * @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; pass = false;
} else if (this.freehand_) { } else if (this.freehand_) {
this.finishCoordinate_ = null; this.abortDrawing();
this.abortDrawing_();
} }
if (!pass && this.stopClick_) { if (!pass && this.stopClick_) {
event.stopPropagation(); event.stopPropagation();
@@ -834,7 +839,7 @@ class Draw extends PointerInteraction {
} }
if (coordinates.length === 0) { if (coordinates.length === 0) {
this.finishCoordinate_ = null; this.abortDrawing();
} }
this.updateSketchFeatures_(); this.updateSketchFeatures_();
@@ -901,6 +906,18 @@ class Draw extends PointerInteraction {
return sketchFeature; 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. * Append coordinates to the end of the geometry that is currently being drawn.
* This can be used when drawing LineStrings or Polygons. Coordinates will * This can be used when drawing LineStrings or Polygons. Coordinates will
@@ -982,7 +999,7 @@ class Draw extends PointerInteraction {
const map = this.getMap(); const map = this.getMap();
const active = this.getActive(); const active = this.getActive();
if (!map || !active) { if (!map || !active) {
this.abortDrawing_(); this.abortDrawing();
} }
this.overlay_.setMap(active ? map : null); this.overlay_.setMap(active ? map : null);
} }

View File

@@ -215,16 +215,20 @@ describe('ol.interaction.Draw', function() {
it('triggers draw events', function() { it('triggers draw events', function() {
const ds = sinon.spy(); const ds = sinon.spy();
const de = sinon.spy(); const de = sinon.spy();
const da = sinon.spy();
listen(draw, 'drawstart', ds); listen(draw, 'drawstart', ds);
listen(draw, 'drawend', de); listen(draw, 'drawend', de);
listen(draw, 'drawabort', da);
simulateEvent('pointermove', 10, 20); simulateEvent('pointermove', 10, 20);
simulateEvent('pointerdown', 10, 20); simulateEvent('pointerdown', 10, 20);
simulateEvent('pointerup', 10, 20); simulateEvent('pointerup', 10, 20);
expect(ds.called).to.be(true); expect(ds.called).to.be(true);
expect(de.called).to.be(true); expect(de.called).to.be(true);
expect(da.called).to.be(false);
simulateEvent('pointermove', 20, 20); simulateEvent('pointermove', 20, 20);
expect(ds.callCount).to.be(1); expect(ds.callCount).to.be(1);
expect(de.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() { it('triggers drawend event before inserting the feature', function() {
@@ -463,8 +467,10 @@ describe('ol.interaction.Draw', function() {
it('triggers draw events', function() { it('triggers draw events', function() {
const ds = sinon.spy(); const ds = sinon.spy();
const de = sinon.spy(); const de = sinon.spy();
const da = sinon.spy();
listen(draw, 'drawstart', ds); listen(draw, 'drawstart', ds);
listen(draw, 'drawend', de); listen(draw, 'drawend', de);
listen(draw, 'drawabort', da);
// first point // first point
simulateEvent('pointermove', 10, 20); simulateEvent('pointermove', 10, 20);
@@ -485,6 +491,8 @@ describe('ol.interaction.Draw', function() {
expect(ds.callCount).to.be(1); expect(ds.callCount).to.be(1);
expect(de.called).to.be(true); expect(de.called).to.be(true);
expect(de.callCount).to.be(1); 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() { 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() { it('triggers draw events', function() {
const ds = sinon.spy(); const ds = sinon.spy();
const de = sinon.spy(); const de = sinon.spy();
const da = sinon.spy();
listen(draw, 'drawstart', ds); listen(draw, 'drawstart', ds);
listen(draw, 'drawend', de); listen(draw, 'drawend', de);
listen(draw, 'drawabort', da);
// first point // first point
simulateEvent('pointermove', 10, 20); simulateEvent('pointermove', 10, 20);
@@ -808,6 +818,8 @@ describe('ol.interaction.Draw', function() {
expect(ds.callCount).to.be(1); expect(ds.callCount).to.be(1);
expect(de.called).to.be(true); expect(de.called).to.be(true);
expect(de.callCount).to.be(1); 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() { 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() { it('triggers draw events', function() {
const ds = sinon.spy(); const ds = sinon.spy();
const de = sinon.spy(); const de = sinon.spy();
const da = sinon.spy();
listen(draw, 'drawstart', ds); listen(draw, 'drawstart', ds);
listen(draw, 'drawend', de); listen(draw, 'drawend', de);
listen(draw, 'drawabort', da);
// first point // first point
simulateEvent('pointermove', 10, 20); simulateEvent('pointermove', 10, 20);
@@ -1037,6 +1051,88 @@ describe('ol.interaction.Draw', function() {
expect(ds.callCount).to.be(1); expect(ds.callCount).to.be(1);
expect(de.called).to.be(true); expect(de.called).to.be(true);
expect(de.callCount).to.be(1); 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() { it('works if finishDrawing is called when the sketch feature is not defined', function() {