Refactor JS into modules, and add basic MVC.
[quanweb.git] / js / Main.js
1 //QuanLib:  eBook Library
2 //(C) 2017 by Christian Jaekl (cejaekl@yahoo.com)
3
4 'use strict';
5
6 // Global state information (yuck).  TODO:  refactor this to compartmentalize.
7 var g_state = {
8     mousePos: {    // Last known position of the mouse cursor
9         x: undefined,
10         y: undefined
11     }
12 };
13
14 // ==============
15 // Initialization
16
17 document.onmousemove = onMouseMove;
18
19 BooksView.init(BooksModel);
20 PagingController.init(BooksModel);
21
22 // ================
23 // Global functions
24 // 
25 // TODO:  refactor this to compartmentalize more functionality.
26
27 function report(message) {
28     document.getElementById('books').innerHTML = message;
29 }
30
31 function constructSearchUrl() {
32     var url = window.location.protocol + '//' + window.location.host + '/search/';
33
34     var firstTime = true;
35     var terms = ['aut', 'tit', 'ser'];
36
37     for (var idx in terms) {
38         var term = terms[idx];
39         var elem = document.getElementById(term);
40         if (null === elem) {
41             console.log('Error:  could not find form element for search term "' + term + '".');
42             continue;
43         }
44
45         var value = elem.value;
46         if (value.length > 0) {
47             if (firstTime) {
48                 url += '?';
49                 firstTime = false;
50             }
51             else {
52                 url += '&';
53             }
54             url += term + '=' + encodeURIComponent('%' + value + '%');
55         }
56     }
57
58     return url;
59 }
60
61 function onMouseMove(event) {
62     if (typeof event === 'undefined') {
63         return;
64     }
65     
66     var x = event.pageX;
67     var y = event.pageY;
68     
69     if (  x === g_state.mousePos.x
70        && y === g_state.mousePos.y)
71     {
72         // No change from previous known position. 
73         // Nothing to see (or do) here, move along.
74         return;
75     }
76     
77     // Remember current mouse (x,y) position
78     g_state.mousePos.x = x;
79     g_state.mousePos.y = y;
80
81     ToolTip.mouseMoved(x, y);
82 }
83
84 function onNext() {
85     if (BooksModel.last < (BooksModel.count - 1)) {
86         PagingController.adjustPos(BooksModel.first + BooksModel.pageSize);
87     }
88 }
89
90 function onPrev() {
91     if (BooksModel.first > 0) {
92         PagingController.adjustPos(BooksModel.first - BooksModel.pageSize);
93     }
94 }
95
96 function onSlide(value) {
97     PagingController.adjustPos(value);
98 }
99
100 function onSearch() {
101     var url = constructSearchUrl();
102
103     fetch(url, {method:'GET', cache:'default'})
104         .then(response => response.json())
105         .then((jsonValue) => {
106             // console.log('JSON response:  ', jsonValue);
107             BooksModel.ids = jsonValue;
108             BooksModel.count = BooksModel.ids.length;
109             BooksModel.first = (-1);
110     
111             var elem = document.getElementById('slider');
112             elem.max = BooksModel.count;
113     
114             PagingController.adjustPos(0);
115         })
116         .catch(err => { 
117             var msg = 'Error fetching JSON from URL:  ' + url + ': ' + err + ':' + err.stack;
118             console.log(msg);
119             report(msg);
120         });
121 }
122