component.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { __assign, __extends } from "tslib";
  2. import { Base } from '@antv/g-base';
  3. import { deepMix, each, hasKey, isObject } from '@antv/util';
  4. var LOCATION_FIELD_MAP = {
  5. none: [],
  6. point: ['x', 'y'],
  7. region: ['start', 'end'],
  8. points: ['points'],
  9. circle: ['center', 'radius', 'startAngle', 'endAngle'],
  10. };
  11. var Component = /** @class */ (function (_super) {
  12. __extends(Component, _super);
  13. function Component(cfg) {
  14. var _this = _super.call(this, cfg) || this;
  15. _this.initCfg();
  16. return _this;
  17. }
  18. /**
  19. * @protected
  20. * 默认的配置项
  21. * @returns {object} 默认的配置项
  22. */
  23. Component.prototype.getDefaultCfg = function () {
  24. return {
  25. id: '',
  26. name: '',
  27. type: '',
  28. locationType: 'none',
  29. offsetX: 0,
  30. offsetY: 0,
  31. animate: false,
  32. capture: true,
  33. updateAutoRender: false,
  34. animateOption: {
  35. appear: null,
  36. update: {
  37. duration: 400,
  38. easing: 'easeQuadInOut',
  39. },
  40. enter: {
  41. duration: 400,
  42. easing: 'easeQuadInOut',
  43. },
  44. leave: {
  45. duration: 350,
  46. easing: 'easeQuadIn',
  47. },
  48. },
  49. events: null,
  50. defaultCfg: {},
  51. visible: true,
  52. };
  53. };
  54. /**
  55. * 清理组件的内容,一般配合 render 使用
  56. * @example
  57. * axis.clear();
  58. * axis.render();
  59. */
  60. Component.prototype.clear = function () { };
  61. /**
  62. * 更新组件
  63. * @param {object} cfg 更新属性
  64. */
  65. Component.prototype.update = function (cfg) {
  66. var _this = this;
  67. var defaultCfg = this.get('defaultCfg') || {};
  68. each(cfg, function (value, name) {
  69. var originCfg = _this.get(name);
  70. var newCfg = value;
  71. if (originCfg !== value) {
  72. // 判断两者是否相等,主要是进行 null 的判定
  73. if (isObject(value) && defaultCfg[name]) {
  74. // 新设置的属性与默认值进行合并
  75. newCfg = deepMix({}, defaultCfg[name], value);
  76. }
  77. _this.set(name, newCfg);
  78. }
  79. });
  80. this.updateInner(cfg);
  81. this.afterUpdate(cfg);
  82. };
  83. // 更新内部
  84. Component.prototype.updateInner = function (cfg) {
  85. };
  86. Component.prototype.afterUpdate = function (cfg) {
  87. // 更新时考虑显示、隐藏
  88. if (hasKey(cfg, 'visible')) {
  89. if (cfg.visible) {
  90. this.show();
  91. }
  92. else {
  93. this.hide();
  94. }
  95. }
  96. // 更新时考虑capture
  97. if (hasKey(cfg, 'capture')) {
  98. this.setCapture(cfg.capture);
  99. }
  100. };
  101. Component.prototype.getLayoutBBox = function () {
  102. return this.getBBox(); // 默认返回 getBBox,不同的组件内部单独实现
  103. };
  104. Component.prototype.getLocationType = function () {
  105. return this.get('locationType');
  106. };
  107. Component.prototype.getOffset = function () {
  108. return {
  109. offsetX: this.get('offsetX'),
  110. offsetY: this.get('offsetY'),
  111. };
  112. };
  113. // 默认使用 update
  114. Component.prototype.setOffset = function (offsetX, offsetY) {
  115. this.update({
  116. offsetX: offsetX,
  117. offsetY: offsetY,
  118. });
  119. };
  120. Component.prototype.setLocation = function (cfg) {
  121. var location = __assign({}, cfg);
  122. this.update(location);
  123. };
  124. // 实现 ILocation 接口的 getLocation
  125. Component.prototype.getLocation = function () {
  126. var _this = this;
  127. var location = {};
  128. var locationType = this.get('locationType');
  129. var fields = LOCATION_FIELD_MAP[locationType];
  130. each(fields, function (field) {
  131. location[field] = _this.get(field);
  132. });
  133. return location;
  134. };
  135. Component.prototype.isList = function () {
  136. return false;
  137. };
  138. Component.prototype.isSlider = function () {
  139. return false;
  140. };
  141. /**
  142. * @protected
  143. * 初始化,用于具体的组件继承
  144. */
  145. Component.prototype.init = function () { };
  146. // 将组件默认的配置项设置合并到传入的配置项
  147. Component.prototype.initCfg = function () {
  148. var _this = this;
  149. var defaultCfg = this.get('defaultCfg');
  150. each(defaultCfg, function (value, name) {
  151. var cfg = _this.get(name);
  152. if (isObject(cfg)) {
  153. var newCfg = deepMix({}, value, cfg);
  154. _this.set(name, newCfg);
  155. }
  156. });
  157. };
  158. return Component;
  159. }(Base));
  160. export default Component;
  161. //# sourceMappingURL=component.js.map