1 // QuanLib: eBook Library
2 // (C) 2017 by Christian Jaekl (cejaekl@yahoo.com)
12 function constructSearchUrl() {
13 var url = window.location.protocol + '//' + window.location.host + '/search/';
16 var terms = ['aut', 'tit', 'ser'];
19 var term = terms[idx];
20 var elem = document.getElementById(term);
22 console.log('Error: could not find form element for search term "' + term + '".');
26 var value = elem.value;
27 if (value.length > 0) {
35 url += term + '=' + encodeURIComponent('%' + value + '%');
49 console.log('onSearch()');
51 var url = constructSearchUrl();
53 report('Loading data from server, please wait...')
54 console.log('Fetching: "' + url + '"...')
56 fetch(url, {method:'GET', cache:'default'})
57 .then(response => response.json())
58 .then((jsonValue) => {
59 console.log('JSON response: ', jsonValue);
60 g_state.ids = jsonValue
62 g_state.last = (g_state.ids.length) - 1;
63 if (g_state.last > 100) {
69 var msg = 'Error fetching JSON from URL: ' + url + ': ' + err + ':' + err.stack;
75 function refreshData() {
76 report('Loading details for books ' + g_state.first + ' through ' + g_state.last + ', please wait...');
79 var url = '/info/?ids=';
80 for (i = g_state.first; i <= g_state.last; ++i) {
84 url += g_state.ids[i];
87 fetch(url, {method:'GET', cache:'default'})
88 .then(response => response.json())
89 .then((jsonValue) => {
90 console.log('JSON response for info: ', jsonValue);
92 g_state.cache = jsonValue;
96 var msg = 'Error fetching book details via URL: ' + url + ': ' + err;
97 console.log(msg, err.stack);
102 function refreshLayout() {
105 for (i = g_state.first; i <= g_state.last; ++i) {
106 var book = g_state.cache[i];
107 html += bookHtml(book);
110 document.getElementById('books').innerHTML = html;
113 function report(message) {
114 document.getElementById('books').innerHTML = message;
117 function bookHtml(book) {
118 console.log('bookHtml(): ', book);
119 var result = '<div class="book"><table><tr><td><a href="/book/' + book.Id + '">'
120 + '<img class="cover-thumb" src="/download/' + book.CoverId + '"/></a></td>'
121 + '<td><span class="popup">' + book.Description + '</span></td>'
122 + '</tr></table></div>';