view src/tooltip.js @ 2728:4e2f9e5b0579

Change mapGetWindowSize() to mapGetElementSize(elem) and adjust users accordingly.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 07 Mar 2024 11:07:24 +0200
parents 5e6efb5a6b84
children
line wrap: on
line source

//
// Tooltip functions in JavaScript for BatMUD maps
// Programmed by Matti 'ccr' Hamalainen <ccr@tnsp.org>
// (C) Copyright 2006-2024 Tecnic Software productions (TNSP)
//
var mapTipPrev = null;
var mapTipItem = null;
var mapTipXC = 0, mapTipYC = 0;


function mapGetElementSize(elem)
{
  var elemW, elemH;
  if (typeof(elem.innerWidth) == 'number')
  {
    elemW = elem.innerWidth;
    elemH = elem.innerHeight;
  }
  else
  if (elem.clientWidth || elem.clientHeight)
  {
    elemW = elem.clientWidth;
    elemH = elem.clientHeight;
  }
  else
  {
    elemW = elemH = 0;
  }

  return { w: elemW, h: elemH };
}


// Update the tooltip box
function mapTooltipUpdate(ev)
{
  var x = document.all ? (window.event.x + document.body.scrollLeft) : ev.pageX;
  var y = document.all ? (window.event.y + document.body.scrollTop)  : ev.pageY;
  if (mapTipItem != null)
  {
    const dim = mapGetElementSize(window);
    var boxW = mapTipItem.clientWidth + 25, boxH = mapTipItem.clientHeight + 25;

    if (x + boxW + 15 >= dim.w)
      x -= boxW;
    else
      x += 15;

    if (y + boxH + 15 >= dim.h)
      y -= boxH;
    else
      y += 15;

    mapTipXC = x;
    mapTipYC = y;

    mapTipItem.style.left = x + "px";
    mapTipItem.style.top  = y + "px";
  }
}


// Show tooltip
function stt(id)
{
  htt();

  mapTipItem = document.getElementById("tt"+ id);

  mapTipItem.style.left    = mapTipXC + "px";
  mapTipItem.style.top     = mapTipYC + "px";
  mapTipItem.style.display = "block";
}


// Hide tooltip
function htt()
{
  if (mapTipItem)
  {
    mapTipItem.style.display = "none";
  }
}


function mapOnLoad()
{
  document.addEventListener("mousemove", mapTooltipUpdate);
}