From 77ceb6fa475e5f33505e935044316dfb3a36f418 Mon Sep 17 00:00:00 2001 From: Midgard Date: Thu, 21 May 2020 02:26:44 +0200 Subject: [PATCH] Make function more sensible Redo the getParent function: * Rename to findAncestorWithClass, which is a better name. * There's no reason the mouse event's target should not be allowed to be the popup's container itself, so check the element itself too. * Return null if nothing found instead of false. --- src/js/map.js | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/js/map.js b/src/js/map.js index 2b3f762..23ac5c9 100644 --- a/src/js/map.js +++ b/src/js/map.js @@ -87,10 +87,10 @@ $(document).ready(function() { // get the element that the mouse hovered onto var target = e.originalEvent.fromElement || e.originalEvent.relatedTarget; - var parent = this._getParent(target, "leaflet-popup"); + var ancestor = this._findAncestorWithClass(target, "leaflet-popup"); // check to see if the element is a popup, and if it is this marker's popup - if (parent == this._popup._container) + if (ancestor && ancestor === this._popup._container) return true; this.openPopup(); @@ -103,7 +103,7 @@ $(document).ready(function() { var target = e.originalEvent.toElement || e.originalEvent.relatedTarget; // check to see if the element is a popup - if (this._getParent(target, "leaflet-popup")) { + if (this._findAncestorWithClass(target, "leaflet-popup")) { L.DomEvent.on(this._popup._container, "mouseout", this._popupMouseOut, this); return true; } @@ -122,26 +122,24 @@ $(document).ready(function() { var target = e.toElement || e.relatedTarget; // check to see if the element is a popup - if (this._getParent(target, "leaflet-popup")) + if (this._findAncestorWithClass(target, "leaflet-popup")) return true; // check to see if the marker was hovered back onto - if (target == this._icon) + if (target === this._icon) return true; this.closePopup(); }, - _getParent: function(element, className) { - var parent = element.parentNode; - - while (parent) { - if (parent.className && L.DomUtil.hasClass(parent, className)) - return parent; - parent = parent.parentNode; + _findAncestorWithClass: function(element, className) { + while (element) { + if (element.className && L.DomUtil.hasClass(element, className)) + return element; + element = element.parentNode; } - return false; + return null; } });