zent
Version:
一套前端设计语言和基于React的实现
208 lines (162 loc) • 6.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _defineProperty2 = require('babel-runtime/helpers/defineProperty');
var _defineProperty3 = _interopRequireDefault(_defineProperty2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _loading = require('../loading');
var _loading2 = _interopRequireDefault(_loading);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var InfiniteScroller = function (_ref) {
(0, _inherits3['default'])(InfiniteScroller, _ref);
function InfiniteScroller() {
var _ref2;
var _temp, _this, _ret;
(0, _classCallCheck3['default'])(this, InfiniteScroller);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = (0, _possibleConstructorReturn3['default'])(this, (_ref2 = InfiniteScroller.__proto__ || Object.getPrototypeOf(InfiniteScroller)).call.apply(_ref2, [this].concat(args))), _this), _this.state = {
isLoadingShow: false
}, _this.stopLoading = function () {
_this.setState({ isLoadingShow: false });
}, _this.calculateTopPosition = function (el) {
if (!el) {
return 0;
}
return el.offsetTop + _this.calculateTopPosition(el.offsetParent);
}, _this.getWindowScrollTop = function () {
return window.pageYOffset !== undefined ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
}, _this.isScrollAtBottom = function () {
var _this$props = _this.props,
offset = _this$props.offset,
useWindow = _this$props.useWindow;
var offsetDistance = void 0;
if (useWindow) {
var windowScrollTop = _this.getWindowScrollTop();
offsetDistance = _this.calculateTopPosition(_this.scroller) + _this.scroller.offsetHeight - windowScrollTop - window.innerHeight;
} else {
var _this$scroller = _this.scroller,
scrollHeight = _this$scroller.scrollHeight,
clientHeight = _this$scroller.clientHeight,
scrollTop = _this$scroller.scrollTop;
offsetDistance = scrollHeight - clientHeight - scrollTop;
}
return offsetDistance <= offset;
}, _this.handleScroll = function () {
var _this$props2 = _this.props,
hasMore = _this$props2.hasMore,
loadMore = _this$props2.loadMore;
if (!hasMore || !_this.isScrollAtBottom()) {
return;
}
_this.setState({
isLoadingShow: true
});
if (loadMore.length > 0) {
loadMore(_this.stopLoading);
} else {
loadMore().then(function () {
_this.setState({ isLoadingShow: false });
})['catch'](function () {
_this.setState({ isLoadingShow: false });
});
}
}, _this.addScrollListener = function () {
var _this$props3 = _this.props,
useWindow = _this$props3.useWindow,
useCapture = _this$props3.useCapture;
var scrollEl = window;
if (!useWindow) {
scrollEl = _this.scroller;
}
scrollEl.addEventListener('scroll', _this.handleScroll, useCapture);
scrollEl.addEventListener('resize', _this.handleScroll, useCapture);
}, _this.removeScrollListener = function () {
var _this$props4 = _this.props,
useWindow = _this$props4.useWindow,
useCapture = _this$props4.useCapture;
var scrollEl = window;
if (!useWindow) {
scrollEl = _this.scroller;
}
scrollEl.removeEventListener('scroll', _this.handleScroll, useCapture);
scrollEl.removeEventListener('resize', _this.handleScroll, useCapture);
}, _temp), (0, _possibleConstructorReturn3['default'])(_this, _ret);
}
(0, _createClass3['default'])(InfiniteScroller, [{
key: 'componentDidMount',
value: function componentDidMount() {
var _props = this.props,
loadMore = _props.loadMore,
initialLoad = _props.initialLoad;
this.addScrollListener();
if (initialLoad) {
loadMore();
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
this.removeScrollListener();
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
var _props2 = this.props,
prefix = _props2.prefix,
className = _props2.className,
children = _props2.children,
hasMore = _props2.hasMore,
loader = _props2.loader,
useWindow = _props2.useWindow;
var isLoadingShow = this.state.isLoadingShow;
var classString = (0, _classnames2['default'])(prefix + '-infinite-scroller', className, (0, _defineProperty3['default'])({}, prefix + '-infinite-scroller-y', !useWindow));
return _react2['default'].createElement(
'div',
{ ref: function ref(scroller) {
return _this2.scroller = scroller;
}, className: classString },
children,
hasMore && isLoadingShow && loader
);
}
}]);
return InfiniteScroller;
}(_react.PureComponent || _react.Component);
InfiniteScroller.PropTypes = {
prefix: _propTypes2['default'].string,
className: _propTypes2['default'].string,
hasMore: _propTypes2['default'].bool,
loadMore: _propTypes2['default'].func,
offset: _propTypes2['default'].num,
initialLoad: _propTypes2['default'].bool,
useWindow: _propTypes2['default'].bool,
useCapture: _propTypes2['default'].bool,
loader: _propTypes2['default'].node
};
InfiniteScroller.defaultProps = {
prefix: 'zent',
hasMore: true,
offset: 20,
initialLoad: true,
useWindow: true,
useCapture: false,
loader: _react2['default'].createElement(_loading2['default'], { className: 'zent-infinite-scroller-loading', show: true })
};
exports['default'] = InfiniteScroller;