_base.js 5.1 KB

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