Skip to content

Commit

Permalink
don't fire click event if default is prevented on mousedown for a dra…
Browse files Browse the repository at this point in the history
…g event (#6697)
  • Loading branch information
mollymerp authored and sbma44 committed Jun 1, 2018
1 parent a5c4d21 commit 4ca0bc7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/ui/bind_handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default function bindHandlers(map: Map, options: {interactive: boolean})
const el = map.getCanvasContainer();
let contextMenuEvent = null;
let mouseDown = false;
let startPos = null;

for (const name in handlers) {
(map: any)[name] = new handlers[name](map, options);
Expand Down Expand Up @@ -56,6 +57,7 @@ export default function bindHandlers(map: Map, options: {interactive: boolean})

function onMouseDown(e: MouseEvent) {
mouseDown = true;
startPos = DOM.mousePos(el, e);

const mapEvent = new MapMouseEvent('mousedown', map, e);
map.fire(mapEvent);
Expand Down Expand Up @@ -149,7 +151,10 @@ export default function bindHandlers(map: Map, options: {interactive: boolean})
}

function onClick(e: MouseEvent) {
map.fire(new MapMouseEvent('click', map, e));
const pos = DOM.mousePos(el, e);
if (pos.equals(startPos)) {
map.fire(new MapMouseEvent('click', map, e));
}
}

function onDblClick(e: MouseEvent) {
Expand Down
9 changes: 9 additions & 0 deletions test/node_modules/mapbox-gl-js-test/simulate_interaction.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions test/unit/ui/map_events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,19 @@ test(`Map#on mousedown can have default behavior prevented and still fire subseq
map.remove();
t.end();
});

test(`Map#on mousedown doesn't fire subsequent click event if mousepos changes`, (t) => {
const map = createMap();

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

const click = t.spy();
map.on('click', click);
const canvas = map.getCanvas();

simulate.drag(canvas, {}, {clientX: 100, clientY: 100});
t.ok(click.notCalled);

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

0 comments on commit 4ca0bc7

Please sign in to comment.