'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;
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;
});
});
};
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);
});
});
};