Guest User

Untitled

a guest
Apr 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. export function getCoords(elem: any) {
  2. var box = elem.getBoundingClientRect();
  3. return {
  4. top: box.top + window.pageYOffset,
  5. left: box.left + window.pageXOffset,
  6. right: box.right + window.pageXOffset,
  7. bottom: box.bottom + window.pageYOffset,
  8. width: box.width,
  9. height: box.height
  10. };
  11. }
  12.  
  13. export function getWidthHeightDocs() {
  14. return {
  15. height: document.documentElement.clientHeight,
  16. width: document.documentElement.clientWidth
  17. };
  18. }
  19.  
  20. export const createPopoverCoordsFromEvent = (target, popupParams: PopupParams, targetCoords?): Coords => {
  21. targetCoords = targetCoords || getCoords(target);
  22.  
  23. const coords = Object.assign({}, targetCoords, {
  24. top: targetCoords.top + targetCoords.height / 2
  25. });
  26.  
  27. const documentSizes = getWidthHeightDocs();
  28. const restOfHeight = documentSizes.height - coords.top;
  29. const restOfWidth = documentSizes.width - coords.right;
  30. const { width, height, topOffset, bottomOffset } = popupParams;
  31. const leftOffset = 35;
  32. const rightOffset = 30;
  33. let result: Coords = {};
  34.  
  35. if (restOfHeight > height + topOffset) {
  36. result = Object.assign({}, result, {
  37. top: Math.floor(coords.top) - (topOffset * -1) + 'px',
  38. trgBottom: false
  39. });
  40. } else {
  41. let botOffset = restOfHeight < bottomOffset * -1 ? restOfHeight : bottomOffset * -1;
  42. let topPosition = Math.floor(coords.top) - height + botOffset;
  43. topPosition = topPosition < 0 ? 0 : topPosition;
  44. result = Object.assign({}, result, {
  45. top: topPosition + 'px',
  46. trgBottom: true
  47. });
  48. }
  49.  
  50. if (restOfWidth > width + leftOffset) {
  51. result = Object.assign({}, result, {
  52. left: Math.floor(coords.right) + leftOffset + 'px',
  53. trgRight: false
  54. });
  55. } else {
  56. result = Object.assign({}, result, {
  57. left: Math.floor(coords.left) - width - rightOffset + 'px',
  58. trgRight: true
  59. });
  60. }
  61.  
  62. return result;
  63. };
Add Comment
Please, Sign In to add comment