Added query to post list

This commit is contained in:
Marcin Kurczewski 2014-10-01 22:22:43 +02:00
parent da41fd7a96
commit 7d8dfd797f
4 changed files with 47 additions and 5 deletions

View file

@ -7,6 +7,13 @@
margin: 0 auto; margin: 0 auto;
} }
#post-list .search {
margin-bottom: 1em;
}
#post-list .search input {
max-width: 20em;
}
#post-list ul { #post-list ul {
list-style-type: none; list-style-type: none;
padding: 0; padding: 0;

View file

@ -7,19 +7,24 @@ App.Presenters.PostListPresenter = function(
util, util,
promise, promise,
auth, auth,
router,
keyboard, keyboard,
pagerPresenter, pagerPresenter,
topNavigationPresenter, topNavigationPresenter,
messagePresenter) { messagePresenter) {
var KEY_RETURN = 13;
var $el = jQuery('#content'); var $el = jQuery('#content');
var $searchInput;
var listTemplate; var listTemplate;
var itemTemplate; var itemTemplate;
var searchArgs;
function init(args, loaded) { function init(args, loaded) {
topNavigationPresenter.select('posts'); topNavigationPresenter.select('posts');
topNavigationPresenter.changeTitle('Posts'); topNavigationPresenter.changeTitle('Posts');
searchArgs = util.parseComplexRouteArgs(args.searchArgs);
promise.waitAll( promise.waitAll(
util.promiseTemplate('post-list'), util.promiseTemplate('post-list'),
@ -53,16 +58,28 @@ App.Presenters.PostListPresenter = function(
function reinit(args, loaded) { function reinit(args, loaded) {
loaded(); loaded();
var searchArgs = util.parseComplexRouteArgs(args.searchArgs); searchArgs = util.parseComplexRouteArgs(args.searchArgs);
pagerPresenter.reinit({page: searchArgs.page, searchParams: {query: searchArgs.query, order: searchArgs.order}}); pagerPresenter.reinit({
page: searchArgs.page,
searchParams: {
query: searchArgs.query,
order: searchArgs.order}});
} }
function render() { function render() {
$el.html(listTemplate()); $el.html(listTemplate());
$searchInput = $el.find('input[name=query]');
$searchInput.val(searchArgs.query);
$searchInput.keydown(searchInputKeyPressed);
keyboard.keyup('p', function() { keyboard.keyup('p', function() {
$el.find('.posts li a').eq(0).focus(); $el.find('.posts li a').eq(0).focus();
}); });
keyboard.keyup('q', function() {
$searchInput.eq(0).focus();
});
} }
function renderPosts(posts, clear) { function renderPosts(posts, clear) {
@ -82,6 +99,17 @@ App.Presenters.PostListPresenter = function(
} }
} }
function searchInputKeyPressed(e) {
if (e.which !== KEY_RETURN) {
return;
}
$searchInput.blur();
pagerPresenter.setSearchParams({
query: $searchInput.val(),
order: searchArgs.order});
}
return { return {
init: init, init: init,
reinit: reinit, reinit: reinit,
@ -90,4 +118,4 @@ App.Presenters.PostListPresenter = function(
}; };
App.DI.register('postListPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'router', 'keyboard', 'pagerPresenter', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.PostListPresenter); App.DI.register('postListPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'keyboard', 'pagerPresenter', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.PostListPresenter);

View file

@ -13,6 +13,8 @@ App.Presenters.PostUploadPresenter = function(
topNavigationPresenter, topNavigationPresenter,
messagePresenter) { messagePresenter) {
var KEY_RETURN = 13;
var $el = jQuery('#content'); var $el = jQuery('#content');
var $messages; var $messages;
var template; var template;
@ -72,7 +74,7 @@ App.Presenters.PostUploadPresenter = function(
} }
function urlHandlerKeyPressed(e) { function urlHandlerKeyPressed(e) {
if (e.which !== 13) { if (e.which !== KEY_RETURN) {
return; return;
} }

View file

@ -1,4 +1,9 @@
<div id="post-list"> <div id="post-list">
<div class="search">
<input type="text" name="query" placeholder="Search query..."/>
<button type="submit">Search</button>
</div>
<div class="pagination-target"> <div class="pagination-target">
<div class="wrapper"> <div class="wrapper">
<ul class="posts"> <ul class="posts">