2014-06-22 10:23:16 +00:00
|
|
|
var ItemsCol = function(id, headers, cols) {
|
|
|
|
this.id = id;
|
|
|
|
this.url= '/'+id;
|
|
|
|
this.headers = headers;//table headers
|
|
|
|
this.cols = cols;//cell rendering functions
|
|
|
|
};
|
|
|
|
$.extend(ItemsCol.prototype,{
|
|
|
|
data: null, // loaded items
|
|
|
|
bind: function() {
|
|
|
|
var self = this;
|
|
|
|
$('#'+this.id) .click(function() {
|
2014-06-28 07:36:47 +00:00
|
|
|
toggleActive(this);
|
2014-06-22 10:23:16 +00:00
|
|
|
home.pagination.page=0;
|
|
|
|
self.load();
|
|
|
|
});
|
|
|
|
},
|
2014-06-28 07:36:47 +00:00
|
|
|
load: function(addparam) {
|
2014-06-22 10:23:16 +00:00
|
|
|
var self = this;
|
2014-06-28 07:36:47 +00:00
|
|
|
$.getJSON( this.url, $.extend({}, home.pagination, addparam),
|
2014-06-29 15:14:00 +00:00
|
|
|
function(data, textStatus, xhr) { self.loadData(data, textStatus, xhr) });
|
|
|
|
},
|
|
|
|
loadData: function(data, textStatus, xhr) {
|
|
|
|
this.data = data;
|
|
|
|
home.current = this;
|
|
|
|
home.update();
|
|
|
|
var linkHeader = xhr.getResponseHeader('link');
|
|
|
|
home.updatePager(parse_link_header(linkHeader));
|
2014-06-22 10:23:16 +00:00
|
|
|
},
|
|
|
|
renderRow: function(elt) {
|
|
|
|
var item = "<tr id='" + elt.id + "'>";
|
|
|
|
$.each(this.cols, function(icol, col) {
|
|
|
|
item+="<td>"+col(elt)+"</td>";
|
|
|
|
});
|
|
|
|
item += "</tr>";
|
|
|
|
return item;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
var HomePage = function() {
|
2014-06-28 23:21:17 +00:00
|
|
|
this.books= new ItemsCol('book', [$.t('bouquins.cols.booktitle'), $.t('bouquins.cols.bookauthors'), $.t('bouquins.cols.bookserie')], [
|
2014-06-19 20:51:00 +00:00
|
|
|
function(elt) {
|
|
|
|
return link(elt.title, '/book/'+elt.id, 'glyphicon-book');
|
2014-06-22 10:23:16 +00:00
|
|
|
},
|
|
|
|
function(elt) {
|
|
|
|
var links='';
|
2014-06-19 20:51:00 +00:00
|
|
|
if (Array.isArray(elt.authors)) {
|
|
|
|
$.each(elt.authors, function(ida, author) {
|
|
|
|
links+=link(author.name, '/author/'+author.id,'glyphicon-user');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return links;
|
2014-06-22 10:23:16 +00:00
|
|
|
},
|
|
|
|
function(elt) {
|
2014-06-20 18:58:12 +00:00
|
|
|
var content = elt.series_name == null ? '' : elt.series_name + '(' + elt.series_index + ')';
|
2014-06-21 22:35:59 +00:00
|
|
|
return link(content, '/serie/'+elt.series_id, 'glyphicon-list');
|
2014-06-19 20:51:00 +00:00
|
|
|
}
|
2014-06-22 10:23:16 +00:00
|
|
|
]);
|
2014-06-28 23:21:17 +00:00
|
|
|
this.authors= new ItemsCol('author', [$.t('bouquins.cols.authorname'), $.t('bouquins.cols.authorbooks')], [
|
2014-06-22 10:23:16 +00:00
|
|
|
function(elt){ return link(elt.name, '/author/'+elt.id,'glyphicon-user'); },
|
|
|
|
function(elt) { return elt.count; }
|
|
|
|
]);
|
2014-06-28 23:21:17 +00:00
|
|
|
this.series= new ItemsCol('serie', [$.t('bouquins.cols.seriename'), $.t('bouquins.cols.serieauthors'), $.t('bouquins.cols.seriebooks')], [
|
2014-06-22 10:23:16 +00:00
|
|
|
function(elt) { return link(elt.name, '/serie/'+elt.id, 'glyphicon-list'); },
|
2014-06-19 20:51:00 +00:00
|
|
|
function(elt) {
|
2014-06-22 10:23:16 +00:00
|
|
|
var links='';
|
2014-06-19 20:51:00 +00:00
|
|
|
if (Array.isArray(elt.authors)) {
|
|
|
|
$.each(elt.authors, function(ida, author) {
|
|
|
|
links+=link(author.name, '/author/'+author.id,'glyphicon-user');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return links;
|
2014-06-22 10:23:16 +00:00
|
|
|
},
|
|
|
|
function(elt) { return elt.count; }
|
|
|
|
]);
|
2014-06-29 15:14:00 +00:00
|
|
|
this.pagination = { perpage: 10, page: 0, };
|
2014-06-22 10:23:16 +00:00
|
|
|
this.table = $('#items');
|
|
|
|
// bind buttons events
|
|
|
|
$.each([this.books,this.authors,this.series], function(ind, itemsCol) {
|
|
|
|
itemsCol.bind();
|
2014-05-27 19:03:16 +00:00
|
|
|
});
|
2014-06-28 07:36:47 +00:00
|
|
|
$('#recent').click(function() {
|
|
|
|
home.pagination.page=0;
|
|
|
|
home.books.load({sort:'recent'});
|
|
|
|
toggleActive(this);
|
|
|
|
});
|
2014-06-29 15:14:00 +00:00
|
|
|
$('#search').click(function() {
|
|
|
|
home.pagination.page=0;
|
|
|
|
$('#searchpanel').toggleClass('hidden');
|
|
|
|
toggleActive(this);
|
|
|
|
});
|
|
|
|
$('#searchform').submit(function(evt) {
|
|
|
|
var formurl = $(this).attr('action');
|
|
|
|
formurl+='?'+$.param(home.pagination);
|
|
|
|
$.post(formurl, $(this).serialize(), function(data, textStatus, xhr) {
|
|
|
|
home.books.loadData(data, textStatus, xhr);
|
|
|
|
}, 'json');
|
|
|
|
return false;
|
|
|
|
});
|
2014-06-22 10:23:16 +00:00
|
|
|
$(".perpage").click(function() {
|
|
|
|
home.pagination.perpage = $(this).attr("value");
|
2014-06-27 16:40:59 +00:00
|
|
|
$('.perpage').removeClass('active');
|
|
|
|
$(this).addClass('active');
|
|
|
|
home.pagination.page = 0;
|
|
|
|
if (home.current)
|
|
|
|
home.current.load();
|
|
|
|
});
|
|
|
|
$(".initial").click(function() {
|
|
|
|
if ($(this).hasClass('active')) {
|
|
|
|
home.pagination.initial = null;
|
|
|
|
$('.initial').removeClass('active');
|
|
|
|
} else {
|
|
|
|
home.pagination.initial = $(this).attr("value");
|
|
|
|
$('.initial').removeClass('active');
|
|
|
|
$(this).addClass('active');
|
|
|
|
}
|
2014-06-22 10:23:16 +00:00
|
|
|
home.pagination.page = 0;
|
|
|
|
if (home.current)
|
|
|
|
home.current.load();
|
2014-05-27 19:03:16 +00:00
|
|
|
});
|
2014-06-28 23:21:17 +00:00
|
|
|
$.each([this.authors,this.books,this.series],function(i,itemsCol) {
|
|
|
|
if (window.location.hash == '#'+itemsCol.id+'s')
|
|
|
|
itemsCol.load();
|
|
|
|
});
|
2014-06-22 10:23:16 +00:00
|
|
|
};
|
|
|
|
$.extend(HomePage.prototype,{
|
|
|
|
current: null,
|
|
|
|
table: null,
|
|
|
|
displayHeaders: function(headers) {
|
|
|
|
var row = "<tr>";
|
2014-06-19 20:51:00 +00:00
|
|
|
$.each(headers, function(ih, h) {
|
2014-06-22 10:23:16 +00:00
|
|
|
row+="<th>"+h+"</th>";
|
2014-06-19 20:51:00 +00:00
|
|
|
});
|
2014-06-22 10:23:16 +00:00
|
|
|
row += "</tr>";
|
|
|
|
this.table.append(row);
|
|
|
|
},
|
|
|
|
addRow: function(elt) {
|
|
|
|
var row = this.current.renderRow(elt);
|
|
|
|
this.table.append(row);
|
|
|
|
},
|
|
|
|
update: function() {
|
|
|
|
this.table.empty();
|
|
|
|
if (this.current) {
|
2014-06-28 06:44:52 +00:00
|
|
|
$('#itemsanchor').attr('name',this.current.id+'s');
|
|
|
|
$('#blkitems').removeClass('hidden');
|
2014-06-22 10:23:16 +00:00
|
|
|
this.displayHeaders(this.current.headers);
|
|
|
|
var self = this;
|
|
|
|
$.each(this.current.data, function(ind, elt) {
|
|
|
|
self.addRow(elt);
|
2014-05-27 19:03:16 +00:00
|
|
|
});
|
2014-06-28 06:44:52 +00:00
|
|
|
} else {
|
|
|
|
$('#blkitems').addClass('hidden');
|
|
|
|
$('#itemsanchor').attr('name',null);
|
2014-06-22 10:23:16 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
updatePager: function(links) {
|
2014-05-27 19:03:16 +00:00
|
|
|
$.each(['prev','next'], function (i, elt) {
|
2014-06-27 16:40:59 +00:00
|
|
|
var btn = $('.'+elt);
|
2014-05-27 19:03:16 +00:00
|
|
|
if (links[elt]) {
|
|
|
|
btn.parent().removeClass('disabled');
|
|
|
|
} else {
|
|
|
|
btn.parent().addClass('disabled');
|
|
|
|
}
|
2014-06-25 14:30:01 +00:00
|
|
|
btn.unbind();
|
2014-06-29 15:14:00 +00:00
|
|
|
if (links[elt]) {
|
|
|
|
btn.click(function() {
|
|
|
|
var parsed = $.url(links[elt]);
|
|
|
|
var urlp = parsed.param();
|
|
|
|
home.pagination.page = urlp.page;
|
|
|
|
home.pagination.perpage = urlp.perpage;
|
|
|
|
home.current.load();
|
|
|
|
});
|
|
|
|
}
|
2014-05-27 19:03:16 +00:00
|
|
|
});
|
2014-06-22 10:23:16 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
/**
|
|
|
|
* Make link.
|
|
|
|
*/
|
|
|
|
function link(content, href, glyph) {
|
|
|
|
var link="";
|
|
|
|
if (content) {
|
|
|
|
if (glyph) link+="<span class=\"glyphicon "+glyph+"\"></span> ";
|
|
|
|
link += "<a href=\""+href+"\">";
|
|
|
|
link+=content+"</a>";
|
|
|
|
}
|
|
|
|
return link;
|
2014-05-27 19:03:16 +00:00
|
|
|
}
|
2014-06-28 07:36:47 +00:00
|
|
|
/**
|
|
|
|
* Toggle Active buttons.
|
|
|
|
*/
|
|
|
|
function toggleActive(btn) {
|
|
|
|
$(btn).siblings().removeClass('active');
|
|
|
|
$(btn).addClass('active');
|
|
|
|
}
|
2014-05-27 19:03:16 +00:00
|
|
|
/*
|
|
|
|
* parse_link_header()
|
|
|
|
*
|
|
|
|
* Parse the Github Link HTTP header used for pageination
|
|
|
|
* http://developer.github.com/v3/#pagination
|
|
|
|
*/
|
|
|
|
function parse_link_header(header) {
|
2014-06-29 15:14:00 +00:00
|
|
|
var links = {};
|
|
|
|
if (header == null || header.length == 0) {
|
|
|
|
return links;
|
2014-05-27 19:03:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Split parts by comma
|
|
|
|
var parts = header.split(',');
|
|
|
|
// Parse each part into a named link
|
|
|
|
$.each(parts, function(i, p) {
|
|
|
|
var section = p.split(';');
|
|
|
|
if (section.length != 2) {
|
|
|
|
throw new Error("section could not be split on ';'");
|
|
|
|
}
|
|
|
|
var url = section[0].replace(/<(.*)>/, '$1').trim();
|
|
|
|
var name = section[1].replace(/rel="(.*)"/, '$1').trim();
|
|
|
|
links[name] = url;
|
|
|
|
});
|
|
|
|
|
|
|
|
return links;
|
|
|
|
}
|