html-component.js 6.6 KB

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