cursor.js 4.5 KB

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