debian-mirror-gitlab/app/assets/javascripts/pager.js

94 lines
2.3 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2020-06-23 00:09:42 +05:30
import 'vendor/jquery.endless-scroll';
import axios from '~/lib/utils/axios_utils';
2021-09-30 23:02:18 +05:30
import { removeParams, getParameterByName } from '~/lib/utils/url_utility';
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
const ENDLESS_SCROLL_BOTTOM_PX = 400;
const ENDLESS_SCROLL_FIRE_DELAY_MS = 1000;
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
export default {
2021-04-29 21:17:54 +05:30
init({
2018-12-13 13:39:08 +05:30
limit = 0,
preload = false,
disable = false,
prepareData = $.noop,
2021-04-29 21:17:54 +05:30
successCallback = $.noop,
errorCallback = $.noop,
2018-12-13 13:39:08 +05:30
container = '',
2021-04-29 21:17:54 +05:30
} = {}) {
2018-03-17 18:26:18 +05:30
this.limit = limit;
this.offset = parseInt(getParameterByName('offset'), 10) || this.limit;
this.disable = disable;
this.prepareData = prepareData;
2021-04-29 21:17:54 +05:30
this.successCallback = successCallback;
this.errorCallback = errorCallback;
2018-12-13 13:39:08 +05:30
this.loading = $(`${container} .loading`).first();
2018-03-17 18:26:18 +05:30
if (preload) {
this.offset = 0;
this.getOld();
}
this.initLoadMore();
},
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
getOld() {
this.loading.show();
2021-04-29 21:17:54 +05:30
const url = $('.content_list').data('href') || removeParams(['limit', 'offset']);
2018-12-13 13:39:08 +05:30
axios
2021-04-29 21:17:54 +05:30
.get(url, {
2018-12-13 13:39:08 +05:30
params: {
limit: this.limit,
offset: this.offset,
},
})
.then(({ data }) => {
this.append(data.count, this.prepareData(data.html));
2021-04-29 21:17:54 +05:30
this.successCallback();
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
// keep loading until we've filled the viewport height
if (!this.disable && !this.isScrollable()) {
this.getOld();
} else {
this.loading.hide();
}
})
2021-04-29 21:17:54 +05:30
.catch((err) => this.errorCallback(err))
.finally(() => this.loading.hide());
2018-03-17 18:26:18 +05:30
},
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
append(count, html) {
$('.content_list').append(html);
if (count > 0) {
this.offset += count;
2019-12-21 20:55:43 +05:30
if (count < this.limit) {
this.disable = true;
}
2018-03-17 18:26:18 +05:30
} else {
this.disable = true;
}
},
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
isScrollable() {
const $w = $(window);
return $(document).height() > $w.height() + $w.scrollTop() + ENDLESS_SCROLL_BOTTOM_PX;
},
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
initLoadMore() {
2021-02-22 17:27:13 +05:30
// eslint-disable-next-line @gitlab/no-global-event-off
2018-03-27 19:54:05 +05:30
$(document).off('scroll');
2018-03-17 18:26:18 +05:30
$(document).endlessScroll({
bottomPixels: ENDLESS_SCROLL_BOTTOM_PX,
fireDelay: ENDLESS_SCROLL_FIRE_DELAY_MS,
fireOnce: true,
ceaseFire: () => this.disable === true,
callback: () => {
if (!this.loading.is(':visible')) {
this.loading.show();
this.getOld();
}
},
});
},
};