index.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { __assign, __extends, __read } from "tslib";
  2. import { CustomEvent } from '@antv/g';
  3. import { clamp } from '@antv/util';
  4. import { GUI } from '../../core';
  5. import { getEventPos, parseSeriesAttr, select, subStyleProps, superStyleProps } from '../../util';
  6. import { Slider } from '../slider';
  7. var Scrollbar = /** @class */ (function (_super) {
  8. __extends(Scrollbar, _super);
  9. function Scrollbar(options) {
  10. var _this = _super.call(this, options, {
  11. isRound: true,
  12. orientation: 'vertical',
  13. padding: [2, 2, 2, 2],
  14. scrollable: true,
  15. slidable: true,
  16. thumbCursor: 'default',
  17. trackSize: 10,
  18. value: 0,
  19. }) || this;
  20. _this.range = [0, 1];
  21. /**
  22. * 值改变事件
  23. */
  24. _this.onValueChange = function (oldValue) {
  25. var newValue = _this.attributes.value;
  26. if (oldValue === newValue)
  27. return;
  28. var evtVal = {
  29. detail: {
  30. oldValue: oldValue,
  31. value: newValue,
  32. },
  33. };
  34. _this.dispatchEvent(new CustomEvent('scroll', evtVal));
  35. _this.dispatchEvent(new CustomEvent('valuechange', evtVal));
  36. };
  37. /**
  38. * 点击轨道事件
  39. */
  40. _this.onTrackClick = function (e) {
  41. var slidable = _this.attributes.slidable;
  42. if (!slidable)
  43. return;
  44. var _a = __read(_this.getLocalPosition(), 2), x = _a[0], y = _a[1];
  45. var _b = __read(_this.padding, 4), top = _b[0], left = _b[3];
  46. var basePos = _this.getOrientVal([x + left, y + top]);
  47. var clickPos = _this.getOrientVal(getEventPos(e));
  48. var value = (clickPos - basePos) / _this.trackLength;
  49. _this.setValue(value, true);
  50. };
  51. _this.onThumbMouseenter = function (e) {
  52. _this.dispatchEvent(new CustomEvent('thumbMouseenter', { detail: e.detail }));
  53. };
  54. _this.onTrackMouseenter = function (e) {
  55. _this.dispatchEvent(new CustomEvent('trackMouseenter', { detail: e.detail }));
  56. };
  57. _this.onThumbMouseleave = function (e) {
  58. _this.dispatchEvent(new CustomEvent('thumbMouseleave', { detail: e.detail }));
  59. };
  60. _this.onTrackMouseleave = function (e) {
  61. _this.dispatchEvent(new CustomEvent('trackMouseleave', { detail: e.detail }));
  62. };
  63. return _this;
  64. }
  65. Object.defineProperty(Scrollbar.prototype, "padding", {
  66. get: function () {
  67. var padding = this.attributes.padding;
  68. return parseSeriesAttr(padding);
  69. },
  70. enumerable: false,
  71. configurable: true
  72. });
  73. Object.defineProperty(Scrollbar.prototype, "value", {
  74. get: function () {
  75. var value = this.attributes.value;
  76. var _a = __read(this.range, 2), min = _a[0], max = _a[1];
  77. return clamp(value, min, max);
  78. },
  79. enumerable: false,
  80. configurable: true
  81. });
  82. Object.defineProperty(Scrollbar.prototype, "trackLength", {
  83. get: function () {
  84. var _a = this.attributes, viewportLength = _a.viewportLength, _b = _a.trackLength, trackLength = _b === void 0 ? viewportLength : _b;
  85. return trackLength;
  86. },
  87. enumerable: false,
  88. configurable: true
  89. });
  90. Object.defineProperty(Scrollbar.prototype, "availableSpace", {
  91. get: function () {
  92. var trackSize = this.attributes.trackSize;
  93. var trackLength = this.trackLength;
  94. var _a = __read(this.padding, 4), top = _a[0], right = _a[1], bottom = _a[2], left = _a[3];
  95. var _b = __read(this.getOrientVal([
  96. [trackLength, trackSize],
  97. [trackSize, trackLength],
  98. ]), 2), width = _b[0], height = _b[1];
  99. return {
  100. x: left,
  101. y: top,
  102. width: +width - (left + right),
  103. height: +height - (top + bottom),
  104. };
  105. },
  106. enumerable: false,
  107. configurable: true
  108. });
  109. Object.defineProperty(Scrollbar.prototype, "trackRadius", {
  110. get: function () {
  111. var _a = this.attributes, isRound = _a.isRound, trackSize = _a.trackSize;
  112. if (!isRound)
  113. return 0;
  114. return trackSize / 2;
  115. },
  116. enumerable: false,
  117. configurable: true
  118. });
  119. Object.defineProperty(Scrollbar.prototype, "thumbRadius", {
  120. get: function () {
  121. var _a = this.attributes, isRound = _a.isRound, thumbRadius = _a.thumbRadius;
  122. if (!isRound)
  123. return 0;
  124. var _b = this.availableSpace, width = _b.width, height = _b.height;
  125. return thumbRadius || this.getOrientVal([height, width]) / 2;
  126. },
  127. enumerable: false,
  128. configurable: true
  129. });
  130. /**
  131. * accord to thumbLen and value, calculate the values of slider
  132. */
  133. Scrollbar.prototype.getValues = function (value) {
  134. if (value === void 0) { value = this.value; }
  135. var _a = this.attributes, viewportLength = _a.viewportLength, contentLength = _a.contentLength;
  136. var unit = viewportLength / contentLength;
  137. var _b = __read(this.range, 2), min = _b[0], max = _b[1];
  138. var start = value * (max - min - unit);
  139. return [start, start + unit];
  140. };
  141. Scrollbar.prototype.getValue = function () {
  142. return this.value;
  143. };
  144. Scrollbar.prototype.renderSlider = function (container) {
  145. var _a = this.attributes, orientation = _a.orientation, trackSize = _a.trackSize, padding = _a.padding, slidable = _a.slidable;
  146. var trackStyle = subStyleProps(this.attributes, 'track');
  147. var selectionStyle = subStyleProps(this.attributes, 'thumb');
  148. var style = __assign(__assign({ brushable: false, orientation: orientation, padding: padding, selectionRadius: this.thumbRadius, showHandle: false, slidable: slidable, trackLength: this.trackLength, trackRadius: this.trackRadius, trackSize: trackSize, values: this.getValues() }, superStyleProps(trackStyle, 'track')), superStyleProps(selectionStyle, 'selection'));
  149. this.slider = select(container)
  150. .maybeAppendByClassName('scrollbar', function () { return new Slider({ style: style }); })
  151. .update(style)
  152. .node();
  153. };
  154. Scrollbar.prototype.render = function (attributes, container) {
  155. this.renderSlider(container);
  156. };
  157. /**
  158. * 设置value
  159. * @param value 当前位置的占比
  160. */
  161. Scrollbar.prototype.setValue = function (value, animate) {
  162. if (animate === void 0) { animate = false; }
  163. var oldValue = this.attributes.value;
  164. var _a = __read(this.range, 2), min = _a[0], max = _a[1];
  165. this.slider.setValues(this.getValues(clamp(value, min, max)), animate);
  166. // 通知触发valueChange
  167. // todo 调用 setValue 不触发 valuechange
  168. this.onValueChange(oldValue);
  169. };
  170. Scrollbar.prototype.bindEvents = function () {
  171. var _this = this;
  172. this.slider.addEventListener('trackClick', function (e) {
  173. e.stopPropagation();
  174. _this.onTrackClick(e.detail);
  175. });
  176. this.onHover();
  177. };
  178. /**
  179. * 根据orient取出对应轴向上的值
  180. * 主要用于取鼠标坐标在orient方向上的位置
  181. */
  182. Scrollbar.prototype.getOrientVal = function (values) {
  183. var orientation = this.attributes.orientation;
  184. return orientation === 'horizontal' ? values[0] : values[1];
  185. };
  186. /**
  187. * 悬浮事件
  188. */
  189. Scrollbar.prototype.onHover = function () {
  190. this.slider.addEventListener('selectionMouseenter', this.onThumbMouseenter);
  191. this.slider.addEventListener('trackMouseenter', this.onTrackMouseenter);
  192. this.slider.addEventListener('selectionMouseleave', this.onThumbMouseleave);
  193. this.slider.addEventListener('trackMouseleave', this.onTrackMouseleave);
  194. };
  195. Scrollbar.tag = 'scrollbar';
  196. return Scrollbar;
  197. }(GUI));
  198. export { Scrollbar };
  199. //# sourceMappingURL=index.js.map