HoverInfo.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /// <reference path="../Elements.ts" />
  2. module Elements.HoverInfo {
  3. var hoverbox = document.getElementById("hoverInfo");
  4. var contentTarget = document.getElementById("hoverContent");
  5. var width = 0;
  6. var height = 0;
  7. function empty () {
  8. while (contentTarget.firstChild != undefined) {
  9. contentTarget.removeChild(contentTarget.firstChild);
  10. }
  11. }
  12. export function makeHoverable (element : Element, contents : Array<Element | Text>) {
  13. element.addEventListener("mouseenter", (e : MouseEvent) => {
  14. Elements.HoverInfo.hoverStart(e, contents);
  15. });
  16. element.addEventListener("mousemove", (e : MouseEvent) => {
  17. Elements.HoverInfo.hoverMove(e);
  18. });
  19. element.addEventListener("mouseout", (e : MouseEvent) => {
  20. Elements.HoverInfo.hoverEnd();
  21. });
  22. }
  23. export function hoverStart (e : MouseEvent, contents : Array<Element | Text>) {
  24. empty();
  25. for (let i = 0; i < contents.length; i++) {
  26. contentTarget.appendChild(contents[i]);
  27. }
  28. hoverbox.style.display = "block";
  29. hoverbox.style.left = "0px";
  30. hoverbox.style.top = "0px";
  31. width = hoverbox.offsetWidth;
  32. height = hoverbox.offsetHeight;
  33. hoverMove(e);
  34. }
  35. export function hoverMove (e : MouseEvent) {
  36. let left = e.clientX - (width/2);
  37. let top = e.clientY - (height) - 6;
  38. if (top < 0) {
  39. top = e.clientY + 6;
  40. }
  41. if (left < width) {
  42. left = width;
  43. }
  44. // Width seems to not be considering the border, even though we applied border-box
  45. // I don't care enough to figure this out so there's a margin on the side!
  46. if (left + width > Elements.screenWidth) {
  47. left = Elements.screenWidth - width;
  48. }
  49. hoverbox.style.left = Math.round(left) + "px";
  50. hoverbox.style.top = Math.round(top) + "px";
  51. }
  52. export function hoverEnd () {
  53. hoverbox.style.display = "none";
  54. }
  55. }