Skip to content

Commit

Permalink
Introduce map wheel event
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Feb 28, 2018
1 parent fa206ae commit 5e9f458
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 7 deletions.
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
};
6 changes: 3 additions & 3 deletions src/ui/handler/scroll_zoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ class ScrollZoomHandler {
*/
enable(options: any) {
if (this.isEnabled()) return;
this._el.addEventListener('wheel', this._onWheel, false);
this._enabled = true;
this._aroundCenter = options && options.around === 'center';
}
Expand All @@ -106,11 +105,12 @@ class ScrollZoomHandler {
*/
disable() {
if (!this.isEnabled()) return;
this._el.removeEventListener('wheel', this._onWheel);
this._enabled = false;
}

_onWheel(e: WheelEvent) {
onWheel(e: WheelEvent) {
if (!this.isEnabled()) return;

let value = e.deltaY;

// Firefox doubles the values on retina screens...
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();
});

0 comments on commit 5e9f458

Please sign in to comment.