1 jQuery.fn.overflowScrollReset = function() {
2 $(this).scrollTop($(this).scrollTop() - $(this).offset().top);
6 $(document).ready(function(){
7 var xhrGetSuggestions, xhrGetResults;
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();
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");
21 event.preventDefault();
27 /* return the browsing results to empty state in case previous results have been loaded */
28 $("#browse-searchresults").empty().append( card_template );
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 */
37 $('.loading').removeClass('d-none');
39 xhrGetSuggestions = $.get(window.location.pathname, {api: "GetSuggestions", field: userField, prefix: userInput, fuzziness: userFuzziness})
42 $('.loading').addClass('d-none');
44 .done(function(data) {
45 var fragment = document.createDocumentFragment();
47 if (data.length === 0) {
48 $('.no-results').removeClass('d-none');
52 // store the type of search that was performed as an attrib
53 $('#browse-searchresults').data('field', userField);
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
62 .attr("id", "heading" + index)
63 .find("a").attr("data-target", "#collapse" + index)
64 .attr("aria-controls", "collapse" + index)
68 .attr("id", "collapse" + index)
69 .attr("aria-labelledby", "heading" + index);
70 $(fragment).append(card);
73 $('#browse-searchresults').append(fragment.cloneNode(true));
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');
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();
92 var target = link.data("target");
93 var term = link.text();
95 var field = $('#browse-searchresults').data('field');
97 event.preventDefault();
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();
106 if (data.length === 0) {
107 $('#browse-selectionsearch .no-results').removeClass('d-none');
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";
118 var destination = window.location.pathname;
119 destination = destination.replace("browse", "detail");
120 destination = destination + "?biblionumber=" + object.id;
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;
129 elem.appendChild(link);
132 var author = document.createElement("span");
133 author.className = "author";
134 author.textContent = " " + object.author;
135 elem.appendChild(author);
137 fragment.appendChild(elem);
140 $( target ).find(".card-body").append(fragment.cloneNode(true));
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');