Cancel tooltip on mouseout

Prevent a tooltip to be displayed if the user has moved out
the mouse from the anchor.
This commit is contained in:
Yohan Boniface 2023-10-27 18:45:58 +02:00
parent dde6254d16
commit 9d9b726af5
2 changed files with 6 additions and 4 deletions

View file

@ -1736,7 +1736,7 @@ L.U.Editable = L.Editable.extend({
}, },
drawingTooltip: function (e) { drawingTooltip: function (e) {
if (e.layer instanceof L.Marker && e.type != 'editable:drawing:move') { if (e.layer instanceof L.Marker && e.type == 'editable:drawing:start') {
this.map.ui.tooltip({ content: L._('Click to add a marker') }) this.map.ui.tooltip({ content: L._('Click to add a marker') })
} }
if (!(e.layer instanceof L.Polyline)) { if (!(e.layer instanceof L.Polyline)) {

View file

@ -135,8 +135,6 @@ L.U.UI = L.Evented.extend({
}, },
tooltip: function (opts) { tooltip: function (opts) {
this.TOOLTIP_ID = Math.random()
const id = this.TOOLTIP_ID
function showIt() { function showIt() {
if (opts.anchor && opts.position === 'top') { if (opts.anchor && opts.position === 'top') {
this.anchorTooltipTop(opts.anchor) this.anchorTooltipTop(opts.anchor)
@ -150,10 +148,11 @@ L.U.UI = L.Evented.extend({
L.DomUtil.addClass(this.parent, 'umap-tooltip') L.DomUtil.addClass(this.parent, 'umap-tooltip')
this._tooltip.innerHTML = opts.content this._tooltip.innerHTML = opts.content
} }
this.TOOLTIP_ID = window.setTimeout(L.bind(showIt, this), opts.delay || 0)
const id = this.TOOLTIP_ID
function closeIt() { function closeIt() {
this.closeTooltip(id) this.closeTooltip(id)
} }
window.setTimeout(L.bind(showIt, this), opts.delay || 0)
if (opts.anchor) { if (opts.anchor) {
L.DomEvent.once(opts.anchor, 'mouseout', closeIt, this) L.DomEvent.once(opts.anchor, 'mouseout', closeIt, this)
} }
@ -200,6 +199,9 @@ L.U.UI = L.Evented.extend({
}, },
closeTooltip: function (id) { closeTooltip: function (id) {
// Clear timetout even if a new tooltip has been added
// in the meantime. Eg. after a mouseout from the anchor.
window.clearTimeout(id)
if (id && id !== this.TOOLTIP_ID) return if (id && id !== this.TOOLTIP_ID) return
this._tooltip.className = '' this._tooltip.className = ''
this._tooltip.innerHTML = '' this._tooltip.innerHTML = ''