refactoring initial

link home page with items
This commit is contained in:
Meutel 2014-06-28 08:44:52 +02:00
parent 37d25a0bc1
commit f9a2ce72d6
9 changed files with 53 additions and 49 deletions

View File

@ -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.

View File

@ -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);

View File

@ -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());
}
);
}

View File

@ -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

View File

@ -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;
}
};

View File

@ -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

View File

@ -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

View File

@ -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
&nbsp;
a#author.btn.btn-primary.btn-lg(role="button") Auteurs
a#author.btn.btn-primary.btn-lg(href="#authors",role="button") Auteurs
&nbsp;
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<initials.length;i++) {

View File

@ -5,7 +5,7 @@ block content
li
a(href="/") Home
li
a(href="/serie") Series
a(href="/#series") Series
li.active= title
div.container
div.page-header