66f925850a14131bdea0db9eab155321087ad4c6b27710657ced1165904fec0cbbc107e1e6cc68b6920914dbc07c079762eb5bf02afc6249b3beb4c714ec33 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import {isNumeric} from './../../../helpers/number';
  2. const STATE_INITIALIZED = 0;
  3. const STATE_BUILT = 1;
  4. const STATE_APPENDED = 2;
  5. const UNIT = 'px';
  6. /**
  7. * @class
  8. * @private
  9. */
  10. class BaseUI {
  11. constructor(hotInstance) {
  12. /**
  13. * Instance of Handsontable.
  14. *
  15. * @type {Core}
  16. */
  17. this.hot = hotInstance;
  18. /**
  19. * DOM element representing the ui element.
  20. *
  21. * @type {HTMLElement}
  22. * @private
  23. */
  24. this._element = null;
  25. /**
  26. * Flag which determines build state of element.
  27. *
  28. * @type {Boolean}
  29. */
  30. this.state = STATE_INITIALIZED;
  31. }
  32. /**
  33. * Add created UI elements to table.
  34. *
  35. * @param {HTMLElement} wrapper Element which are parent for our UI element.
  36. */
  37. appendTo(wrapper) {
  38. wrapper.appendChild(this._element);
  39. this.state = STATE_APPENDED;
  40. }
  41. /**
  42. * Method for create UI element. Only create, without append to table.
  43. */
  44. build() {
  45. this._element = document.createElement('div');
  46. this.state = STATE_BUILT;
  47. }
  48. /**
  49. * Method for remove UI element.
  50. */
  51. destroy() {
  52. if (this.isAppended()) {
  53. this._element.parentElement.removeChild(this._element);
  54. }
  55. this._element = null;
  56. this.state = STATE_INITIALIZED;
  57. }
  58. /**
  59. * Check if UI element are appended.
  60. *
  61. * @returns {Boolean}
  62. */
  63. isAppended() {
  64. return this.state === STATE_APPENDED;
  65. }
  66. /**
  67. * Check if UI element are built.
  68. *
  69. * @returns {Boolean}
  70. */
  71. isBuilt() {
  72. return this.state >= STATE_BUILT;
  73. }
  74. /**
  75. * Setter for position.
  76. *
  77. * @param {Number} top New top position of the element.
  78. * @param {Number} left New left position of the element.
  79. */
  80. setPosition(top, left) {
  81. if (isNumeric(top)) {
  82. this._element.style.top = top + UNIT;
  83. }
  84. if (isNumeric(left)) {
  85. this._element.style.left = left + UNIT;
  86. }
  87. }
  88. /**
  89. * Getter for the element position.
  90. *
  91. * @returns {Object} Object contains left and top position of the element.
  92. */
  93. getPosition() {
  94. return {
  95. top: this._element.style.top ? parseInt(this._element.style.top, 10) : 0,
  96. left: this._element.style.left ? parseInt(this._element.style.left, 10) : 0
  97. };
  98. }
  99. /**
  100. * Setter for the element size.
  101. *
  102. * @param {Number} width New width of the element.
  103. * @param {Number} height New height of the element.
  104. */
  105. setSize(width, height) {
  106. if (isNumeric(width)) {
  107. this._element.style.width = width + UNIT;
  108. }
  109. if (isNumeric(height)) {
  110. this._element.style.height = height + UNIT;
  111. }
  112. }
  113. /**
  114. * Getter for the element position.
  115. *
  116. * @returns {Object} Object contains height and width of the element.
  117. */
  118. getSize() {
  119. return {
  120. width: this._element.style.width ? parseInt(this._element.style.width, 10) : 0,
  121. height: this._element.style.height ? parseInt(this._element.style.height, 10) : 0
  122. };
  123. }
  124. /**
  125. * Setter for the element offset. Offset means marginTop and marginLeft of the element.
  126. *
  127. * @param {Number} top New margin top of the element.
  128. * @param {Number} left New margin left of the element.
  129. */
  130. setOffset(top, left) {
  131. if (isNumeric(top)) {
  132. this._element.style.marginTop = top + UNIT;
  133. }
  134. if (isNumeric(left)) {
  135. this._element.style.marginLeft = left + UNIT;
  136. }
  137. }
  138. /**
  139. * Getter for the element offset.
  140. *
  141. * @returns {Object} Object contains top and left offset of the element.
  142. */
  143. getOffset() {
  144. return {
  145. top: this._element.style.marginTop ? parseInt(this._element.style.marginTop, 10) : 0,
  146. left: this._element.style.marginLeft ? parseInt(this._element.style.marginLeft, 10) : 0
  147. };
  148. }
  149. }
  150. export default BaseUI;