bouquins-bchs/index.js

146 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-12-30 16:16:43 +00:00
var app = new Vue({
el: '#app',
data: {
2016-12-30 17:18:11 +00:00
books: [],
2017-01-07 11:27:59 +00:00
authors: [],
2017-01-08 16:15:01 +00:00
series: [],
2016-12-31 16:19:47 +00:00
booksCount: 0,
page: 1,
2017-01-13 20:17:07 +00:00
perpage: 20,
sort_by: null,
order_desc: false
2016-12-30 17:18:11 +00:00
},
2016-12-31 11:16:42 +00:00
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);
2016-12-31 11:16:42 +00:00
},
2017-01-07 11:27:59 +00:00
authorsSuccess: function(resp) {
this.authors = resp;
},
2016-12-31 11:16:42 +00:00
booksSuccess: function(resp) {
this.books = resp;
},
2017-01-08 16:15:01 +00:00
seriesSuccess: function(resp) {
this.series = resp;
},
2017-01-13 20:17:07 +00:00
sortBy: function(col) {
if (this.sort_by == col) {
if (this.order_desc) {
this.order_desc = false;
this.sort_by = null;
} else {
this.order_desc = true;
}
} else {
this.order_desc = false;
this.sort_by = col;
2016-12-31 16:19:47 +00:00
}
2017-01-13 20:17:07 +00:00
this.reload();
2016-12-31 16:19:47 +00:00
},
2017-01-13 20:17:07 +00:00
reload: function() {
2017-01-07 11:42:52 +00:00
if (this.books.length > 0)
this.loadBooks();
if (this.authors.length > 0)
this.loadAuthors();
2017-01-08 16:15:01 +00:00
if (this.series.length > 0)
this.loadSeries();
2016-12-31 16:19:47 +00:00
},
2017-01-13 20:17:07 +00:00
prevPage: function() {
if (this.page > 1) {
this.page--;
this.reload();
}
},
nextPage: function() {
this.page++;
this.reload();
},
order: function(query) {
if (this.order_desc)
return query + '&order=desc';
return query;
},
sort: function(query) {
if (this.sort_by)
return query + '&sort=' + this.sort_by;
return query;
},
paginate: function(query) {
return query + '?page=' + this.page + '&perpage=' + this.perpage;
},
params: function(url) {
return this.order(this.sort(this.paginate(url)));
},
2017-01-07 11:27:59 +00:00
loadAuthors: function() {
2017-01-13 20:17:07 +00:00
this.sendQuery(this.params('cgi-bin/bouquins/authors'), this.stdError, this.authorsSuccess);
2017-01-07 11:27:59 +00:00
},
2016-12-31 11:16:42 +00:00
loadBooks: function() {
2017-01-13 20:17:07 +00:00
this.sendQuery(this.params('cgi-bin/bouquins/books'), this.stdError, this.booksSuccess);
2017-01-07 11:42:52 +00:00
},
2017-01-08 16:15:01 +00:00
loadSeries: function() {
2017-01-13 20:17:07 +00:00
this.sendQuery(this.params('cgi-bin/bouquins/series'), this.stdError, this.seriesSuccess);
2017-01-08 16:15:01 +00:00
},
showSeries: function() {
this.books = [];
this.authors = [];
this.page = 1;
this.perpage = 20;
2017-01-13 20:17:07 +00:00
this.order_desc = false;
this.sort_by = null;
2017-01-08 16:15:01 +00:00
this.loadSeries();
},
2017-01-07 11:42:52 +00:00
showAuthors: function() {
this.books = [];
2017-01-08 16:15:01 +00:00
this.series = [];
2017-01-07 11:42:52 +00:00
this.page = 1;
this.perpage = 20;
2017-01-13 20:17:07 +00:00
this.order_desc = false;
this.sort_by = null;
2017-01-07 11:42:52 +00:00
this.loadAuthors();
},
showBooks: function() {
this.authors = [];
2017-01-08 16:15:01 +00:00
this.series = [];
2017-01-07 11:42:52 +00:00
this.page = 1;
this.perpage = 20;
2017-01-13 20:17:07 +00:00
this.order_desc = false;
this.sort_by = null;
2017-01-07 11:42:52 +00:00
this.loadBooks();
2016-12-31 11:16:42 +00:00
}
},
2016-12-30 17:18:11 +00:00
mounted: function() {
2016-12-31 11:16:42 +00:00
this.loadIndex();
2016-12-30 16:16:43 +00:00
}
})