_base.js 5.1 KB

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