2017-08-06 11:12:59 +00:00
|
|
|
var bus = new Vue();
|
|
|
|
|
2017-08-06 15:18:27 +00:00
|
|
|
// COMMONS //
|
|
|
|
|
|
|
|
var BOOKS = 'books', AUTHORS = 'authors', SERIES = 'series';
|
|
|
|
var BOUQUINS_TYPES = {
|
|
|
|
books: { icon: 'book', singular: 'livre', plural: 'livres',
|
|
|
|
tab_cols: [ { id: 'title', name: 'Titre', sort: 'title' },
|
|
|
|
{ id: 'authors', name: 'Auteur(s)' },
|
|
|
|
{ id: 'series', name: 'Serie' } ] },
|
|
|
|
authors: { icon: 'user', singular: 'auteur', plural: 'auteurs',
|
|
|
|
tab_cols: [ { id: 'author_name', name: 'Nom', sort: 'name' },
|
|
|
|
{ id: 'count', name: 'Livre(s)' } ] },
|
|
|
|
series: { icon: 'list', singular: 'serie', plural: 'series',
|
|
|
|
tab_cols: [ { id: 'serie_name', name: 'Nom', sort: 'name' },
|
|
|
|
{ id: 'count', name: 'Livre(s)' },
|
|
|
|
{ id: 'authors', name: 'Auteur(s)' } ] }
|
|
|
|
};
|
|
|
|
function ty(type) {
|
|
|
|
if (BOUQUINS_TYPES[type]) return BOUQUINS_TYPES[type]
|
|
|
|
console.log("ERROR: Unknown type: " + type);
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
function icon(type) {
|
|
|
|
return ty(type).icon;
|
|
|
|
}
|
|
|
|
function iconClass(type) {
|
2019-09-12 17:09:52 +00:00
|
|
|
return 'fas fa-' + icon(type);
|
2017-08-06 15:18:27 +00:00
|
|
|
}
|
|
|
|
function url(type, id) {
|
|
|
|
if (id) return ty(type) ? '/'+type+'/'+id:'';
|
|
|
|
return ty(type) ? '/'+type+'/':'';
|
|
|
|
}
|
|
|
|
function label(type, count) {
|
|
|
|
return count == 1 ? ty(type).singular : ty(type).plural;
|
|
|
|
}
|
|
|
|
function stdError(code, resp) {
|
|
|
|
console.log('ERROR ' + code + ': ' + resp);
|
|
|
|
}
|
|
|
|
function sendQuery(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.setRequestHeader('Accept','application/json');
|
|
|
|
xmh.send(null);
|
|
|
|
}
|
|
|
|
|
2017-08-06 11:12:59 +00:00
|
|
|
// COMPONENTS //
|
|
|
|
|
|
|
|
Vue.component('results-list', {
|
|
|
|
template: '#results-list-template',
|
|
|
|
props: ['results', 'count', 'type'],
|
|
|
|
methods: {
|
2017-08-06 15:18:27 +00:00
|
|
|
url: function(item) { return url(this.type, item.id); },
|
2017-08-06 11:12:59 +00:00
|
|
|
label: function(item) {
|
|
|
|
switch (this.type) {
|
2017-08-06 15:18:27 +00:00
|
|
|
case BOOKS:
|
2017-08-06 11:12:59 +00:00
|
|
|
return item.title;
|
2017-08-06 15:18:27 +00:00
|
|
|
case AUTHORS:
|
|
|
|
case SERIES:
|
2017-08-06 11:12:59 +00:00
|
|
|
return item.name;
|
|
|
|
default:
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
iconClass: function() {
|
2017-08-06 15:18:27 +00:00
|
|
|
return iconClass(this.type);
|
2017-08-06 11:12:59 +00:00
|
|
|
},
|
|
|
|
countlabel: function() {
|
2017-08-06 15:18:27 +00:00
|
|
|
return label(this.type, this.count);
|
2017-08-06 11:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Vue.component('results', {
|
|
|
|
template: '#results-template',
|
|
|
|
props: ['results', 'cols','sort_by','order_desc'],
|
|
|
|
methods: {
|
|
|
|
sortBy: function(col) {
|
|
|
|
bus.$emit('sort-on', col);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Vue.component('result-cell', {
|
|
|
|
render: function(h) {
|
|
|
|
return h('td', this.cellContent(h));
|
|
|
|
},
|
|
|
|
props: ['item', 'col'],
|
|
|
|
methods: {
|
2017-08-06 15:18:27 +00:00
|
|
|
link: function(h, type, text, id) {
|
2017-08-06 11:12:59 +00:00
|
|
|
return [
|
2017-08-06 15:18:27 +00:00
|
|
|
h('span',{ attrs: { class: iconClass(type) } },''),
|
2017-08-06 11:12:59 +00:00
|
|
|
' ',
|
2017-08-06 15:18:27 +00:00
|
|
|
h('a', { attrs: { href: url(type, id) } }, text)
|
2017-08-06 11:12:59 +00:00
|
|
|
];
|
|
|
|
},
|
|
|
|
badge: function(h, num) {
|
2019-09-12 17:09:52 +00:00
|
|
|
return h('span', { attrs: { class: 'ml-1 badge badge-pill badge-secondary' } }, num);
|
2017-08-06 11:12:59 +00:00
|
|
|
},
|
|
|
|
cellContent: function(h) {
|
|
|
|
switch (this.col.id) {
|
|
|
|
case 'author_name':
|
2017-08-06 15:18:27 +00:00
|
|
|
return this.link(h, AUTHORS, this.item.name, this.item.id);
|
2017-08-06 11:12:59 +00:00
|
|
|
case 'serie_name':
|
2017-08-06 15:18:27 +00:00
|
|
|
return this.link(h, SERIES, this.item.name, this.item.id);
|
2017-08-06 11:12:59 +00:00
|
|
|
case 'count':
|
|
|
|
return this.item.count;
|
|
|
|
case 'title':
|
2017-08-06 15:18:27 +00:00
|
|
|
return this.link(h, BOOKS, this.item.title, this.item.id);
|
2017-08-06 11:12:59 +00:00
|
|
|
case 'authors':
|
|
|
|
var elts = [];
|
|
|
|
var authors = this.item.authors;
|
|
|
|
if (authors) {
|
|
|
|
for (i=0;i<authors.length;i++) {
|
2017-08-06 15:18:27 +00:00
|
|
|
elts[i] = this.link(h, AUTHORS, authors[i].name, authors[i].id);
|
2017-08-06 11:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return elts;
|
|
|
|
case 'series':
|
|
|
|
var series = this.item.series;
|
|
|
|
if (series) {
|
|
|
|
return [
|
2017-08-06 15:18:27 +00:00
|
|
|
this.link(h, SERIES, series.name, series.id),
|
2019-09-12 17:09:52 +00:00
|
|
|
h('span', { attrs: { class: 'ml-1 badge badge-pill badge-secondary' } }, this.item.series_idx)
|
2017-08-06 11:12:59 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
default:
|
|
|
|
console.log('ERROR unknown col: ' + this.col.id)
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Vue.component('paginate', {
|
|
|
|
template: '#paginate-template',
|
|
|
|
props: ['page','more'],
|
|
|
|
methods: {
|
|
|
|
prevPage: function() {
|
|
|
|
if (this.page > 1) bus.$emit('update-page', -1);
|
|
|
|
},
|
|
|
|
nextPage: function() {
|
|
|
|
if (this.more) bus.$emit('update-page', 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// PAGES //
|
|
|
|
|
|
|
|
if (document.getElementById("index")) {
|
|
|
|
new Vue({
|
|
|
|
el: '#index',
|
|
|
|
data: {
|
|
|
|
url: '',
|
|
|
|
page: 0,
|
|
|
|
perpage: 20,
|
|
|
|
more: false,
|
|
|
|
sort_by: null,
|
|
|
|
order_desc: false,
|
|
|
|
cols: [],
|
|
|
|
results: []
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
this.updateResults();
|
|
|
|
},
|
|
|
|
updatePage: function(p) {
|
|
|
|
this.page += p;
|
|
|
|
this.updateResults();
|
|
|
|
},
|
|
|
|
order: function(query) {
|
2017-08-06 15:18:27 +00:00
|
|
|
return query + (this.order_desc ? '&order=desc' : '');
|
2017-08-06 11:12:59 +00:00
|
|
|
},
|
|
|
|
sort: function(query) {
|
2017-08-06 15:18:27 +00:00
|
|
|
return query + (this.sort_by ? '&sort=' + this.sort_by : '');
|
2017-08-06 11:12:59 +00:00
|
|
|
},
|
|
|
|
paginate: function(query) {
|
|
|
|
return query + '?page=' + this.page + '&perpage=' + this.perpage;
|
|
|
|
},
|
|
|
|
params: function(url) {
|
|
|
|
return this.order(this.sort(this.paginate(url)));
|
|
|
|
},
|
|
|
|
updateResults: function() {
|
2017-08-06 15:18:27 +00:00
|
|
|
sendQuery(this.params(this.url), stdError, this.loadResults);
|
2017-08-06 11:12:59 +00:00
|
|
|
},
|
|
|
|
showSeries: function() {
|
2017-08-06 15:18:27 +00:00
|
|
|
this.url = url(SERIES);
|
2017-08-06 11:12:59 +00:00
|
|
|
this.updateResults();
|
|
|
|
},
|
|
|
|
showAuthors: function() {
|
2017-08-06 15:18:27 +00:00
|
|
|
this.url = url(AUTHORS);
|
2017-08-06 11:12:59 +00:00
|
|
|
this.updateResults();
|
|
|
|
},
|
|
|
|
showBooks: function() {
|
2017-08-06 15:18:27 +00:00
|
|
|
this.url = url(BOOKS);
|
2017-08-06 11:12:59 +00:00
|
|
|
this.updateResults();
|
|
|
|
},
|
|
|
|
loadCols: function(type) {
|
2017-08-06 15:18:27 +00:00
|
|
|
this.cols = ty(type).tab_cols;
|
2017-08-06 11:12:59 +00:00
|
|
|
},
|
|
|
|
loadResults(resp) {
|
|
|
|
this.results = [];
|
|
|
|
this.more = resp.more;
|
|
|
|
this.loadCols(resp.type);
|
|
|
|
if (resp.results) {
|
|
|
|
this.results = resp.results;
|
|
|
|
if (this.page == 0) this.page = 1;
|
|
|
|
} else {
|
|
|
|
this.page = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted: function() {
|
|
|
|
bus.$on('sort-on', this.sortBy);
|
|
|
|
bus.$on('update-page', this.updatePage);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (document.getElementById("author")) {
|
|
|
|
new Vue({
|
|
|
|
el: '#author',
|
|
|
|
data: {
|
2017-08-06 15:18:27 +00:00
|
|
|
tab: BOOKS
|
2017-08-06 11:12:59 +00:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
showBooks: function() {
|
2017-08-06 15:18:27 +00:00
|
|
|
this.tab = BOOKS;
|
2017-08-06 11:12:59 +00:00
|
|
|
},
|
|
|
|
showAuthors: function() {
|
2017-08-06 15:18:27 +00:00
|
|
|
this.tab = AUTHORS;
|
2017-08-06 11:12:59 +00:00
|
|
|
},
|
|
|
|
showSeries: function() {
|
2017-08-06 15:18:27 +00:00
|
|
|
this.tab = SERIES;
|
2017-08-06 11:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (document.getElementById("search")) {
|
|
|
|
new Vue({
|
|
|
|
el: '#search',
|
|
|
|
data: {
|
2017-08-06 13:41:58 +00:00
|
|
|
urlParams: [],
|
2017-08-06 11:12:59 +00:00
|
|
|
authors: [],
|
|
|
|
books: [],
|
|
|
|
series: [],
|
|
|
|
authorsCount: 0,
|
|
|
|
booksCount: 0,
|
|
|
|
seriesCount: 0,
|
|
|
|
q: '',
|
|
|
|
which: 'all',
|
|
|
|
all: false,
|
|
|
|
perpage: 10
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
searchParams: function(url) {
|
2017-08-06 15:18:27 +00:00
|
|
|
var res = url + '?perpage=' + this.perpage;
|
2017-08-06 11:12:59 +00:00
|
|
|
for (var i=0; i<this.terms.length; i++) {
|
|
|
|
var t = this.terms[i];
|
|
|
|
if (t.trim())
|
|
|
|
res += '&term=' + encodeURIComponent(t.trim());
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
},
|
|
|
|
searchAuthorsSuccess: function(res) {
|
|
|
|
this.authorsCount = res.count;
|
|
|
|
this.authors = res.results;
|
|
|
|
},
|
|
|
|
searchAuthors: function() {
|
2017-08-06 15:18:27 +00:00
|
|
|
sendQuery(this.searchParams(url(AUTHORS)), stdError, this.searchAuthorsSuccess);
|
2017-08-06 11:12:59 +00:00
|
|
|
},
|
|
|
|
searchBooksSuccess: function(res) {
|
|
|
|
this.booksCount = res.count;
|
|
|
|
this.books = res.results;
|
|
|
|
},
|
|
|
|
searchBooks: function() {
|
2017-08-06 15:18:27 +00:00
|
|
|
sendQuery(this.searchParams(url(BOOKS)), stdError, this.searchBooksSuccess);
|
2017-08-06 11:12:59 +00:00
|
|
|
},
|
|
|
|
searchSeriesSuccess: function(res) {
|
|
|
|
this.seriesCount = res.count;
|
|
|
|
this.series = res.results;
|
|
|
|
},
|
|
|
|
searchSeries: function() {
|
2017-08-06 15:18:27 +00:00
|
|
|
sendQuery(this.searchParams(url(SERIES)), stdError, this.searchSeriesSuccess);
|
2017-08-06 11:12:59 +00:00
|
|
|
},
|
|
|
|
searchAll: function() {
|
|
|
|
this.searchAuthors();
|
|
|
|
this.searchBooks();
|
|
|
|
this.searchSeries();
|
|
|
|
},
|
|
|
|
clear: function() {
|
|
|
|
this.authors = [];
|
|
|
|
this.books = [];
|
|
|
|
this.series = [];
|
|
|
|
this.authorsCount = 0;
|
|
|
|
this.booksCount = 0;
|
|
|
|
this.seriesCount = 0;
|
|
|
|
},
|
|
|
|
searchFull: function() {
|
|
|
|
if (this.q) {
|
|
|
|
this.terms = this.q.split(' ');
|
2017-08-06 15:18:27 +00:00
|
|
|
this.clear();
|
2017-08-06 11:12:59 +00:00
|
|
|
switch (this.which) {
|
2017-08-06 15:18:27 +00:00
|
|
|
case AUTHORS:
|
2017-08-06 11:12:59 +00:00
|
|
|
this.searchAuthors();
|
|
|
|
break;
|
2017-08-06 15:18:27 +00:00
|
|
|
case BOOKS:
|
2017-08-06 11:12:59 +00:00
|
|
|
this.searchBooks();
|
|
|
|
break;
|
2017-08-06 15:18:27 +00:00
|
|
|
case SERIES:
|
2017-08-06 11:12:59 +00:00
|
|
|
this.searchSeries();
|
|
|
|
break;
|
2017-08-06 15:18:27 +00:00
|
|
|
default:
|
|
|
|
this.searchAll();
|
|
|
|
break;
|
2017-08-06 11:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
searchUrl: function() {
|
|
|
|
if (this.urlParams.q) {
|
|
|
|
this.terms = this.urlParams.q.split(' ');
|
2017-08-06 15:18:27 +00:00
|
|
|
this.clear();
|
2017-08-06 11:12:59 +00:00
|
|
|
this.searchAll();
|
|
|
|
this.q = this.urlParams.q;
|
|
|
|
}
|
|
|
|
},
|
2017-08-06 13:41:58 +00:00
|
|
|
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]);
|
2017-08-06 15:18:27 +00:00
|
|
|
}
|
2017-08-06 13:41:58 +00:00
|
|
|
},
|
|
|
|
created: function() {
|
|
|
|
this.urlParse();
|
|
|
|
},
|
|
|
|
mounted: function() {
|
|
|
|
this.searchUrl();
|
2017-08-06 11:12:59 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|