2b457e678fc5f5884f0530fa461c056161c569755fd5a156150277dbaa22d18e6ebae330684962a3e574c341f2e37f794789423537c1b39137cbbb01fdee9f 3.5 KB

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