tabindex without focux condition

This commit is contained in:
Andreas Hocevar
2019-11-13 22:51:45 +01:00
parent 8b036ba96c
commit 643a3ad86b
5 changed files with 42 additions and 22 deletions

View File

@@ -1,14 +0,0 @@
---
layout: example.html
title: Interaction Options
shortdesc: Shows interaction options for custom scroll and zoom behavior.
docs: >
This example uses a custom `ol/interaction/defaults` configuration:
by default, wheel/trackpad zoom and drag panning is always active, which
can be unexpected on pages with a lot of scrollable content and an embedded
map. To perform wheel/trackpad zoom and drag-pan actions only when the map
has the focus, set `onFocusOnly: true` as option. This requires a map div
with a `tabindex` attribute set.
tags: "trackpad, mousewheel, zoom, scroll, interaction, fractional"
---
<div tabindex="1" id="map" class="map"></div>

10
examples/page-scroll.html Normal file
View File

@@ -0,0 +1,10 @@
---
layout: example.html
title: Page Scrolling
shortdesc: Shows a map that does not interrupt page scrolling.
docs: >
To perform wheel/trackpad zoom and drag-pan actions only when the map
has the focus, configure your map div with a `tabindex` attribute.
tags: "trackpad, mousewheel, zoom, scroll, page"
---
<div tabindex="1" id="map" class="map"></div>

View File

@@ -1,14 +1,10 @@
import Map from '../src/ol/Map.js'; import Map from '../src/ol/Map.js';
import View from '../src/ol/View.js'; import View from '../src/ol/View.js';
import {defaults as defaultInteractions} from '../src/ol/interaction.js';
import TileLayer from '../src/ol/layer/Tile.js'; import TileLayer from '../src/ol/layer/Tile.js';
import OSM from '../src/ol/source/OSM.js'; import OSM from '../src/ol/source/OSM.js';
const map = new Map({ const map = new Map({
interactions: defaultInteractions({
onFocusOnly: true
}),
layers: [ layers: [
new TileLayer({ new TileLayer({
source: new OSM() source: new OSM()

View File

@@ -3,7 +3,7 @@
*/ */
import {scale as scaleCoordinate, rotate as rotateCoordinate} from '../coordinate.js'; import {scale as scaleCoordinate, rotate as rotateCoordinate} from '../coordinate.js';
import {easeOut} from '../easing.js'; import {easeOut} from '../easing.js';
import {noModifierKeys, primaryAction} from '../events/condition.js'; import {noModifierKeys, primaryAction, focus} from '../events/condition.js';
import {FALSE} from '../functions.js'; import {FALSE} from '../functions.js';
import PointerInteraction, {centroid as centroidFromPointers} from './Pointer.js'; import PointerInteraction, {centroid as centroidFromPointers} from './Pointer.js';
@@ -69,6 +69,20 @@ class DragPan extends PointerInteraction {
} }
/**
* @private
* @param {import("../MapBrowserEvent").default} mapBrowserEvent Event.
* @return {boolean} Condition passes.
*/
conditionInternal_(mapBrowserEvent) {
let pass = true;
if (mapBrowserEvent.map.getTargetElement().hasAttribute('tabindex')) {
pass = focus(mapBrowserEvent);
}
return pass && this.condition_(mapBrowserEvent);
}
/** /**
* @inheritDoc * @inheritDoc
*/ */
@@ -145,7 +159,7 @@ class DragPan extends PointerInteraction {
* @inheritDoc * @inheritDoc
*/ */
handleDownEvent(mapBrowserEvent) { handleDownEvent(mapBrowserEvent) {
if (this.targetPointers.length > 0 && this.condition_(mapBrowserEvent)) { if (this.targetPointers.length > 0 && this.conditionInternal_(mapBrowserEvent)) {
const map = mapBrowserEvent.map; const map = mapBrowserEvent.map;
const view = map.getView(); const view = map.getView();
this.lastCentroid = null; this.lastCentroid = null;

View File

@@ -1,7 +1,7 @@
/** /**
* @module ol/interaction/MouseWheelZoom * @module ol/interaction/MouseWheelZoom
*/ */
import {always} from '../events/condition.js'; import {always, focus} from '../events/condition.js';
import EventType from '../events/EventType.js'; import EventType from '../events/EventType.js';
import {DEVICE_PIXEL_RATIO, FIREFOX} from '../has.js'; import {DEVICE_PIXEL_RATIO, FIREFOX} from '../has.js';
import Interaction, {zoomByDelta} from './Interaction.js'; import Interaction, {zoomByDelta} from './Interaction.js';
@@ -134,6 +134,20 @@ class MouseWheelZoom extends Interaction {
} }
/**
* @private
* @param {import("../MapBrowserEvent").default} mapBrowserEvent Event.
* @return {boolean} Condition passes.
*/
conditionInternal_(mapBrowserEvent) {
let pass = true;
if (mapBrowserEvent.map.getTargetElement().hasAttribute('tabindex')) {
pass = focus(mapBrowserEvent);
}
return pass && this.condition_(mapBrowserEvent);
}
/** /**
* @private * @private
*/ */
@@ -149,7 +163,7 @@ class MouseWheelZoom extends Interaction {
* @override * @override
*/ */
handleEvent(mapBrowserEvent) { handleEvent(mapBrowserEvent) {
if (!this.condition_(mapBrowserEvent)) { if (!this.conditionInternal_(mapBrowserEvent)) {
return true; return true;
} }
const type = mapBrowserEvent.type; const type = mapBrowserEvent.type;