cursor.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. import { getWindowScrollLeft, getWindowScrollTop } from './../../helpers/dom/element';
  4. import { pageX, pageY } from './../../helpers/dom/event';
  5. /**
  6. * Helper class for checking if element will fit at the desired side of cursor.
  7. *
  8. * @class Cursor
  9. * @plugin ContextMenu
  10. */
  11. var Cursor = function () {
  12. function Cursor(object) {
  13. _classCallCheck(this, Cursor);
  14. var windowScrollTop = getWindowScrollTop();
  15. var windowScrollLeft = getWindowScrollLeft();
  16. var top = void 0,
  17. topRelative = void 0;
  18. var left = void 0,
  19. leftRelative = void 0;
  20. var cellHeight = void 0,
  21. cellWidth = void 0;
  22. this.type = this.getSourceType(object);
  23. if (this.type === 'literal') {
  24. top = parseInt(object.top, 10);
  25. left = parseInt(object.left, 10);
  26. cellHeight = object.height || 0;
  27. cellWidth = object.width || 0;
  28. topRelative = top;
  29. leftRelative = left;
  30. top += windowScrollTop;
  31. left += windowScrollLeft;
  32. } else if (this.type === 'event') {
  33. top = parseInt(pageY(object), 10);
  34. left = parseInt(pageX(object), 10);
  35. cellHeight = object.target.clientHeight;
  36. cellWidth = object.target.clientWidth;
  37. topRelative = top - windowScrollTop;
  38. leftRelative = left - windowScrollLeft;
  39. }
  40. this.top = top;
  41. this.topRelative = topRelative;
  42. this.left = left;
  43. this.leftRelative = leftRelative;
  44. this.scrollTop = windowScrollTop;
  45. this.scrollLeft = windowScrollLeft;
  46. this.cellHeight = cellHeight;
  47. this.cellWidth = cellWidth;
  48. }
  49. /**
  50. * Get source type name.
  51. *
  52. * @param {*} object Event or Object with coordinates.
  53. * @returns {String} Returns one of this values: `'literal'`, `'event'`.
  54. */
  55. _createClass(Cursor, [{
  56. key: 'getSourceType',
  57. value: function getSourceType(object) {
  58. var type = 'literal';
  59. if (object instanceof Event) {
  60. type = 'event';
  61. }
  62. return type;
  63. }
  64. /**
  65. * Checks if element can be placed above the cursor.
  66. *
  67. * @param {HTMLElement} element Element to check if it's size will fit above the cursor.
  68. * @returns {Boolean}
  69. */
  70. }, {
  71. key: 'fitsAbove',
  72. value: function fitsAbove(element) {
  73. return this.topRelative >= element.offsetHeight;
  74. }
  75. /**
  76. * Checks if element can be placed below the cursor.
  77. *
  78. * @param {HTMLElement} element Element to check if it's size will fit below the cursor.
  79. * @param {Number} [viewportHeight] The viewport height.
  80. * @returns {Boolean}
  81. */
  82. }, {
  83. key: 'fitsBelow',
  84. value: function fitsBelow(element) {
  85. var viewportHeight = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window.innerHeight;
  86. return this.topRelative + element.offsetHeight <= viewportHeight;
  87. }
  88. /**
  89. * Checks if element can be placed on the right of the cursor.
  90. *
  91. * @param {HTMLElement} element Element to check if it's size will fit on the right of the cursor.
  92. * @param {Number} [viewportWidth] The viewport width.
  93. * @returns {Boolean}
  94. */
  95. }, {
  96. key: 'fitsOnRight',
  97. value: function fitsOnRight(element) {
  98. var viewportWidth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window.innerWidth;
  99. return this.leftRelative + this.cellWidth + element.offsetWidth <= viewportWidth;
  100. }
  101. /**
  102. * Checks if element can be placed on the left on the cursor.
  103. *
  104. * @param {HTMLElement} element Element to check if it's size will fit on the left of the cursor.
  105. * @returns {Boolean}
  106. */
  107. }, {
  108. key: 'fitsOnLeft',
  109. value: function fitsOnLeft(element) {
  110. return this.leftRelative >= element.offsetWidth;
  111. }
  112. }]);
  113. return Cursor;
  114. }();
  115. export default Cursor;