X-Git-Url: http://jaekl.net/gitweb/?p=quanweb.git;a=blobdiff_plain;f=app%2Flib.js;fp=app%2Flib.js;h=7175471c084b4822b8f479e84b91858c78d079e8;hp=0000000000000000000000000000000000000000;hb=ca96e0b6276f6efe56b102ad8286a2534d6e264b;hpb=bb350871a3ae81484ae3a736b16018e340908251 diff --git a/app/lib.js b/app/lib.js new file mode 100644 index 0000000..7175471 --- /dev/null +++ b/app/lib.js @@ -0,0 +1,125 @@ +// QuanLib: eBook Library +// (C) 2017 by Christian Jaekl (cejaekl@yahoo.com) + +g_state = { + cache: {}, + count: 0, + first: 0, + ids: [], + last: 0, +} + +function constructSearchUrl() { + var url = window.location.protocol + '//' + window.location.host + '/search/'; + + var firstTime = true; + var terms = ['aut', 'tit', 'ser']; + + for (idx in terms) { + var term = terms[idx]; + var elem = document.getElementById(term); + if (null === elem) { + console.log('Error: could not find form element for search term "' + term + '".'); + continue; + } + + var value = elem.value; + if (value.length > 0) { + if (firstTime) { + url += '?'; + firstTime = false; + } + else { + url += '&'; + } + url += term + '=' + encodeURIComponent('%' + value + '%'); + } + } + + return url; +} + +function onNext() { +} + +function onPrev() { +} + +function onSearch() { + console.log('onSearch()'); + + var url = constructSearchUrl(); + + report('Loading data from server, please wait...') + console.log('Fetching: "' + url + '"...') + + fetch(url, {method:'GET', cache:'default'}) + .then(response => response.json()) + .then((jsonValue) => { + console.log('JSON response: ', jsonValue); + g_state.ids = jsonValue + g_state.first = 0 + g_state.last = (g_state.ids.length) - 1; + if (g_state.last > 100) { + g_state.last = 100; + } + refreshData() + }) + .catch(err => { + var msg = 'Error fetching JSON from URL: ' + url + ': ' + err + ':' + err.stack; + console.log(msg); + report(msg); + }); +} + +function refreshData() { + report('Loading details for books ' + g_state.first + ' through ' + g_state.last + ', please wait...'); + + var i; + var url = '/info/?ids='; + for (i = g_state.first; i <= g_state.last; ++i) { + if (i > 0) { + url += ','; + } + url += g_state.ids[i]; + } + + fetch(url, {method:'GET', cache:'default'}) + .then(response => response.json()) + .then((jsonValue) => { + console.log('JSON response for info: ', jsonValue); + report(''); + g_state.cache = jsonValue; + refreshLayout(); + }) + .catch(err => { + var msg = 'Error fetching book details via URL: ' + url + ': ' + err; + console.log(msg, err.stack); + report(msg); + }); +} + +function refreshLayout() { + var i; + var html = ''; + for (i = g_state.first; i <= g_state.last; ++i) { + var book = g_state.cache[i]; + html += bookHtml(book); + } + + document.getElementById('books').innerHTML = html; +} + +function report(message) { + document.getElementById('books').innerHTML = message; +} + +function bookHtml(book) { + console.log('bookHtml(): ', book); + var result = '
' + + '' + + '
' + + '' + book.Description + '
'; + return result; +} +