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

Introduce map wheel event #6237

Merged
merged 2 commits into from
Feb 28, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/ui/bind_handlers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// @flow

const {MapMouseEvent, MapTouchEvent} = require('../ui/events');
const {
MapMouseEvent,
MapTouchEvent,
MapWheelEvent
} = require('../ui/events');

import type Map from './map';

Expand Down Expand Up @@ -38,6 +42,7 @@ module.exports = function bindHandlers(map: Map, options: {}) {
el.addEventListener('click', onClick, false);
el.addEventListener('dblclick', onDblClick, false);
el.addEventListener('contextmenu', onContextMenu, false);
el.addEventListener('wheel', onWheel, false);

function onMouseDown(e: MouseEvent) {
mouseDown = true;
Expand Down Expand Up @@ -158,4 +163,15 @@ module.exports = function bindHandlers(map: Map, options: {}) {

e.preventDefault();
}

function onWheel(e: WheelEvent) {
const mapEvent = new MapWheelEvent('wheel', map, e);
map.fire(mapEvent);

if (mapEvent.defaultPrevented) {
return;
}

map.scrollZoom.onWheel(e);
}
};
61 changes: 60 additions & 1 deletion src/ui/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,54 @@ class MapTouchEvent extends Event {
}
}


/**
* `MapWheelEvent` is the event type for the `wheel` map event.
* @extends {Object}
*/
class MapWheelEvent extends Event {
/**
* The event type.
*/
type: 'wheel';

/**
* The `Map` object that fired the event.
*/
map: Map;

/**
* The DOM event which caused the map event.
*/
originalEvent: WheelEvent;

/**
* Prevents subsequent default processing of the event by the map.
*
* Calling this method will prevent the the behavior of {@link ScrollZoomHandler}.
*/
preventDefault() {
this._defaultPrevented = true;
}

/**
* `true` if `preventDefault` has been called.
*/
get defaultPrevented(): boolean {
return this._defaultPrevented;
}

_defaultPrevented: boolean;

/**
* @private
*/
constructor(type: string, map: Map, originalEvent: WheelEvent) {
super(type, { originalEvent });
this._defaultPrevented = false;
}
}

/**
* @typedef {Object} MapBoxZoomEvent
* @property {MouseEvent} originalEvent
Expand Down Expand Up @@ -339,6 +387,16 @@ export type MapEvent =
*/
| 'contextmenu'

/**
* Fired when a [`wheel`](https://developer.mozilla.org/en-US/docs/Web/Events/wheel) event occurs within the map.
*
* @event wheel
* @memberof Map
* @instance
* @property {MapWheelEvent} data
*/
| 'wheel'

/**
* Fired when a [`touchstart`](https://developer.mozilla.org/en-US/docs/Web/Events/touchstart) event occurs within the map.
*
Expand Down Expand Up @@ -729,5 +787,6 @@ export type MapEvent =

module.exports = {
MapMouseEvent,
MapTouchEvent
MapTouchEvent,
MapWheelEvent
};
29 changes: 11 additions & 18 deletions src/ui/handler/scroll_zoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ const wheelZoomRate = 1 / 450;
// is used to limit zoom rate in the case of very fast scrolling
const maxScalePerFrame = 2;

const ua = window.navigator.userAgent.toLowerCase(),
firefox = ua.indexOf('firefox') !== -1,
safari = ua.indexOf('safari') !== -1 && ua.indexOf('chrom') === -1;
const firefox = window.navigator.userAgent.toLowerCase().indexOf('firefox') !== -1;

/**
* The `ScrollZoomHandler` allows the user to zoom the map by scrolling.
Expand Down Expand Up @@ -95,8 +93,6 @@ class ScrollZoomHandler {
*/
enable(options: any) {
if (this.isEnabled()) return;
this._el.addEventListener('wheel', this._onWheel, false);
this._el.addEventListener('mousewheel', this._onWheel, false);
this._enabled = true;
this._aroundCenter = options && options.around === 'center';
}
Expand All @@ -109,24 +105,21 @@ class ScrollZoomHandler {
*/
disable() {
if (!this.isEnabled()) return;
this._el.removeEventListener('wheel', this._onWheel);
this._el.removeEventListener('mousewheel', this._onWheel);
this._enabled = false;
}

_onWheel(e: any) {
let value = 0;
onWheel(e: WheelEvent) {
if (!this.isEnabled()) return;

if (e.type === 'wheel') {
value = e.deltaY;
// Firefox doubles the values on retina screens...
// Remove `any` casts when https://github.com/facebook/flow/issues/4879 is fixed.
if (firefox && e.deltaMode === (window.WheelEvent: any).DOM_DELTA_PIXEL) value /= browser.devicePixelRatio;
if (e.deltaMode === (window.WheelEvent: any).DOM_DELTA_LINE) value *= 40;
let value = e.deltaY;

} else if (e.type === 'mousewheel') {
value = -e.wheelDeltaY;
if (safari) value = value / 3;
// Firefox doubles the values on retina screens...
// Remove `any` casts when https://github.com/facebook/flow/issues/4879 is fixed.
if (firefox && e.deltaMode === (window.WheelEvent: any).DOM_DELTA_PIXEL) {
value /= browser.devicePixelRatio;
}
if (e.deltaMode === (window.WheelEvent: any).DOM_DELTA_LINE) {
value *= 40;
}

const now = browser.now(),
Expand Down
20 changes: 18 additions & 2 deletions test/unit/ui/handler/scroll_zoom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function createMap(options) {
}, options));
}

test('ScrollZoomHandler zooms in response to wheel events', (t) => {
test('ScrollZoomHandler', (t) => {
const browserNow = t.stub(browser, 'now');
let now = 1555555555555;
browserNow.callsFake(() => now);
Expand Down Expand Up @@ -115,6 +115,22 @@ test('ScrollZoomHandler zooms in response to wheel events', (t) => {
t.end();
});

test('does not zoom if preventDefault is called on the wheel event', (t) => {
const map = createMap();

map.on('wheel', e => e.preventDefault());

simulate.wheel(map.getCanvas(), {type: 'wheel', deltaY: -simulate.magicWheelZoomDelta});
map._updateCamera();

now += 400;
map._updateCamera();

t.equal(map.getZoom(), 0);

map.remove();
t.end();
});

t.end();
});