Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up and document event propagation properties #5880

Merged
merged 3 commits into from
Oct 27, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions spec/suites/core/EventsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,37 @@ describe('Events', function () {
expect(spy1.callCount).to.eql(0);
expect(spy2.callCount).to.eql(1);
});

it('sets target, sourceTarget and layer correctly', function () {
var obj = new L.Evented(),
parent = new L.Evented(),
spy1 = sinon.spy(),
spy2 = sinon.spy();

/* register without context */
obj.on('test2', spy1);
parent.on('test2', spy2);

obj.addEventParent(parent);

/* Should be called once */
obj.fire('test2', null, true);

expect(spy1.calledWith({
type: 'test2',
target: obj,
sourceTarget: obj
})).to.be.ok();
expect(spy2.calledWith({
type: 'test2',
target: parent,
// layer should be deprecated in the future
// in favor of sourceTarget
layer: obj,
sourceTarget: obj,
propagatedFrom: obj
})).to.be.ok();
});
});

describe('#listens', function () {
Expand Down
6 changes: 3 additions & 3 deletions spec/suites/layer/FeatureGroupSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
marker = L.marker([0, 0]);
});
describe("when a Marker is added to multiple FeatureGroups ", function () {
it("e.layer should be the Marker", function () {
it("e.propagatedFrom should be the Marker", function () {
var fg1 = L.featureGroup(),
fg2 = L.featureGroup();

Expand All @@ -21,13 +21,13 @@
wasClicked2;

fg2.on('click', function (e) {
expect(e.layer).to.be(marker);
expect(e.propagatedFrom).to.be(marker);
expect(e.target).to.be(fg2);
wasClicked2 = true;
});

fg1.on('click', function (e) {
expect(e.layer).to.be(marker);
expect(e.propagatedFrom).to.be(marker);
expect(e.target).to.be(fg1);
wasClicked1 = true;
});
Expand Down
11 changes: 9 additions & 2 deletions src/core/Events.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ export var Events = {
fire: function (type, data, propagate) {
if (!this.listens(type, propagate)) { return this; }

var event = Util.extend({}, data, {type: type, target: this});
var event = Util.extend({}, data, {
type: type,
target: this,
sourceTarget: data && data.sourceTarget || this
});

if (this._events) {
var listeners = this._events[type];
Expand Down Expand Up @@ -255,7 +259,10 @@ export var Events = {

_propagateEvent: function (e) {
for (var id in this._eventParents) {
this._eventParents[id].fire(e.type, Util.extend({layer: e.target}, e), true);
this._eventParents[id].fire(e.type, Util.extend({
layer: e.target,
propagatedFrom: e.target
}, e), true);
}
}
};
Expand Down
11 changes: 10 additions & 1 deletion src/core/Events.leafdoc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ The base event object. All other event objects contain these properties too.
@property type: String
The event type (e.g. `'click'`).
@property target: Object
The object that fired the event.
The object that fired the event. For propagated events, the last object in
the propagation chain that fired the event.
@property sourceTarget: Object
The object that originally fired the event. For non-propagated events, this will
be the same as the `target`.
@property propagatedFrom: Object
For propagated events, the last object that propagated the event to its
event parent.
@property layer: Object
**Deprecated.** The same as `propagetedFrom`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo here: propageted vs propagated

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch 👍, fixed now.



@miniclass KeyboardEvent (Event objects)
Expand Down