Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add condition option to Extent interaction #11198

Merged
merged 3 commits into from Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 2 additions & 14 deletions examples/extent-interaction.js
Expand Up @@ -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',
Expand All @@ -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);
}
});
14 changes: 13 additions & 1 deletion src/ol/interaction/Extent.js
Expand Up @@ -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,
Expand All @@ -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]
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -280,7 +292,7 @@ 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)
Expand Down