component.js 4.9 KB

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