ifui
Version:
139 lines (115 loc) • 4.22 kB
JavaScript
'use strict';
exports.__esModule = true;
var _create = require('../utils/create');
var _create2 = _interopRequireDefault(_create);
var _scroll = require('../utils/scroll');
var _scroll2 = _interopRequireDefault(_scroll);
var _touch = require('../mixins/touch');
var _touch2 = _interopRequireDefault(_touch);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.default = (0, _create2.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._e(), _vm.status === 'loosing' ? _vm._t("lossing", [_c('span', { class: _vm.b('text') }, [_vm._v(_vm._s(_vm.loosingText || '释放即可刷新...'))])]) : _vm._e(), _vm.status === 'loading' ? _vm._t("loading", [_c('div', { class: _vm.b('loading') }, [_c('span', [_vm._v(_vm._s(_vm.loadingText || '加载中...'))])])]) : _vm._e()], 2), _vm._t("default")], 2)]);
},
name: 'pull-refresh',
mixins: [_touch2.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 = _scroll2.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.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 = _scroll2.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;
}
}
}
});