2014-10-01 22:18:50 +02:00
|
|
|
var App = App || {};
|
|
|
|
|
|
|
|
App.Pager = function(
|
|
|
|
_,
|
|
|
|
promise,
|
|
|
|
api) {
|
|
|
|
|
|
|
|
var totalPages;
|
|
|
|
var pageNumber;
|
|
|
|
var searchParams;
|
|
|
|
var url;
|
2014-10-05 10:09:02 +02:00
|
|
|
var cache = {};
|
2014-10-01 22:18:50 +02:00
|
|
|
|
|
|
|
function init(args) {
|
|
|
|
url = args.url;
|
|
|
|
|
|
|
|
setSearchParams(args.searchParams);
|
|
|
|
if (typeof(args.page) !== 'undefined') {
|
|
|
|
setPage(args.page);
|
|
|
|
} else {
|
|
|
|
setPage(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPage() {
|
|
|
|
return pageNumber;
|
|
|
|
}
|
|
|
|
|
2014-10-20 20:30:13 +02:00
|
|
|
function getTotalPages() {
|
|
|
|
return totalPages;
|
|
|
|
}
|
|
|
|
|
2014-10-01 22:18:50 +02:00
|
|
|
function prevPage() {
|
|
|
|
if (pageNumber > 1) {
|
|
|
|
setPage(pageNumber - 1);
|
2014-11-04 19:00:47 +01:00
|
|
|
return true;
|
2014-10-01 22:18:50 +02:00
|
|
|
}
|
2014-11-04 19:00:47 +01:00
|
|
|
return false;
|
2014-10-01 22:18:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function nextPage() {
|
|
|
|
if (pageNumber < totalPages) {
|
|
|
|
setPage(pageNumber + 1);
|
2014-11-04 19:00:47 +01:00
|
|
|
return true;
|
2014-10-01 22:18:50 +02:00
|
|
|
}
|
2014-11-04 19:00:47 +01:00
|
|
|
return false;
|
2014-10-01 22:18:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setPage(newPageNumber) {
|
|
|
|
pageNumber = parseInt(newPageNumber);
|
|
|
|
if (!pageNumber || isNaN(pageNumber)) {
|
|
|
|
throw new Error('Trying to set page to a non-number (' + newPageNumber + ')');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSearchParams() {
|
|
|
|
return searchParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setSearchParams(newSearchParams) {
|
|
|
|
setPage(1);
|
2014-10-09 21:41:46 +02:00
|
|
|
searchParams = _.extend({}, newSearchParams);
|
|
|
|
delete searchParams.page;
|
2014-10-01 22:18:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function retrieve() {
|
|
|
|
return promise.make(function(resolve, reject) {
|
2014-10-09 23:46:32 +02:00
|
|
|
promise.wait(api.get(url, _.extend({}, searchParams, {page: pageNumber})))
|
2014-10-02 00:30:25 +02:00
|
|
|
.then(function(response) {
|
|
|
|
var pageSize = response.json.pageSize;
|
|
|
|
var totalRecords = response.json.totalRecords;
|
|
|
|
totalPages = Math.ceil(totalRecords / pageSize);
|
|
|
|
|
|
|
|
resolve({
|
|
|
|
entities: response.json.data,
|
2014-10-09 21:41:46 +02:00
|
|
|
totalRecords: totalRecords,
|
|
|
|
totalPages: totalPages});
|
2014-10-02 00:30:25 +02:00
|
|
|
|
|
|
|
}).fail(function(response) {
|
|
|
|
reject(response);
|
|
|
|
});
|
2014-10-01 22:18:50 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-05 10:09:02 +02:00
|
|
|
function retrieveCached() {
|
|
|
|
return promise.make(function(resolve, reject) {
|
|
|
|
var cacheKey = JSON.stringify(_.extend({}, searchParams, {url: url, page: getPage()}));
|
|
|
|
if (cacheKey in cache) {
|
|
|
|
resolve.apply(this, cache[cacheKey]);
|
|
|
|
} else {
|
|
|
|
promise.wait(retrieve())
|
|
|
|
.then(function() {
|
|
|
|
cache[cacheKey] = arguments;
|
|
|
|
resolve.apply(this, arguments);
|
|
|
|
}).fail(function() {
|
|
|
|
reject.apply(this, arguments);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function resetCache() {
|
|
|
|
cache = {};
|
|
|
|
}
|
|
|
|
|
2014-10-01 22:18:50 +02:00
|
|
|
function getVisiblePages() {
|
|
|
|
var pages = [1, totalPages || 1];
|
|
|
|
var pagesAroundCurrent = 2;
|
|
|
|
for (var i = -pagesAroundCurrent; i <= pagesAroundCurrent; i ++) {
|
|
|
|
if (pageNumber + i >= 1 && pageNumber + i <= totalPages) {
|
|
|
|
pages.push(pageNumber + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pageNumber - pagesAroundCurrent - 1 === 2) {
|
|
|
|
pages.push(2);
|
|
|
|
}
|
|
|
|
if (pageNumber + pagesAroundCurrent + 1 === totalPages - 1) {
|
|
|
|
pages.push(totalPages - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pages.sort(function(a, b) { return a - b; }).filter(function(item, pos) {
|
|
|
|
return !pos || item !== pages[pos - 1];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
init: init,
|
|
|
|
getPage: getPage,
|
2014-10-20 20:30:13 +02:00
|
|
|
getTotalPages: getTotalPages,
|
2014-10-01 22:18:50 +02:00
|
|
|
prevPage: prevPage,
|
|
|
|
nextPage: nextPage,
|
|
|
|
setPage: setPage,
|
|
|
|
getSearchParams: getSearchParams,
|
|
|
|
setSearchParams: setSearchParams,
|
|
|
|
retrieve: retrieve,
|
2014-10-05 10:09:02 +02:00
|
|
|
retrieveCached: retrieveCached,
|
2014-10-01 22:18:50 +02:00
|
|
|
getVisiblePages: getVisiblePages,
|
2014-10-05 10:09:02 +02:00
|
|
|
resetCache: resetCache,
|
2014-10-01 22:18:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
App.DI.register('pager', ['_', 'promise', 'api'], App.Pager);
|