export class MapTooltip { constructor(viewer, options = {}) { this.viewer = viewer this.options = options this.container = viewer?.container || null this.tooltipEl = null this.isVisible = false this.handleMouseLeave = this.handleMouseLeave.bind(this) this.init() } init() { if (!this.container || this.tooltipEl) return const el = document.createElement('div') el.style.position = 'absolute' el.style.pointerEvents = 'none' el.style.padding = '6px 8px' el.style.background = 'rgba(0, 0, 0, 0.65)' el.style.color = '#fff' el.style.fontSize = '12px' el.style.borderRadius = '4px' el.style.whiteSpace = 'nowrap' el.style.zIndex = '1000' el.style.display = 'none' this.container.appendChild(el) this.tooltipEl = el this.container.addEventListener('mouseleave', this.handleMouseLeave) this.container.addEventListener('pointerleave', this.handleMouseLeave) } show(text, position) { if (!this.tooltipEl) return this.tooltipEl.textContent = text || '' this.tooltipEl.style.display = 'block' this.isVisible = true if (position) this.move(position) } move(position) { if (!this.tooltipEl || !position) return if (!this.isVisible) { this.tooltipEl.style.display = 'block' this.isVisible = true } const width = this.container?.clientWidth ?? 0 const height = this.container?.clientHeight ?? 0 if (!width || !height || position.x < 0 || position.y < 0 || position.x > width || position.y > height) { this.hide() return } const offset = this.options.offset || { x: 12, y: 12 } this.tooltipEl.style.left = `${position.x + offset.x}px` this.tooltipEl.style.top = `${position.y + offset.y}px` } hide() { if (!this.tooltipEl) return this.tooltipEl.style.display = 'none' this.isVisible = false } handleMouseLeave() { this.hide() } destroy() { this.hide() if (this.container) { this.container.removeEventListener('mouseleave', this.handleMouseLeave) this.container.removeEventListener('pointerleave', this.handleMouseLeave) } if (this.tooltipEl && this.container) { this.container.removeChild(this.tooltipEl) } this.tooltipEl = null this.container = null this.viewer = null } }