1 //QuanLib: eBook Library
2 //(C) 2017 by Christian Jaekl (cejaekl@yahoo.com)
6 // Global state information (yuck). TODO: refactor this to compartmentalize.
8 mousePos: { // Last known position of the mouse cursor
17 document.onmousemove = onMouseMove;
19 BooksView.init(BooksModel);
20 PagingController.init(BooksModel);
25 // TODO: refactor this to compartmentalize more functionality.
27 function report(message) {
28 document.getElementById('books').innerHTML = message;
31 function constructSearchUrl() {
32 var url = window.location.protocol + '//' + window.location.host + '/search/';
35 var terms = ['aut', 'tit', 'ser'];
37 for (var idx in terms) {
38 var term = terms[idx];
39 var elem = document.getElementById(term);
41 console.log('Error: could not find form element for search term "' + term + '".');
45 var value = elem.value;
46 if (value.length > 0) {
54 url += term + '=' + encodeURIComponent('%' + value + '%');
61 function onMouseMove(event) {
62 if (typeof event === 'undefined') {
69 if ( x === g_state.mousePos.x
70 && y === g_state.mousePos.y)
72 // No change from previous known position.
73 // Nothing to see (or do) here, move along.
77 // Remember current mouse (x,y) position
78 g_state.mousePos.x = x;
79 g_state.mousePos.y = y;
81 ToolTip.mouseMoved(x, y);
85 if (BooksModel.last < (BooksModel.count - 1)) {
86 PagingController.adjustPos(BooksModel.first + BooksModel.pageSize);
91 if (BooksModel.first > 0) {
92 PagingController.adjustPos(BooksModel.first - BooksModel.pageSize);
96 function onSlide(value) {
97 PagingController.adjustPos(value);
100 function onSearch() {
101 var url = constructSearchUrl();
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);
111 var elem = document.getElementById('slider');
112 elem.max = BooksModel.count;
114 PagingController.adjustPos(0);
117 var msg = 'Error fetching JSON from URL: ' + url + ': ' + err + ':' + err.stack;