UNPKG

13.7 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var base_1 = require("./base");
5var dependents_1 = require("../../dependents");
6var bbox_1 = require("../../util/bbox");
7var direction_1 = require("../../util/direction");
8var constant_1 = require("../../constant");
9var util_1 = require("@antv/util");
10var helper_1 = require("../../util/helper");
11var DEFAULT_PADDING = 0;
12var DEFAULT_SIZE = 8;
13var DEFAULT_CATEGORY_SIZE = 32;
14var MIN_THUMB_LENGTH = 20;
15var Scrollbar = /** @class */ (function (_super) {
16 tslib_1.__extends(Scrollbar, _super);
17 function Scrollbar(view) {
18 var _this = _super.call(this, view) || this;
19 _this.onChangeFn = util_1.noop;
20 _this.resetMeasure = function () {
21 _this.clear();
22 };
23 _this.onValueChange = function (_a) {
24 var ratio = _a.ratio;
25 var animate = _this.getValidScrollbarCfg().animate;
26 _this.ratio = (0, util_1.clamp)(ratio, 0, 1);
27 var originalAnimate = _this.view.getOptions().animate;
28 if (!animate) {
29 _this.view.animate(false);
30 }
31 _this.changeViewData(_this.getScrollRange(), true);
32 _this.view.animate(originalAnimate);
33 };
34 _this.container = _this.view.getLayer(constant_1.LAYER.FORE).addGroup();
35 _this.onChangeFn = (0, util_1.throttle)(_this.onValueChange, 20, {
36 leading: true,
37 });
38 _this.trackLen = 0;
39 _this.thumbLen = 0;
40 _this.ratio = 0;
41 _this.view.on(constant_1.VIEW_LIFE_CIRCLE.BEFORE_CHANGE_DATA, _this.resetMeasure);
42 _this.view.on(constant_1.VIEW_LIFE_CIRCLE.BEFORE_CHANGE_SIZE, _this.resetMeasure);
43 return _this;
44 }
45 Object.defineProperty(Scrollbar.prototype, "name", {
46 get: function () {
47 return 'scrollbar';
48 },
49 enumerable: false,
50 configurable: true
51 });
52 Scrollbar.prototype.destroy = function () {
53 _super.prototype.destroy.call(this);
54 this.view.off(constant_1.VIEW_LIFE_CIRCLE.BEFORE_CHANGE_DATA, this.resetMeasure);
55 this.view.off(constant_1.VIEW_LIFE_CIRCLE.BEFORE_CHANGE_SIZE, this.resetMeasure);
56 };
57 Scrollbar.prototype.init = function () { };
58 /**
59 * 渲染
60 */
61 Scrollbar.prototype.render = function () {
62 this.option = this.view.getOptions().scrollbar;
63 if (this.option) {
64 if (this.scrollbar) {
65 // exist, update
66 this.scrollbar = this.updateScrollbar();
67 }
68 else {
69 // not exist, create
70 this.scrollbar = this.createScrollbar();
71 this.scrollbar.component.on('scrollchange', this.onChangeFn);
72 }
73 }
74 else {
75 if (this.scrollbar) {
76 // exist, destroy
77 this.scrollbar.component.destroy();
78 this.scrollbar = undefined;
79 }
80 }
81 };
82 /**
83 * 布局
84 */
85 Scrollbar.prototype.layout = function () {
86 var _this = this;
87 if (this.option && !this.trackLen) {
88 this.measureScrollbar();
89 setTimeout(function () {
90 if (!_this.view.destroyed) {
91 _this.changeViewData(_this.getScrollRange(), true);
92 }
93 });
94 }
95 if (this.scrollbar) {
96 var width = this.view.coordinateBBox.width;
97 var padding = this.scrollbar.component.get('padding');
98 var bboxObject = this.scrollbar.component.getLayoutBBox();
99 var bbox = new bbox_1.BBox(bboxObject.x, bboxObject.y, Math.min(bboxObject.width, width), bboxObject.height).expand(padding);
100 var cfg = this.getScrollbarComponentCfg();
101 var x = void 0;
102 var y = void 0;
103 if (cfg.isHorizontal) {
104 var _a = tslib_1.__read((0, direction_1.directionToPosition)(this.view.viewBBox, bbox, constant_1.DIRECTION.BOTTOM), 2), x1 = _a[0], y1 = _a[1];
105 var _b = tslib_1.__read((0, direction_1.directionToPosition)(this.view.coordinateBBox, bbox, constant_1.DIRECTION.BOTTOM), 2), x2 = _b[0], y2 = _b[1];
106 x = x2;
107 y = y1;
108 }
109 else {
110 var _c = tslib_1.__read((0, direction_1.directionToPosition)(this.view.viewBBox, bbox, constant_1.DIRECTION.RIGHT), 2), x1 = _c[0], y1 = _c[1];
111 var _d = tslib_1.__read((0, direction_1.directionToPosition)(this.view.viewBBox, bbox, constant_1.DIRECTION.RIGHT), 2), x2 = _d[0], y2 = _d[1];
112 x = x2;
113 y = y1;
114 }
115 x += padding[3];
116 y += padding[0];
117 // 默认放在 bottom
118 if (this.trackLen) {
119 this.scrollbar.component.update(tslib_1.__assign(tslib_1.__assign({}, cfg), { x: x, y: y, trackLen: this.trackLen, thumbLen: this.thumbLen, thumbOffset: (this.trackLen - this.thumbLen) * this.ratio }));
120 }
121 else {
122 this.scrollbar.component.update(tslib_1.__assign(tslib_1.__assign({}, cfg), { x: x, y: y }));
123 }
124 this.view.viewBBox = this.view.viewBBox.cut(bbox, cfg.isHorizontal ? constant_1.DIRECTION.BOTTOM : constant_1.DIRECTION.RIGHT);
125 }
126 };
127 /**
128 * 更新
129 */
130 Scrollbar.prototype.update = function () {
131 // 逻辑和 render 保持一致
132 this.render();
133 };
134 Scrollbar.prototype.getComponents = function () {
135 return this.scrollbar ? [this.scrollbar] : [];
136 };
137 Scrollbar.prototype.clear = function () {
138 if (this.scrollbar) {
139 this.scrollbar.component.destroy();
140 this.scrollbar = undefined;
141 }
142 this.trackLen = 0;
143 this.thumbLen = 0;
144 this.ratio = 0;
145 this.cnt = 0;
146 this.step = 0;
147 this.data = undefined;
148 this.xScaleCfg = undefined;
149 this.yScalesCfg = [];
150 };
151 /** 设置滚动条位置 */
152 Scrollbar.prototype.setValue = function (ratio) {
153 this.onValueChange({ ratio: ratio });
154 };
155 /** 获得滚动条位置 */
156 Scrollbar.prototype.getValue = function () {
157 return this.ratio;
158 };
159 /**
160 * 获取 scrollbar 的主题配置
161 */
162 Scrollbar.prototype.getThemeOptions = function () {
163 var theme = this.view.getTheme();
164 return (0, util_1.get)(theme, ['components', 'scrollbar', 'common'], {});
165 };
166 /**
167 * 获取 scrollbar 组件的主题样式
168 */
169 Scrollbar.prototype.getScrollbarTheme = function (style) {
170 var theme = (0, util_1.get)(this.view.getTheme(), ['components', 'scrollbar']);
171 var _a = style || {}, thumbHighlightColor = _a.thumbHighlightColor, restStyles = tslib_1.__rest(_a, ["thumbHighlightColor"]);
172 return {
173 default: (0, util_1.deepMix)({}, (0, util_1.get)(theme, ['default', 'style'], {}), restStyles),
174 hover: (0, util_1.deepMix)({}, (0, util_1.get)(theme, ['hover', 'style'], {}), { thumbColor: thumbHighlightColor }),
175 };
176 };
177 Scrollbar.prototype.measureScrollbar = function () {
178 var xScale = this.view.getXScale();
179 var yScales = this.view.getYScales().slice();
180 this.data = this.getScrollbarData();
181 this.step = this.getStep();
182 this.cnt = this.getCnt();
183 var _a = this.getScrollbarComponentCfg(), trackLen = _a.trackLen, thumbLen = _a.thumbLen;
184 this.trackLen = trackLen;
185 this.thumbLen = thumbLen;
186 this.xScaleCfg = {
187 field: xScale.field,
188 values: xScale.values || [],
189 };
190 this.yScalesCfg = yScales;
191 };
192 Scrollbar.prototype.getScrollRange = function () {
193 var startIdx = Math.floor((this.cnt - this.step) * (0, util_1.clamp)(this.ratio, 0, 1));
194 var endIdx = Math.min(startIdx + this.step - 1, this.cnt - 1);
195 return [startIdx, endIdx];
196 };
197 Scrollbar.prototype.changeViewData = function (_a, render) {
198 var _this = this;
199 var _b = tslib_1.__read(_a, 2), startIdx = _b[0], endIdx = _b[1];
200 var type = this.getValidScrollbarCfg().type;
201 var isHorizontal = type !== 'vertical';
202 var values = (0, util_1.valuesOfKey)(this.data, this.xScaleCfg.field);
203 // 如果是 xScale 数值类型,则进行排序
204 var xScaleValues = this.view.getXScale().isLinear ? values.sort(function (a, b) { return Number(a) - Number(b); }) : values;
205 var xValues = isHorizontal ? xScaleValues : xScaleValues.reverse();
206 this.yScalesCfg.forEach(function (cfg) {
207 _this.view.scale(cfg.field, {
208 formatter: cfg.formatter,
209 type: cfg.type,
210 min: cfg.min,
211 max: cfg.max,
212 tickMethod: cfg.tickMethod
213 });
214 });
215 this.view.filter(this.xScaleCfg.field, function (val) {
216 var idx = xValues.indexOf(val);
217 return idx > -1 ? (0, helper_1.isBetween)(idx, startIdx, endIdx) : true;
218 });
219 this.view.render(true);
220 };
221 Scrollbar.prototype.createScrollbar = function () {
222 var type = this.getValidScrollbarCfg().type;
223 var isHorizontal = type !== 'vertical';
224 var component = new dependents_1.Scrollbar(tslib_1.__assign(tslib_1.__assign({ container: this.container }, this.getScrollbarComponentCfg()), { x: 0, y: 0 }));
225 component.init();
226 return {
227 component: component,
228 layer: constant_1.LAYER.FORE,
229 direction: isHorizontal ? constant_1.DIRECTION.BOTTOM : constant_1.DIRECTION.RIGHT,
230 type: constant_1.COMPONENT_TYPE.SCROLLBAR,
231 };
232 };
233 Scrollbar.prototype.updateScrollbar = function () {
234 var config = this.getScrollbarComponentCfg();
235 var realConfig = this.trackLen
236 ? tslib_1.__assign(tslib_1.__assign({}, config), { trackLen: this.trackLen, thumbLen: this.thumbLen, thumbOffset: (this.trackLen - this.thumbLen) * this.ratio }) : tslib_1.__assign({}, config);
237 this.scrollbar.component.update(realConfig);
238 return this.scrollbar;
239 };
240 Scrollbar.prototype.getStep = function () {
241 if (this.step) {
242 return this.step;
243 }
244 var coordinateBBox = this.view.coordinateBBox;
245 var _a = this.getValidScrollbarCfg(), type = _a.type, categorySize = _a.categorySize;
246 var isHorizontal = type !== 'vertical';
247 return Math.floor((isHorizontal ? coordinateBBox.width : coordinateBBox.height) / categorySize);
248 };
249 Scrollbar.prototype.getCnt = function () {
250 if (this.cnt) {
251 return this.cnt;
252 }
253 var xScale = this.view.getXScale();
254 var data = this.getScrollbarData();
255 var values = (0, util_1.valuesOfKey)(data, xScale.field);
256 return (0, util_1.size)(values);
257 };
258 Scrollbar.prototype.getScrollbarComponentCfg = function () {
259 var _a = this.view, coordinateBBox = _a.coordinateBBox, viewBBox = _a.viewBBox;
260 var _b = this.getValidScrollbarCfg(), type = _b.type, padding = _b.padding, width = _b.width, height = _b.height, style = _b.style;
261 var isHorizontal = type !== 'vertical';
262 var _c = tslib_1.__read(padding, 4), paddingTop = _c[0], paddingRight = _c[1], paddingBottom = _c[2], paddingLeft = _c[3];
263 var position = isHorizontal
264 ? {
265 x: coordinateBBox.minX + paddingLeft,
266 y: viewBBox.maxY - height - paddingBottom,
267 }
268 : {
269 x: viewBBox.maxX - width - paddingRight,
270 y: coordinateBBox.minY + paddingTop,
271 };
272 var step = this.getStep();
273 var cnt = this.getCnt();
274 var trackLen = isHorizontal
275 ? coordinateBBox.width - paddingLeft - paddingRight
276 : coordinateBBox.height - paddingTop - paddingBottom;
277 var thumbLen = Math.max(trackLen * (0, util_1.clamp)(step / cnt, 0, 1), MIN_THUMB_LENGTH);
278 return tslib_1.__assign(tslib_1.__assign({}, this.getThemeOptions()), { x: position.x, y: position.y, size: isHorizontal ? height : width, isHorizontal: isHorizontal, trackLen: trackLen, thumbLen: thumbLen, thumbOffset: 0, theme: this.getScrollbarTheme(style) });
279 };
280 /**
281 * 填充一些默认的配置项目
282 */
283 Scrollbar.prototype.getValidScrollbarCfg = function () {
284 var cfg = {
285 type: 'horizontal',
286 categorySize: DEFAULT_CATEGORY_SIZE,
287 width: DEFAULT_SIZE,
288 height: DEFAULT_SIZE,
289 padding: [0, 0, 0, 0],
290 animate: true,
291 style: {},
292 };
293 if ((0, util_1.isObject)(this.option)) {
294 cfg = tslib_1.__assign(tslib_1.__assign({}, cfg), this.option);
295 }
296 if (!(0, util_1.isObject)(this.option) || !this.option.padding) {
297 cfg.padding =
298 cfg.type === 'horizontal' ? [DEFAULT_PADDING, 0, DEFAULT_PADDING, 0] : [0, DEFAULT_PADDING, 0, DEFAULT_PADDING];
299 }
300 return cfg;
301 };
302 /**
303 * 获取数据
304 */
305 Scrollbar.prototype.getScrollbarData = function () {
306 var coordinate = this.view.getCoordinate();
307 var cfg = this.getValidScrollbarCfg();
308 var data = this.view.getOptions().data || [];
309 // 纵向做了 y 轴镜像之后,数据也需要镜像反转
310 if (coordinate.isReflect('y') && cfg.type === 'vertical') {
311 data = tslib_1.__spreadArray([], tslib_1.__read(data), false).reverse();
312 }
313 return data;
314 };
315 return Scrollbar;
316}(base_1.Controller));
317exports.default = Scrollbar;
318//# sourceMappingURL=scrollbar.js.map
\No newline at end of file