2014-09-03 19:07:53 +02:00
|
|
|
var App = App || {};
|
|
|
|
App.Presenters = App.Presenters || {};
|
|
|
|
|
2014-09-12 19:33:27 +02:00
|
|
|
App.Presenters.PagedCollectionPresenter = function(_, util, promise, api, mousetrap, router) {
|
2014-09-03 19:07:53 +02:00
|
|
|
|
|
|
|
var searchOrder;
|
|
|
|
var searchQuery;
|
|
|
|
var pageNumber;
|
|
|
|
var baseUri;
|
|
|
|
var backendUri;
|
|
|
|
var renderCallback;
|
2014-09-04 19:57:06 +02:00
|
|
|
var failCallback;
|
2014-09-03 19:07:53 +02:00
|
|
|
|
|
|
|
var template;
|
|
|
|
var pageSize;
|
2014-09-12 19:33:27 +02:00
|
|
|
var totalPages;
|
2014-09-03 19:07:53 +02:00
|
|
|
var totalRecords;
|
|
|
|
|
|
|
|
function init(args) {
|
2014-09-07 09:57:01 +02:00
|
|
|
pageNumber = parseInt(args.page) || 1;
|
|
|
|
searchOrder = args.order;
|
|
|
|
searchQuery = args.query;
|
2014-09-03 19:07:53 +02:00
|
|
|
baseUri = args.baseUri;
|
|
|
|
backendUri = args.backendUri;
|
|
|
|
renderCallback = args.renderCallback;
|
2014-09-04 19:57:06 +02:00
|
|
|
failCallback = args.failCallback;
|
2014-09-03 19:07:53 +02:00
|
|
|
|
2014-09-04 18:06:25 +02:00
|
|
|
promise.wait(util.promiseTemplate('pager')).then(function(html) {
|
2014-09-03 19:07:53 +02:00
|
|
|
template = _.template(html);
|
|
|
|
changePage(pageNumber);
|
2014-09-12 19:33:27 +02:00
|
|
|
|
|
|
|
mousetrap.bind('a', prevPage);
|
|
|
|
mousetrap.bind('d', nextPage);
|
2014-09-03 19:07:53 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-09-12 22:03:29 +02:00
|
|
|
function prevPage(e) {
|
|
|
|
if (e.altKey || e.ctrlKey) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-12 19:33:27 +02:00
|
|
|
if (pageNumber > 1) {
|
|
|
|
router.navigate(getPageChangeLink(pageNumber - 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-12 22:03:29 +02:00
|
|
|
function nextPage(e) {
|
|
|
|
if (e.altKey || e.ctrlKey) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-12 19:33:27 +02:00
|
|
|
if (pageNumber < totalPages) {
|
|
|
|
router.navigate(getPageChangeLink(pageNumber + 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-03 19:07:53 +02:00
|
|
|
function changePage(newPageNumber) {
|
|
|
|
pageNumber = newPageNumber;
|
|
|
|
|
2014-09-04 18:06:25 +02:00
|
|
|
promise.wait(
|
|
|
|
api.get(backendUri, {
|
|
|
|
order: searchOrder,
|
|
|
|
query: searchQuery,
|
|
|
|
page: pageNumber
|
|
|
|
}))
|
|
|
|
.then(function(response) {
|
|
|
|
pageSize = response.json.pageSize;
|
2014-09-12 19:33:27 +02:00
|
|
|
totalRecords = response.json.totalRecords;
|
|
|
|
totalPages = Math.ceil(totalRecords / pageSize);
|
2014-09-04 18:06:25 +02:00
|
|
|
renderCallback({
|
|
|
|
entities: response.json.data,
|
|
|
|
totalRecords: response.json.totalRecords});
|
|
|
|
}).fail(function(response) {
|
2014-09-04 19:57:06 +02:00
|
|
|
if (typeof(failCallback) !== 'undefined') {
|
|
|
|
failCallback(response);
|
|
|
|
} else {
|
2014-09-10 13:55:35 +02:00
|
|
|
console.log(new Error(response.json && response.json.error || response));
|
2014-09-04 19:57:06 +02:00
|
|
|
}
|
2014-09-04 18:06:25 +02:00
|
|
|
});
|
2014-09-03 19:07:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function render($target) {
|
|
|
|
var pages = [1, totalPages];
|
|
|
|
var pagesAroundCurrent = 2;
|
2014-09-08 22:02:28 +02:00
|
|
|
for (var i = -pagesAroundCurrent; i <= pagesAroundCurrent; i ++) {
|
|
|
|
if (pageNumber + i >= 1 && pageNumber + i <= totalPages) {
|
2014-09-03 19:07:53 +02:00
|
|
|
pages.push(pageNumber + i);
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pageNumber - pagesAroundCurrent - 1 === 2) {
|
2014-09-03 19:07:53 +02:00
|
|
|
pages.push(2);
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
|
|
|
if (pageNumber + pagesAroundCurrent + 1 === totalPages - 1) {
|
2014-09-03 19:07:53 +02:00
|
|
|
pages.push(totalPages - 1);
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-09-03 19:07:53 +02:00
|
|
|
|
|
|
|
pages = pages.sort(function(a, b) { return a - b; }).filter(function(item, pos) {
|
2014-09-08 22:02:28 +02:00
|
|
|
return !pos || item !== pages[pos - 1];
|
2014-09-03 19:07:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$target.html(template({
|
|
|
|
pages: pages,
|
|
|
|
pageNumber: pageNumber,
|
|
|
|
link: getPageChangeLink,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSearchQueryChangeLink(newSearchQuery) {
|
|
|
|
return util.compileComplexRouteArgs(baseUri, {
|
|
|
|
page: 1,
|
|
|
|
order: searchOrder,
|
|
|
|
query: newSearchQuery,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSearchOrderChangeLink(newSearchOrder) {
|
|
|
|
return util.compileComplexRouteArgs(baseUri, {
|
|
|
|
page: 1,
|
|
|
|
order: newSearchOrder,
|
|
|
|
query: searchQuery,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPageChangeLink(newPageNumber) {
|
|
|
|
return util.compileComplexRouteArgs(baseUri, {
|
|
|
|
page: newPageNumber,
|
|
|
|
order: searchOrder,
|
|
|
|
query: searchQuery,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
init: init,
|
|
|
|
render: render,
|
|
|
|
changePage: changePage,
|
|
|
|
getSearchQueryChangeLink: getSearchQueryChangeLink,
|
|
|
|
getSearchOrderChangeLink: getSearchOrderChangeLink,
|
|
|
|
getPageChangeLink: getPageChangeLink
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-09-12 19:33:27 +02:00
|
|
|
App.DI.register('pagedCollectionPresenter', ['_', 'util', 'promise', 'api', 'mousetrap', 'router'], App.Presenters.PagedCollectionPresenter);
|