bouquins-bchs/index.js

113 lines
2.8 KiB
JavaScript

var app = new Vue({
el: '#app',
data: {
books: [],
authors: [],
series: [],
booksCount: 0,
page: 1,
perpage: 20
},
methods: {
sendQuery: function(url, error, success) {
var xmh = new XMLHttpRequest();
var v;
xmh.onreadystatechange = function() {
v = xmh.responseText;
if (xmh.readyState === 4 && xmh.status === 200) {
var res;
try {
res = JSON.parse(v);
} catch (err) {
if (null !== error)
error(err.name, err.message);
}
if (null !== success)
success(res);
} else if (xmh.readyState === 4) {
if (null !== error)
error(xmh.status, v);
}
};
xmh.open('GET', url, true);
xmh.send(null);
},
stdError: function(code, resp) {
console.log('ERROR ' + code + ': ' + resp);
},
indexSuccess: function(resp) {
this.booksCount = resp.count;
},
loadIndex: function() {
this.sendQuery('cgi-bin/bouquins/index', this.stdError, this.indexSuccess);
},
authorsSuccess: function(resp) {
this.authors = resp;
},
booksSuccess: function(resp) {
this.books = resp;
},
seriesSuccess: function(resp) {
this.series = resp;
},
prevPage: function() {
if (this.page > 1) {
this.page--;
if (this.books.length > 0)
this.loadBooks();
if (this.authors.length > 0)
this.loadAuthors();
if (this.series.length > 0)
this.loadSeries();
}
},
nextPage: function() {
this.page++;
if (this.books.length > 0)
this.loadBooks();
if (this.authors.length > 0)
this.loadAuthors();
if (this.series.length > 0)
this.loadSeries();
},
loadAuthors: function() {
this.sendQuery('cgi-bin/bouquins/authors?page=' + this.page + '&perpage=' + this.perpage,
this.stdError, this.authorsSuccess);
},
loadBooks: function() {
this.sendQuery('cgi-bin/bouquins/books?page=' + this.page + '&perpage=' + this.perpage,
this.stdError, this.booksSuccess);
},
loadSeries: function() {
this.sendQuery('cgi-bin/bouquins/series?page=' + this.page + '&perpage=' + this.perpage,
this.stdError, this.seriesSuccess);
},
showSeries: function() {
this.books = [];
this.authors = [];
this.page = 1;
this.perpage = 20;
this.loadSeries();
},
showAuthors: function() {
this.books = [];
this.series = [];
this.page = 1;
this.perpage = 20;
this.loadAuthors();
},
showBooks: function() {
this.authors = [];
this.series = [];
this.page = 1;
this.perpage = 20;
this.loadBooks();
}
},
mounted: function() {
this.loadIndex();
}
})