From 130b4d94598caf3b1e9dfd78e416e10ce28d5308 Mon Sep 17 00:00:00 2001 From: giohappy Date: Fri, 22 Apr 2016 11:07:42 +0200 Subject: [PATCH 1/6] add finishCondition to ol.interaction.Draw --- src/ol/interaction/drawinteraction.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/ol/interaction/drawinteraction.js b/src/ol/interaction/drawinteraction.js index 0b38d43c77..c6e5bce257 100644 --- a/src/ol/interaction/drawinteraction.js +++ b/src/ol/interaction/drawinteraction.js @@ -160,6 +160,13 @@ ol.interaction.Draw = function(options) { */ this.maxPoints_ = options.maxPoints ? options.maxPoints : Infinity; + /** + * A function to decide if a potential finish coordinate is permissable + * @private + * @type {ol.events.ConditionType} + */ + this.finishCondition_ = options.finishCondition ? options.finishCondition : goog.functions.TRUE; + var geometryFunction = options.geometryFunction; if (!geometryFunction) { if (this.type_ === ol.geom.GeometryType.CIRCLE) { @@ -404,7 +411,9 @@ ol.interaction.Draw.handleUpEvent_ = function(event) { } else if (this.mode_ === ol.interaction.DrawMode.CIRCLE) { this.finishDrawing(); } else if (this.atFinish_(event)) { - this.finishDrawing(); + if (this.finishCondition_(event)) { + this.finishDrawing(); + } } else { this.addToDrawing_(event); } From 2076ef02ca5c2d2e18c1767f960fbd55fc744615 Mon Sep 17 00:00:00 2001 From: giohappy Date: Fri, 22 Apr 2016 15:07:59 +0200 Subject: [PATCH 2/6] Add test for finishCondition --- .../ol/interaction/drawinteraction.test.js | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/test/spec/ol/interaction/drawinteraction.test.js b/test/spec/ol/interaction/drawinteraction.test.js index 1a491caddc..23df2c6ba6 100644 --- a/test/spec/ol/interaction/drawinteraction.test.js +++ b/test/spec/ol/interaction/drawinteraction.test.js @@ -332,6 +332,54 @@ describe('ol.interaction.Draw', function() { }); }); + + describe('drawing with a finishCondition', function() { + beforeEach(function() { + var draw = new ol.interaction.Draw({ + source: source, + type: ol.geom.GeometryType.LINE_STRING, + finishFunction: function(event){ + if (ol.array.equals(event.pixel,[30,20])){ + return true; + } + return false; + } + }); + map.addInteraction(draw); + }); + + it('draws a linestring failing to finish it first, the finishes it', function() { + var features; + // 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); + + // cannot finish on this point + simulateEvent('pointerdown', 30, 20); + simulateEvent('pointerup', 30, 20); + + features = source.getFeatures(); + expect(features).to.have.length(1); + + // second point + simulateEvent('pointermove', 40, 30); + simulateEvent('pointerdown', 40, 30); + simulateEvent('pointerup', 40, 30); + + // finish on this point + simulateEvent('pointerdown', 40, 30); + simulateEvent('pointerup', 40, 30); + + features = source.getFeatures(); + expect(features).to.have.length(1); + }); + }); describe('drawing multi-linestrings', function() { From c7af6bd494edda88aa51adbe5e9d99d381c8b4e9 Mon Sep 17 00:00:00 2001 From: giohappy Date: Fri, 22 Apr 2016 15:11:54 +0200 Subject: [PATCH 3/6] Add ol.interaction.Draw.finishCondition to olx.js --- externs/olx.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/externs/olx.js b/externs/olx.js index 09e1f63b39..82c3fde073 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -2659,6 +2659,15 @@ olx.interaction.DrawOptions.prototype.maxPoints; olx.interaction.DrawOptions.prototype.minPoints; +/** + * A function that takes an {@link ol.MapBrowserEvent} and returns a boolean + * to indicate whether the drawing can be finished. + * @type {ol.events.ConditionType|undefined} + * @api + */ +olx.interaction.DrawOptions.prototype.finishCondition; + + /** * Style for sketch features. * @type {ol.style.Style|Array.|ol.style.StyleFunction|undefined} From ead60631701cbb274c774456f23e4431404d69d9 Mon Sep 17 00:00:00 2001 From: giohappy Date: Fri, 22 Apr 2016 15:16:11 +0200 Subject: [PATCH 4/6] make lint happy with drawinteraction.test.js --- test/spec/ol/interaction/drawinteraction.test.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/spec/ol/interaction/drawinteraction.test.js b/test/spec/ol/interaction/drawinteraction.test.js index 23df2c6ba6..a75d150189 100644 --- a/test/spec/ol/interaction/drawinteraction.test.js +++ b/test/spec/ol/interaction/drawinteraction.test.js @@ -332,14 +332,14 @@ describe('ol.interaction.Draw', function() { }); }); - + describe('drawing with a finishCondition', function() { beforeEach(function() { var draw = new ol.interaction.Draw({ source: source, type: ol.geom.GeometryType.LINE_STRING, - finishFunction: function(event){ - if (ol.array.equals(event.pixel,[30,20])){ + finishFunction: function(event) { + if (ol.array.equals(event.pixel,[30,20])) { return true; } return false; @@ -366,7 +366,7 @@ describe('ol.interaction.Draw', function() { features = source.getFeatures(); expect(features).to.have.length(1); - + // second point simulateEvent('pointermove', 40, 30); simulateEvent('pointerdown', 40, 30); @@ -375,7 +375,7 @@ describe('ol.interaction.Draw', function() { // finish on this point simulateEvent('pointerdown', 40, 30); simulateEvent('pointerup', 40, 30); - + features = source.getFeatures(); expect(features).to.have.length(1); }); @@ -859,6 +859,7 @@ describe('ol.interaction.Draw', function() { }); }); +goog.require('ol.array'); goog.require('ol.events'); goog.require('ol.Feature'); goog.require('ol.Map'); From 5934f9d41615df07e7471289763adc203e424e1e Mon Sep 17 00:00:00 2001 From: giohappy Date: Fri, 22 Apr 2016 15:21:11 +0200 Subject: [PATCH 5/6] add olx.interaction.DrawOptions.prototype.finishCondition to typedef --- externs/olx.js | 1 + 1 file changed, 1 insertion(+) diff --git a/externs/olx.js b/externs/olx.js index 82c3fde073..c3fec4bade 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -2585,6 +2585,7 @@ olx.interaction.DragZoomOptions.prototype.out; * type: ol.geom.GeometryType, * maxPoints: (number|undefined), * minPoints: (number|undefined), + * finishCondition: (ol.events.ConditionType|undefined), * style: (ol.style.Style|Array.|ol.style.StyleFunction|undefined), * geometryFunction: (ol.interaction.DrawGeometryFunctionType|undefined), * geometryName: (string|undefined), From 394ef94fd3ab11b1316c34f67ef0b95941839cbc Mon Sep 17 00:00:00 2001 From: giohappy Date: Fri, 22 Apr 2016 23:17:21 +0200 Subject: [PATCH 6/6] Fix finishCondition test --- .../ol/interaction/drawinteraction.test.js | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/test/spec/ol/interaction/drawinteraction.test.js b/test/spec/ol/interaction/drawinteraction.test.js index a75d150189..1925892958 100644 --- a/test/spec/ol/interaction/drawinteraction.test.js +++ b/test/spec/ol/interaction/drawinteraction.test.js @@ -338,8 +338,8 @@ describe('ol.interaction.Draw', function() { var draw = new ol.interaction.Draw({ source: source, type: ol.geom.GeometryType.LINE_STRING, - finishFunction: function(event) { - if (ol.array.equals(event.pixel,[30,20])) { + finishCondition: function(event) { + if (ol.array.equals(event.coordinate,[30,-20])) { return true; } return false; @@ -350,32 +350,33 @@ describe('ol.interaction.Draw', function() { it('draws a linestring failing to finish it first, the finishes it', function() { var features; + // 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); - - // cannot finish on this point - simulateEvent('pointerdown', 30, 20); - simulateEvent('pointerup', 30, 20); - - features = source.getFeatures(); - expect(features).to.have.length(1); - // second point simulateEvent('pointermove', 40, 30); simulateEvent('pointerdown', 40, 30); simulateEvent('pointerup', 40, 30); - // finish on this point + // try to finish on this point simulateEvent('pointerdown', 40, 30); simulateEvent('pointerup', 40, 30); + features = source.getFeatures(); + expect(features).to.have.length(0); + + // third point + simulateEvent('pointermove', 30, 20); + simulateEvent('pointerdown', 30, 20); + simulateEvent('pointerup', 30, 20); + + // finish on this point + simulateEvent('pointerdown', 30, 20); + simulateEvent('pointerup', 30, 20); + features = source.getFeatures(); expect(features).to.have.length(1); });