| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- import { __assign, __extends, __read } from "tslib";
- import { CustomEvent } from '@antv/g';
- import { transition } from '../../animation';
- import { GUI } from '../../core';
- import { Text } from '../../shapes';
- import { getEventPos, ifShow, parseSeriesAttr, select, subStyleProps, superStyleProps, toPrecision, } from '../../util';
- import { Sparkline } from '../sparkline';
- import { CLASS_NAMES, HANDLE_DEFAULT_CFG, HANDLE_ICON_DEFAULT_CFG, HANDLE_LABEL_DEFAULT_CFG } from './constant';
- import { Handle } from './handle';
- var Slider = /** @class */ (function (_super) {
- __extends(Slider, _super);
- function Slider(options) {
- var _this = _super.call(this, options, __assign(__assign(__assign({ animate: { duration: 100, fill: 'both' }, brushable: true, formatter: function (val) { return val.toString(); }, handleSpacing: 2, orientation: 'horizontal', padding: 0, autoFitLabel: true, scrollable: true, selectionCursor: 'move', selectionFill: '#5B8FF9', selectionFillOpacity: 0.45, selectionZIndex: 2, showHandle: true, showLabel: true, slidable: true, trackFill: '#416180', trackLength: 200, trackOpacity: 0.05, trackSize: 20, trackZIndex: -1, values: [0, 1] }, superStyleProps(HANDLE_DEFAULT_CFG, 'handle')), superStyleProps(HANDLE_ICON_DEFAULT_CFG, 'handleIcon')), superStyleProps(HANDLE_LABEL_DEFAULT_CFG, 'handleLabel'))) || this;
- _this.range = [0, 1];
- _this.onDragStart = function (target) { return function (e) {
- e.stopPropagation();
- _this.target = target;
- _this.prevPos = _this.getOrientVal(getEventPos(e));
- var _a = _this.availableSpace, x = _a.x, y = _a.y;
- var _b = _this.attributes, X = _b.x, Y = _b.y;
- _this.selectionStartPos = _this.getRatio(_this.prevPos - _this.getOrientVal([x, y]) - _this.getOrientVal([+X, +Y]));
- _this.selectionWidth = 0;
- document.addEventListener('mousemove', _this.onDragging);
- document.addEventListener('touchmove', _this.onDragging);
- document.addEventListener('mouseup', _this.onDragEnd);
- document.addEventListener('touchend', _this.onDragEnd);
- }; };
- _this.onDragging = function (e) {
- var _a = _this.attributes, slidable = _a.slidable, brushable = _a.brushable;
- e.stopPropagation();
- var currPos = _this.getOrientVal(getEventPos(e));
- var diffPos = currPos - _this.prevPos;
- if (!diffPos)
- return;
- var deltaVal = _this.getRatio(diffPos);
- switch (_this.target) {
- case 'start':
- if (slidable)
- _this.setValuesOffset(deltaVal);
- break;
- case 'end':
- if (slidable)
- _this.setValuesOffset(0, deltaVal);
- break;
- case 'selection':
- if (slidable)
- _this.setValuesOffset(deltaVal, deltaVal);
- break;
- case 'track':
- if (!brushable)
- return;
- // 绘制蒙板
- _this.selectionWidth += deltaVal;
- _this.innerSetValues([_this.selectionStartPos, _this.selectionStartPos + _this.selectionWidth].sort(), true);
- break;
- default:
- break;
- }
- _this.prevPos = currPos;
- };
- _this.onDragEnd = function () {
- document.removeEventListener('mousemove', _this.onDragging);
- document.removeEventListener('mousemove', _this.onDragging);
- document.removeEventListener('mouseup', _this.onDragEnd);
- document.removeEventListener('touchend', _this.onDragEnd);
- };
- _this.onValueChange = function (oldValue) {
- var evt = new CustomEvent('valuechange', {
- detail: {
- oldValue: oldValue,
- value: _this.getValues(),
- },
- });
- _this.dispatchEvent(evt);
- };
- _this.selectionStartPos = 0;
- _this.selectionWidth = 0;
- _this.prevPos = 0;
- _this.target = '';
- return _this;
- }
- Object.defineProperty(Slider.prototype, "values", {
- get: function () {
- return this.attributes.values;
- },
- set: function (values) {
- this.attributes.values = this.clampValues(values);
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Slider.prototype, "sparklineStyle", {
- get: function () {
- var orientation = this.attributes.orientation;
- if (orientation !== 'horizontal')
- return null;
- var attr = subStyleProps(this.attributes, 'sparkline');
- return __assign(__assign({ zIndex: 0 }, this.availableSpace), attr);
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Slider.prototype, "shape", {
- get: function () {
- var _a = this.attributes, trackLength = _a.trackLength, trackSize = _a.trackSize;
- var _b = __read(this.getOrientVal([
- [trackLength, trackSize],
- [trackSize, trackLength],
- ]), 2), width = _b[0], height = _b[1];
- return { width: width, height: height };
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Slider.prototype, "availableSpace", {
- get: function () {
- var padding = this.attributes.padding;
- var _a = __read(parseSeriesAttr(padding), 4), top = _a[0], right = _a[1], bottom = _a[2], left = _a[3];
- var _b = this.shape, width = _b.width, height = _b.height;
- return {
- x: left,
- y: top,
- width: width - (left + right),
- height: height - (top + bottom),
- };
- },
- enumerable: false,
- configurable: true
- });
- Slider.prototype.getValues = function () {
- return this.values;
- };
- /** 不触发重绘 */
- Slider.prototype.setValues = function (values, animate) {
- if (values === void 0) { values = [0, 0]; }
- if (animate === void 0) { animate = false; }
- this.attributes.values = values;
- var animation = animate === false ? false : this.attributes.animate;
- transition(this.selectionShape.node(), this.selectionStyle, animation);
- this.updateHandlesPosition(animation);
- };
- Slider.prototype.updateHandlesPosition = function (animation) {
- if (!this.attributes.showHandle)
- return;
- transition(this.startHandle, this.getHandleStyle('start'), animation);
- transition(this.endHandle, this.getHandleStyle('end'), animation);
- };
- Slider.prototype.innerSetValues = function (values, trigger) {
- if (values === void 0) { values = [0, 0]; }
- if (trigger === void 0) { trigger = false; }
- var oldValues = this.values;
- var newValues = this.clampValues(values);
- this.attr('values', newValues);
- this.setValues(newValues);
- if (trigger) {
- this.onValueChange(oldValues);
- }
- };
- Slider.prototype.renderTrack = function (container) {
- var brushable = this.attributes.brushable;
- var style = subStyleProps(this.attributes, 'track');
- this.trackShape = select(container)
- .maybeAppendByClassName(CLASS_NAMES.track, 'rect')
- .styles(__assign(__assign({ cursor: brushable ? 'crosshair' : 'default' }, this.shape), style));
- };
- Slider.prototype.renderSparkline = function (container) {
- var _this = this;
- var orientation = this.attributes.orientation;
- var sparklineGroup = select(container).maybeAppendByClassName(CLASS_NAMES.sparklineGroup, 'g');
- ifShow(orientation === 'horizontal', sparklineGroup, function (group) {
- var style = _this.sparklineStyle;
- group.maybeAppendByClassName(CLASS_NAMES.sparkline, function () { return new Sparkline({ style: style }); }).update(style);
- });
- };
- Object.defineProperty(Slider.prototype, "selectionStyle", {
- get: function () {
- var style = subStyleProps(this.attributes, 'selection');
- return __assign(__assign({}, style), this.calcMask());
- },
- enumerable: false,
- configurable: true
- });
- Slider.prototype.renderHandles = function () {
- var _this = this;
- var _a;
- var showHandle = this.attributes.showHandle;
- var data = (showHandle ? ['start', 'end'] : []).map(function (type) { return ({ type: type }); });
- var that = this;
- (_a = this.foregroundGroup) === null || _a === void 0 ? void 0 : _a.selectAll(CLASS_NAMES.handle.class).data(data, function (d) { return d.type; }).join(function (enter) {
- return enter
- .append(function (_a) {
- var type = _a.type;
- return new Handle({ style: _this.getHandleStyle(type) });
- })
- .each(function (_a) {
- var type = _a.type;
- this.attr('class', "".concat(CLASS_NAMES.handle.name, " ").concat(type, "-handle"));
- var name = "".concat(type, "Handle");
- that[name] = this;
- this.addEventListener('pointerdown', function (e) {
- that.onDragStart(type)(e);
- });
- });
- }, function (update) {
- return update.each(function (_a) {
- var type = _a.type;
- this.update(that.getHandleStyle(type));
- });
- }, function (exit) {
- return exit
- .each(function (_a) {
- var type = _a.type;
- var name = "".concat(type, "Handle");
- that[name] = undefined;
- })
- .remove();
- });
- };
- Slider.prototype.renderSelection = function (container) {
- this.foregroundGroup = select(container).maybeAppendByClassName(CLASS_NAMES.foreground, 'g');
- this.selectionShape = this.foregroundGroup
- .maybeAppendByClassName(CLASS_NAMES.selection, 'rect')
- .styles(this.selectionStyle);
- this.renderHandles();
- };
- Slider.prototype.render = function (attributes, container) {
- this.renderTrack(container);
- this.renderSparkline(container);
- this.renderSelection(container);
- };
- Slider.prototype.clampValues = function (values, precision) {
- var _a;
- if (precision === void 0) { precision = 4; }
- var _b = __read(this.range, 2), min = _b[0], max = _b[1];
- var _c = __read(this.getValues().map(function (num) { return toPrecision(num, precision); }), 2), prevStart = _c[0], prevEnd = _c[1];
- var _d = __read((values || [prevStart, prevEnd]).map(function (num) { return toPrecision(num, precision); }), 2), startVal = _d[0], endVal = _d[1];
- // 交换startVal endVal
- if (startVal > endVal) {
- _a = __read([endVal, startVal], 2), startVal = _a[0], endVal = _a[1];
- }
- var range = endVal - startVal;
- // 超出范围就全选
- if (range > max - min)
- return [min, max];
- if (startVal < min) {
- if (prevStart === min && prevEnd === endVal)
- return [min, endVal];
- return [min, range + min];
- }
- if (endVal > max) {
- if (prevEnd === max && prevStart === startVal)
- return [startVal, max];
- return [max - range, max];
- }
- // 保留小数
- return [startVal, endVal];
- };
- /**
- * 计算蒙板坐标和宽高
- * 默认用来计算前景位置大小
- */
- Slider.prototype.calcMask = function (values) {
- var _a = __read(this.clampValues(values), 2), start = _a[0], end = _a[1];
- var _b = this.availableSpace, x = _b.x, y = _b.y, width = _b.width, height = _b.height;
- return this.getOrientVal([
- {
- y: y,
- height: height,
- x: start * width + x,
- width: (end - start) * width,
- },
- {
- x: x,
- width: width,
- y: start * height + y,
- height: (end - start) * height,
- },
- ]);
- };
- /**
- * 计算手柄的x y
- */
- Slider.prototype.calcHandlePosition = function (handleType) {
- var _a = this.availableSpace, x = _a.x, y = _a.y, width = _a.width, height = _a.height;
- var _b = __read(this.clampValues(), 2), stVal = _b[0], endVal = _b[1];
- var L = (handleType === 'start' ? stVal : endVal) * this.getOrientVal([width, height]);
- return {
- x: x + this.getOrientVal([L, width / 2]),
- y: y + this.getOrientVal([height / 2, L]),
- };
- };
- /**
- * 计算手柄应当处于的位置
- * @param handleType start手柄还是end手柄
- * @returns
- */
- Slider.prototype.calcHandleText = function (handleType) {
- var _a;
- var _b = this.attributes, orientation = _b.orientation, formatter = _b.formatter, autoFitLabel = _b.autoFitLabel;
- var handleStyle = subStyleProps(this.attributes, 'handle');
- var labelStyle = subStyleProps(handleStyle, 'label');
- var spacing = handleStyle.spacing;
- var size = this.getHandleSize();
- var values = this.clampValues();
- var value = handleType === 'start' ? values[0] : values[1];
- var text = formatter(value);
- var temp = new Text({
- style: __assign(__assign({}, labelStyle), { text: text }),
- });
- // 文字包围盒的宽高
- var _c = temp.getBBox(), textWidth = _c.width, textHeight = _c.height;
- temp.destroy();
- if (!autoFitLabel) {
- var finaleWidth = spacing + size + (orientation === 'horizontal' ? textWidth / 2 : 0);
- return _a = { text: text }, _a[orientation === 'horizontal' ? 'x' : 'y'] = handleType === 'start' ? -finaleWidth : finaleWidth, _a;
- }
- var x = 0;
- var y = 0;
- // 相对于获取两端可用空间
- var _d = this.availableSpace, iW = _d.width, iH = _d.height;
- var _e = this.calcMask(), fX = _e.x, fY = _e.y, fW = _e.width, fH = _e.height;
- if (orientation === 'horizontal') {
- var totalSpacing = spacing + size;
- var finalWidth = totalSpacing + textWidth / 2;
- if (handleType === 'start') {
- var left = fX - totalSpacing - textWidth;
- x = left > 0 ? -finalWidth : finalWidth;
- }
- else {
- var sign = iW - fX - fW - totalSpacing > textWidth;
- x = sign ? finalWidth : -finalWidth;
- }
- }
- else {
- var finalWidth = spacing + size;
- if (handleType === 'start') {
- y = fY - size > textHeight ? -finalWidth : finalWidth;
- }
- else {
- y = iH - fY - fH - size > textHeight ? finalWidth : -finalWidth;
- }
- }
- return { x: x, y: y, text: text };
- };
- Slider.prototype.getHandleLabelStyle = function (handleType) {
- var showLabel = this.attributes.showLabel;
- if (!showLabel)
- return {};
- var style = subStyleProps(this.attributes, 'handleLabel');
- return __assign(__assign({}, style), this.calcHandleText(handleType));
- };
- Slider.prototype.getHandleIconStyle = function () {
- var shape = this.attributes.handleIconShape;
- var style = subStyleProps(this.attributes, 'handleIcon');
- var cursor = this.getOrientVal(['ew-resize', 'ns-resize']);
- var size = this.getHandleSize();
- return __assign({ cursor: cursor, shape: shape, size: size }, style);
- };
- Slider.prototype.getHandleStyle = function (handleType) {
- var _a = this.attributes, showLabel = _a.showLabel, orientation = _a.orientation;
- var handlePosition = this.calcHandlePosition(handleType);
- var textStyle = this.calcHandleText(handleType);
- return __assign(__assign(__assign(__assign({}, superStyleProps(this.getHandleIconStyle(), 'icon')), superStyleProps(__assign(__assign({}, this.getHandleLabelStyle(handleType)), textStyle), 'label')), handlePosition), { orientation: orientation, showLabel: showLabel, type: handleType, zIndex: 3 });
- };
- Slider.prototype.getHandleSize = function () {
- var _a = this.attributes, size = _a.handleIconSize, width = _a.width, height = _a.height;
- if (size)
- return size;
- // 没设置 size 的话,高度就取 height + 4 高度,手柄宽度是高度的 1/ 2.4
- return Math.floor((this.getOrientVal([+height, +width]) + 4) / 2.4);
- };
- Slider.prototype.getOrientVal = function (_a) {
- var _b = __read(_a, 2), x = _b[0], y = _b[1];
- var orientation = this.attributes.orientation;
- return orientation === 'horizontal' ? x : y;
- };
- Slider.prototype.setValuesOffset = function (stOffset, endOffset, animate) {
- if (endOffset === void 0) { endOffset = 0; }
- if (animate === void 0) { animate = false; }
- var _a = __read(this.getValues(), 2), oldStartVal = _a[0], oldEndVal = _a[1];
- var values = [oldStartVal + stOffset, oldEndVal + endOffset].sort();
- if (animate)
- this.setValues(values);
- else
- this.innerSetValues(values, true);
- };
- Slider.prototype.getRatio = function (val) {
- var _a = this.availableSpace, width = _a.width, height = _a.height;
- return val / this.getOrientVal([width, height]);
- };
- Slider.prototype.dispatchCustomEvent = function (target, event, name) {
- var _this = this;
- target.on(event, function (e) {
- e.stopPropagation();
- _this.dispatchEvent(new CustomEvent(name, { detail: e }));
- });
- };
- Slider.prototype.bindEvents = function () {
- var selection = this.selectionShape;
- // scroll 事件
- this.addEventListener('wheel', this.onScroll);
- // 选区drag事件
- selection.on('mousedown', this.onDragStart('selection'));
- selection.on('touchstart', this.onDragStart('selection'));
- // 选区hover事件
- this.dispatchCustomEvent(selection, 'mouseenter', 'selectionMouseenter');
- this.dispatchCustomEvent(selection, 'mouseleave', 'selectionMouseleave');
- this.dispatchCustomEvent(selection, 'click', 'selectionClick');
- var track = this.trackShape;
- this.dispatchCustomEvent(track, 'click', 'trackClick');
- this.dispatchCustomEvent(track, 'mouseenter', 'trackMouseenter');
- this.dispatchCustomEvent(track, 'mouseleave', 'trackMouseleave');
- // Drag and brush
- track.on('mousedown', this.onDragStart('track'));
- track.on('touchstart', this.onDragStart('track'));
- };
- Slider.prototype.onScroll = function (event) {
- var scrollable = this.attributes.scrollable;
- if (scrollable) {
- var deltaX = event.deltaX, deltaY = event.deltaY;
- var offset = deltaY || deltaX;
- var deltaVal = this.getRatio(offset);
- this.setValuesOffset(deltaVal, deltaVal, true);
- }
- };
- Slider.tag = 'slider';
- return Slider;
- }(GUI));
- export { Slider };
- //# sourceMappingURL=index.js.map
|