|
Line 0
Link Here
|
|
|
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 leftPaneResults = $('#browse-searchresults li').not('.loading, .no-results'); |
| 20 |
var rightPaneResults = $('#browse-selectionsearch ol li'); |
| 21 |
|
| 22 |
event.preventDefault(); |
| 23 |
|
| 24 |
if(!userInput) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
// remove any error states and show the results area (except right pane) |
| 29 |
$('#browse-suggestionserror').addClass('hidden'); |
| 30 |
$('#browse-searchresults .no-results').addClass('hidden'); |
| 31 |
$('#browse-resultswrapper').removeClass('hidden'); |
| 32 |
$('#browse-selection').addClass('hidden').text(""); |
| 33 |
$('#browse-selectionsearch').addClass('hidden'); |
| 34 |
|
| 35 |
// clear any results from left and right panes |
| 36 |
leftPaneResults.remove(); |
| 37 |
rightPaneResults.remove(); |
| 38 |
|
| 39 |
// show the spinner in the left pane |
| 40 |
$('#browse-searchresults .loading').removeClass('hidden'); |
| 41 |
|
| 42 |
xhrGetSuggestions = $.get(window.location.pathname, {api: "GetSuggestions", field: userField, prefix: userInput, fuzziness: userFuzziness}) |
| 43 |
.always(function() { |
| 44 |
// hide spinner |
| 45 |
$('#browse-searchresults .loading').addClass('hidden'); |
| 46 |
}) |
| 47 |
.done(function(data) { |
| 48 |
var fragment = document.createDocumentFragment(); |
| 49 |
|
| 50 |
if (data.length === 0) { |
| 51 |
$('#browse-searchresults .no-results').removeClass('hidden'); |
| 52 |
|
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
// scroll to top of container again |
| 57 |
$("#browse-searchresults").overflowScrollReset(); |
| 58 |
|
| 59 |
// store the type of search that was performed as an attrib |
| 60 |
$('#browse-searchresults').data('field', userField); |
| 61 |
|
| 62 |
$.each(data, function(index, object) { |
| 63 |
// use a document fragment so we don't need to nest the elems |
| 64 |
// or append during each iteration (which would be slow) |
| 65 |
var elem = document.createElement("li"); |
| 66 |
var link = document.createElement("a"); |
| 67 |
link.textContent = object.text; |
| 68 |
link.setAttribute("href", "#"); |
| 69 |
elem.appendChild(link); |
| 70 |
fragment.appendChild(elem); |
| 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('hidden'); |
| 79 |
$('#browse-suggestionserror').removeClass('hidden'); |
| 80 |
} |
| 81 |
}); |
| 82 |
}); |
| 83 |
|
| 84 |
$('#browse-searchresults').on("click", 'a', 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 term = $(this).text(); |
| 92 |
var field = $('#browse-searchresults').data('field'); |
| 93 |
var rightPaneResults = $('#browse-selectionsearch ol li'); |
| 94 |
|
| 95 |
event.preventDefault(); |
| 96 |
|
| 97 |
// clear any current selected classes and add a new one |
| 98 |
$(this).parent().siblings().children().removeClass('selected'); |
| 99 |
$(this).addClass('selected'); |
| 100 |
|
| 101 |
// copy in the clicked text |
| 102 |
$('#browse-selection').removeClass('hidden').text(term); |
| 103 |
|
| 104 |
// show the right hand pane if it is not shown already |
| 105 |
$('#browse-selectionsearch').removeClass('hidden'); |
| 106 |
|
| 107 |
// hide the no results element |
| 108 |
$('#browse-selectionsearch .no-results').addClass('hidden'); |
| 109 |
|
| 110 |
// clear results |
| 111 |
rightPaneResults.remove(); |
| 112 |
|
| 113 |
// turn the spinner on |
| 114 |
$('#browse-selectionsearch .loading').removeClass('hidden'); |
| 115 |
|
| 116 |
// do the query for the term |
| 117 |
xhrGetResults = $.get(window.location.pathname, {api: "GetResults", field: field, term: term}) |
| 118 |
.always(function() { |
| 119 |
// hide spinner |
| 120 |
$('#browse-selectionsearch .loading').addClass('hidden'); |
| 121 |
}) |
| 122 |
.done(function(data) { |
| 123 |
var fragment = document.createDocumentFragment(); |
| 124 |
|
| 125 |
if (data.length === 0) { |
| 126 |
$('#browse-selectionsearch .no-results').removeClass('hidden'); |
| 127 |
|
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
// scroll to top of container again |
| 132 |
$("#browse-selectionsearch").overflowScrollReset(); |
| 133 |
|
| 134 |
$.each(data, function(index, object) { |
| 135 |
// use a document fragment so we don't need to nest the elems |
| 136 |
// or append during each iteration (which would be slow) |
| 137 |
var elem = document.createElement("li"); |
| 138 |
var title = document.createElement("h4"); |
| 139 |
var link = document.createElement("a"); |
| 140 |
var author = document.createElement("p"); |
| 141 |
var subjects = document.createElement("p"); |
| 142 |
var destination = window.location.pathname; |
| 143 |
|
| 144 |
destination = destination.replace("browse", "detail"); |
| 145 |
destination = destination + "?biblionumber=" + object.id; |
| 146 |
|
| 147 |
author.className = "author"; |
| 148 |
subjects.className = "subjects"; |
| 149 |
|
| 150 |
link.setAttribute("href", destination); |
| 151 |
link.setAttribute("target", "_blank"); |
| 152 |
link.textContent = object.title; |
| 153 |
title.appendChild(link); |
| 154 |
|
| 155 |
author.textContent = object.authors.join(', '); |
| 156 |
subjects.textContent = object.subjects.join(' | '); |
| 157 |
|
| 158 |
elem.appendChild(title); |
| 159 |
elem.appendChild(author); |
| 160 |
elem.appendChild(subjects); |
| 161 |
fragment.appendChild(elem); |
| 162 |
}); |
| 163 |
|
| 164 |
$('#browse-selectionsearch ol').append(fragment.cloneNode(true)); |
| 165 |
}) |
| 166 |
.fail(function(jqXHR) { |
| 167 |
//if 500 or 404 (abort is okay though) |
| 168 |
if (jqXHR.statusText !== "abort") { |
| 169 |
$('#browse-resultswrapper').addClass('hidden'); |
| 170 |
$('#browse-suggestionserror').removeClass('hidden'); |
| 171 |
} |
| 172 |
}); |
| 173 |
|
| 174 |
}); |
| 175 |
|
| 176 |
}); |