1 // QuanLib: eBook Library
2 // (C) 2017 by Christian Jaekl (cejaekl@yahoo.com)
13 function adjustPos(setting) {
14 var value = parseInt(setting)
16 if (g_state.first === value) {
21 var maxFirst = g_state.count - g_state.pageSize;
25 } else if (value > maxFirst) {
26 g_state.first = maxFirst;
28 g_state.first = value;
31 g_state.last = g_state.first + g_state.pageSize - 1;
32 if (g_state.last >= g_state.count) {
33 g_state.last = g_state.count - 1;
39 function constructSearchUrl() {
40 var url = window.location.protocol + '//' + window.location.host + '/search/';
43 var terms = ['aut', 'tit', 'ser'];
46 var term = terms[idx];
47 var elem = document.getElementById(term);
49 console.log('Error: could not find form element for search term "' + term + '".');
53 var value = elem.value;
54 if (value.length > 0) {
62 url += term + '=' + encodeURIComponent('%' + value + '%');
70 if (g_state.last < (g_state.count - 1)) {
71 adjustPos(g_state.first + g_state.pageSize);
76 if (g_state.first > 0) {
77 adjustPos(g_state.first - g_state.pageSize);
81 function onSlide(value) {
86 console.log('onSearch()');
88 var url = constructSearchUrl();
90 report('Loading data from server, please wait...')
91 console.log('Fetching: "' + url + '"...')
93 fetch(url, {method:'GET', cache:'default'})
94 .then(response => response.json())
95 .then((jsonValue) => {
96 console.log('JSON response: ', jsonValue);
97 g_state.ids = jsonValue
98 g_state.count = g_state.ids.length;
101 var elem = document.getElementById('slider');
102 elem.max = g_state.count;
108 var msg = 'Error fetching JSON from URL: ' + url + ': ' + err + ':' + err.stack;
114 function refreshData() {
115 report('Loading details for books ' + g_state.first + ' through ' + g_state.last + ', please wait...');
118 var url = '/info/?ids=';
119 for (i = g_state.first; i <= g_state.last; ++i) {
120 if (i > g_state.first) {
123 url += g_state.ids[i];
126 fetch(url, {method:'GET', cache:'default'})
127 .then(response => response.json())
128 .then((jsonValue) => {
129 console.log('JSON response for info: ', jsonValue);
131 g_state.cache = jsonValue;
135 var msg = 'Error fetching book details via URL: ' + url + ': ' + err;
136 console.log(msg, err.stack);
141 function refreshLayout() {
144 var limit = g_state.last - g_state.first;
145 for (i = 0; i <= limit; ++i) {
146 var book = g_state.cache[i];
147 html += bookHtml(book);
150 document.getElementById('books').innerHTML = html;
151 document.getElementById('first').innerHTML = (g_state.first + 1);
152 document.getElementById('last').innerHTML = (g_state.last + 1);
153 document.getElementById('count').innerHTML = g_state.count;
156 function report(message) {
157 document.getElementById('books').innerHTML = message;
160 function bookHtml(book) {
161 console.log('bookHtml(): ', book);
162 var result = '<div class="book">'
165 + '<td><a href="/book/' + book.Id + '">';
166 if (0 == book.CoverId) {
167 result += '(No cover available)'
169 result += '<img class="cover-thumb" src="/download/' + book.CoverId + '"/>'
171 result += '</a></td>'
173 + '<p><b>' + book.Title + '</b></p>'
175 + '<i>' + book.AuthorReading + '</i>';
176 if (typeof(book.SeriesName) !== 'undefined' && book.SeriesName.length > 0) {
177 result += '<br/><i>' + book.SeriesName + ' ' + book.Volume + '</i>';