From f9a2ce72d6baacc9f820b793d24715426c0f3640 Mon Sep 17 00:00:00 2001 From: Meutel Date: Sat, 28 Jun 2014 08:44:52 +0200 Subject: [PATCH] refactoring initial link home page with items --- public/js/home.js | 11 +++++++++-- routes/author.js | 19 ++++--------------- routes/book.js | 28 +++++----------------------- routes/serie.js | 10 ++++++++-- util/paginate.js | 19 +++++++++++++++++++ views/author.jade | 2 +- views/book.jade | 2 +- views/home.jade | 9 +++++---- views/serie.jade | 2 +- 9 files changed, 53 insertions(+), 49 deletions(-) diff --git a/public/js/home.js b/public/js/home.js index 1386965..ac93980 100644 --- a/public/js/home.js +++ b/public/js/home.js @@ -9,7 +9,6 @@ $.extend(ItemsCol.prototype,{ bind: function() { var self = this; $('#'+this.id) .click(function() { - window.location.hash=self.id; home.pagination.page=0; home.pagination.perpage=10; self.load(); @@ -119,11 +118,16 @@ $.extend(HomePage.prototype,{ update: function() { this.table.empty(); if (this.current) { + $('#itemsanchor').attr('name',this.current.id+'s'); + $('#blkitems').removeClass('hidden'); this.displayHeaders(this.current.headers); var self = this; $.each(this.current.data, function(ind, elt) { self.addRow(elt); }); + } else { + $('#blkitems').addClass('hidden'); + $('#itemsanchor').attr('name',null); } }, updatePager: function(links) { @@ -146,7 +150,10 @@ $.extend(HomePage.prototype,{ }, }); var home = new HomePage(); - +$.each([home.authors,home.books,home.series],function(i,itemsCol) { + if (window.location.hash == '#'+itemsCol.id+'s') + itemsCol.load(); +}); /** * Make link. diff --git a/routes/author.js b/routes/author.js index 9377618..94e70e0 100644 --- a/routes/author.js +++ b/routes/author.js @@ -5,29 +5,18 @@ var HashMap = require('hashmap').HashMap; /* All authors */ router.get('/', function(req, res) { + var authors = new Array(); + var qparams = new Array(); var query = 'SELECT authors.id as id,name,count(*) as count'+ ' FROM authors,books_authors_link'+ ' WHERE authors.id = books_authors_link.author'; - var initial = req.query.initial; - console.log(initial); - if (initial) { - query+=' AND '; - if (initial == '0') { - query+=' (substr(authors.sort,1,1) < ? OR substr(authors.sort,1,1) > ?)'; - qparams.push('A'); - qparams.push('Z'); - } else { - query+=' UPPER(authors.sort) LIKE ?'; - qparams.push(req.query.initial.toUpperCase()+'%'); - } - } + req.paginate = new paginate(req); + query = req.paginate.appendInitialQuery(query,'authors.sort',qparams,false); query+=' GROUP BY books_authors_link.author'+ ' ORDER BY sort LIMIT ? OFFSET ?'; - req.paginate = new paginate(req); qparams.push(req.paginate.perpage + 1); qparams.push(req.paginate.offset); - var authors = new Array(); req.db.each(query, qparams, function (err, row) { if (authors.length < req.paginate.perpage) authors.push(row); diff --git a/routes/book.js b/routes/book.js index 537f9c7..112cbf2 100644 --- a/routes/book.js +++ b/routes/book.js @@ -5,26 +5,16 @@ var HashMap = require('hashmap').HashMap; /* All books */ router.get('/', function(req, res) { + var books = new HashMap(); + + req.paginate = new paginate(req); var qparams = new Array(); var query = 'SELECT books.id as id,title,series_index,name as series_name,series.id AS series_id'+ ' FROM books' + ' LEFT OUTER JOIN books_series_link ON books.id = books_series_link.book' + ' LEFT OUTER JOIN series ON series.id = books_series_link.series'; - var initial = req.query.initial; - if (initial) { - query+=' WHERE'; - if (initial == '0') { - query+=' substr(books.sort,1,1) < ? OR substr(books.sort,1,1) > ?'; - qparams.push('A'); - qparams.push('Z'); - } else { - query+=' UPPER(books.sort) LIKE ?'; - qparams.push(req.query.initial.toUpperCase()+'%'); - } - } + query = req.paginate.appendInitialQuery(query,'books.sort',qparams,true); query+= ' ORDER BY books.sort LIMIT ? OFFSET ?'; - var books = new HashMap(); - req.paginate = new paginate(req); qparams.push(req.paginate.perpage + 1); qparams.push(req.paginate.offset); req.db.each(query, qparams, @@ -49,15 +39,7 @@ router.get('/', function(req, res) { }, function(err) { if (err) console.log(err); - res.format({ - html: function(){ - //TODO load items in home page - res.render('home', books.values()); - }, - json: function(){ - res.json(books.values()); - } - }); + res.json(books.values()); } ); } diff --git a/routes/serie.js b/routes/serie.js index 912b997..acf22e5 100644 --- a/routes/serie.js +++ b/routes/serie.js @@ -5,10 +5,16 @@ var HashMap = require('hashmap').HashMap; /* All series */ router.get('/', function(req, res) { - var query = 'SELECT series.id as id,name,count(*) as count FROM series,books_series_link WHERE books_series_link.series = series.id GROUP BY books_series_link.series ORDER BY sort LIMIT ? OFFSET ?'; var series = new HashMap(); + + var qparams = new Array(); + var query = 'SELECT series.id as id,name,count(*) as count FROM series,books_series_link WHERE books_series_link.series = series.id'; req.paginate = new paginate(req); - req.db.each(query, req.paginate.perpage + 1, req.paginate.offset, function (err, row) { + query = req.paginate.appendInitialQuery(query,'sort',qparams,false); + query+=' GROUP BY books_series_link.series ORDER BY sort LIMIT ? OFFSET ?'; + qparams.push(req.paginate.perpage + 1); + qparams.push(req.paginate.offset); + req.db.each(query, qparams, function (err, row) { if (series.count() < req.paginate.perpage) series.set(''+row.id,row); else diff --git a/util/paginate.js b/util/paginate.js index d3ed2c0..af620c5 100644 --- a/util/paginate.js +++ b/util/paginate.js @@ -6,6 +6,7 @@ var paginate = function(req) { this.offset = this.page * this.perpage; this.oUrl = url.parse(req.originalUrl, true); this.hasNext = false; + this.initial = req.query.initial; }; paginate.prototype = { @@ -23,6 +24,24 @@ paginate.prototype = { this.oUrl.query.page = this.page; } return links; + }, + /** Append search clause for initial to query */ + appendInitialQuery: function(query, column ,qparams, where) { + if (this.initial) { + if(where) + query+=' WHERE'; + else + query+=' AND'; + if (this.initial == '0') { + query+=' substr('+column+',1,1) < ? OR substr('+column+',1,1) > ?'; + qparams.push('A'); + qparams.push('Z'); + } else { + query+=' UPPER('+column+') LIKE ?'; + qparams.push(this.initial.toUpperCase()+'%'); + } + } + return query; } }; diff --git a/views/author.jade b/views/author.jade index dcdd2f3..d3b3887 100644 --- a/views/author.jade +++ b/views/author.jade @@ -5,7 +5,7 @@ block content li a(href="/") Home li - a(href="/author") Auteurs + a(href="/#authors") Auteurs li.active= title div.container div.page-header diff --git a/views/book.jade b/views/book.jade index 274b0ac..f442216 100644 --- a/views/book.jade +++ b/views/book.jade @@ -5,7 +5,7 @@ block content li a(href="/") Home li - a(href="/book") Livres + a(href="/#books") Livres li.active= title div.container div.page-header diff --git a/views/home.jade b/views/home.jade index 2d9db52..92de603 100644 --- a/views/home.jade +++ b/views/home.jade @@ -5,12 +5,13 @@ block content h1= title p Naviguez dans la bibliothèque. p - a#book.btn.btn-primary.btn-lg(role="button") Livres + a#book.btn.btn-primary.btn-lg(href="#books",role="button") Livres   - a#author.btn.btn-primary.btn-lg(role="button") Auteurs + a#author.btn.btn-primary.btn-lg(href="#authors",role="button") Auteurs   - a#serie.btn.btn-primary.btn-lg(role="button") Series - div.container-fluid + a#serie.btn.btn-primary.btn-lg(href="#series",role="button") Series + div#blkitems.container-fluid.hidden + a#itemsanchor div.btn-group - var initials = '0ABCDEFGHIPQRSTUVWXYZ'; - for (var i=0;i