2014-09-07 19:49:11 +02:00
|
|
|
var App = App || {};
|
|
|
|
App.Presenters = App.Presenters || {};
|
|
|
|
|
|
|
|
App.Presenters.PostListPresenter = function(
|
2014-09-17 18:34:57 +02:00
|
|
|
_,
|
2014-09-07 19:49:11 +02:00
|
|
|
jQuery,
|
2014-09-17 18:34:57 +02:00
|
|
|
util,
|
|
|
|
promise,
|
|
|
|
auth,
|
2014-09-19 17:37:10 +02:00
|
|
|
keyboard,
|
2014-10-01 23:22:20 +02:00
|
|
|
pagerPresenter,
|
2014-10-01 23:43:39 +02:00
|
|
|
topNavigationPresenter) {
|
2014-09-07 19:49:11 +02:00
|
|
|
|
2014-10-01 22:22:43 +02:00
|
|
|
var KEY_RETURN = 13;
|
|
|
|
|
2014-10-05 10:41:12 +02:00
|
|
|
var templates = {};
|
2014-09-07 19:49:11 +02:00
|
|
|
var $el = jQuery('#content');
|
2014-10-01 22:22:43 +02:00
|
|
|
var $searchInput;
|
2014-09-07 19:49:11 +02:00
|
|
|
|
2014-10-01 22:22:43 +02:00
|
|
|
var searchArgs;
|
|
|
|
|
2014-09-17 22:37:49 +02:00
|
|
|
function init(args, loaded) {
|
2014-09-07 19:49:11 +02:00
|
|
|
topNavigationPresenter.select('posts');
|
2014-09-11 13:59:12 +02:00
|
|
|
topNavigationPresenter.changeTitle('Posts');
|
2014-10-01 22:22:43 +02:00
|
|
|
searchArgs = util.parseComplexRouteArgs(args.searchArgs);
|
2014-10-05 10:09:02 +02:00
|
|
|
searchArgs.page = parseInt(searchArgs.page) || 1;
|
2014-09-17 18:34:57 +02:00
|
|
|
|
2014-10-02 00:30:25 +02:00
|
|
|
promise.wait(
|
2014-09-17 22:37:49 +02:00
|
|
|
util.promiseTemplate('post-list'),
|
|
|
|
util.promiseTemplate('post-list-item'))
|
2014-10-05 10:41:12 +02:00
|
|
|
.then(function(listTemplate, listItemTemplate) {
|
|
|
|
templates.list = listTemplate;
|
|
|
|
templates.listItem = listItemTemplate;
|
2014-09-17 22:37:49 +02:00
|
|
|
|
|
|
|
render();
|
2014-09-18 11:29:19 +02:00
|
|
|
loaded();
|
|
|
|
|
2014-10-01 23:22:20 +02:00
|
|
|
pagerPresenter.init({
|
2014-09-18 11:29:19 +02:00
|
|
|
baseUri: '#/posts',
|
|
|
|
backendUri: '/posts',
|
|
|
|
$target: $el.find('.pagination-target'),
|
|
|
|
updateCallback: function(data, clear) {
|
|
|
|
renderPosts(data.entities, clear);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
function() {
|
2014-10-04 14:06:44 +02:00
|
|
|
onArgsChanged(args);
|
2014-09-18 11:29:19 +02:00
|
|
|
});
|
2014-09-17 22:37:49 +02:00
|
|
|
});
|
2014-09-17 18:34:57 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 22:37:49 +02:00
|
|
|
function reinit(args, loaded) {
|
2014-09-18 11:29:19 +02:00
|
|
|
loaded();
|
2014-10-04 14:06:44 +02:00
|
|
|
onArgsChanged(args);
|
|
|
|
}
|
2014-09-17 18:34:57 +02:00
|
|
|
|
2014-10-04 14:06:44 +02:00
|
|
|
function onArgsChanged(args) {
|
2014-10-01 22:22:43 +02:00
|
|
|
searchArgs = util.parseComplexRouteArgs(args.searchArgs);
|
|
|
|
pagerPresenter.reinit({
|
|
|
|
page: searchArgs.page,
|
|
|
|
searchParams: {
|
|
|
|
query: searchArgs.query,
|
|
|
|
order: searchArgs.order}});
|
2014-09-07 19:49:11 +02:00
|
|
|
}
|
|
|
|
|
2014-10-02 00:30:25 +02:00
|
|
|
function deinit() {
|
|
|
|
pagerPresenter.deinit();
|
|
|
|
}
|
|
|
|
|
2014-09-07 19:49:11 +02:00
|
|
|
function render() {
|
2014-10-05 10:41:12 +02:00
|
|
|
$el.html(templates.list());
|
2014-10-01 22:22:43 +02:00
|
|
|
$searchInput = $el.find('input[name=query]');
|
|
|
|
|
|
|
|
$searchInput.val(searchArgs.query);
|
|
|
|
$searchInput.keydown(searchInputKeyPressed);
|
2014-10-03 23:02:13 +02:00
|
|
|
$el.find('form').submit(searchFormSubmitted);
|
2014-09-19 17:37:10 +02:00
|
|
|
|
|
|
|
keyboard.keyup('p', function() {
|
|
|
|
$el.find('.posts li a').eq(0).focus();
|
|
|
|
});
|
2014-10-01 22:22:43 +02:00
|
|
|
|
|
|
|
keyboard.keyup('q', function() {
|
|
|
|
$searchInput.eq(0).focus();
|
|
|
|
});
|
2014-09-17 18:34:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function renderPosts(posts, clear) {
|
|
|
|
var $target = $el.find('.posts');
|
|
|
|
|
2014-10-04 14:06:44 +02:00
|
|
|
if (clear) {
|
|
|
|
$target.empty();
|
|
|
|
}
|
|
|
|
|
2014-09-17 18:34:57 +02:00
|
|
|
_.each(posts, function(post) {
|
2014-10-05 11:05:34 +02:00
|
|
|
var $post = jQuery('<li>' + templates.listItem({
|
2014-10-05 10:09:02 +02:00
|
|
|
searchArgs: searchArgs,
|
2014-09-17 18:34:57 +02:00
|
|
|
post: post,
|
2014-10-05 11:05:34 +02:00
|
|
|
}) + '</li>')
|
|
|
|
util.loadImagesNicely($post.find('img'));
|
|
|
|
$target.append($post);
|
2014-09-17 18:34:57 +02:00
|
|
|
});
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-09-07 19:49:11 +02:00
|
|
|
|
2014-10-01 22:22:43 +02:00
|
|
|
function searchInputKeyPressed(e) {
|
|
|
|
if (e.which !== KEY_RETURN) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-03 23:02:13 +02:00
|
|
|
updateSearch();
|
|
|
|
}
|
|
|
|
|
|
|
|
function searchFormSubmitted(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
updateSearch();
|
|
|
|
}
|
2014-10-01 22:22:43 +02:00
|
|
|
|
2014-10-03 23:02:13 +02:00
|
|
|
function updateSearch() {
|
2014-10-01 22:22:43 +02:00
|
|
|
$searchInput.blur();
|
|
|
|
pagerPresenter.setSearchParams({
|
|
|
|
query: $searchInput.val(),
|
|
|
|
order: searchArgs.order});
|
|
|
|
}
|
|
|
|
|
2014-09-07 19:49:11 +02:00
|
|
|
return {
|
|
|
|
init: init,
|
2014-09-17 18:34:57 +02:00
|
|
|
reinit: reinit,
|
2014-10-02 00:30:25 +02:00
|
|
|
deinit: deinit,
|
2014-09-07 19:49:11 +02:00
|
|
|
render: render,
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-10-01 23:43:39 +02:00
|
|
|
App.DI.register('postListPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'keyboard', 'pagerPresenter', 'topNavigationPresenter'], App.Presenters.PostListPresenter);
|