html-component.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { __assign, __extends } from "tslib";
  2. import { createDom, modifyCSS } from '@antv/dom-util';
  3. import { isNil, isString, deepMix, each, hasKey } from '@antv/util';
  4. import { clearDom, createBBox, hasClass } from '../util/util';
  5. import Component from './component';
  6. var HtmlComponent = /** @class */ (function (_super) {
  7. __extends(HtmlComponent, _super);
  8. function HtmlComponent() {
  9. return _super !== null && _super.apply(this, arguments) || this;
  10. }
  11. HtmlComponent.prototype.getDefaultCfg = function () {
  12. var cfg = _super.prototype.getDefaultCfg.call(this);
  13. return __assign(__assign({}, cfg), { container: null, containerTpl: '<div></div>', updateAutoRender: true, containerClassName: '', parent: null });
  14. };
  15. HtmlComponent.prototype.getContainer = function () {
  16. return this.get('container');
  17. };
  18. /**
  19. * 显示组件
  20. */
  21. HtmlComponent.prototype.show = function () {
  22. var container = this.get('container');
  23. container.style.display = '';
  24. this.set('visible', true);
  25. };
  26. /**
  27. * 隐藏组件
  28. */
  29. HtmlComponent.prototype.hide = function () {
  30. var container = this.get('container');
  31. container.style.display = 'none';
  32. this.set('visible', false);
  33. };
  34. /**
  35. * 是否允许捕捉事件
  36. * @param capture 事件捕捉
  37. */
  38. HtmlComponent.prototype.setCapture = function (capture) {
  39. var container = this.getContainer();
  40. var value = capture ? 'auto' : 'none';
  41. container.style.pointerEvents = value;
  42. this.set('capture', capture);
  43. };
  44. HtmlComponent.prototype.getBBox = function () {
  45. var container = this.getContainer();
  46. var x = parseFloat(container.style.left) || 0;
  47. var y = parseFloat(container.style.top) || 0;
  48. return createBBox(x, y, container.clientWidth, container.clientHeight);
  49. };
  50. HtmlComponent.prototype.clear = function () {
  51. var container = this.get('container');
  52. clearDom(container);
  53. };
  54. HtmlComponent.prototype.destroy = function () {
  55. this.removeEvent();
  56. this.removeDom();
  57. _super.prototype.destroy.call(this);
  58. };
  59. /**
  60. * 复写 init,主要是初始化 DOM 和事件
  61. */
  62. HtmlComponent.prototype.init = function () {
  63. _super.prototype.init.call(this);
  64. this.initContainer();
  65. this.initDom();
  66. this.resetStyles(); // 初始化样式
  67. this.applyStyles(); // 应用样式
  68. this.initEvent();
  69. this.initCapture();
  70. this.initVisible();
  71. };
  72. HtmlComponent.prototype.initCapture = function () {
  73. this.setCapture(this.get('capture'));
  74. };
  75. HtmlComponent.prototype.initVisible = function () {
  76. if (!this.get('visible')) {
  77. // 设置初始显示状态
  78. this.hide();
  79. }
  80. else {
  81. this.show();
  82. }
  83. };
  84. HtmlComponent.prototype.initDom = function () {
  85. };
  86. HtmlComponent.prototype.initContainer = function () {
  87. var container = this.get('container');
  88. if (isNil(container)) {
  89. // 未指定 container 则创建
  90. container = this.createDom();
  91. var parent_1 = this.get('parent');
  92. if (isString(parent_1)) {
  93. parent_1 = document.getElementById(parent_1);
  94. this.set('parent', parent_1);
  95. }
  96. parent_1.appendChild(container);
  97. if (this.get('containerId')) {
  98. container.setAttribute('id', this.get('containerId'));
  99. }
  100. this.set('container', container);
  101. }
  102. else if (isString(container)) {
  103. // 用户传入的 id, 作为 container
  104. container = document.getElementById(container);
  105. this.set('container', container);
  106. } // else container 是 DOM
  107. if (!this.get('parent')) {
  108. this.set('parent', container.parentNode);
  109. }
  110. };
  111. // 样式需要进行合并,不能单纯的替换,否则使用非常不方便
  112. HtmlComponent.prototype.resetStyles = function () {
  113. var style = this.get('domStyles');
  114. var defaultStyles = this.get('defaultStyles');
  115. if (!style) {
  116. style = defaultStyles;
  117. }
  118. else {
  119. style = deepMix({}, defaultStyles, style);
  120. }
  121. this.set('domStyles', style);
  122. };
  123. // 应用所有的样式
  124. HtmlComponent.prototype.applyStyles = function () {
  125. var domStyles = this.get('domStyles');
  126. if (!domStyles) {
  127. return;
  128. }
  129. var container = this.getContainer();
  130. this.applyChildrenStyles(container, domStyles);
  131. var containerClassName = this.get('containerClassName');
  132. if (containerClassName && hasClass(container, containerClassName)) {
  133. var containerCss = domStyles[containerClassName];
  134. modifyCSS(container, containerCss);
  135. }
  136. };
  137. HtmlComponent.prototype.applyChildrenStyles = function (element, styles) {
  138. each(styles, function (style, name) {
  139. var elements = element.getElementsByClassName(name);
  140. each(elements, function (el) {
  141. modifyCSS(el, style);
  142. });
  143. });
  144. };
  145. // 应用到单个 DOM
  146. HtmlComponent.prototype.applyStyle = function (cssName, dom) {
  147. var domStyles = this.get('domStyles');
  148. modifyCSS(dom, domStyles[cssName]);
  149. };
  150. /**
  151. * @protected
  152. */
  153. HtmlComponent.prototype.createDom = function () {
  154. var containerTpl = this.get('containerTpl');
  155. return createDom(containerTpl);
  156. };
  157. /**
  158. * @protected
  159. * 初始化事件
  160. */
  161. HtmlComponent.prototype.initEvent = function () { };
  162. /**
  163. * @protected
  164. * 清理 DOM
  165. */
  166. HtmlComponent.prototype.removeDom = function () {
  167. var container = this.get('container');
  168. // 节点不一定有parentNode
  169. container && container.parentNode && container.parentNode.removeChild(container);
  170. };
  171. /**
  172. * @protected
  173. * 清理事件
  174. */
  175. HtmlComponent.prototype.removeEvent = function () { };
  176. HtmlComponent.prototype.updateInner = function (cfg) {
  177. // 更新样式
  178. if (hasKey(cfg, 'domStyles')) {
  179. this.resetStyles();
  180. this.applyStyles();
  181. }
  182. // 只要属性发生变化,都调整一些位置
  183. this.resetPosition();
  184. };
  185. HtmlComponent.prototype.resetPosition = function () { };
  186. ;
  187. return HtmlComponent;
  188. }(Component));
  189. export default HtmlComponent;
  190. //# sourceMappingURL=html-component.js.map