2022-04-12 21:03:06 +08:00
|
|
|
/* eslint-disable no-use-before-define */
|
2020-03-02 23:11:14 +08:00
|
|
|
/* eslint-disable no-unused-vars */
|
|
|
|
|
|
|
|
var InfiniteScroll = (function() {
|
|
|
|
function scrollNotVisible($container) {
|
2022-04-07 18:22:52 +08:00
|
|
|
let eventTarget = $($container.data('config').eventTarget || $container);
|
|
|
|
return scrollHitBottom(eventTarget[0]);
|
2020-03-02 23:11:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function loadData($container, page = 1) {
|
|
|
|
var customParams = $container.data('config').customParams;
|
|
|
|
var params = (customParams ? customParams({ page: page }) : { page: page });
|
|
|
|
|
|
|
|
if ($container.hasClass('loading') || $container.hasClass('last-page')) return;
|
|
|
|
$container.addClass('loading');
|
2022-04-07 18:22:52 +08:00
|
|
|
renderPlaceholder($container);
|
2020-03-02 23:11:14 +08:00
|
|
|
$.get($container.data('config').url, params, function(result) {
|
2022-04-07 18:22:52 +08:00
|
|
|
$container.find('.placeholder-block').remove();
|
2020-03-02 23:11:14 +08:00
|
|
|
if ($container.data('config').customResponse) {
|
|
|
|
$container.data('config').customResponse(result, $container);
|
|
|
|
} else {
|
|
|
|
$(result.data).appendTo($container);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.next_page) {
|
|
|
|
$container.data('next-page', result.next_page);
|
|
|
|
} else {
|
|
|
|
$container.addClass('last-page');
|
2022-04-28 20:11:11 +08:00
|
|
|
if ($container.data('config').endOfListTemplate && page > 2) {
|
2022-04-07 18:22:52 +08:00
|
|
|
$($($container.data('config').endOfListTemplate).html()).appendTo($container);
|
|
|
|
}
|
2020-03-02 23:11:14 +08:00
|
|
|
}
|
|
|
|
$container.removeClass('loading');
|
|
|
|
|
2020-03-06 20:51:18 +08:00
|
|
|
if ($container.data('config').afterAction) {
|
|
|
|
$container.data('config').afterAction(result, $container);
|
|
|
|
}
|
|
|
|
|
2020-03-02 23:11:14 +08:00
|
|
|
if (scrollNotVisible($container)) {
|
|
|
|
loadData($container, $container.data('next-page'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initScroll(object, config = {}) {
|
|
|
|
var $container = $(object);
|
|
|
|
$container.data('next-page', 2);
|
|
|
|
$container.data('config', config);
|
|
|
|
if (config.loadFirstPage) {
|
|
|
|
loadData($container, 1);
|
|
|
|
}
|
|
|
|
|
2022-04-07 18:22:52 +08:00
|
|
|
let eventTarget = $($container.data('config').eventTarget || $container);
|
|
|
|
eventTarget.on('scroll', () => {
|
|
|
|
if (scrollHitBottom(eventTarget[0]) && !$container.hasClass('last-page')) {
|
2020-03-02 23:11:14 +08:00
|
|
|
loadData($container, $container.data('next-page'));
|
|
|
|
}
|
|
|
|
});
|
2022-04-07 18:22:52 +08:00
|
|
|
|
|
|
|
if (scrollNotVisible($container)) {
|
|
|
|
loadData($container, $container.data('next-page'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// support functions
|
|
|
|
|
|
|
|
// Full scroll height
|
|
|
|
function scrollHeight(con) {
|
|
|
|
return con.scrollHeight || document.documentElement.scrollHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Top scroll position
|
|
|
|
function scrollTop(con) {
|
|
|
|
return con.scrollTop || con.scrollY || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get container size
|
|
|
|
function containerSize(con) {
|
|
|
|
return con.innerHeight || con.offsetHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Container position
|
|
|
|
function containerPosition(con) {
|
|
|
|
return scrollTop(con) + containerSize(con);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check when load next page
|
|
|
|
function scrollHitBottom(con) {
|
|
|
|
return scrollHeight(con) - containerPosition(con) <= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeScroll(con) {
|
|
|
|
let $container = $(con);
|
|
|
|
|
|
|
|
if (!$container.data('config')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let eventTarget = $($container.data('config').eventTarget) || $container;
|
|
|
|
$container.data('config', null);
|
|
|
|
$container.data('next-page', null);
|
2022-08-26 22:29:53 +08:00
|
|
|
$container.removeClass('last-page');
|
2022-04-07 18:22:52 +08:00
|
|
|
eventTarget.off('scroll');
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderPlaceholder($container) {
|
|
|
|
let palceholder = '';
|
|
|
|
$.each(Array($container.data('config').pageSize || 10), function() {
|
|
|
|
palceholder += $($container.data('config').placeholderTemplate).html();
|
|
|
|
});
|
|
|
|
$(palceholder).addClass('placeholder-block').appendTo($container);
|
2020-03-02 23:11:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
init: (object, config) => {
|
2022-04-07 18:22:52 +08:00
|
|
|
removeScroll(object);
|
2020-03-02 23:11:14 +08:00
|
|
|
initScroll(object, config);
|
|
|
|
},
|
|
|
|
resetScroll: (object) => {
|
2020-03-06 20:51:18 +08:00
|
|
|
$(object).data('next-page', $(object).data('config').loadFirstPage ? 1 : 2).removeClass('last-page');
|
2020-03-02 23:11:14 +08:00
|
|
|
if (scrollNotVisible($(object))) {
|
|
|
|
loadData($(object), $(object).data('next-page'));
|
|
|
|
}
|
2022-04-07 18:22:52 +08:00
|
|
|
},
|
|
|
|
removeScroll: (object) => {
|
|
|
|
removeScroll(object);
|
|
|
|
},
|
|
|
|
loadMore: (object) => {
|
|
|
|
let $container = $(object);
|
|
|
|
if (scrollNotVisible($container)) {
|
|
|
|
loadData($container, $container.data('next-page'));
|
|
|
|
}
|
2020-03-02 23:11:14 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}());
|