Merge pull request #11198 from mike-000/patch-17
Add condition option to Extent interaction
This commit is contained in:
@@ -4,6 +4,7 @@ import Map from '../src/ol/Map.js';
|
|||||||
import View from '../src/ol/View.js';
|
import View from '../src/ol/View.js';
|
||||||
import {OSM, Vector as VectorSource} from '../src/ol/source.js';
|
import {OSM, Vector as VectorSource} from '../src/ol/source.js';
|
||||||
import {Tile as TileLayer, Vector as VectorLayer} from '../src/ol/layer.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({
|
const vectorSource = new VectorSource({
|
||||||
url: 'data/geojson/countries.geojson',
|
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);
|
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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import Point from '../geom/Point.js';
|
|||||||
import PointerInteraction from './Pointer.js';
|
import PointerInteraction from './Pointer.js';
|
||||||
import VectorLayer from '../layer/Vector.js';
|
import VectorLayer from '../layer/Vector.js';
|
||||||
import VectorSource from '../source/Vector.js';
|
import VectorSource from '../source/Vector.js';
|
||||||
|
import {always} from '../events/condition.js';
|
||||||
import {boundingExtent, getArea} from '../extent.js';
|
import {boundingExtent, getArea} from '../extent.js';
|
||||||
import {
|
import {
|
||||||
closestOnSegment,
|
closestOnSegment,
|
||||||
@@ -22,6 +23,10 @@ import {toUserExtent} from '../proj.js';
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} Options
|
* @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
|
* @property {import("../extent.js").Extent} [extent] Initial extent. Defaults to no
|
||||||
* initial extent.
|
* initial extent.
|
||||||
* @property {import("../style/Style.js").StyleLike} [boxStyle]
|
* @property {import("../style/Style.js").StyleLike} [boxStyle]
|
||||||
@@ -87,6 +92,13 @@ class Extent extends PointerInteraction {
|
|||||||
|
|
||||||
super(/** @type {import("./Pointer.js").Options} */ (options));
|
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
|
* Extent of the drawn box
|
||||||
* @type {import("../extent.js").Extent}
|
* @type {import("../extent.js").Extent}
|
||||||
@@ -280,7 +292,7 @@ class Extent extends PointerInteraction {
|
|||||||
* @return {boolean} `false` to stop event propagation.
|
* @return {boolean} `false` to stop event propagation.
|
||||||
*/
|
*/
|
||||||
handleEvent(mapBrowserEvent) {
|
handleEvent(mapBrowserEvent) {
|
||||||
if (!mapBrowserEvent.originalEvent) {
|
if (!mapBrowserEvent.originalEvent || !this.condition_(mapBrowserEvent)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//display pointer (if not dragging)
|
//display pointer (if not dragging)
|
||||||
|
|||||||
Reference in New Issue
Block a user