index.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. import { __assign, __extends, __read } from "tslib";
  2. import { CustomEvent } from '@antv/g';
  3. import { transition } from '../../animation';
  4. import { GUI } from '../../core';
  5. import { Text } from '../../shapes';
  6. import { getEventPos, ifShow, parseSeriesAttr, select, subStyleProps, superStyleProps, toPrecision, } from '../../util';
  7. import { Sparkline } from '../sparkline';
  8. import { CLASS_NAMES, HANDLE_DEFAULT_CFG, HANDLE_ICON_DEFAULT_CFG, HANDLE_LABEL_DEFAULT_CFG } from './constant';
  9. import { Handle } from './handle';
  10. var Slider = /** @class */ (function (_super) {
  11. __extends(Slider, _super);
  12. function Slider(options) {
  13. 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;
  14. _this.range = [0, 1];
  15. _this.onDragStart = function (target) { return function (e) {
  16. e.stopPropagation();
  17. _this.target = target;
  18. _this.prevPos = _this.getOrientVal(getEventPos(e));
  19. var _a = _this.availableSpace, x = _a.x, y = _a.y;
  20. var _b = _this.attributes, X = _b.x, Y = _b.y;
  21. _this.selectionStartPos = _this.getRatio(_this.prevPos - _this.getOrientVal([x, y]) - _this.getOrientVal([+X, +Y]));
  22. _this.selectionWidth = 0;
  23. document.addEventListener('mousemove', _this.onDragging);
  24. document.addEventListener('touchmove', _this.onDragging);
  25. document.addEventListener('mouseup', _this.onDragEnd);
  26. document.addEventListener('touchend', _this.onDragEnd);
  27. }; };
  28. _this.onDragging = function (e) {
  29. var _a = _this.attributes, slidable = _a.slidable, brushable = _a.brushable;
  30. e.stopPropagation();
  31. var currPos = _this.getOrientVal(getEventPos(e));
  32. var diffPos = currPos - _this.prevPos;
  33. if (!diffPos)
  34. return;
  35. var deltaVal = _this.getRatio(diffPos);
  36. switch (_this.target) {
  37. case 'start':
  38. if (slidable)
  39. _this.setValuesOffset(deltaVal);
  40. break;
  41. case 'end':
  42. if (slidable)
  43. _this.setValuesOffset(0, deltaVal);
  44. break;
  45. case 'selection':
  46. if (slidable)
  47. _this.setValuesOffset(deltaVal, deltaVal);
  48. break;
  49. case 'track':
  50. if (!brushable)
  51. return;
  52. // 绘制蒙板
  53. _this.selectionWidth += deltaVal;
  54. _this.innerSetValues([_this.selectionStartPos, _this.selectionStartPos + _this.selectionWidth].sort(), true);
  55. break;
  56. default:
  57. break;
  58. }
  59. _this.prevPos = currPos;
  60. };
  61. _this.onDragEnd = function () {
  62. document.removeEventListener('mousemove', _this.onDragging);
  63. document.removeEventListener('mousemove', _this.onDragging);
  64. document.removeEventListener('mouseup', _this.onDragEnd);
  65. document.removeEventListener('touchend', _this.onDragEnd);
  66. };
  67. _this.onValueChange = function (oldValue) {
  68. var evt = new CustomEvent('valuechange', {
  69. detail: {
  70. oldValue: oldValue,
  71. value: _this.getValues(),
  72. },
  73. });
  74. _this.dispatchEvent(evt);
  75. };
  76. _this.selectionStartPos = 0;
  77. _this.selectionWidth = 0;
  78. _this.prevPos = 0;
  79. _this.target = '';
  80. return _this;
  81. }
  82. Object.defineProperty(Slider.prototype, "values", {
  83. get: function () {
  84. return this.attributes.values;
  85. },
  86. set: function (values) {
  87. this.attributes.values = this.clampValues(values);
  88. },
  89. enumerable: false,
  90. configurable: true
  91. });
  92. Object.defineProperty(Slider.prototype, "sparklineStyle", {
  93. get: function () {
  94. var orientation = this.attributes.orientation;
  95. if (orientation !== 'horizontal')
  96. return null;
  97. var attr = subStyleProps(this.attributes, 'sparkline');
  98. return __assign(__assign({ zIndex: 0 }, this.availableSpace), attr);
  99. },
  100. enumerable: false,
  101. configurable: true
  102. });
  103. Object.defineProperty(Slider.prototype, "shape", {
  104. get: function () {
  105. var _a = this.attributes, trackLength = _a.trackLength, trackSize = _a.trackSize;
  106. var _b = __read(this.getOrientVal([
  107. [trackLength, trackSize],
  108. [trackSize, trackLength],
  109. ]), 2), width = _b[0], height = _b[1];
  110. return { width: width, height: height };
  111. },
  112. enumerable: false,
  113. configurable: true
  114. });
  115. Object.defineProperty(Slider.prototype, "availableSpace", {
  116. get: function () {
  117. var padding = this.attributes.padding;
  118. var _a = __read(parseSeriesAttr(padding), 4), top = _a[0], right = _a[1], bottom = _a[2], left = _a[3];
  119. var _b = this.shape, width = _b.width, height = _b.height;
  120. return {
  121. x: left,
  122. y: top,
  123. width: width - (left + right),
  124. height: height - (top + bottom),
  125. };
  126. },
  127. enumerable: false,
  128. configurable: true
  129. });
  130. Slider.prototype.getValues = function () {
  131. return this.values;
  132. };
  133. /** 不触发重绘 */
  134. Slider.prototype.setValues = function (values, animate) {
  135. if (values === void 0) { values = [0, 0]; }
  136. if (animate === void 0) { animate = false; }
  137. this.attributes.values = values;
  138. var animation = animate === false ? false : this.attributes.animate;
  139. transition(this.selectionShape.node(), this.selectionStyle, animation);
  140. this.updateHandlesPosition(animation);
  141. };
  142. Slider.prototype.updateHandlesPosition = function (animation) {
  143. if (!this.attributes.showHandle)
  144. return;
  145. transition(this.startHandle, this.getHandleStyle('start'), animation);
  146. transition(this.endHandle, this.getHandleStyle('end'), animation);
  147. };
  148. Slider.prototype.innerSetValues = function (values, trigger) {
  149. if (values === void 0) { values = [0, 0]; }
  150. if (trigger === void 0) { trigger = false; }
  151. var oldValues = this.values;
  152. var newValues = this.clampValues(values);
  153. this.attr('values', newValues);
  154. this.setValues(newValues);
  155. if (trigger) {
  156. this.onValueChange(oldValues);
  157. }
  158. };
  159. Slider.prototype.renderTrack = function (container) {
  160. var brushable = this.attributes.brushable;
  161. var style = subStyleProps(this.attributes, 'track');
  162. this.trackShape = select(container)
  163. .maybeAppendByClassName(CLASS_NAMES.track, 'rect')
  164. .styles(__assign(__assign({ cursor: brushable ? 'crosshair' : 'default' }, this.shape), style));
  165. };
  166. Slider.prototype.renderSparkline = function (container) {
  167. var _this = this;
  168. var orientation = this.attributes.orientation;
  169. var sparklineGroup = select(container).maybeAppendByClassName(CLASS_NAMES.sparklineGroup, 'g');
  170. ifShow(orientation === 'horizontal', sparklineGroup, function (group) {
  171. var style = _this.sparklineStyle;
  172. group.maybeAppendByClassName(CLASS_NAMES.sparkline, function () { return new Sparkline({ style: style }); }).update(style);
  173. });
  174. };
  175. Object.defineProperty(Slider.prototype, "selectionStyle", {
  176. get: function () {
  177. var style = subStyleProps(this.attributes, 'selection');
  178. return __assign(__assign({}, style), this.calcMask());
  179. },
  180. enumerable: false,
  181. configurable: true
  182. });
  183. Slider.prototype.renderHandles = function () {
  184. var _this = this;
  185. var _a;
  186. var showHandle = this.attributes.showHandle;
  187. var data = (showHandle ? ['start', 'end'] : []).map(function (type) { return ({ type: type }); });
  188. var that = this;
  189. (_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) {
  190. return enter
  191. .append(function (_a) {
  192. var type = _a.type;
  193. return new Handle({ style: _this.getHandleStyle(type) });
  194. })
  195. .each(function (_a) {
  196. var type = _a.type;
  197. this.attr('class', "".concat(CLASS_NAMES.handle.name, " ").concat(type, "-handle"));
  198. var name = "".concat(type, "Handle");
  199. that[name] = this;
  200. this.addEventListener('pointerdown', function (e) {
  201. that.onDragStart(type)(e);
  202. });
  203. });
  204. }, function (update) {
  205. return update.each(function (_a) {
  206. var type = _a.type;
  207. this.update(that.getHandleStyle(type));
  208. });
  209. }, function (exit) {
  210. return exit
  211. .each(function (_a) {
  212. var type = _a.type;
  213. var name = "".concat(type, "Handle");
  214. that[name] = undefined;
  215. })
  216. .remove();
  217. });
  218. };
  219. Slider.prototype.renderSelection = function (container) {
  220. this.foregroundGroup = select(container).maybeAppendByClassName(CLASS_NAMES.foreground, 'g');
  221. this.selectionShape = this.foregroundGroup
  222. .maybeAppendByClassName(CLASS_NAMES.selection, 'rect')
  223. .styles(this.selectionStyle);
  224. this.renderHandles();
  225. };
  226. Slider.prototype.render = function (attributes, container) {
  227. this.renderTrack(container);
  228. this.renderSparkline(container);
  229. this.renderSelection(container);
  230. };
  231. Slider.prototype.clampValues = function (values, precision) {
  232. var _a;
  233. if (precision === void 0) { precision = 4; }
  234. var _b = __read(this.range, 2), min = _b[0], max = _b[1];
  235. var _c = __read(this.getValues().map(function (num) { return toPrecision(num, precision); }), 2), prevStart = _c[0], prevEnd = _c[1];
  236. var _d = __read((values || [prevStart, prevEnd]).map(function (num) { return toPrecision(num, precision); }), 2), startVal = _d[0], endVal = _d[1];
  237. // 交换startVal endVal
  238. if (startVal > endVal) {
  239. _a = __read([endVal, startVal], 2), startVal = _a[0], endVal = _a[1];
  240. }
  241. var range = endVal - startVal;
  242. // 超出范围就全选
  243. if (range > max - min)
  244. return [min, max];
  245. if (startVal < min) {
  246. if (prevStart === min && prevEnd === endVal)
  247. return [min, endVal];
  248. return [min, range + min];
  249. }
  250. if (endVal > max) {
  251. if (prevEnd === max && prevStart === startVal)
  252. return [startVal, max];
  253. return [max - range, max];
  254. }
  255. // 保留小数
  256. return [startVal, endVal];
  257. };
  258. /**
  259. * 计算蒙板坐标和宽高
  260. * 默认用来计算前景位置大小
  261. */
  262. Slider.prototype.calcMask = function (values) {
  263. var _a = __read(this.clampValues(values), 2), start = _a[0], end = _a[1];
  264. var _b = this.availableSpace, x = _b.x, y = _b.y, width = _b.width, height = _b.height;
  265. return this.getOrientVal([
  266. {
  267. y: y,
  268. height: height,
  269. x: start * width + x,
  270. width: (end - start) * width,
  271. },
  272. {
  273. x: x,
  274. width: width,
  275. y: start * height + y,
  276. height: (end - start) * height,
  277. },
  278. ]);
  279. };
  280. /**
  281. * 计算手柄的x y
  282. */
  283. Slider.prototype.calcHandlePosition = function (handleType) {
  284. var _a = this.availableSpace, x = _a.x, y = _a.y, width = _a.width, height = _a.height;
  285. var _b = __read(this.clampValues(), 2), stVal = _b[0], endVal = _b[1];
  286. var L = (handleType === 'start' ? stVal : endVal) * this.getOrientVal([width, height]);
  287. return {
  288. x: x + this.getOrientVal([L, width / 2]),
  289. y: y + this.getOrientVal([height / 2, L]),
  290. };
  291. };
  292. /**
  293. * 计算手柄应当处于的位置
  294. * @param handleType start手柄还是end手柄
  295. * @returns
  296. */
  297. Slider.prototype.calcHandleText = function (handleType) {
  298. var _a;
  299. var _b = this.attributes, orientation = _b.orientation, formatter = _b.formatter, autoFitLabel = _b.autoFitLabel;
  300. var handleStyle = subStyleProps(this.attributes, 'handle');
  301. var labelStyle = subStyleProps(handleStyle, 'label');
  302. var spacing = handleStyle.spacing;
  303. var size = this.getHandleSize();
  304. var values = this.clampValues();
  305. var value = handleType === 'start' ? values[0] : values[1];
  306. var text = formatter(value);
  307. var temp = new Text({
  308. style: __assign(__assign({}, labelStyle), { text: text }),
  309. });
  310. // 文字包围盒的宽高
  311. var _c = temp.getBBox(), textWidth = _c.width, textHeight = _c.height;
  312. temp.destroy();
  313. if (!autoFitLabel) {
  314. var finaleWidth = spacing + size + (orientation === 'horizontal' ? textWidth / 2 : 0);
  315. return _a = { text: text }, _a[orientation === 'horizontal' ? 'x' : 'y'] = handleType === 'start' ? -finaleWidth : finaleWidth, _a;
  316. }
  317. var x = 0;
  318. var y = 0;
  319. // 相对于获取两端可用空间
  320. var _d = this.availableSpace, iW = _d.width, iH = _d.height;
  321. var _e = this.calcMask(), fX = _e.x, fY = _e.y, fW = _e.width, fH = _e.height;
  322. if (orientation === 'horizontal') {
  323. var totalSpacing = spacing + size;
  324. var finalWidth = totalSpacing + textWidth / 2;
  325. if (handleType === 'start') {
  326. var left = fX - totalSpacing - textWidth;
  327. x = left > 0 ? -finalWidth : finalWidth;
  328. }
  329. else {
  330. var sign = iW - fX - fW - totalSpacing > textWidth;
  331. x = sign ? finalWidth : -finalWidth;
  332. }
  333. }
  334. else {
  335. var finalWidth = spacing + size;
  336. if (handleType === 'start') {
  337. y = fY - size > textHeight ? -finalWidth : finalWidth;
  338. }
  339. else {
  340. y = iH - fY - fH - size > textHeight ? finalWidth : -finalWidth;
  341. }
  342. }
  343. return { x: x, y: y, text: text };
  344. };
  345. Slider.prototype.getHandleLabelStyle = function (handleType) {
  346. var showLabel = this.attributes.showLabel;
  347. if (!showLabel)
  348. return {};
  349. var style = subStyleProps(this.attributes, 'handleLabel');
  350. return __assign(__assign({}, style), this.calcHandleText(handleType));
  351. };
  352. Slider.prototype.getHandleIconStyle = function () {
  353. var shape = this.attributes.handleIconShape;
  354. var style = subStyleProps(this.attributes, 'handleIcon');
  355. var cursor = this.getOrientVal(['ew-resize', 'ns-resize']);
  356. var size = this.getHandleSize();
  357. return __assign({ cursor: cursor, shape: shape, size: size }, style);
  358. };
  359. Slider.prototype.getHandleStyle = function (handleType) {
  360. var _a = this.attributes, showLabel = _a.showLabel, orientation = _a.orientation;
  361. var handlePosition = this.calcHandlePosition(handleType);
  362. var textStyle = this.calcHandleText(handleType);
  363. 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 });
  364. };
  365. Slider.prototype.getHandleSize = function () {
  366. var _a = this.attributes, size = _a.handleIconSize, width = _a.width, height = _a.height;
  367. if (size)
  368. return size;
  369. // 没设置 size 的话,高度就取 height + 4 高度,手柄宽度是高度的 1/ 2.4
  370. return Math.floor((this.getOrientVal([+height, +width]) + 4) / 2.4);
  371. };
  372. Slider.prototype.getOrientVal = function (_a) {
  373. var _b = __read(_a, 2), x = _b[0], y = _b[1];
  374. var orientation = this.attributes.orientation;
  375. return orientation === 'horizontal' ? x : y;
  376. };
  377. Slider.prototype.setValuesOffset = function (stOffset, endOffset, animate) {
  378. if (endOffset === void 0) { endOffset = 0; }
  379. if (animate === void 0) { animate = false; }
  380. var _a = __read(this.getValues(), 2), oldStartVal = _a[0], oldEndVal = _a[1];
  381. var values = [oldStartVal + stOffset, oldEndVal + endOffset].sort();
  382. if (animate)
  383. this.setValues(values);
  384. else
  385. this.innerSetValues(values, true);
  386. };
  387. Slider.prototype.getRatio = function (val) {
  388. var _a = this.availableSpace, width = _a.width, height = _a.height;
  389. return val / this.getOrientVal([width, height]);
  390. };
  391. Slider.prototype.dispatchCustomEvent = function (target, event, name) {
  392. var _this = this;
  393. target.on(event, function (e) {
  394. e.stopPropagation();
  395. _this.dispatchEvent(new CustomEvent(name, { detail: e }));
  396. });
  397. };
  398. Slider.prototype.bindEvents = function () {
  399. var selection = this.selectionShape;
  400. // scroll 事件
  401. this.addEventListener('wheel', this.onScroll);
  402. // 选区drag事件
  403. selection.on('mousedown', this.onDragStart('selection'));
  404. selection.on('touchstart', this.onDragStart('selection'));
  405. // 选区hover事件
  406. this.dispatchCustomEvent(selection, 'mouseenter', 'selectionMouseenter');
  407. this.dispatchCustomEvent(selection, 'mouseleave', 'selectionMouseleave');
  408. this.dispatchCustomEvent(selection, 'click', 'selectionClick');
  409. var track = this.trackShape;
  410. this.dispatchCustomEvent(track, 'click', 'trackClick');
  411. this.dispatchCustomEvent(track, 'mouseenter', 'trackMouseenter');
  412. this.dispatchCustomEvent(track, 'mouseleave', 'trackMouseleave');
  413. // Drag and brush
  414. track.on('mousedown', this.onDragStart('track'));
  415. track.on('touchstart', this.onDragStart('track'));
  416. };
  417. Slider.prototype.onScroll = function (event) {
  418. var scrollable = this.attributes.scrollable;
  419. if (scrollable) {
  420. var deltaX = event.deltaX, deltaY = event.deltaY;
  421. var offset = deltaY || deltaX;
  422. var deltaVal = this.getRatio(offset);
  423. this.setValuesOffset(deltaVal, deltaVal, true);
  424. }
  425. };
  426. Slider.tag = 'slider';
  427. return Slider;
  428. }(GUI));
  429. export { Slider };
  430. //# sourceMappingURL=index.js.map