Add basic html/js to search for and display some books.
[quanweb.git] / app / lib.js
diff --git a/app/lib.js b/app/lib.js
new file mode 100644 (file)
index 0000000..7175471
--- /dev/null
@@ -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 = '<div class="book"><table><tr><td><a href="/book/' + book.Id + '">'
+             + '<img class="cover-thumb" src="/download/' + book.CoverId + '"/></a></td>'
+             + '<td><span class="popup">' + book.Description + '</span></td>'
+             + '</tr></table></div>';
+  return result;
+}
+