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

Fix "Marker#togglePopup", refactor "Marker" #3104

Merged
merged 7 commits into from
Sep 1, 2016
Merged

Fix "Marker#togglePopup", refactor "Marker" #3104

merged 7 commits into from
Sep 1, 2016

Conversation

lucaswoj
Copy link
Contributor

@lucaswoj lucaswoj commented Aug 30, 2016

This PR fixes #3096 and addresses @jfirebaugh's feedback in #3056

cc @jfirebaugh @mollymerp @apkoponen

Checklist

* set on this `Marker` instance is unset
* @returns {Marker} `this`
*/

setPopup: function (popup) {
if (popup == null) {
this._closePopup();
delete this._popupHandlersAdded;
Copy link
Contributor Author

@lucaswoj lucaswoj Aug 31, 2016

Choose a reason for hiding this comment

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

Instead of tracking state in _popupHandlersAdded, callremoveEventListener when the popup is removed. This will prevent memory leaks and unexpected behavior if the Marker continues to be used without a popup. You'll need to save the this-bound version of _openPopup, say by using util.bindAll(['_openPopup'], this), so that it can be passed to removeEventListener.

#3056 (comment)

@jfirebaugh
Copy link
Contributor

This prevents all click events on a marker from being propagated as map click events, which is probably not what a user expects. If it really is the desired behavior, a better way to accomplish it is to add the marker element to the control container instead of preventing event propagation.

@lucaswoj lucaswoj changed the title Fix "Marker#togglePopup", refactor marker Fix "Marker#togglePopup", refactor "Marker" Aug 31, 2016
@lucaswoj
Copy link
Contributor Author

@jfirebaugh Makes sense. Done in the latest commit.

@mollymerp
Copy link
Contributor

Thank you @lucaswoj 😳

@lucaswoj
Copy link
Contributor Author

@mollymerp my 😳 for merging #3056 prematurely!

Ready to 🚢 @jfirebaugh @mollymerp?

@jfirebaugh
Copy link
Contributor

I really think it will be surprising for clicks on markers not to produce map click events. If they don't, then for instance the closeOnClick option of Popup will not work as expected for the common case of having multiple markers with popups where opening one popup via its marker should close any previously open popup.

@lucaswoj
Copy link
Contributor Author

@jfirebaugh I understand now. I misread #3104 (comment). The latest commit ensures that this event is forwarded to map as expected.

// the map's container. If we attached the listener syncronously
// the popup would close when the click event propogated to `map`.
var that = this;
setTimeout(function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there another way? Ad hoc setTimeouts in library code are really dangerous. For example, this will throw if for some reason the popup is removed before the timeout triggers, because that._map will be undefined after remove deletes it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. There are some other approaches we can take. I'll push another commit soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The latest commit fixes this problem by attaching the click listener to the map and checking the event's target to determine whether or not the popup should toggle. I did not see any performance degradation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good solution! It will probably be a performance improvement for lots of markers because adding native event listeners tends to be slower than event delegation.

@lucaswoj lucaswoj merged commit 1c82b0b into master Sep 1, 2016
@lucaswoj lucaswoj deleted the bindpopup-2 branch September 1, 2016 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Marker.togglePopup() does not work
3 participants