import { use } from '../utils'; import Loading from '../loading'; import { on, off } from '../utils/event'; import { getScrollTop, getElementTop, getVisibleHeight, getComputedStyle, getScrollEventTarget } from '../utils/scroll'; const [sfc, bem, t] = use('list'); export default sfc({ model: { prop: 'loading' }, props: { error: Boolean, loading: Boolean, finished: Boolean, errorText: String, loadingText: String, finishedText: String, immediateCheck: { type: Boolean, default: true }, offset: { type: Number, default: 300 } }, mounted() { this.scroller = getScrollEventTarget(this.$el); this.handler(true); if (this.immediateCheck) { this.$nextTick(this.check); } }, destroyed() { this.handler(false); }, activated() { this.handler(true); }, deactivated() { this.handler(false); }, watch: { loading() { this.$nextTick(this.check); }, finished() { this.$nextTick(this.check); } }, methods: { check() { if (this.loading || this.finished || this.error) { return; } const el = this.$el; const { scroller } = this; const scrollerHeight = getVisibleHeight(scroller); /* istanbul ignore next */ if (!scrollerHeight || getComputedStyle(el).display === 'none' || el.offsetParent === null) { return; } const scrollTop = getScrollTop(scroller); const targetBottom = scrollTop + scrollerHeight; let reachBottom = false; /* istanbul ignore next */ if (el === scroller) { reachBottom = scroller.scrollHeight - targetBottom < this.offset; } else { const elBottom = getElementTop(el) - getElementTop(scroller) + getVisibleHeight(el); reachBottom = elBottom - scrollerHeight < this.offset; } /* istanbul ignore else */ if (reachBottom) { this.$emit('input', true); this.$emit('load'); } }, clickErrorText() { this.$emit('update:error', false); this.$nextTick(this.check); }, handler(bind) { /* istanbul ignore else */ if (this.binded !== bind) { this.binded = bind; (bind ? on : off)(this.scroller, 'scroll', this.check); } } }, render(h) { return (
{this.slots()} {this.loading && (
{this.slots('loading') || [ , {this.loadingText || t('loading')} ]}
)} {this.finished && this.finishedText && (
{this.finishedText}
)} {this.error && this.errorText && (
{this.errorText}
)}
); } });