Merge pull request #12185 from ahocevar/prevent-default
MapBrowserEvent#preventDefault() behaves the same as with native events
This commit is contained in:
@@ -201,10 +201,10 @@ class MapBrowserEventHandler extends Target {
|
|||||||
// to 0).
|
// to 0).
|
||||||
// See http://www.w3.org/TR/pointerevents/#button-states
|
// See http://www.w3.org/TR/pointerevents/#button-states
|
||||||
// We only fire click, singleclick, and doubleclick if nobody has called
|
// We only fire click, singleclick, and doubleclick if nobody has called
|
||||||
// event.stopPropagation() or event.preventDefault().
|
// event.preventDefault().
|
||||||
if (
|
if (
|
||||||
this.emulateClicks_ &&
|
this.emulateClicks_ &&
|
||||||
!newEvent.propagationStopped &&
|
!newEvent.defaultPrevented &&
|
||||||
!this.dragging_ &&
|
!this.dragging_ &&
|
||||||
this.isMouseActionButton_(pointerEvent)
|
this.isMouseActionButton_(pointerEvent)
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -22,6 +22,11 @@ class BaseEvent {
|
|||||||
*/
|
*/
|
||||||
this.propagationStopped;
|
this.propagationStopped;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {boolean}
|
||||||
|
*/
|
||||||
|
this.defaultPrevented;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The event type.
|
* The event type.
|
||||||
* @type {string}
|
* @type {string}
|
||||||
@@ -38,11 +43,12 @@ class BaseEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stop event propagation.
|
* Prevent default. This means that no emulated `click`, `singleclick` or `doubleclick` events
|
||||||
|
* will be fired.
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
preventDefault() {
|
preventDefault() {
|
||||||
this.propagationStopped = true;
|
this.defaultPrevented = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -650,7 +650,7 @@ class Draw extends PointerInteraction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!pass && this.stopClick_) {
|
if (!pass && this.stopClick_) {
|
||||||
event.stopPropagation();
|
event.preventDefault();
|
||||||
}
|
}
|
||||||
return pass;
|
return pass;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,10 +16,10 @@ describe('ol.events.Event', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('#preventDefault', function () {
|
describe('#preventDefault', function () {
|
||||||
it('sets the propagationStopped flag', function () {
|
it('sets the defaultPrevented flag', function () {
|
||||||
const event = new Event('foo');
|
const event = new Event('foo');
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
expect(event.propagationStopped).to.be(true);
|
expect(event.defaultPrevented).to.be(true);
|
||||||
});
|
});
|
||||||
it('does the same as #stopPropagation', function () {
|
it('does the same as #stopPropagation', function () {
|
||||||
const event = new Event('foo');
|
const event = new Event('foo');
|
||||||
|
|||||||
@@ -105,10 +105,10 @@ describe('ol.events.EventTarget', function () {
|
|||||||
eventTarget.dispatchEvent('foo');
|
eventTarget.dispatchEvent('foo');
|
||||||
expect(called).to.eql([1, 2]);
|
expect(called).to.eql([1, 2]);
|
||||||
});
|
});
|
||||||
it('stops propagation when listeners call preventDefault()', function () {
|
it('stops propagation when listeners call stopPropagation()', function () {
|
||||||
eventTarget.addEventListener('foo', function (evt) {
|
eventTarget.addEventListener('foo', function (evt) {
|
||||||
spy2();
|
spy2();
|
||||||
evt.preventDefault();
|
evt.stopPropagation();
|
||||||
});
|
});
|
||||||
eventTarget.addEventListener('foo', spy1);
|
eventTarget.addEventListener('foo', spy1);
|
||||||
eventTarget.dispatchEvent('foo');
|
eventTarget.dispatchEvent('foo');
|
||||||
|
|||||||
Reference in New Issue
Block a user