UNPKG

5.63 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 };
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15var __importDefault = (this && this.__importDefault) || function (mod) {
16 return (mod && mod.__esModule) ? mod : { "default": mod };
17};
18Object.defineProperty(exports, "__esModule", { value: true });
19var react_1 = __importDefault(require("react"));
20var is_string_1 = __importDefault(require("@antv/util/lib/is-string"));
21var is_function_1 = __importDefault(require("@antv/util/lib/is-function"));
22var base_1 = __importDefault(require("@antv/g2/lib/geometry/label/base"));
23var view_1 = __importDefault(require("../context/view"));
24var core_1 = require("../core");
25var compareProps_1 = __importDefault(require("../utils/compareProps"));
26var warning_1 = __importDefault(require("warning"));
27require("./Label");
28// 交互事件
29require("./actions");
30var cloneDeep_1 = __importDefault(require("../utils/cloneDeep"));
31core_1.registerGeometryLabel('base', base_1.default);
32var DEFAULT_SORT_GEOMETRYS = ['line', 'area'];
33var GeomHelper = /** @class */ (function () {
34 function GeomHelper() {
35 this.config = {};
36 }
37 GeomHelper.prototype.setView = function (view) {
38 this.view = view;
39 this.rootChart = view.rootChart || view; // 顶层chart实例
40 };
41 GeomHelper.prototype.createGeomInstance = function (GemoBaseClassName, cfg) {
42 this.geom = this.view[GemoBaseClassName](cfg);
43 var sortable = cfg.sortable;
44 // 复写原型
45 // @ts-ignore
46 this.geom.__beforeMapping = this.geom.beforeMapping;
47 // @ts-ignore
48 this.geom.beforeMapping = function (data) {
49 var xScale = this.getXScale();
50 if (sortable !== false && data && data[0] && DEFAULT_SORT_GEOMETRYS.includes(GemoBaseClassName) && ['time', 'timeCat'].includes(xScale.type)) {
51 this.sort(data);
52 }
53 return this.__beforeMapping(data);
54 };
55 this.GemoBaseClassName = GemoBaseClassName;
56 };
57 GeomHelper.prototype.update = function (newConfig, component) {
58 var _this = this;
59 if (!this.geom) {
60 this.setView(component.context);
61 // 如果是时间类型则对数据排序
62 this.createGeomInstance(component.GemoBaseClassName, newConfig);
63 this.interactionTypes = component.interactionTypes;
64 }
65 compareProps_1.default(this.config, newConfig, ['position', 'shape', 'color', 'label', 'style', 'tooltip', 'size', 'animate', 'state', 'customInfo'], function (value, key) {
66 var _a;
67 // value 已被转为array
68 warning_1.default(!(key === 'label' && value[0] === true), 'label 值类型错误,应为false | LabelOption | FieldString');
69 (_a = _this.geom)[key].apply(_a, value);
70 });
71 compareProps_1.default(this.config, newConfig, ['adjust'], function (value, key) {
72 if (is_string_1.default(value[0])) {
73 _this.geom[key](value[0]);
74 }
75 else {
76 _this.geom[key](value);
77 }
78 });
79 // 状态设置
80 this.geom.state(newConfig.state || {});
81 // selected 和 active 使用 interacttion 替代
82 // setElements 设置 selected 和 active 默认值
83 this.rootChart.on('processElemens', function () {
84 if (is_function_1.default(newConfig.setElements)) {
85 newConfig.setElements(_this.geom.elements);
86 }
87 });
88 // 交互
89 // interaction
90 compareProps_1.default(this.config, newConfig, this.interactionTypes, function (value, key) {
91 if (value[0]) {
92 _this.rootChart.interaction(key);
93 }
94 else {
95 _this.rootChart.removeInteraction(key);
96 }
97 });
98 // 缓存
99 this.config = cloneDeep_1.default(newConfig);
100 };
101 GeomHelper.prototype.destroy = function () {
102 if (this.geom) {
103 this.geom.destroy();
104 this.geom = null;
105 }
106 this.config = {};
107 };
108 return GeomHelper;
109}());
110var BaseGeom = /** @class */ (function (_super) {
111 __extends(BaseGeom, _super);
112 function BaseGeom(props) {
113 var _this = _super.call(this, props) || this;
114 _this.interactionTypes = [];
115 _this.geomHelper = new GeomHelper();
116 return _this;
117 }
118 BaseGeom.prototype.componentWillUnmount = function () {
119 this.geomHelper.destroy();
120 };
121 BaseGeom.prototype.render = function () {
122 var _this = this;
123 this.geomHelper.update(this.props, this);
124 return react_1.default.createElement(react_1.default.Fragment, null, react_1.default.Children.map(this.props.children, function (ele) {
125 return react_1.default.isValidElement(ele) ? react_1.default.cloneElement(ele, { parentInstance: _this.geomHelper.geom }) : react_1.default.createElement(react_1.default.Fragment, null);
126 }));
127 };
128 return BaseGeom;
129}(react_1.default.Component));
130BaseGeom.contextType = view_1.default;
131exports.default = BaseGeom;
132//# sourceMappingURL=Base.js.map
\No newline at end of file