Added current page info to endless scroll

This commit is contained in:
Marcin Kurczewski 2014-10-20 20:30:13 +02:00
parent 20d75e177d
commit 72bd4c479a
10 changed files with 40 additions and 49 deletions

1
TODO
View file

@ -10,7 +10,6 @@ first major release.
- tags: add tag merging - tags: add tag merging
- tags: add tag descriptions - tags: add tag descriptions
- tags: add tag edit snapshots (backed-only) - tags: add tag edit snapshots (backed-only)
- misc: endless pager should include information about page number
- misc: add spinner to forms such as registration, login, settings... - misc: add spinner to forms such as registration, login, settings...
- post notes - post notes

View file

@ -24,7 +24,7 @@
#user-list .users { #user-list .users {
display: inline-block; display: inline-block;
margin: 1em auto; margin: 0 auto;
} }
#user-list .users li { #user-list .users li {
text-align: left; text-align: left;

View file

@ -26,6 +26,10 @@ App.Pager = function(
return pageNumber; return pageNumber;
} }
function getTotalPages() {
return totalPages;
}
function prevPage() { function prevPage() {
if (pageNumber > 1) { if (pageNumber > 1) {
setPage(pageNumber - 1); setPage(pageNumber - 1);
@ -118,6 +122,7 @@ App.Pager = function(
return { return {
init: init, init: init,
getPage: getPage, getPage: getPage,
getTotalPages: getTotalPages,
prevPage: prevPage, prevPage: prevPage,
nextPage: nextPage, nextPage: nextPage,
setPage: setPage, setPage: setPage,

View file

@ -32,8 +32,8 @@ App.Presenters.GlobalCommentListPresenter = function(
baseUri: '#/comments', baseUri: '#/comments',
backendUri: '/comments', backendUri: '/comments',
$target: $el.find('.pagination-target'), $target: $el.find('.pagination-target'),
updateCallback: function(data, clear) { updateCallback: function($page, data) {
renderPosts(data.entities, clear); renderPosts($page, data.entities);
}, },
}, },
function() { function() {
@ -59,13 +59,8 @@ App.Presenters.GlobalCommentListPresenter = function(
$el.html(templates.list()); $el.html(templates.list());
} }
function renderPosts(data, clear) { function renderPosts($page, data) {
var $target = $el.find('.posts'); var $target = $page.find('.posts');
if (clear) {
$target.empty();
}
_.each(data, function(data) { _.each(data, function(data) {
var post = data.post; var post = data.post;
var comments = data.comments; var comments = data.comments;

View file

@ -91,7 +91,18 @@ App.Presenters.PagerPresenter = function(
promise.wait(pager.retrieve()) promise.wait(pager.retrieve())
.then(function(response) { .then(function(response) {
progress.done(); progress.done();
updateCallback(response, forceClear || !endlessScroll);
if (forceClear || !endlessScroll) {
clearContent();
}
var $page = jQuery('<div class="page">');
if (endlessScroll && pager.getTotalPages() > 1) {
$page.append('<p>Page ' + pager.getPage() + ' of ' + pager.getTotalPages() + '</p>');
}
$page.append(targetContent);
$target.find('.pagination-content').append($page);
updateCallback($page, response);
forceClear = false; forceClear = false;
if (!response.entities.length) { if (!response.entities.length) {
messagePresenter.showInfo($messages, 'No data to show'); messagePresenter.showInfo($messages, 'No data to show');
@ -122,7 +133,7 @@ App.Presenters.PagerPresenter = function(
function clearContent() { function clearContent() {
detachNextPageLoader(); detachNextPageLoader();
updateCallback({entities: [], totalRecords: 0}, true); $target.find('.pagination-content').empty();
} }
function attachNextPageLoader() { function attachNextPageLoader() {
@ -183,7 +194,7 @@ App.Presenters.PagerPresenter = function(
} }
function render() { function render() {
$target.html(templates.pager({originalHtml: targetContent})); $target.html(templates.pager());
$messages = $target.find('.pagination-content'); $messages = $target.find('.pagination-content');
$pageList = $target.find('.page-list'); $pageList = $target.find('.page-list');
if (endlessScroll) { if (endlessScroll) {

View file

@ -78,10 +78,10 @@ App.Presenters.PostCommentListPresenter = function(
privileges))); privileges)));
$el.find('.comment-add form button[type=submit]').click(function(e) { commentFormSubmitted(e, null); }); $el.find('.comment-add form button[type=submit]').click(function(e) { commentFormSubmitted(e, null); });
renderComments(comments, true); renderComments(comments);
} }
function renderComments(comments, clear) { function renderComments(comments) {
var $target = $el.find('.comments'); var $target = $el.find('.comments');
var $targetList = $el.find('ul'); var $targetList = $el.find('ul');
@ -91,10 +91,7 @@ App.Presenters.PostCommentListPresenter = function(
$target.hide(); $target.hide();
} }
if (clear) { $targetList.empty();
$targetList.empty();
}
_.each(comments, function(comment) { _.each(comments, function(comment) {
renderComment($targetList, comment); renderComment($targetList, comment);
}); });
@ -206,7 +203,7 @@ App.Presenters.PostCommentListPresenter = function(
promise.wait(api.delete('/comments/' + comment.id)) promise.wait(api.delete('/comments/' + comment.id))
.then(function(response) { .then(function(response) {
comments = _.filter(comments, function(c) { return c.id !== comment.id; }); comments = _.filter(comments, function(c) { return c.id !== comment.id; });
renderComments(comments, true); renderComments(comments);
}).fail(showGenericError); }).fail(showGenericError);
} }

View file

@ -43,8 +43,8 @@ App.Presenters.PostListPresenter = function(
baseUri: '#/posts', baseUri: '#/posts',
backendUri: '/posts', backendUri: '/posts',
$target: $el.find('.pagination-target'), $target: $el.find('.pagination-target'),
updateCallback: function(data, clear) { updateCallback: function($page, data) {
renderPosts(data.entities, clear); renderPosts($page, data.entities);
}, },
}, },
function() { function() {
@ -102,13 +102,8 @@ App.Presenters.PostListPresenter = function(
_.map($el.find('.posts .post-small'), function(postNode) { softRenderPost(jQuery(postNode).parents('li')); }); _.map($el.find('.posts .post-small'), function(postNode) { softRenderPost(jQuery(postNode).parents('li')); });
} }
function renderPosts(posts, clear) { function renderPosts($page, posts) {
var $target = $el.find('.posts'); var $target = $page.find('.posts');
if (clear) {
$target.empty();
}
_.each(posts, function(post) { _.each(posts, function(post) {
var $post = renderPost(post); var $post = renderPost(post);
softRenderPost($post); softRenderPost($post);

View file

@ -38,8 +38,8 @@ App.Presenters.TagListPresenter = function(
baseUri: '#/tags', baseUri: '#/tags',
backendUri: '/tags', backendUri: '/tags',
$target: $el.find('.pagination-target'), $target: $el.find('.pagination-target'),
updateCallback: function(data, clear) { updateCallback: function($page, data) {
renderTags(data.entities, clear); renderTags($page, data.entities);
}, },
}, },
function() { function() {
@ -111,13 +111,8 @@ App.Presenters.TagListPresenter = function(
$el.find('.order [href*="' + activeOrder + '"]').addClass('active'); $el.find('.order [href*="' + activeOrder + '"]').addClass('active');
} }
function renderTags(tags, clear) { function renderTags($page, tags) {
var $target = $el.find('tbody'); var $target = $page.find('tbody');
if (clear) {
$target.empty();
}
_.each(tags, function(tag) { _.each(tags, function(tag) {
var $item = jQuery(templates.listItem({ var $item = jQuery(templates.listItem({
tag: tag, tag: tag,

View file

@ -32,8 +32,8 @@ App.Presenters.UserListPresenter = function(
baseUri: '#/users', baseUri: '#/users',
backendUri: '/users', backendUri: '/users',
$target: $el.find('.pagination-target'), $target: $el.find('.pagination-target'),
updateCallback: function(data, clear) { updateCallback: function($page, data) {
renderUsers(data.entities, clear); renderUsers($page, data.entities);
}, },
}, },
function() { function() {
@ -68,13 +68,8 @@ App.Presenters.UserListPresenter = function(
$el.find('.order [href*="' + activeOrder + '"]').addClass('active'); $el.find('.order [href*="' + activeOrder + '"]').addClass('active');
} }
function renderUsers(users, clear) { function renderUsers($page, users) {
var $target = $el.find('.users'); var $target = $page.find('.users');
if (clear) {
$target.empty();
}
_.each(users, function(user) { _.each(users, function(user) {
var $item = jQuery('<li>' + templates.listItem({ var $item = jQuery('<li>' + templates.listItem({
user: user, user: user,

View file

@ -1,5 +1,4 @@
<div class="pagination-content"> <div class="pagination-content">
<%= originalHtml %>
</div> </div>
<ul class="page-list"> <ul class="page-list">