Allow EventTarget to use a different default target

This commit is contained in:
Andreas Hocevar
2019-09-26 17:48:18 +02:00
parent d8f55ea3f1
commit 899af3e5c7
3 changed files with 22 additions and 3 deletions

View File

@@ -18,7 +18,7 @@ class MapBrowserEventHandler extends EventTarget {
*/
constructor(map, moveTolerance) {
super();
super(map);
/**
* This is the element that we will listen to the real events on.

View File

@@ -28,10 +28,20 @@ import {clear} from '../obj.js';
* returns false.
*/
class Target extends Disposable {
constructor() {
/**
* @param {*=} opt_target Default event target for dispatched events.
*/
constructor(opt_target) {
super();
/**
* @private
* @type {*}
*/
this.target_ = opt_target;
/**
* @private
* @type {!Object<string, number>}
@@ -86,7 +96,7 @@ class Target extends Disposable {
const evt = typeof event === 'string' ? new Event(event) : event;
const type = evt.type;
if (!evt.target) {
evt.target = this;
evt.target = this.target_ || this;
}
const listeners = this.listeners_[type];
let propagate;

View File

@@ -28,6 +28,15 @@ describe('ol.events.EventTarget', function() {
it('creates an empty listeners_ object', function() {
expect(Object.keys(eventTarget.listeners_)).to.have.length(0);
});
it('accepts a default target', function(done) {
const defaultTarget = {};
const target = new EventTarget(defaultTarget);
target.addEventListener('my-event', function(event) {
expect(event.target).to.eql(defaultTarget);
done();
});
target.dispatchEvent('my-event');
});
});
describe('#hasListener', function() {