lulouis-vant
Version:
Lightweight Mobile UI Components built on Vue
148 lines (134 loc) • 4.03 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _utils = require("../utils");
var _loading = _interopRequireDefault(require("../loading"));
var _scroll = _interopRequireDefault(require("../utils/scroll"));
var _touch = _interopRequireDefault(require("../mixins/touch"));
var _use = (0, _utils.use)('pull-refresh'),
sfc = _use[0],
bem = _use[1],
t = _use[2];
var _default = sfc({
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: {
untouchable: function untouchable() {
return this.status === 'loading' || this.disabled;
}
},
watch: {
value: function value(val) {
this.duration = this.animationDuration;
this.getStatus(val ? this.headHeight : 0, val);
}
},
mounted: function mounted() {
this.scrollEl = _scroll.default.getScrollEventTarget(this.$el);
},
methods: {
onTouchStart: function onTouchStart(event) {
if (!this.untouchable && 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 && 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;
}
}
},
render: function render(h) {
var status = this.status;
var text = this[status + "Text"] || t(status);
var style = {
transition: this.duration + "ms",
transform: "translate3d(0," + this.height + "px, 0)"
};
var Status = this.$slots[status] || [(status === 'pulling' || status === 'loosing') && h("div", {
"class": bem('text')
}, [text]), status === 'loading' && h("div", {
"class": bem('loading')
}, [h(_loading.default), h("span", [text])])];
return h("div", {
"class": bem()
}, [h("div", {
"class": bem('track'),
"style": style,
"on": {
"touchstart": this.onTouchStart,
"touchmove": this.onTouchMove,
"touchend": this.onTouchEnd,
"touchcancel": this.onTouchEnd
}
}, [h("div", {
"class": bem('head')
}, [Status]), this.$slots.default])]);
}
});
exports.default = _default;