2017-01-14 16:38:40 +00:00
|
|
|
var app = new Vue({
|
|
|
|
el: '#app',
|
|
|
|
data: {
|
|
|
|
urlParams: {},
|
|
|
|
authors: [],
|
2017-01-14 16:52:52 +00:00
|
|
|
books: [],
|
2017-01-14 16:38:40 +00:00
|
|
|
series: [],
|
2017-01-15 15:00:05 +00:00
|
|
|
authorsCount: 0,
|
2017-01-15 14:25:49 +00:00
|
|
|
booksCount: 0,
|
|
|
|
seriesCount: 0,
|
|
|
|
q: '',
|
|
|
|
which: 'all',
|
2017-01-15 15:00:05 +00:00
|
|
|
all: false,
|
|
|
|
perpage: 10
|
2017-01-14 16:38:40 +00:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
urlParse: function() {
|
|
|
|
var match,
|
|
|
|
pl = /\+/g, // Regex for replacing addition symbol with a space
|
|
|
|
search = /([^&=]+)=?([^&]*)/g,
|
|
|
|
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
|
|
|
|
query = window.location.search.substring(1);
|
|
|
|
while (match = search.exec(query))
|
|
|
|
this.urlParams[decode(match[1])] = decode(match[2]);
|
|
|
|
},
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
searchParams: function(url) {
|
|
|
|
var res = url;
|
2017-01-15 15:00:05 +00:00
|
|
|
res += '?perpage=' + this.perpage;
|
2017-01-14 16:38:40 +00:00
|
|
|
for (var i=0; i<this.terms.length; i++) {
|
|
|
|
var t = this.terms[i];
|
2017-01-15 15:00:05 +00:00
|
|
|
if (t.trim())
|
|
|
|
res += '&term=' + encodeURIComponent(t.trim());
|
2017-01-14 16:38:40 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
},
|
2017-01-15 15:00:05 +00:00
|
|
|
searchAuthorsSuccess: function(res) {
|
|
|
|
this.authorsCount = res.count;
|
|
|
|
this.authors = res.authors;
|
|
|
|
},
|
|
|
|
searchAuthors: function() {
|
|
|
|
this.sendQuery(this.searchParams('cgi-bin/bouquins/authors'), this.stdError, this.searchAuthorsSuccess);
|
|
|
|
},
|
2017-01-14 16:38:40 +00:00
|
|
|
searchBooksSuccess: function(res) {
|
2017-01-14 16:52:52 +00:00
|
|
|
this.booksCount = res.count;
|
|
|
|
this.books = res.books;
|
2017-01-14 16:38:40 +00:00
|
|
|
},
|
|
|
|
searchBooks: function() {
|
|
|
|
this.sendQuery(this.searchParams('cgi-bin/bouquins/books'), this.stdError, this.searchBooksSuccess);
|
|
|
|
},
|
2017-01-15 14:25:49 +00:00
|
|
|
searchSeriesSuccess: function(res) {
|
|
|
|
this.seriesCount = res.count;
|
|
|
|
this.series = res.series;
|
|
|
|
},
|
|
|
|
searchSeries: function() {
|
|
|
|
this.sendQuery(this.searchParams('cgi-bin/bouquins/series'), this.stdError, this.searchSeriesSuccess);
|
|
|
|
},
|
2017-01-14 16:38:40 +00:00
|
|
|
searchAll: function() {
|
2017-01-14 17:48:08 +00:00
|
|
|
this.clear();
|
2017-01-15 15:00:05 +00:00
|
|
|
this.searchAuthors();
|
2017-01-14 17:48:08 +00:00
|
|
|
this.searchBooks();
|
2017-01-15 14:25:49 +00:00
|
|
|
this.searchSeries();
|
2017-01-14 17:48:08 +00:00
|
|
|
},
|
|
|
|
clear: function() {
|
2017-01-14 16:38:40 +00:00
|
|
|
this.authors = [];
|
|
|
|
this.books = [];
|
|
|
|
this.series = [];
|
2017-01-15 15:00:05 +00:00
|
|
|
this.authorsCount = 0;
|
2017-01-14 16:52:52 +00:00
|
|
|
this.booksCount = 0;
|
2017-01-15 14:25:49 +00:00
|
|
|
this.seriesCount = 0;
|
2017-01-14 17:48:08 +00:00
|
|
|
},
|
|
|
|
searchFull: function() {
|
2017-01-15 14:25:49 +00:00
|
|
|
if (this.q) {
|
|
|
|
this.terms = this.q.split(' ');
|
|
|
|
switch (this.which) {
|
|
|
|
case 'all':
|
|
|
|
this.searchAll();
|
|
|
|
break;
|
2017-01-15 15:00:05 +00:00
|
|
|
case 'authors':
|
|
|
|
this.clear();
|
|
|
|
this.searchAuthors();
|
|
|
|
break;
|
2017-01-15 14:25:49 +00:00
|
|
|
case 'books':
|
|
|
|
this.clear();
|
|
|
|
this.searchBooks();
|
|
|
|
break;
|
|
|
|
case 'series':
|
|
|
|
this.clear();
|
|
|
|
this.searchSeries();
|
|
|
|
break;
|
|
|
|
}
|
2017-01-14 17:48:08 +00:00
|
|
|
}
|
2017-01-15 15:12:04 +00:00
|
|
|
return false;
|
2017-01-14 16:38:40 +00:00
|
|
|
},
|
|
|
|
searchUrl: function() {
|
|
|
|
if (this.urlParams.q) {
|
|
|
|
this.terms = this.urlParams.q.split(' ');
|
|
|
|
this.searchAll();
|
2017-01-15 14:25:49 +00:00
|
|
|
this.q = this.urlParams.q;
|
2017-01-14 16:38:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
created: function() {
|
|
|
|
this.urlParse();
|
|
|
|
},
|
|
|
|
mounted: function() {
|
|
|
|
this.searchUrl();
|
|
|
|
}
|
|
|
|
})
|