c900343345b84e80e6a4eeb5c4d63998510356477242d6c12906db805b87080e22117280f3aa5540200a379076d879f73326f6366e1b0b03a48a2553780ca8 5.2 KB

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