only propagate events if given true to fire, #2311

This commit is contained in:
Vladimir Agafonkin 2013-12-18 23:23:22 -05:00
parent ce4a031d40
commit 6d1142a978
6 changed files with 49 additions and 11 deletions

View File

@ -381,4 +381,36 @@ describe('Events', function () {
expect(spy.called).to.be(true);
});
});
describe('addEventParent && removeEventParent', function () {
it('makes the object propagate events with to the given one if fired with propagate=true', function () {
var obj = new L.Evented(),
parent1 = new L.Evented(),
parent2 = new L.Evented(),
spy1 = sinon.spy(),
spy2 = sinon.spy();
parent1.on('test', spy1);
parent2.on('test', spy2);
obj.addEventParent(parent1).addEventParent(parent2);
obj.fire('test');
expect(spy1.called).to.be(false);
expect(spy2.called).to.be(false);
obj.fire('test', null, true);
expect(spy1.called).to.be(true);
expect(spy2.called).to.be(true);
obj.removeEventParent(parent1);
obj.fire('test', null, true);
expect(spy1.callCount).to.be(1);
expect(spy2.callCount).to.be(2);
});
});
});

View File

@ -32,7 +32,7 @@
wasClicked1 = true;
});
marker.fire('click', { type: 'click' });
marker.fire('click', {type: 'click'}, true);
expect(wasClicked1).to.be(true);
expect(wasClicked2).to.be(true);

View File

@ -126,8 +126,8 @@ L.Evented = L.Class.extend({
}
},
fire: function (type, data) {
if (!this.hasEventListeners(type)) {
fire: function (type, data, propagate) {
if (!this.hasEventListeners(type, propagate)) {
return this;
}
@ -161,19 +161,23 @@ L.Evented = L.Class.extend({
}
}
this._propagateEvent(event);
if (propagate) {
this._propagateEvent(event);
}
return this;
},
hasEventListeners: function (type) {
hasEventListeners: function (type, propagate) {
var events = this[eventsKey];
if (events && ((type in events && events[type].length > 0) ||
(type + '_idx' in events && events[type + '_idx_len'] > 0))) {
return true;
}
for (var id in this._eventParents) {
if (this._eventParents[id].hasEventListeners(type)) { return true; }
if (propagate) {
for (var id in this._eventParents) {
if (this._eventParents[id].hasEventListeners(type)) { return true; }
}
}
return false;
},
@ -206,12 +210,14 @@ L.Evented = L.Class.extend({
addEventParent: function (obj) {
this._eventParents = this._eventParents || {};
this._eventParents[L.stamp(obj)] = obj;
return this;
},
removeEventParent: function (obj) {
if (this._eventParents) {
delete this._eventParents[L.stamp(obj)];
}
return this;
},
_propagateEvent: function (e) {

View File

@ -54,7 +54,7 @@ L.Popup = L.Layer.extend({
map.fire('popupopen', {popup: this});
if (this._source) {
this._source.fire('popupopen', {popup: this});
this._source.fire('popupopen', {popup: this}, true);
}
},
@ -74,7 +74,7 @@ L.Popup = L.Layer.extend({
map.fire('popupclose', {popup: this});
if (this._source) {
this._source.fire('popupclose', {popup: this});
this._source.fire('popupclose', {popup: this}, true);
}
},

View File

@ -257,7 +257,7 @@ L.Marker = L.Layer.extend({
this.fire(e.type, {
originalEvent: e,
latlng: this._latlng
});
}, true);
// TODO proper custom event propagation
// this line will always be called if marker is in a FeatureGroup

View File

@ -89,7 +89,7 @@ L.Path = L.Layer.extend({
layerPoint: layerPoint,
containerPoint: containerPoint,
originalEvent: e
});
}, true);
if (type === 'contextmenu') {
L.DomEvent.preventDefault(e);