|
|
|
@ -1012,6 +1012,120 @@ class PatternImage extends PatternElement<L.TB.PatternImageOptions> implements L |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const tooltipProto = L.Tooltip.prototype as any; |
|
|
|
|
|
|
|
class LabelTooltip extends L.Tooltip implements L.TB.LabelTooltip { |
|
|
|
|
|
|
|
private labelSize: L.Point; |
|
|
|
private labelDirection: string; |
|
|
|
private fontsReadyPending: boolean; |
|
|
|
private readonly onLabelLayoutChange = () => { |
|
|
|
this.labelSize = null; |
|
|
|
if (this._map) { |
|
|
|
tooltipProto._updatePosition.call(this); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
onAdd(map: L.Map): this { |
|
|
|
this.labelSize = null; |
|
|
|
this.labelDirection = null; |
|
|
|
super.onAdd(map); |
|
|
|
this.watchFonts(); |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
_updateContent() { |
|
|
|
tooltipProto._updateContent.call(this); |
|
|
|
this.labelSize = null; |
|
|
|
this.watchImages(); |
|
|
|
} |
|
|
|
|
|
|
|
_animateZoom(e: L.ZoomAnimEvent) { |
|
|
|
if (e.zoom === this._map.getZoom()) { |
|
|
|
tooltipProto._animateZoom.call(this, e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
_setPosition(pos: L.Point) { |
|
|
|
const container = this.getElement(); |
|
|
|
const size = this.getLabelSize(container); |
|
|
|
const offset = L.point(this.options.offset as L.PointTuple); |
|
|
|
const anchor: L.Point = tooltipProto._getAnchor.call(this); |
|
|
|
let direction = this.options.direction; |
|
|
|
let subX: number; |
|
|
|
let subY: number; |
|
|
|
if (direction === 'top') { |
|
|
|
subX = size.x / 2; |
|
|
|
subY = size.y; |
|
|
|
} else if (direction === 'bottom') { |
|
|
|
subX = size.x / 2; |
|
|
|
subY = 0; |
|
|
|
} else if (direction === 'center') { |
|
|
|
subX = size.x / 2; |
|
|
|
subY = size.y / 2; |
|
|
|
} else if (direction === 'right') { |
|
|
|
subX = 0; |
|
|
|
subY = size.y / 2; |
|
|
|
} else if (direction === 'left') { |
|
|
|
subX = size.x; |
|
|
|
subY = size.y / 2; |
|
|
|
} else if (this._map.layerPointToContainerPoint(pos).x < this._map.latLngToContainerPoint(this._map.getCenter()).x) { |
|
|
|
direction = 'right'; |
|
|
|
subX = 0; |
|
|
|
subY = size.y / 2; |
|
|
|
} else { |
|
|
|
direction = 'left'; |
|
|
|
subX = size.x + (offset.x + anchor.x) * 2; |
|
|
|
subY = size.y / 2; |
|
|
|
} |
|
|
|
pos = pos.subtract(L.point(subX, subY, true)).add(offset).add(anchor); |
|
|
|
if (this.labelDirection !== direction) { |
|
|
|
L.DomUtil.removeClass(container, 'leaflet-tooltip-right'); |
|
|
|
L.DomUtil.removeClass(container, 'leaflet-tooltip-left'); |
|
|
|
L.DomUtil.removeClass(container, 'leaflet-tooltip-top'); |
|
|
|
L.DomUtil.removeClass(container, 'leaflet-tooltip-bottom'); |
|
|
|
L.DomUtil.addClass(container, 'leaflet-tooltip-' + direction); |
|
|
|
this.labelDirection = direction; |
|
|
|
} |
|
|
|
L.DomUtil.setPosition(container, pos); |
|
|
|
} |
|
|
|
|
|
|
|
private getLabelSize(container: HTMLElement): L.Point { |
|
|
|
if (this.labelSize) { |
|
|
|
return this.labelSize; |
|
|
|
} |
|
|
|
const size = L.point(container.offsetWidth, container.offsetHeight); |
|
|
|
if (size.x > 0 && size.y > 0) { |
|
|
|
this.labelSize = size; |
|
|
|
} |
|
|
|
return size; |
|
|
|
} |
|
|
|
|
|
|
|
private watchImages() { |
|
|
|
this.getElement()?.querySelectorAll('img').forEach((img) => { |
|
|
|
if (!img.complete) { |
|
|
|
img.addEventListener('load', this.onLabelLayoutChange, {once: true}); |
|
|
|
img.addEventListener('error', this.onLabelLayoutChange, {once: true}); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private watchFonts() { |
|
|
|
const fonts = document.fonts; |
|
|
|
if (fonts && fonts.status !== 'loaded' && !this.fontsReadyPending) { |
|
|
|
this.fontsReadyPending = true; |
|
|
|
fonts.ready.then(() => { |
|
|
|
this.fontsReadyPending = false; |
|
|
|
this.onLabelLayoutChange(); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const labelTooltip = (content: L.Content, options?: L.TooltipOptions): L.TB.LabelTooltip => { |
|
|
|
return new LabelTooltip(options).setContent(content); |
|
|
|
} |
|
|
|
|
|
|
|
const sidebar = (options: TB.SidebarControlOptions): L.TB.SidebarControl => { |
|
|
|
return new SidebarControl(options); |
|
|
|
} |
|
|
|
@ -1478,6 +1592,8 @@ L.TB = L.TB || { |
|
|
|
PatternRect, |
|
|
|
PatternPath, |
|
|
|
PatternImage, |
|
|
|
LabelTooltip, |
|
|
|
labelTooltip, |
|
|
|
sidebar, |
|
|
|
sidebarPane, |
|
|
|
layers, |
|
|
|
|