Added hotkeys to paged collections (closed #36)

This commit is contained in:
Marcin Kurczewski 2014-09-12 19:33:27 +02:00
parent c1739d1d1d
commit fa8b7b5376
3 changed files with 26 additions and 8 deletions

View file

@ -8,10 +8,11 @@
<!-- build:template
<title><%= serviceName %></title>
/build -->
<script src="//cdnjs.cloudflare.com/ajax/libs/path.js/0.8.4/path.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/path.js/0.8.4/path.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/mousetrap/1.4.6/mousetrap.min.js"></script>
<!-- build:template
<link rel="stylesheet" type="text/css" href="app.min.css?<%= timestamp %>"/>
/build -->

View file

@ -29,4 +29,5 @@ App.DI.registerSingleton('bootstrap', ['auth', 'router', 'util', 'promise', 'pre
App.DI.registerManual('jQuery', function() { return window.$; });
App.DI.registerManual('pathJs', function() { return window.Path; });
App.DI.registerManual('_', function() { return window._; });
App.DI.registerManual('mousetrap', function() { return window.Mousetrap; });
App.DI.get('bootstrap');

View file

@ -1,7 +1,7 @@
var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.PagedCollectionPresenter = function(_, util, promise, api) {
App.Presenters.PagedCollectionPresenter = function(_, util, promise, api, mousetrap, router) {
var searchOrder;
var searchQuery;
@ -13,6 +13,7 @@ App.Presenters.PagedCollectionPresenter = function(_, util, promise, api) {
var template;
var pageSize;
var totalPages;
var totalRecords;
function init(args) {
@ -27,9 +28,24 @@ App.Presenters.PagedCollectionPresenter = function(_, util, promise, api) {
promise.wait(util.promiseTemplate('pager')).then(function(html) {
template = _.template(html);
changePage(pageNumber);
mousetrap.bind('a', prevPage);
mousetrap.bind('d', nextPage);
});
}
function prevPage() {
if (pageNumber > 1) {
router.navigate(getPageChangeLink(pageNumber - 1));
}
}
function nextPage() {
if (pageNumber < totalPages) {
router.navigate(getPageChangeLink(pageNumber + 1));
}
}
function changePage(newPageNumber) {
pageNumber = newPageNumber;
@ -40,8 +56,9 @@ App.Presenters.PagedCollectionPresenter = function(_, util, promise, api) {
page: pageNumber
}))
.then(function(response) {
totalRecords = response.json.totalRecords;
pageSize = response.json.pageSize;
totalRecords = response.json.totalRecords;
totalPages = Math.ceil(totalRecords / pageSize);
renderCallback({
entities: response.json.data,
totalRecords: response.json.totalRecords});
@ -55,7 +72,6 @@ App.Presenters.PagedCollectionPresenter = function(_, util, promise, api) {
}
function render($target) {
var totalPages = Math.ceil(totalRecords / pageSize);
var pages = [1, totalPages];
var pagesAroundCurrent = 2;
for (var i = -pagesAroundCurrent; i <= pagesAroundCurrent; i ++) {
@ -116,4 +132,4 @@ App.Presenters.PagedCollectionPresenter = function(_, util, promise, api) {
};
App.DI.register('pagedCollectionPresenter', ['_', 'util', 'promise', 'api'], App.Presenters.PagedCollectionPresenter);
App.DI.register('pagedCollectionPresenter', ['_', 'util', 'promise', 'api', 'mousetrap', 'router'], App.Presenters.PagedCollectionPresenter);