pull2refresh
Version:
Pull to refresh library
120 lines (116 loc) • 4.39 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.pull2refresh = factory());
}(this, (function () { 'use strict';
var PullToRefresh = /** @class */ (function () {
function PullToRefresh(options) {
this.startY = null;
this.refreshInProgress = false;
this.options = options;
this.options.refreshElement.style.display = "block";
this.maxHeight = this.computeOffsetHeight(options.refreshElement);
this.initalMarginTop = this.computeMarginTop(this.options.pullableElement);
if (!this.options.threshold) {
this.options.threshold = this.maxHeight;
}
this.reset();
this.options.pullableElement.addEventListener("touchstart", this.start.bind(this));
this.options.pullableElement.addEventListener("mousedown", this.start.bind(this));
document.addEventListener("touchmove", this.move.bind(this));
document.addEventListener("mousemove", this.move.bind(this));
document.addEventListener("touchend", this.end.bind(this));
document.addEventListener("mouseup", this.end.bind(this));
}
PullToRefresh.prototype.done = function () {
this.refreshInProgress = false;
this.reset();
};
PullToRefresh.prototype.reset = function () {
this.startY = null;
this.options.pullableElement.style.marginTop = this.initalMarginTop - this.maxHeight + "px";
};
PullToRefresh.prototype.start = function (event) {
if (!this.isScrollToTop()) {
// Don't pull to refresh when scroll bar is not at top
return;
}
if (this.refreshInProgress) {
return;
}
this.reset();
this.startY = this.pageY(event);
};
PullToRefresh.prototype.move = function (event) {
if (this.startY === null || this.refreshInProgress) {
return;
}
var delta = this.pageY(event) - this.startY;
if (delta <= 0) {
return;
}
// Display the refresh element
var margin = this.maxHeight - delta;
if (margin < 0) {
margin = 0;
}
this.options.pullableElement.style.marginTop = -margin + "px";
// Callback
if (this.options.onPull && this.options.threshold) {
var ratio = delta / this.options.threshold;
this.options.onPull(ratio > 1 ? 1 : ratio);
}
};
PullToRefresh.prototype.end = function (event) {
if (this.refreshInProgress) {
return;
}
if (this.startY !== null) {
var delta = this.pageY(event) - this.startY;
if (this.options.threshold && delta >= this.options.threshold) {
this.refreshInProgress = true;
this.options.onRefresh();
return;
}
}
this.reset();
};
PullToRefresh.prototype.pageY = function (event) {
if (event instanceof MouseEvent) {
return event.pageY;
}
else {
if (event.touches && event.touches.length) {
return event.touches[event.touches.length - 1].pageY;
}
}
return 0;
};
PullToRefresh.prototype.computeOffsetHeight = function (element) {
if (element.offsetHeight) {
return element.offsetHeight;
}
var height = window.getComputedStyle(element).height;
if (!height) {
return 0;
}
var computed = parseInt(height, 10);
if (isNaN(computed)) {
return 0;
}
return computed;
};
PullToRefresh.prototype.computeMarginTop = function (element) {
var margin = parseInt(window.getComputedStyle(element).getPropertyValue("margin-top"), 10);
if (isNaN(margin)) {
return 0;
}
return margin;
};
PullToRefresh.prototype.isScrollToTop = function () {
return this.options.pullableElement.getBoundingClientRect().top === this.computeMarginTop(this.options.pullableElement);
};
return PullToRefresh;
}());
return PullToRefresh;
})));