From 3acb0b28ded30f8192a92d9837c26f7a811b85a8 Mon Sep 17 00:00:00 2001 From: mike-000 <49240900+mike-000@users.noreply.github.com> Date: Thu, 18 Jun 2020 15:17:55 +0100 Subject: [PATCH 1/3] Add condition option --- src/ol/interaction/Extent.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/ol/interaction/Extent.js b/src/ol/interaction/Extent.js index e6a7496b92..da905c75ed 100644 --- a/src/ol/interaction/Extent.js +++ b/src/ol/interaction/Extent.js @@ -9,6 +9,7 @@ import Point from '../geom/Point.js'; import PointerInteraction from './Pointer.js'; import VectorLayer from '../layer/Vector.js'; import VectorSource from '../source/Vector.js'; +import {always} from '../events/condition.js'; import {boundingExtent, getArea} from '../extent.js'; import { closestOnSegment, @@ -22,6 +23,10 @@ import {toUserExtent} from '../proj.js'; /** * @typedef {Object} Options + * @property {import("../events/condition.js").Condition} [condition] A function that + * takes an {@link module:ol/MapBrowserEvent~MapBrowserEvent} and returns a + * boolean to indicate whether that event should be handled. + * Default is {@link module:ol/events/condition~always}. * @property {import("../extent.js").Extent} [extent] Initial extent. Defaults to no * initial extent. * @property {import("../style/Style.js").StyleLike} [boxStyle] @@ -87,6 +92,13 @@ class Extent extends PointerInteraction { super(/** @type {import("./Pointer.js").Options} */ (options)); + /** + * Condition + * @type {import("../events/condition.js").Condition} + * @private + */ + this.condition_ = options.condition ? options.condition : always; + /** * Extent of the drawn box * @type {import("../extent.js").Extent} @@ -280,7 +292,10 @@ class Extent extends PointerInteraction { * @return {boolean} `false` to stop event propagation. */ handleEvent(mapBrowserEvent) { - if (!mapBrowserEvent.originalEvent) { + if ( + !mapBrowserEvent.originalEvent || + !this.condition_(mapBrowserEvent)) + { return true; } //display pointer (if not dragging) From 64167a5d7eba261f3f6c1b6e223d5f6b855b8df5 Mon Sep 17 00:00:00 2001 From: mike-000 <49240900+mike-000@users.noreply.github.com> Date: Thu, 18 Jun 2020 15:31:33 +0100 Subject: [PATCH 2/3] Use condition instead of setActive and listeners --- examples/extent-interaction.js | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/examples/extent-interaction.js b/examples/extent-interaction.js index 907e702f04..dbd519a137 100644 --- a/examples/extent-interaction.js +++ b/examples/extent-interaction.js @@ -4,6 +4,7 @@ import Map from '../src/ol/Map.js'; import View from '../src/ol/View.js'; import {OSM, Vector as VectorSource} from '../src/ol/source.js'; import {Tile as TileLayer, Vector as VectorLayer} from '../src/ol/layer.js'; +import {shiftKeyOnly} from '../src/ol/events/condition.js'; const vectorSource = new VectorSource({ url: 'data/geojson/countries.geojson', @@ -26,18 +27,5 @@ const map = new Map({ }), }); -const extent = new ExtentInteraction(); +const extent = new ExtentInteraction({condition: shiftKeyOnly}); map.addInteraction(extent); -extent.setActive(false); - -//Enable interaction by holding shift -window.addEventListener('keydown', function (event) { - if (event.keyCode == 16) { - extent.setActive(true); - } -}); -window.addEventListener('keyup', function (event) { - if (event.keyCode == 16) { - extent.setActive(false); - } -}); From 342b69fe926fa296939f1a7670055296583dcaeb Mon Sep 17 00:00:00 2001 From: mike-000 <49240900+mike-000@users.noreply.github.com> Date: Thu, 18 Jun 2020 15:42:48 +0100 Subject: [PATCH 3/3] fix prettier --- src/ol/interaction/Extent.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/ol/interaction/Extent.js b/src/ol/interaction/Extent.js index da905c75ed..d547f567cb 100644 --- a/src/ol/interaction/Extent.js +++ b/src/ol/interaction/Extent.js @@ -292,10 +292,7 @@ class Extent extends PointerInteraction { * @return {boolean} `false` to stop event propagation. */ handleEvent(mapBrowserEvent) { - if ( - !mapBrowserEvent.originalEvent || - !this.condition_(mapBrowserEvent)) - { + if (!mapBrowserEvent.originalEvent || !this.condition_(mapBrowserEvent)) { return true; } //display pointer (if not dragging)