From 00c02da766f33a1914eb6438098276d912ed13ea Mon Sep 17 00:00:00 2001 From: Meutel Date: Sat, 28 Jun 2014 09:36:47 +0200 Subject: [PATCH] recent books --- public/js/home.js | 29 +++++++++++++++++++++-------- routes/book.js | 6 +++++- views/home.jade | 2 ++ 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/public/js/home.js b/public/js/home.js index ac93980..144704f 100644 --- a/public/js/home.js +++ b/public/js/home.js @@ -9,19 +9,20 @@ $.extend(ItemsCol.prototype,{ bind: function() { var self = this; $('#'+this.id) .click(function() { + toggleActive(this); home.pagination.page=0; - home.pagination.perpage=10; self.load(); }); }, - load: function() { + load: function(addparam) { var self = this; - $.getJSON( this.url, home.pagination, function( data, textStatus, xhr ) { - self.data = data; - home.current = self; - home.update(); - var linkHeader = xhr.getResponseHeader('link'); - home.updatePager(parse_link_header(linkHeader)); + $.getJSON( this.url, $.extend({}, home.pagination, addparam), + function( data, textStatus, xhr ) { + self.data = data; + home.current = self; + home.update(); + var linkHeader = xhr.getResponseHeader('link'); + home.updatePager(parse_link_header(linkHeader)); }); }, renderRow: function(elt) { @@ -78,6 +79,11 @@ var HomePage = function() { $.each([this.books,this.authors,this.series], function(ind, itemsCol) { itemsCol.bind(); }); + $('#recent').click(function() { + home.pagination.page=0; + home.books.load({sort:'recent'}); + toggleActive(this); + }); $(".perpage").click(function() { home.pagination.perpage = $(this).attr("value"); $('.perpage').removeClass('active'); @@ -167,6 +173,13 @@ function link(content, href, glyph) { } return link; } +/** + * Toggle Active buttons. + */ +function toggleActive(btn) { + $(btn).siblings().removeClass('active'); + $(btn).addClass('active'); +} /* * parse_link_header() * diff --git a/routes/book.js b/routes/book.js index 112cbf2..3de4851 100644 --- a/routes/book.js +++ b/routes/book.js @@ -14,7 +14,11 @@ router.get('/', function(req, res) { ' LEFT OUTER JOIN books_series_link ON books.id = books_series_link.book' + ' LEFT OUTER JOIN series ON series.id = books_series_link.series'; query = req.paginate.appendInitialQuery(query,'books.sort',qparams,true); - query+= ' ORDER BY books.sort LIMIT ? OFFSET ?'; + if (req.query.sort == 'recent') + query+= ' ORDER BY books.last_modified DESC'; + else + query+= ' ORDER BY books.sort'; + query+= ' LIMIT ? OFFSET ?'; qparams.push(req.paginate.perpage + 1); qparams.push(req.paginate.offset); req.db.each(query, qparams, diff --git a/views/home.jade b/views/home.jade index 92de603..356f8b8 100644 --- a/views/home.jade +++ b/views/home.jade @@ -10,6 +10,8 @@ block content a#author.btn.btn-primary.btn-lg(href="#authors",role="button") Auteurs   a#serie.btn.btn-primary.btn-lg(href="#series",role="button") Series +   + a#recent.btn.btn-warning.btn-lg(href="#books",role="button") Derniers ajouts div#blkitems.container-fluid.hidden a#itemsanchor div.btn-group