UNPKG

vant-fork

Version:

Lightweight Mobile UI Components built on Vue

157 lines (140 loc) 4.37 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.default = void 0; var _create = _interopRequireDefault(require("../utils/create")); var _scroll = _interopRequireDefault(require("../utils/scroll")); var _touch = _interopRequireDefault(require("../mixins/touch")); var _default = (0, _create.default)({ render: function render() { var _vm = this; var _h = _vm.$createElement; var _c = _vm._self._c || _h; return _c('div', { class: _vm.b() }, [_c('div', { class: _vm.b('track'), style: _vm.style, on: { "touchstart": _vm.onTouchStart, "touchmove": _vm.onTouchMove, "touchend": _vm.onTouchEnd, "touchcancel": _vm.onTouchEnd } }, [_c('div', { class: _vm.b('head') }, [_vm.status === 'normal' ? _vm._t("normal") : _vm._e(), _vm.status === 'pulling' ? _vm._t("pulling", [_c('span', { class: _vm.b('text') }, [_vm._v(_vm._s(_vm.pullingText || _vm.$t('pulling')))])]) : _vm._e(), _vm.status === 'loosing' ? _vm._t("loosing", [_c('span', { class: _vm.b('text') }, [_vm._v(_vm._s(_vm.loosingText || _vm.$t('loosing')))])]) : _vm._e(), _vm.status === 'loading' ? _vm._t("loading", [_c('div', { class: _vm.b('loading') }, [_c('loading'), _c('span', [_vm._v(_vm._s(_vm.loadingText || _vm.$t('loadingTip')))])], 1)]) : _vm._e()], 2), _vm._t("default")], 2)]); }, name: 'pull-refresh', mixins: [_touch.default], props: { disabled: Boolean, pullingText: String, loosingText: String, loadingText: String, value: { type: Boolean, required: true }, animationDuration: { type: Number, default: 300 }, headHeight: { type: Number, default: 50 } }, data: function data() { return { status: 'normal', height: 0, duration: 0 }; }, computed: { style: function style() { return { transition: this.duration + "ms", transform: "translate3d(0," + this.height + "px, 0)" }; }, untouchable: function untouchable() { return this.status === 'loading' || this.disabled; } }, mounted: function mounted() { this.scrollEl = _scroll.default.getScrollEventTarget(this.$el); }, watch: { value: function value(val) { this.duration = this.animationDuration; this.getStatus(val ? this.headHeight : 0, val); } }, methods: { onTouchStart: function onTouchStart(event) { if (this.untouchable) { return; } if (this.getCeiling()) { this.duration = 0; this.touchStart(event); } }, onTouchMove: function onTouchMove(event) { if (this.untouchable) { return; } this.touchMove(event); if (!this.ceiling && this.getCeiling()) { this.duration = 0; this.startY = event.touches[0].clientY; this.deltaY = 0; } if (this.ceiling && this.deltaY >= 0) { if (this.direction === 'vertical') { this.getStatus(this.ease(this.deltaY)); event.cancelable && event.preventDefault(); } } }, onTouchEnd: function onTouchEnd() { if (this.untouchable) { return; } if (this.ceiling && this.deltaY) { this.duration = this.animationDuration; if (this.status === 'loosing') { this.getStatus(this.headHeight, true); this.$emit('input', true); this.$emit('refresh'); } else { this.getStatus(0); } } }, getCeiling: function getCeiling() { this.ceiling = _scroll.default.getScrollTop(this.scrollEl) === 0; return this.ceiling; }, ease: function ease(height) { var headHeight = this.headHeight; return height < headHeight ? height : height < headHeight * 2 ? Math.round(headHeight + (height - headHeight) / 2) : Math.round(headHeight * 1.5 + (height - headHeight * 2) / 4); }, getStatus: function getStatus(height, isLoading) { this.height = height; var status = isLoading ? 'loading' : height === 0 ? 'normal' : height < this.headHeight ? 'pulling' : 'loosing'; if (status !== this.status) { this.status = status; } } } }); exports.default = _default;