UNPKG

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