index.js 8.3 KB

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