From a0ee68558fcd3f439ee09ff8bf01aa6e3ac58ccc Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Sat, 30 Jul 2016 18:20:46 +0200 Subject: [PATCH] Close non permanent tooltip on map click on touch (fix #4741) --- spec/suites/layer/TooltipSpec.js | 25 +++++++++++++++++++++++++ src/layer/Tooltip.js | 10 ++++++++++ 2 files changed, 35 insertions(+) diff --git a/spec/suites/layer/TooltipSpec.js b/spec/suites/layer/TooltipSpec.js index ebce1273..cc6ddaab 100644 --- a/spec/suites/layer/TooltipSpec.js +++ b/spec/suites/layer/TooltipSpec.js @@ -253,5 +253,30 @@ describe('Tooltip', function () { expect(map.hasLayer(layer._tooltip)).to.be(true); }); + it("is opened when tapping on touch", function () { + var oldTouch = L.Browser.touch; + L.Browser.touch = true; + var layer = new L.Marker(center).addTo(map); + + layer.bindTooltip('Tooltip'); + expect(map.hasLayer(layer._tooltip)).to.be(false); + happen.click(layer._icon); + expect(map.hasLayer(layer._tooltip)).to.be(true); + L.Browser.touch = oldTouch; + }); + + it("is closed if not permanent when clicking on the map elsewhere on touch", function () { + var oldTouch = L.Browser.touch; + L.Browser.touch = true; + var layer = new L.Marker(center).addTo(map); + + layer.bindTooltip('Tooltip'); + happen.click(layer._icon); + expect(map.hasLayer(layer._tooltip)).to.be(true); + happen.click(map._container); + expect(map.hasLayer(layer._tooltip)).to.be(false); + L.Browser.touch = oldTouch; + }); + }); diff --git a/src/layer/Tooltip.js b/src/layer/Tooltip.js index d96ad9f9..dcbd82c6 100644 --- a/src/layer/Tooltip.js +++ b/src/layer/Tooltip.js @@ -94,6 +94,16 @@ L.Tooltip = L.DivOverlay.extend({ } }, + getEvents: function () { + var events = L.DivOverlay.prototype.getEvents.call(this); + + if (L.Browser.touch && !this.options.permanent) { + events.preclick = this._close; + } + + return events; + }, + _close: function () { if (this._map) { this._map.closeTooltip(this);