ScrollBar.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _vue = require("vue");
  8. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  9. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  10. var _classNames3 = _interopRequireDefault(require("../_util/classNames"));
  11. var _createRef = _interopRequireDefault(require("../_util/createRef"));
  12. var _raf = _interopRequireDefault(require("../_util/raf"));
  13. var _supportsPassive = _interopRequireDefault(require("../_util/supportsPassive"));
  14. var MIN_SIZE = 20;
  15. function getPageY(e) {
  16. return 'touches' in e ? e.touches[0].pageY : e.pageY;
  17. }
  18. var _default = (0, _vue.defineComponent)({
  19. compatConfig: {
  20. MODE: 3
  21. },
  22. name: 'ScrollBar',
  23. inheritAttrs: false,
  24. props: {
  25. prefixCls: String,
  26. scrollTop: Number,
  27. scrollHeight: Number,
  28. height: Number,
  29. count: Number,
  30. onScroll: {
  31. type: Function
  32. },
  33. onStartMove: {
  34. type: Function
  35. },
  36. onStopMove: {
  37. type: Function
  38. }
  39. },
  40. setup: function setup() {
  41. return {
  42. moveRaf: null,
  43. scrollbarRef: (0, _createRef.default)(),
  44. thumbRef: (0, _createRef.default)(),
  45. visibleTimeout: null,
  46. state: (0, _vue.reactive)({
  47. dragging: false,
  48. pageY: null,
  49. startTop: null,
  50. visible: false
  51. })
  52. };
  53. },
  54. watch: {
  55. scrollTop: {
  56. handler: function handler() {
  57. this.delayHidden();
  58. },
  59. flush: 'post'
  60. }
  61. },
  62. mounted: function mounted() {
  63. var _this$scrollbarRef$cu, _this$thumbRef$curren;
  64. (_this$scrollbarRef$cu = this.scrollbarRef.current) === null || _this$scrollbarRef$cu === void 0 ? void 0 : _this$scrollbarRef$cu.addEventListener('touchstart', this.onScrollbarTouchStart, _supportsPassive.default ? {
  65. passive: false
  66. } : false);
  67. (_this$thumbRef$curren = this.thumbRef.current) === null || _this$thumbRef$curren === void 0 ? void 0 : _this$thumbRef$curren.addEventListener('touchstart', this.onMouseDown, _supportsPassive.default ? {
  68. passive: false
  69. } : false);
  70. },
  71. beforeUnmount: function beforeUnmount() {
  72. this.removeEvents();
  73. clearTimeout(this.visibleTimeout);
  74. },
  75. methods: {
  76. delayHidden: function delayHidden() {
  77. var _this = this;
  78. clearTimeout(this.visibleTimeout);
  79. this.state.visible = true;
  80. this.visibleTimeout = setTimeout(function () {
  81. _this.state.visible = false;
  82. }, 2000);
  83. },
  84. onScrollbarTouchStart: function onScrollbarTouchStart(e) {
  85. e.preventDefault();
  86. },
  87. onContainerMouseDown: function onContainerMouseDown(e) {
  88. e.stopPropagation();
  89. e.preventDefault();
  90. },
  91. // ======================= Clean =======================
  92. patchEvents: function patchEvents() {
  93. window.addEventListener('mousemove', this.onMouseMove);
  94. window.addEventListener('mouseup', this.onMouseUp);
  95. this.thumbRef.current.addEventListener('touchmove', this.onMouseMove, _supportsPassive.default ? {
  96. passive: false
  97. } : false);
  98. this.thumbRef.current.addEventListener('touchend', this.onMouseUp);
  99. },
  100. removeEvents: function removeEvents() {
  101. window.removeEventListener('mousemove', this.onMouseMove);
  102. window.removeEventListener('mouseup', this.onMouseUp);
  103. this.scrollbarRef.current.removeEventListener('touchstart', this.onScrollbarTouchStart, _supportsPassive.default ? {
  104. passive: false
  105. } : false);
  106. if (this.thumbRef.current) {
  107. this.thumbRef.current.removeEventListener('touchstart', this.onMouseDown, _supportsPassive.default ? {
  108. passive: false
  109. } : false);
  110. this.thumbRef.current.removeEventListener('touchmove', this.onMouseMove, _supportsPassive.default ? {
  111. passive: false
  112. } : false);
  113. this.thumbRef.current.removeEventListener('touchend', this.onMouseUp);
  114. }
  115. _raf.default.cancel(this.moveRaf);
  116. },
  117. // ======================= Thumb =======================
  118. onMouseDown: function onMouseDown(e) {
  119. var onStartMove = this.$props.onStartMove;
  120. (0, _extends2.default)(this.state, {
  121. dragging: true,
  122. pageY: getPageY(e),
  123. startTop: this.getTop()
  124. });
  125. onStartMove();
  126. this.patchEvents();
  127. e.stopPropagation();
  128. e.preventDefault();
  129. },
  130. onMouseMove: function onMouseMove(e) {
  131. var _this$state = this.state,
  132. dragging = _this$state.dragging,
  133. pageY = _this$state.pageY,
  134. startTop = _this$state.startTop;
  135. var onScroll = this.$props.onScroll;
  136. _raf.default.cancel(this.moveRaf);
  137. if (dragging) {
  138. var offsetY = getPageY(e) - pageY;
  139. var newTop = startTop + offsetY;
  140. var enableScrollRange = this.getEnableScrollRange();
  141. var enableHeightRange = this.getEnableHeightRange();
  142. var ptg = enableHeightRange ? newTop / enableHeightRange : 0;
  143. var newScrollTop = Math.ceil(ptg * enableScrollRange);
  144. this.moveRaf = (0, _raf.default)(function () {
  145. onScroll(newScrollTop);
  146. });
  147. }
  148. },
  149. onMouseUp: function onMouseUp() {
  150. var onStopMove = this.$props.onStopMove;
  151. this.state.dragging = false;
  152. onStopMove();
  153. this.removeEvents();
  154. },
  155. // ===================== Calculate =====================
  156. getSpinHeight: function getSpinHeight() {
  157. var _this$$props = this.$props,
  158. height = _this$$props.height,
  159. count = _this$$props.count;
  160. var baseHeight = height / count * 10;
  161. baseHeight = Math.max(baseHeight, MIN_SIZE);
  162. baseHeight = Math.min(baseHeight, height / 2);
  163. return Math.floor(baseHeight);
  164. },
  165. getEnableScrollRange: function getEnableScrollRange() {
  166. var _this$$props2 = this.$props,
  167. scrollHeight = _this$$props2.scrollHeight,
  168. height = _this$$props2.height;
  169. return scrollHeight - height || 0;
  170. },
  171. getEnableHeightRange: function getEnableHeightRange() {
  172. var height = this.$props.height;
  173. var spinHeight = this.getSpinHeight();
  174. return height - spinHeight || 0;
  175. },
  176. getTop: function getTop() {
  177. var scrollTop = this.$props.scrollTop;
  178. var enableScrollRange = this.getEnableScrollRange();
  179. var enableHeightRange = this.getEnableHeightRange();
  180. if (scrollTop === 0 || enableScrollRange === 0) {
  181. return 0;
  182. }
  183. var ptg = scrollTop / enableScrollRange;
  184. return ptg * enableHeightRange;
  185. },
  186. // Not show scrollbar when height is large than scrollHeight
  187. showScroll: function showScroll() {
  188. var _this$$props3 = this.$props,
  189. height = _this$$props3.height,
  190. scrollHeight = _this$$props3.scrollHeight;
  191. return scrollHeight > height;
  192. }
  193. },
  194. render: function render() {
  195. // eslint-disable-next-line no-unused-vars
  196. var _this$state2 = this.state,
  197. dragging = _this$state2.dragging,
  198. visible = _this$state2.visible;
  199. var prefixCls = this.$props.prefixCls;
  200. var spinHeight = this.getSpinHeight() + 'px';
  201. var top = this.getTop() + 'px';
  202. var canScroll = this.showScroll();
  203. var mergedVisible = canScroll && visible;
  204. return (0, _vue.createVNode)("div", {
  205. "ref": this.scrollbarRef,
  206. "class": (0, _classNames3.default)("".concat(prefixCls, "-scrollbar"), (0, _defineProperty2.default)({}, "".concat(prefixCls, "-scrollbar-show"), canScroll)),
  207. "style": {
  208. width: '8px',
  209. top: 0,
  210. bottom: 0,
  211. right: 0,
  212. position: 'absolute',
  213. display: mergedVisible ? undefined : 'none'
  214. },
  215. "onMousedown": this.onContainerMouseDown,
  216. "onMousemove": this.delayHidden
  217. }, [(0, _vue.createVNode)("div", {
  218. "ref": this.thumbRef,
  219. "class": (0, _classNames3.default)("".concat(prefixCls, "-scrollbar-thumb"), (0, _defineProperty2.default)({}, "".concat(prefixCls, "-scrollbar-thumb-moving"), dragging)),
  220. "style": {
  221. width: '100%',
  222. height: spinHeight,
  223. top: top,
  224. left: 0,
  225. position: 'absolute',
  226. background: 'rgba(0, 0, 0, 0.5)',
  227. borderRadius: '99px',
  228. cursor: 'pointer',
  229. userSelect: 'none'
  230. },
  231. "onMousedown": this.onMouseDown
  232. }, null)]);
  233. }
  234. });
  235. exports.default = _default;