Refactor JS into modules, and add basic MVC.
[quanweb.git] / js / Main.js
diff --git a/js/Main.js b/js/Main.js
new file mode 100644 (file)
index 0000000..246044e
--- /dev/null
@@ -0,0 +1,122 @@
+//QuanLib:  eBook Library
+//(C) 2017 by Christian Jaekl (cejaekl@yahoo.com)
+
+'use strict';
+
+// Global state information (yuck).  TODO:  refactor this to compartmentalize.
+var g_state = {
+    mousePos: {    // Last known position of the mouse cursor
+        x: undefined,
+        y: undefined
+    }
+};
+
+// ==============
+// Initialization
+
+document.onmousemove = onMouseMove;
+
+BooksView.init(BooksModel);
+PagingController.init(BooksModel);
+
+// ================
+// Global functions
+// 
+// TODO:  refactor this to compartmentalize more functionality.
+
+function report(message) {
+    document.getElementById('books').innerHTML = message;
+}
+
+function constructSearchUrl() {
+    var url = window.location.protocol + '//' + window.location.host + '/search/';
+
+    var firstTime = true;
+    var terms = ['aut', 'tit', 'ser'];
+
+    for (var 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 onMouseMove(event) {
+    if (typeof event === 'undefined') {
+        return;
+    }
+    
+    var x = event.pageX;
+    var y = event.pageY;
+    
+    if (  x === g_state.mousePos.x
+       && y === g_state.mousePos.y)
+    {
+        // No change from previous known position. 
+        // Nothing to see (or do) here, move along.
+        return;
+    }
+    
+    // Remember current mouse (x,y) position
+    g_state.mousePos.x = x;
+    g_state.mousePos.y = y;
+
+    ToolTip.mouseMoved(x, y);
+}
+
+function onNext() {
+    if (BooksModel.last < (BooksModel.count - 1)) {
+        PagingController.adjustPos(BooksModel.first + BooksModel.pageSize);
+    }
+}
+
+function onPrev() {
+    if (BooksModel.first > 0) {
+        PagingController.adjustPos(BooksModel.first - BooksModel.pageSize);
+    }
+}
+
+function onSlide(value) {
+    PagingController.adjustPos(value);
+}
+
+function onSearch() {
+    var url = constructSearchUrl();
+
+    fetch(url, {method:'GET', cache:'default'})
+        .then(response => response.json())
+        .then((jsonValue) => {
+            // console.log('JSON response:  ', jsonValue);
+            BooksModel.ids = jsonValue;
+            BooksModel.count = BooksModel.ids.length;
+            BooksModel.first = (-1);
+    
+            var elem = document.getElementById('slider');
+            elem.max = BooksModel.count;
+    
+            PagingController.adjustPos(0);
+        })
+        .catch(err => { 
+            var msg = 'Error fetching JSON from URL:  ' + url + ': ' + err + ':' + err.stack;
+            console.log(msg);
+            report(msg);
+        });
+}
+