Refactor search into its own controller.
[quanweb.git] / js / Main.js
index 246044e2f0edae799c91f7de676a60387296b650..6ceac8256665e4fbd49b5aaaf3661ccf918d1d5f 100644 (file)
@@ -18,6 +18,7 @@ document.onmousemove = onMouseMove;
 
 BooksView.init(BooksModel);
 PagingController.init(BooksModel);
+SearchController.init(BooksModel);
 
 // ================
 // Global functions
@@ -28,36 +29,6 @@ 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;
@@ -98,25 +69,6 @@ function onSlide(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);
-        });
+    SearchController.onSearch();
 }