-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move tooltip to a dedicated module
- Loading branch information
1 parent
142a89f
commit 1755535
Showing
11 changed files
with
192 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#umap-tooltip-container { | ||
line-height: 20px; | ||
padding: 5px 10px; | ||
width: auto; | ||
position: absolute; | ||
box-shadow: 0 1px 7px #999999; | ||
display: none; | ||
background-color: rgba(40, 40, 40, 0.8); | ||
color: #eeeeec; | ||
font-size: 0.8em; | ||
border-radius: 2px; | ||
z-index: 1011; | ||
font-weight: normal; | ||
max-width: 300px; | ||
} | ||
.umap-tooltip #umap-tooltip-container { | ||
display: block; | ||
} | ||
#umap-tooltip-container.tooltip-top:after { | ||
top: 100%; | ||
left: calc(50% - 11px); | ||
border: solid transparent; | ||
content: " "; | ||
height: 0; | ||
width: 0; | ||
position: absolute; | ||
pointer-events: none; | ||
border-top-color: rgba(30, 30, 30, 0.8); | ||
border-width: 11px; | ||
margin-left: calc(-50% + 21px); | ||
} | ||
#umap-tooltip-container.tooltip-bottom:before { | ||
top: -22px; | ||
left: calc(50% - 11px); | ||
border: solid transparent; | ||
content: " "; | ||
height: 0; | ||
width: 0; | ||
position: absolute; | ||
pointer-events: none; | ||
border-top-color: rgba(30, 30, 30, 0.7); | ||
border-width: 11px; | ||
transform: rotate(180deg); | ||
} | ||
#umap-tooltip-container.tooltip.tooltip-left:after { | ||
left: 100%; | ||
top: 50%; | ||
border: solid transparent; | ||
content: " "; | ||
height: 0; | ||
width: 0; | ||
position: absolute; | ||
pointer-events: none; | ||
border-color: rgba(136, 183, 213, 0); | ||
border-left-color: #333; | ||
border-width: 11px; | ||
margin-top: -10px; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { DomUtil, DomEvent } from '../../../vendors/leaflet/leaflet-src.esm.js' | ||
import { translate } from '../i18n.js' | ||
|
||
export default class Tooltip { | ||
constructor(map) { | ||
this.parent = map._controlContainer | ||
this.map = map | ||
this.container = DomUtil.create('div', 'with-transition', this.parent) | ||
this.container.id = 'umap-tooltip-container' | ||
DomEvent.disableClickPropagation(this.container) | ||
DomEvent.on(this.container, 'contextmenu', DomEvent.stopPropagation) // Do not activate our custom context menu. | ||
DomEvent.on(this.container, 'wheel', DomEvent.stopPropagation) | ||
DomEvent.on(this.container, 'MozMousePixelScroll', DomEvent.stopPropagation) | ||
} | ||
|
||
open(opts) { | ||
function showIt() { | ||
if (opts.anchor && opts.position === 'top') { | ||
this.anchorTop(opts.anchor) | ||
} else if (opts.anchor && opts.position === 'left') { | ||
this.anchorLeft(opts.anchor) | ||
} else if (opts.anchor && opts.position === 'bottom') { | ||
this.anchorBottom(opts.anchor) | ||
} else { | ||
this.anchorAbsolute() | ||
} | ||
L.DomUtil.addClass(this.parent, 'umap-tooltip') | ||
this.container.innerHTML = U.Utils.escapeHTML(opts.content) | ||
} | ||
this.TOOLTIP_ID = window.setTimeout(L.bind(showIt, this), opts.delay || 0) | ||
const id = this.TOOLTIP_ID | ||
const closeIt = () => { | ||
this.close(id) | ||
} | ||
if (opts.anchor) { | ||
L.DomEvent.once(opts.anchor, 'mouseout', closeIt) | ||
} | ||
if (opts.duration !== Infinity) { | ||
window.setTimeout(closeIt, opts.duration || 3000) | ||
} | ||
} | ||
|
||
anchorAbsolute() { | ||
this.container.className = '' | ||
const left = | ||
this.parent.offsetLeft + | ||
this.parent.clientWidth / 2 - | ||
this.container.clientWidth / 2, | ||
top = this.parent.offsetTop + 75 | ||
this.setPosition({ top: top, left: left }) | ||
} | ||
|
||
anchorTop(el) { | ||
this.container.className = 'tooltip-top' | ||
const coords = this.getPosition(el) | ||
this.setPosition({ | ||
left: coords.left - 10, | ||
bottom: this.getDocHeight() - coords.top + 11, | ||
}) | ||
} | ||
|
||
anchorBottom(el) { | ||
this.container.className = 'tooltip-bottom' | ||
const coords = this.getPosition(el) | ||
this.setPosition({ | ||
left: coords.left, | ||
top: coords.bottom + 11, | ||
}) | ||
} | ||
|
||
anchorLeft(el) { | ||
this.container.className = 'tooltip-left' | ||
const coords = this.getPosition(el) | ||
this.setPosition({ | ||
top: coords.top, | ||
right: document.documentElement.offsetWidth - coords.left + 11, | ||
}) | ||
} | ||
|
||
close(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 | ||
this.container.className = '' | ||
this.container.innerHTML = '' | ||
this.setPosition({}) | ||
L.DomUtil.removeClass(this.parent, 'umap-tooltip') | ||
} | ||
|
||
getPosition(el) { | ||
return el.getBoundingClientRect() | ||
} | ||
|
||
setPosition(coords) { | ||
if (coords.left) this.container.style.left = `${coords.left}px` | ||
else this.container.style.left = 'initial' | ||
if (coords.right) this.container.style.right = `${coords.right}px` | ||
else this.container.style.right = 'initial' | ||
if (coords.top) this.container.style.top = `${coords.top}px` | ||
else this.container.style.top = 'initial' | ||
if (coords.bottom) this.container.style.bottom = `${coords.bottom}px` | ||
else this.container.style.bottom = 'initial' | ||
} | ||
|
||
getDocHeight() { | ||
const D = document | ||
return Math.max( | ||
D.body.scrollHeight, | ||
D.documentElement.scrollHeight, | ||
D.body.offsetHeight, | ||
D.documentElement.offsetHeight, | ||
D.body.clientHeight, | ||
D.documentElement.clientHeight | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.