7175471c084b4822b8f479e84b91858c78d079e8
[quanweb.git] / app / lib.js
1 // QuanLib:  eBook Library
2 // (C) 2017 by Christian Jaekl (cejaekl@yahoo.com)
3
4 g_state = {
5   cache: {},
6   count: 0,
7   first: 0,
8   ids: [],
9   last: 0,
10 }
11
12 function constructSearchUrl() {
13   var url = window.location.protocol + '//' + window.location.host + '/search/';
14
15   var firstTime = true;
16   var terms = ['aut', 'tit', 'ser'];
17
18   for (idx in terms) {
19     var term = terms[idx];
20     var elem = document.getElementById(term);
21     if (null === elem) {
22       console.log('Error:  could not find form element for search term "' + term + '".');
23       continue;
24     }
25     
26     var value = elem.value;
27     if (value.length > 0) {
28       if (firstTime) {
29         url += '?';
30         firstTime = false;
31       }
32       else {
33         url += '&';
34       }
35       url += term + '=' + encodeURIComponent('%' + value + '%');
36     }
37   }
38
39   return url;
40 }
41
42 function onNext() {
43 }
44
45 function onPrev() {
46 }
47
48 function onSearch() {
49   console.log('onSearch()');
50
51   var url = constructSearchUrl();
52
53   report('Loading data from server, please wait...')
54   console.log('Fetching:  "' + url + '"...')
55
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
61     g_state.first = 0
62     g_state.last = (g_state.ids.length) - 1;
63     if (g_state.last > 100) {
64       g_state.last = 100;
65     }
66     refreshData()
67   })
68   .catch(err => { 
69     var msg = 'Error fetching JSON from URL:  ' + url + ': ' + err + ':' + err.stack;
70     console.log(msg);
71     report(msg);
72   });
73 }
74
75 function refreshData() {
76   report('Loading details for books ' + g_state.first + ' through ' + g_state.last + ', please wait...');
77
78   var i;
79   var url = '/info/?ids=';
80   for (i = g_state.first; i <= g_state.last; ++i) {
81     if (i > 0) {
82       url += ',';
83     }
84     url += g_state.ids[i];
85   }
86
87   fetch(url, {method:'GET', cache:'default'})
88   .then(response => response.json())
89   .then((jsonValue) => {
90     console.log('JSON response for info:  ', jsonValue);
91     report('');
92     g_state.cache = jsonValue;
93     refreshLayout();
94   })
95   .catch(err => {
96     var msg = 'Error fetching book details via URL:  ' + url + ': ' + err;
97     console.log(msg, err.stack);
98     report(msg);
99   });
100 }
101
102 function refreshLayout() {
103   var i;
104   var html = '';
105   for (i = g_state.first; i <= g_state.last; ++i) {
106     var book = g_state.cache[i];
107     html += bookHtml(book);
108   }
109
110   document.getElementById('books').innerHTML = html;
111 }
112
113 function report(message) {
114   document.getElementById('books').innerHTML = message;
115 }
116
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>';
123   return result;
124 }
125