Merge pull request #10024 from ahocevar/focus-condition-target

Allow EventTarget to use a different default target
This commit is contained in:
Andreas Hocevar
2019-09-26 19:59:27 +02:00
committed by GitHub
3 changed files with 22 additions and 3 deletions

View File

@@ -18,7 +18,7 @@ class MapBrowserEventHandler extends EventTarget {
*/ */
constructor(map, moveTolerance) { constructor(map, moveTolerance) {
super(); super(map);
/** /**
* This is the element that we will listen to the real events on. * 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. * returns false.
*/ */
class Target extends Disposable { class Target extends Disposable {
constructor() {
/**
* @param {*=} opt_target Default event target for dispatched events.
*/
constructor(opt_target) {
super(); super();
/**
* @private
* @type {*}
*/
this.target_ = opt_target;
/** /**
* @private * @private
* @type {!Object<string, number>} * @type {!Object<string, number>}
@@ -86,7 +96,7 @@ class Target extends Disposable {
const evt = typeof event === 'string' ? new Event(event) : event; const evt = typeof event === 'string' ? new Event(event) : event;
const type = evt.type; const type = evt.type;
if (!evt.target) { if (!evt.target) {
evt.target = this; evt.target = this.target_ || this;
} }
const listeners = this.listeners_[type]; const listeners = this.listeners_[type];
let propagate; let propagate;

View File

@@ -28,6 +28,15 @@ describe('ol.events.EventTarget', function() {
it('creates an empty listeners_ object', function() { it('creates an empty listeners_ object', function() {
expect(Object.keys(eventTarget.listeners_)).to.have.length(0); 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() { describe('#hasListener', function() {