4 var SearchController = (function () {
6 booksModel = undefined;
8 const terms = ['aut', 'tit', 'ser'];
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 var elem = Browser.getElementById('slider');
33 elem.max = booksModel.count;
35 PagingController.adjustPos(0);
37 .catch(function(err) {
38 var msg = 'Error fetching JSON from URL: ' + url + ': ' + err + ':' + err.stack;
47 // KeyUp listener. If the key is [Enter], then trigger a click on the [Search] button.
48 function addEnterListener(ctrlId) {
49 Browser.getElementById(ctrlId).addEventListener('keyup', function(event) {
50 event.preventDefault();
51 if (event.keyCode === 13) {
52 Browser.getElementById('search').click();
57 function constructSearchUrl() {
58 var url = window.location.protocol + '//' + window.location.host + '/search/';
62 for (var idx in terms) {
63 var term = terms[idx];
64 var elem = Browser.getElementById(term);
66 console.log('Error: could not find form element for search term "' + term + '".');
70 var value = elem.value;
71 if (value.length > 0) {
79 url += term + '=' + encodeURIComponent('%' + value + '%');