acud
Version:
acg react 组件库
218 lines (217 loc) • 9.22 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _isNativeReflectConstruct2 = _interopRequireDefault(require("@babel/runtime/helpers/isNativeReflectConstruct"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var React = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _raf = _interopRequireDefault(require("rc-util/lib/raf"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, (0, _isNativeReflectConstruct2["default"])() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(t).constructor) : o.apply(t, e)); }
var MIN_SIZE = 20;
function getPageY(e) {
return 'touches' in e ? e.touches[0].pageY : e.pageY;
}
var ScrollBar = exports["default"] = /*#__PURE__*/function (_React$Component) {
function ScrollBar() {
var _this;
(0, _classCallCheck2["default"])(this, ScrollBar);
_this = _callSuper(this, ScrollBar, arguments);
_this.moveRaf = null;
_this.scrollbarRef = /*#__PURE__*/React.createRef();
_this.thumbRef = /*#__PURE__*/React.createRef();
// eslint-disable-next-line no-undef
_this.visibleTimeout = null;
_this.state = {
dragging: false,
pageY: null,
startTop: null,
visible: false
};
_this.delayHidden = function () {
clearTimeout(_this.visibleTimeout);
_this.setState({
visible: true
});
_this.visibleTimeout = setTimeout(function () {
_this.setState({
visible: false
});
}, 2000);
};
_this.onScrollbarTouchStart = function (e) {
e.preventDefault();
};
_this.onContainerMouseDown = function (e) {
e.stopPropagation();
e.preventDefault();
};
// ======================= Clean =======================
_this.patchEvents = function () {
window.addEventListener('mousemove', _this.onMouseMove);
window.addEventListener('mouseup', _this.onMouseUp);
_this.thumbRef.current.addEventListener('touchmove', _this.onMouseMove);
_this.thumbRef.current.addEventListener('touchend', _this.onMouseUp);
};
_this.removeEvents = function () {
window.removeEventListener('mousemove', _this.onMouseMove);
window.removeEventListener('mouseup', _this.onMouseUp);
_this.scrollbarRef.current.removeEventListener('touchstart', _this.onScrollbarTouchStart);
_this.thumbRef.current.removeEventListener('touchstart', _this.onMouseDown);
_this.thumbRef.current.removeEventListener('touchmove', _this.onMouseMove);
_this.thumbRef.current.removeEventListener('touchend', _this.onMouseUp);
_raf["default"].cancel(_this.moveRaf);
};
// ======================= Thumb =======================
_this.onMouseDown = function (e) {
var onStartMove = _this.props.onStartMove;
_this.setState({
dragging: true,
pageY: getPageY(e),
startTop: _this.getTop()
});
onStartMove();
_this.patchEvents();
e.stopPropagation();
e.preventDefault();
};
_this.onMouseMove = function (e) {
var _this$state = _this.state,
dragging = _this$state.dragging,
pageY = _this$state.pageY,
startTop = _this$state.startTop;
var onScroll = _this.props.onScroll;
_raf["default"].cancel(_this.moveRaf);
if (dragging) {
var offsetY = getPageY(e) - pageY;
var newTop = startTop + offsetY;
var enableScrollRange = _this.getEnableScrollRange();
var enableHeightRange = _this.getEnableHeightRange();
var ptg = enableHeightRange ? newTop / enableHeightRange : 0;
var newScrollTop = Math.ceil(ptg * enableScrollRange);
_this.moveRaf = (0, _raf["default"])(function () {
onScroll(newScrollTop);
});
}
};
_this.onMouseUp = function () {
var onStopMove = _this.props.onStopMove;
_this.setState({
dragging: false
});
onStopMove();
_this.removeEvents();
};
// ===================== Calculate =====================
_this.getSpinHeight = function () {
var _this$props = _this.props,
height = _this$props.height,
count = _this$props.count;
var baseHeight = height / count * 10;
baseHeight = Math.max(baseHeight, MIN_SIZE);
baseHeight = Math.min(baseHeight, height / 2);
return Math.floor(baseHeight);
};
_this.getEnableScrollRange = function () {
var _this$props2 = _this.props,
scrollHeight = _this$props2.scrollHeight,
height = _this$props2.height;
return scrollHeight - height || 0;
};
_this.getEnableHeightRange = function () {
var height = _this.props.height;
var spinHeight = _this.getSpinHeight();
return height - spinHeight || 0;
};
_this.getTop = function () {
var scrollTop = _this.props.scrollTop;
var enableScrollRange = _this.getEnableScrollRange();
var enableHeightRange = _this.getEnableHeightRange();
if (scrollTop === 0 || enableScrollRange === 0) {
return 0;
}
var ptg = scrollTop / enableScrollRange;
return ptg * enableHeightRange;
};
// Not show scrollbar when height is large than scrollHeight
_this.showScroll = function () {
var _this$props3 = _this.props,
height = _this$props3.height,
scrollHeight = _this$props3.scrollHeight;
return scrollHeight > height;
};
return _this;
}
(0, _inherits2["default"])(ScrollBar, _React$Component);
return (0, _createClass2["default"])(ScrollBar, [{
key: "componentDidMount",
value: function componentDidMount() {
this.scrollbarRef.current.addEventListener('touchstart', this.onScrollbarTouchStart);
this.thumbRef.current.addEventListener('touchstart', this.onMouseDown);
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
if (prevProps.scrollTop !== this.props.scrollTop) {
this.delayHidden();
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.removeEvents();
clearTimeout(this.visibleTimeout);
}
// ====================== Render =======================
}, {
key: "render",
value: function render() {
var _this$state2 = this.state,
dragging = _this$state2.dragging,
visible = _this$state2.visible;
var prefixCls = this.props.prefixCls;
var spinHeight = this.getSpinHeight();
var top = this.getTop();
var canScroll = this.showScroll();
var mergedVisible = canScroll && visible;
return /*#__PURE__*/React.createElement("div", {
ref: this.scrollbarRef,
className: (0, _classnames["default"])("".concat(prefixCls, "-scrollbar"), (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-scrollbar-show"), canScroll)),
style: {
width: 6,
top: 0,
bottom: 0,
right: 0,
position: 'absolute',
display: mergedVisible ? null : 'none'
},
onMouseDown: this.onContainerMouseDown,
onMouseMove: this.delayHidden
}, /*#__PURE__*/React.createElement("div", {
ref: this.thumbRef,
className: (0, _classnames["default"])("".concat(prefixCls, "-scrollbar-thumb"), (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-scrollbar-thumb-moving"), dragging)),
style: {
width: '100%',
height: spinHeight,
top: top,
left: 0,
position: 'absolute',
background: '#D4D6D9',
borderRadius: 99,
cursor: 'pointer',
userSelect: 'none'
},
onMouseDown: this.onMouseDown
}));
}
}]);
}(React.Component);