1 // QuanLib: eBook Library
2 // (C) 2017 by Christian Jaekl (cejaekl@yahoo.com)
13 function constructSearchUrl() {
14 var url = window.location.protocol + '//' + window.location.host + '/search/';
17 var terms = ['aut', 'tit', 'ser'];
20 var term = terms[idx];
21 var elem = document.getElementById(term);
23 console.log('Error: could not find form element for search term "' + term + '".');
27 var value = elem.value;
28 if (value.length > 0) {
36 url += term + '=' + encodeURIComponent('%' + value + '%');
44 if (g_state.last < (g_state.count - 1)) {
45 g_state.first += g_state.pageSize;
46 g_state.last += g_state.pageSize;
47 if (g_state.last >= g_state.count) {
48 g_state.last = g_state.count - 1;
56 if (g_state.first > 0) {
57 g_state.first -= g_state.pageSize;
58 if (g_state.first < 0) {
61 g_state.last = g_state.first + g_state.pageSize;
68 console.log('onSearch()');
70 var url = constructSearchUrl();
72 report('Loading data from server, please wait...')
73 console.log('Fetching: "' + url + '"...')
75 fetch(url, {method:'GET', cache:'default'})
76 .then(response => response.json())
77 .then((jsonValue) => {
78 console.log('JSON response: ', jsonValue);
79 g_state.ids = jsonValue
80 g_state.count = g_state.ids.length;
82 g_state.last = (g_state.ids.length) - 1;
83 if (g_state.last > g_state.pageSize) {
84 g_state.last = g_state.pageSize;
89 var msg = 'Error fetching JSON from URL: ' + url + ': ' + err + ':' + err.stack;
95 function refreshData() {
96 report('Loading details for books ' + g_state.first + ' through ' + g_state.last + ', please wait...');
99 var url = '/info/?ids=';
100 for (i = g_state.first; i <= g_state.last; ++i) {
101 if (i > g_state.first) {
104 url += g_state.ids[i];
107 fetch(url, {method:'GET', cache:'default'})
108 .then(response => response.json())
109 .then((jsonValue) => {
110 console.log('JSON response for info: ', jsonValue);
112 g_state.cache = jsonValue;
116 var msg = 'Error fetching book details via URL: ' + url + ': ' + err;
117 console.log(msg, err.stack);
122 function refreshLayout() {
125 var limit = g_state.last - g_state.first;
126 for (i = 0; i <= limit; ++i) {
127 var book = g_state.cache[i];
128 html += bookHtml(book);
131 document.getElementById('books').innerHTML = html;
132 document.getElementById('first').innerHTML = (g_state.first + 1);
133 document.getElementById('last').innerHTML = (g_state.last + 1);
134 document.getElementById('count').innerHTML = g_state.count;
137 function report(message) {
138 document.getElementById('books').innerHTML = message;
141 function bookHtml(book) {
142 console.log('bookHtml(): ', book);
143 var result = '<div class="book">'
146 + '<td><a href="/book/' + book.Id + '">';
147 if (0 == book.CoverId) {
148 result += '(No cover available)'
150 result += '<img class="cover-thumb" src="/download/' + book.CoverId + '"/>'
152 result += '</a></td>'
154 + '<p><b>' + book.Title + '</b></p>'
156 + '<i>' + book.AuthorReading + '</i>';
157 if (typeof(book.SeriesName) !== 'undefined' && book.SeriesName.length > 0) {
158 result += '<br/><i>' + book.SeriesName + ' ' + book.Volume + '</i>';