"use strict";
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function Page(config) {
this.defaultConfig = {
url: "",
limit: 10,
page: 1,
moreElement: undefined,
moreCallback: undefined,
iscroll: function iscroll() {}
};
this.cacheConfig = _extends({}, config || {});
this.total = 0;
this.list = [];
this.config = _extends({}, this.defaultConfig, config || {});
return this;
}
Page.prototype.init = function () {
var _this = this;
this.bindScroll();
return new Promise(function (resolve, reject) {
_this.get().then(function (data) {
_this.total = data.total;
_this.list = _this.list.concat(data.list);
_this.size = Math.ceil(_this.total / _this.config.limit);
resolve(_this.getReturn(data.list));
_this.bindScrollEvent();
}, function (data) {
reject(data);
});
});
};
Page.prototype.bindScrollEvent = function () {
clearInterval(this.checkTimer);
this.checkTimer = setInterval(this.checkNext.bind(this), 300);
};
Page.prototype.bindScroll = function () {
this.moreElement = $(this.config.moreElement);
this.moreElement.html("").append("\n
正在加载
\n 到达最后
\n ");
this.moreElement.find(".ing").addClass("show");
this.moreElement.find(".end").removeClass("show");
};
Page.prototype.checkNext = function () {
var _this2 = this;
var offset = $(window).scrollTop() + $(window).height() - this.moreElement.offset().top;
var iscroll = this.config.iscroll();
if (iscroll) {
if (Math.abs(iscroll.maxScrollY) - Math.abs(iscroll.y) < 50) {
offset = 51;
} else {
offset = 0;
}
}
if (offset > 50) {
this.next().then(function (data) {
if (typeof _this2.config.moreCallback === "function") {
_this2.config.moreCallback(data);
}
});
}
};
Page.prototype.getReturn = function (list) {
return {
list: list,
full: this.list,
total: this.total,
size: this.size,
page: this.config.page,
limit: this.config.limit
};
};
Page.prototype.get = function () {
var _this3 = this;
var iscroll = this.config.iscroll();
var passOutChange = false;
if (this.timer) {
clearTimeout(this.timer);
this.timer = undefined;
passOutChange = true;
}
if (iscroll && iscroll.options.passOut === true) {
iscroll.options.passOut = false;
}
return new Promise(function (resolve, reject) {
_this3.requesting = true;
$.ajax({
url: _this3.config.url,
data: _extends({}, _this3.config, {
moreCallback: null,
moreElement: null
}),
method: "GET",
dataType: "json"
}).then(function (data) {
resolve(data);
}, function (data) {
reject(data);
}).done(function () {
_this3.requesting = false;
_this3.timer = setTimeout(function () {
passOutChange && (iscroll.options.passOut = true);
}, 4000);
});
});
};
Page.prototype.reset = function (config) {
this.config = _extends({}, this.config, this.defaultConfig, this.cacheConfig, config || {});
return this.init();
};
Page.prototype.next = function () {
var _this4 = this;
if (this.requesting) {
return new Promise(function (resolve, reject) {
// setTimeout(() => {
// reject();
// }, 0);
});
}
if (this.config.page >= this.size) {
return new Promise(function (resolve, reject) {
_this4.moreElement.find(".ing").removeClass("show");
_this4.moreElement.find(".end").addClass("show");
clearInterval(_this4.checkTimer);
// setTimeout(() => {
// reject();
// }, 0);
});
}
this.config.page++;
return new Promise(function (resolve, reject) {
_this4.get().then(function (data) {
_this4.list = _this4.list.concat(data.list);
resolve(_this4.getReturn(data.list));
}, function (data) {
reject(data);
});
});
};