d1534113a015e0c0887a1e08ea6bf829558fe6eaa4354da7edf810d829234d0f572d30b22b426ae903441b72d2a4b0646cf2d4254405998a1f29378fb84546 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import {getWindowScrollLeft, getWindowScrollTop} from './../../helpers/dom/element';
  2. import {pageX, pageY} from './../../helpers/dom/event';
  3. /**
  4. * Helper class for checking if element will fit at the desired side of cursor.
  5. *
  6. * @class Cursor
  7. * @plugin ContextMenu
  8. */
  9. class Cursor {
  10. constructor(object) {
  11. let windowScrollTop = getWindowScrollTop();
  12. let windowScrollLeft = getWindowScrollLeft();
  13. let top,
  14. topRelative;
  15. let left,
  16. leftRelative;
  17. let cellHeight,
  18. cellWidth;
  19. this.type = this.getSourceType(object);
  20. if (this.type === 'literal') {
  21. top = parseInt(object.top, 10);
  22. left = parseInt(object.left, 10);
  23. cellHeight = object.height || 0;
  24. cellWidth = object.width || 0;
  25. topRelative = top;
  26. leftRelative = left;
  27. top += windowScrollTop;
  28. left += windowScrollLeft;
  29. } else if (this.type === 'event') {
  30. top = parseInt(pageY(object), 10);
  31. left = parseInt(pageX(object), 10);
  32. cellHeight = object.target.clientHeight;
  33. cellWidth = object.target.clientWidth;
  34. topRelative = top - windowScrollTop;
  35. leftRelative = left - windowScrollLeft;
  36. }
  37. this.top = top;
  38. this.topRelative = topRelative;
  39. this.left = left;
  40. this.leftRelative = leftRelative;
  41. this.scrollTop = windowScrollTop;
  42. this.scrollLeft = windowScrollLeft;
  43. this.cellHeight = cellHeight;
  44. this.cellWidth = cellWidth;
  45. }
  46. /**
  47. * Get source type name.
  48. *
  49. * @param {*} object Event or Object with coordinates.
  50. * @returns {String} Returns one of this values: `'literal'`, `'event'`.
  51. */
  52. getSourceType(object) {
  53. let type = 'literal';
  54. if (object instanceof Event) {
  55. type = 'event';
  56. }
  57. return type;
  58. }
  59. /**
  60. * Checks if element can be placed above the cursor.
  61. *
  62. * @param {HTMLElement} element Element to check if it's size will fit above the cursor.
  63. * @returns {Boolean}
  64. */
  65. fitsAbove(element) {
  66. return this.topRelative >= element.offsetHeight;
  67. }
  68. /**
  69. * Checks if element can be placed below the cursor.
  70. *
  71. * @param {HTMLElement} element Element to check if it's size will fit below the cursor.
  72. * @param {Number} [viewportHeight] The viewport height.
  73. * @returns {Boolean}
  74. */
  75. fitsBelow(element, viewportHeight = window.innerHeight) {
  76. return this.topRelative + element.offsetHeight <= viewportHeight;
  77. }
  78. /**
  79. * Checks if element can be placed on the right of the cursor.
  80. *
  81. * @param {HTMLElement} element Element to check if it's size will fit on the right of the cursor.
  82. * @param {Number} [viewportWidth] The viewport width.
  83. * @returns {Boolean}
  84. */
  85. fitsOnRight(element, viewportWidth = window.innerWidth) {
  86. return this.leftRelative + this.cellWidth + element.offsetWidth <= viewportWidth;
  87. }
  88. /**
  89. * Checks if element can be placed on the left on the cursor.
  90. *
  91. * @param {HTMLElement} element Element to check if it's size will fit on the left of the cursor.
  92. * @returns {Boolean}
  93. */
  94. fitsOnLeft(element) {
  95. return this.leftRelative >= element.offsetWidth;
  96. }
  97. }
  98. export default Cursor;