From b7974b7193fe1a42799dc0c2578ad15db6e566c9 Mon Sep 17 00:00:00 2001 From: Meutel Date: Tue, 27 May 2014 21:03:16 +0200 Subject: [PATCH] =?UTF-8?q?utilisation=20moteur=20template=20jade=20am?= =?UTF-8?q?=C3=A9liration=20pagination?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bouquins.js | 4 +- public/index.html | 148 ----------------------------------- public/js/home.js | 86 ++++++++++++++++++++ routes/{index.js => home.js} | 2 +- views/home.jade | 24 ++++++ views/index.jade | 5 -- views/layout.jade | 9 ++- 7 files changed, 119 insertions(+), 159 deletions(-) delete mode 100644 public/index.html create mode 100644 public/js/home.js rename routes/{index.js => home.js} (75%) create mode 100644 views/home.jade delete mode 100644 views/index.jade diff --git a/bouquins.js b/bouquins.js index 5c6a4fd..f1d79ac 100644 --- a/bouquins.js +++ b/bouquins.js @@ -6,7 +6,7 @@ var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var sqlite3 = require('sqlite3').verbose(); -var routes = require('./routes/index'); +var home = require('./routes/home'); var author = require('./routes/author'); var book = require('./routes/book'); var tag = require('./routes/tag'); @@ -33,7 +33,7 @@ app.use(bodyParser.urlencoded()); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); -app.use('/', routes); +app.use('/', home); app.use('/author', author); app.use('/book', book); app.use('/tag', tag); diff --git a/public/index.html b/public/index.html deleted file mode 100644 index c7094d8..0000000 --- a/public/index.html +++ /dev/null @@ -1,148 +0,0 @@ - - - - - - - Bouquins - - - - - - - - - -
-

Bibliothèque

-

Naviguez dans la bibliothèque.

-

- Livres - Auteurs - Series -

-
-
- - - -
Empty
-
-

Elements per page:

-
- -
-
-
- - - - - - - - - - - diff --git a/public/js/home.js b/public/js/home.js new file mode 100644 index 0000000..458031f --- /dev/null +++ b/public/js/home.js @@ -0,0 +1,86 @@ +var url; +var links; +var cols; +var urlp={}; +$('#book').click(function() { + cols = [ 'title', 'author_sort' ]; +}); +$('#author').click(function() { + cols = [ 'name' ]; +}); +$('#serie').click(function() { + cols = [ 'name' ]; +}); +$.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 ) { + $('#items').empty(); + var items = []; + $.each( data, function(i, elt ) { + var item = ""; + $.each(cols, function(icol, col) { + item+=""+elt[col]+""; + }); + item += ""; + items.push(item); + }); + $('#items').append(items.join("")); + + 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; +} diff --git a/routes/index.js b/routes/home.js similarity index 75% rename from routes/index.js rename to routes/home.js index 896c948..398b36e 100644 --- a/routes/index.js +++ b/routes/home.js @@ -3,7 +3,7 @@ var router = express.Router(); /* GET home page. */ router.get('/', function(req, res) { - res.render('index', { title: 'Express' }); + res.render('home', { title: 'Bibliothèque' }); }); module.exports = router; diff --git a/views/home.jade b/views/home.jade new file mode 100644 index 0000000..4ea3529 --- /dev/null +++ b/views/home.jade @@ -0,0 +1,24 @@ +extends layout + +block content + div.jumbotron + h1= title + p Naviguez dans la bibliothèque. + p + a#book.btn.btn-primary.btn-lg(role="button") Livres +   + a#author.btn.btn-primary.btn-lg(role="button") Auteurs +   + a#serie.btn.btn-primary.btn-lg(role="button") Series + div.container-fluid + ul.pager + li.previous.disabled: a#prev Précédent + each p in [10,20,50,100] + li: button.btn.btn-link.perpage(type="button",value=p)= p + li.next.disabled: a#next Suivant + table#items.table.table-striped + tr: td.disabled Vide + script(src="js/jquery.min.js") + script(src="js/bootstrap.min.js") + script(src="js/purl.js") + script(src="js/home.js") diff --git a/views/index.jade b/views/index.jade deleted file mode 100644 index 3d63b9a..0000000 --- a/views/index.jade +++ /dev/null @@ -1,5 +0,0 @@ -extends layout - -block content - h1= title - p Welcome to #{title} diff --git a/views/layout.jade b/views/layout.jade index b945f57..826d8ab 100644 --- a/views/layout.jade +++ b/views/layout.jade @@ -1,7 +1,10 @@ doctype html -html +html(lang="fr") head + meta(charset="utf-8") + meta(http-equiv="X-UA-Compatible",content="IE=edge") + meta(name="viewport",content="width=device-width, initial-scale=1") title= title - link(rel='stylesheet', href='/stylesheets/style.css') + link(rel='stylesheet', href='/css/bootstrap.min.css') body - block content \ No newline at end of file + block content