1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-03 14:24:23 +01:00
fab-manager/app/assets/javascripts/services/pagination_service.js

60 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-11-21 11:08:53 +01:00
'use strict';
Application.Services.factory('paginationService', [function () {
2018-11-21 11:08:53 +01:00
const helpers = {};
2018-11-21 11:08:53 +01:00
helpers.pageCount = (totalCount, perPage) => Math.ceil(totalCount / perPage);
helpers.hasNextPage = function (currentPage, totalCount, perPage) {
2018-11-21 11:08:53 +01:00
const _pageCount = helpers.pageCount(totalCount, perPage);
return (_pageCount !== currentPage) && (_pageCount !== 0);
};
const Instance = function (resourceService, currentPage, perPage, totalCount, defaultQueryParams, callback, functionName) {
2018-11-21 11:08:53 +01:00
this.resourceService = resourceService;
this.currentPage = currentPage;
this.perPage = perPage;
this.totalCount = totalCount;
this.defaultQueryParams = defaultQueryParams;
this.callback = callback;
this.functionName = functionName || 'query';
this.loading = false;
this.pageCount = function () {
2018-11-21 11:08:53 +01:00
return helpers.pageCount(this.totalCount, this.perPage);
};
this.hasNextPage = function () {
2018-11-21 11:08:53 +01:00
return helpers.hasNextPage(this.currentPage, this.totalCount, this.perPage);
};
this.loadMore = function (queryParams) {
2018-11-21 11:08:53 +01:00
let k, v;
this.currentPage += 1;
this.loading = true;
2018-11-21 11:08:53 +01:00
const _queryParams = { page: this.currentPage, per_page: this.perPage };
if (queryParams) {
for (k in queryParams) {
2018-11-21 11:08:53 +01:00
v = queryParams[k];
_queryParams[k] = v;
}
}
for (k in this.defaultQueryParams) {
2018-11-21 11:08:53 +01:00
v = this.defaultQueryParams[k];
_queryParams[k] = v;
}
2018-11-27 15:16:23 +01:00
this.resourceService[this.functionName](_queryParams, dataPromise => {
2018-11-21 11:08:53 +01:00
this.callback(dataPromise);
2018-11-27 15:16:23 +01:00
this.loading = false;
2018-11-21 11:08:53 +01:00
});
};
};
2018-11-21 11:08:53 +01:00
return { Instance };
}
2018-11-21 11:08:53 +01:00
]);