_base.js 5.0 KB

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