4 var SearchController = (function () {
6 booksModel = undefined;
8 const terms = ['aut', 'lan', 'lst', 'ser', 'tit'];
13 my.init = function(linkedBooksModel) {
14 booksModel = linkedBooksModel;
16 for (var idx in terms) {
17 addEnterListener(terms[idx]);
21 my.onSearch = function() {
22 var url = constructSearchUrl();
24 fetch(url, {method:'GET', cache:'default'})
25 .then(function(response) {return response.json();})
26 .then(function(jsonValue) {
27 // console.log('JSON response: ', jsonValue);
28 booksModel.ids = jsonValue;
29 booksModel.count = booksModel.ids.length;
30 booksModel.first = (-1);
32 const max_pos = Math.max(0, booksModel.count - booksModel.pageSize);
34 var elem = Browser.getElementById('slider');
36 elem = Browser.getElementById('bottom_slider');
39 PagingController.adjustPos(0);
41 .catch(function(err) {
42 var msg = 'Error fetching JSON from URL: ' + url + ': ' + err + ':' + err.stack;
51 // KeyUp listener. If the key is [Enter], then trigger a click on the [Search] button.
52 function addEnterListener(ctrlId) {
53 Browser.getElementById(ctrlId).addEventListener('keyup', function(event) {
54 event.preventDefault();
55 if (event.keyCode === 13) {
56 Browser.getElementById('search').click();
61 function constructSearchUrl() {
62 var url = window.location.protocol + '//' + window.location.host + '/search/';
66 for (var idx in terms) {
67 var term = terms[idx];
68 var elem = Browser.getElementById(term);
70 console.log('Error: could not find form element for search term "' + term + '".');
74 var value = elem.value;
75 if (value.length > 0) {
84 url += term + '=' + encodeURIComponent('' + value);
87 url += term + '=' + encodeURIComponent('%' + value + '%');