2014-05-27 19:03:16 +00:00
|
|
|
var url;
|
|
|
|
var links;
|
|
|
|
var cols;
|
2014-06-19 20:51:00 +00:00
|
|
|
var headers;
|
2014-05-27 19:03:16 +00:00
|
|
|
var urlp={};
|
2014-06-19 20:51:00 +00:00
|
|
|
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
|
|
|
$('#book').click(function() {
|
2014-06-19 20:51:00 +00:00
|
|
|
headers = ['Titre', 'Auteur(s)', 'Serie'];
|
|
|
|
cols = [
|
|
|
|
function(elt) {
|
|
|
|
return link(elt.title, '/book/'+elt.id, 'glyphicon-book');
|
|
|
|
}, function(elt) {
|
|
|
|
var links="";
|
|
|
|
if (Array.isArray(elt.authors)) {
|
|
|
|
$.each(elt.authors, function(ida, author) {
|
|
|
|
links+=link(author.name, '/author/'+author.id,'glyphicon-user');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return links;
|
|
|
|
}, function(elt) {
|
2014-06-20 18:58:12 +00:00
|
|
|
var content = elt.series_name == null ? '' : elt.series_name + '(' + elt.series_index + ')';
|
|
|
|
return link(content, '/serie/'+elt.id, 'glyphicon-list');
|
2014-06-19 20:51:00 +00:00
|
|
|
}
|
|
|
|
];
|
2014-05-27 19:03:16 +00:00
|
|
|
});
|
|
|
|
$('#author').click(function() {
|
2014-06-19 20:51:00 +00:00
|
|
|
headers = ['Nom', 'Livres'];
|
|
|
|
cols = [ function(elt){
|
|
|
|
return link(elt.name, '/author/'+elt.id,'glyphicon-user');
|
|
|
|
}, function(elt) { return elt.count; } ];
|
2014-05-27 19:03:16 +00:00
|
|
|
});
|
|
|
|
$('#serie').click(function() {
|
2014-06-19 20:51:00 +00:00
|
|
|
headers = ['Nom', 'Auteur(s)', 'Livres'];
|
|
|
|
cols = [
|
|
|
|
function(elt) {
|
|
|
|
return link(elt.name, '/serie/'+elt.id, 'glyphicon-list');
|
|
|
|
}, function(elt) {
|
|
|
|
var links="";
|
|
|
|
if (Array.isArray(elt.authors)) {
|
|
|
|
$.each(elt.authors, function(ida, author) {
|
|
|
|
links+=link(author.name, '/author/'+author.id,'glyphicon-user');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return links;
|
|
|
|
}, function(elt) {
|
|
|
|
return elt.count;
|
|
|
|
}
|
|
|
|
];
|
2014-05-27 19:03:16 +00:00
|
|
|
});
|
|
|
|
$.each(['book','author','serie'], function (i, elt) {
|
|
|
|
$('#'+elt).click(function() {
|
|
|
|
url = '/'+elt;
|
|
|
|
loadItems();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
$.each(['prev','next'], function (i, elt) {
|
|
|
|
$('#'+elt).click(function() {
|
|
|
|
var parsed = $.url(links[elt]);
|
|
|
|
url = parsed.attr('path');
|
|
|
|
urlp = parsed.param();
|
|
|
|
loadItems();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
$(".perpage").click(function() {
|
|
|
|
urlp.perpage = $(this).attr("value");
|
|
|
|
urlp.page = 0;
|
|
|
|
loadItems();
|
|
|
|
});
|
|
|
|
|
|
|
|
function loadItems() {
|
|
|
|
$.getJSON( url, urlp, function( data, textStatus, xhr ) {
|
2014-06-19 20:51:00 +00:00
|
|
|
var items = $('#items');
|
|
|
|
items.empty();
|
|
|
|
var item = "<tr>";
|
|
|
|
$.each(headers, function(ih, h) {
|
|
|
|
item+="<th>"+h+"</th>";
|
|
|
|
});
|
|
|
|
item += "</tr>";
|
|
|
|
items.append(item);
|
2014-05-27 19:03:16 +00:00
|
|
|
$.each( data, function(i, elt ) {
|
|
|
|
var item = "<tr id='" + elt.id + "'>";
|
|
|
|
$.each(cols, function(icol, col) {
|
2014-06-19 20:51:00 +00:00
|
|
|
item+="<td>"+col(elt)+"</td>";
|
2014-05-27 19:03:16 +00:00
|
|
|
});
|
|
|
|
item += "</tr>";
|
2014-06-19 20:51:00 +00:00
|
|
|
items.append(item);
|
2014-05-27 19:03:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var linkHeader = xhr.getResponseHeader('link');
|
|
|
|
links = parse_link_header(linkHeader);
|
|
|
|
$.each(['prev','next'], function (i, elt) {
|
|
|
|
var btn = $('#'+elt);
|
|
|
|
if (links[elt]) {
|
|
|
|
btn.parent().removeClass('disabled');
|
|
|
|
} else {
|
|
|
|
btn.parent().addClass('disabled');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* parse_link_header()
|
|
|
|
*
|
|
|
|
* Parse the Github Link HTTP header used for pageination
|
|
|
|
* http://developer.github.com/v3/#pagination
|
|
|
|
*/
|
|
|
|
function parse_link_header(header) {
|
|
|
|
if (header.length == 0) {
|
|
|
|
throw new Error("input must not be of zero length");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split parts by comma
|
|
|
|
var parts = header.split(',');
|
|
|
|
var links = {};
|
|
|
|
// 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;
|
|
|
|
}
|