diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/maps/data-layer/circles-data-layer.ts b/ui-ngx/src/app/modules/home/components/widget/lib/maps/data-layer/circles-data-layer.ts index 95c845b164..316f7b2486 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/maps/data-layer/circles-data-layer.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/maps/data-layer/circles-data-layer.ts @@ -76,7 +76,7 @@ class TbCircleDataLayerItem extends TbLatestDataLayerItem, dsData: FormattedData[]) { diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/maps/data-layer/polygons-data-layer.ts b/ui-ngx/src/app/modules/home/components/widget/lib/maps/data-layer/polygons-data-layer.ts index 0942e0dcdb..f52f2a506a 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/maps/data-layer/polygons-data-layer.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/maps/data-layer/polygons-data-layer.ts @@ -81,7 +81,7 @@ class TbPolygonDataLayerItem extends TbLatestDataLayerItem 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, diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/maps/map.scss b/ui-ngx/src/app/modules/home/components/widget/lib/maps/map.scss index dfad7c41b0..09c412fe5e 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/maps/map.scss +++ b/ui-ngx/src/app/modules/home/components/widget/lib/maps/map.scss @@ -208,6 +208,9 @@ div.tb-widget .tb-widget-content.tb-no-interaction { } } } + .leaflet-map-pane.leaflet-zoom-anim .leaflet-tooltip-pane { + visibility: hidden; + } .leaflet-map-pane:not(.leaflet-zoom-anim) { .leaflet-marker-icon { &.tb-hoverable:not(.tb-selected) { diff --git a/ui-ngx/src/typings/leaflet-extend-tb.d.ts b/ui-ngx/src/typings/leaflet-extend-tb.d.ts index afc264c17d..620a274725 100644 --- a/ui-ngx/src/typings/leaflet-extend-tb.d.ts +++ b/ui-ngx/src/typings/leaflet-extend-tb.d.ts @@ -265,6 +265,12 @@ declare module 'leaflet' { setStyle(style: PatternImageOptions): this; } + class LabelTooltip extends Tooltip { + constructor(options?: TooltipOptions, source?: Layer); + } + + function labelTooltip(content: Content, options?: TooltipOptions): LabelTooltip; + function sidebar(options: SidebarControlOptions): SidebarControl; function sidebarPane(options: O): SidebarPaneControl;