3a3bcc26a3f10dc2381277be67c3ccb3dc99fa1d
[koha.git] / koha-tmpl / opac-tmpl / bootstrap / js / browse.js
1 jQuery.fn.overflowScrollReset = function() {
2     $(this).scrollTop($(this).scrollTop() - $(this).offset().top);
3     return this;
4 };
5
6 $(document).ready(function(){
7     var xhrGetSuggestions, xhrGetResults;
8
9     $('#browse-search form').submit(function(event) {
10         // if there is an in progress request, abort it so we
11         // don't end up with  a race condition
12         if(xhrGetSuggestions && xhrGetSuggestions.readyState != 4){
13             xhrGetSuggestions.abort();
14         }
15
16         var userInput = $('#browse-searchterm').val().trim();
17         var userField = $('#browse-searchfield').val();
18         var userFuzziness = $('input[name=browse-searchfuzziness]:checked', '#browse-searchfuzziness').val();
19         var card_template = $("#card_template");
20
21         event.preventDefault();
22
23         if(!userInput) {
24             return;
25         }
26
27         /* return the browsing results to empty state in case previous results have been loaded */
28         $("#browse-searchresults").empty().append( card_template );
29
30         // remove any error states and show the results area
31         $('#browse-suggestionserror').addClass('d-none');
32         $('.no-results').addClass('d-none');
33         $('#browse-resultswrapper').removeClass('d-none');
34         /* Reset results browser to default state */
35
36         // show the spinner
37         $('.loading').removeClass('d-none');
38
39         xhrGetSuggestions = $.get(window.location.pathname, {api: "GetSuggestions", field: userField, prefix: userInput, fuzziness: userFuzziness})
40             .always(function() {
41                 // hide spinner
42                 $('.loading').addClass('d-none');
43             })
44             .done(function(data) {
45                 var fragment = document.createDocumentFragment();
46
47                 if (data.length === 0) {
48                     $('.no-results').removeClass('d-none');
49                     return;
50                 }
51
52                 // store the type of search that was performed as an attrib
53                 $('#browse-searchresults').data('field', userField);
54
55                 $.each(data, function(index, object) {
56                     // use a document fragment so we don't need to nest the elems
57                     // or append during each iteration (which would be slow)
58                     var card = card_template.clone().removeAttr("id");
59                     // change card-header id
60                     card
61                         .find(".card-header")
62                         .attr("id", "heading" + index)
63                         .find("a").attr("data-target", "#collapse" + index)
64                         .attr("aria-controls", "collapse" + index)
65                         .text(object.text);
66                     card
67                         .find(".collapse")
68                         .attr("id", "collapse" + index)
69                         .attr("aria-labelledby", "heading" + index);
70                     $(fragment).append(card);
71                 });
72
73                 $('#browse-searchresults').append(fragment.cloneNode(true));
74             })
75             .fail(function(jqXHR) {
76                 //if 500 or 404 (abort is okay though)
77                 if (jqXHR.statusText !== "abort") {
78                     $('#browse-resultswrapper').addClass('d-none');
79                     $('#browse-suggestionserror').removeClass('d-none');
80                 }
81             });
82     });
83
84     $('#browse-searchresults').on("click", 'a.expand-result', function(event) {
85         // if there is an in progress request, abort it so we
86         // don't end up with  a race condition
87         if(xhrGetResults && xhrGetResults.readyState != 4){
88             xhrGetResults.abort();
89         }
90
91         var link = $(this);
92         var target =  link.data("target");
93         var term = link.text();
94
95         var field = $('#browse-searchresults').data('field');
96
97         event.preventDefault();
98
99         /* Don't load data via AJAX if it has already been loaded */
100         if ($(target).find(".result-title").length == 0) {
101             // do the query for the term
102             xhrGetResults = $.get(window.location.pathname, {api: "GetResults", field: field, term: term})
103                 .done(function(data) {
104                     var fragment = document.createDocumentFragment();
105
106                     if (data.length === 0) {
107                         $('#browse-selectionsearch .no-results').removeClass('d-none');
108                         return;
109                     }
110
111
112                     $.each(data, function(index, object) {
113                         // use a document fragment so we don't need to nest the elems
114                         // or append during each iteration (which would be slow)
115                         var elem = document.createElement("div");
116                         elem.className = "result-title";
117
118                         var destination = window.location.pathname;
119                         destination = destination.replace("browse", "detail");
120                         destination = destination + "?biblionumber=" + object.id;
121
122                         var link = document.createElement("a");
123                         link.setAttribute("href", destination);
124                         link.setAttribute("target", "_blank");
125                         link.textContent = object.title;
126                         if( object.subtitle ){
127                             link.textContent += " " + object.subtitle;
128                         }
129                         elem.appendChild(link);
130
131                         if( object.author ){
132                             var author = document.createElement("span");
133                             author.className = "author";
134                             author.textContent = " " + object.author;
135                             elem.appendChild(author);
136                         }
137                         fragment.appendChild(elem);
138                     });
139
140                     $( target ).find(".card-body").append(fragment.cloneNode(true));
141                 })
142                 .fail(function(jqXHR) {
143                     //if 500 or 404 (abort is okay though)
144                     if (jqXHR.statusText !== "abort") {
145                         $('#browse-resultswrapper').addClass('d-none');
146                         $('#browse-suggestionserror').removeClass('d-none');
147                     }
148                 });
149         }
150     });
151 });