PageSourceSearch

https://www.peachpit.com/display/common/javascript/search.js

js peachpit.com collected 2026-10-01 08:49:46 UTC 9,366 bytes, 247 lines download raw bytes

1// Most recent AJAX request (used to ensure previous requests are aborted when a new one is made)
2var storeSearchAjax = null;
3
4function InjectStoreSearchResults(data, textStatus, jqXHR) {
5	// Remove feedback animation
6	// Removed for updated spinner
7    // $('img.ajaxloader').remove();
8    $('div.spinner-container').remove();
9
10	// Add returned results
11	$('#resultsBarFound').html(formatNumber(data.ResultsFound));
12	$('#resultsBarQuery').html(data.Query);
13	// Check that we have more than 0 results
14	if (data.ResultsFound > 0)
15		$('#columnOne').append(data.ResultsHtml);
16	else
17		$('#columnOne').append('<div class="results"><p><strong>We\'re sorry, no products matched your search criteria.  Please try some different search filters.</strong></p></div>');
18
19	// Update filter onclick events
20	// Get the correct string
21	var onclickString = $('input[name="format"]').attr('onclick');
22	onclickString = onclickString.substring(0, onclickString.lastIndexOf(',')) + ', \'' + data.Sort + '\')';
23	// Update event strings
24	$('input[name="subject"]').attr('onclick', onclickString);
25	$('input[name="format"]').attr('onclick', onclickString);
26	$('input[name="imprint"]').attr('onclick', onclickString);
27
28	// Remove the min-height we added earlier
29	$('#columnOne').css('min-height', '');
30
31	// Fix columns
32    $('#content').equalHeights();
33    gtmManager.productImpressions();
34
35	// Set the current AJAX object to null
36	storeSearchAjax = null;
37}
38
39function InjectSearchResultsFail(jqXHR, textStatus, errorThrown)
40{
41	// Remove feedback animation
42    // Removed for updated spinner 
43    //$('img.ajaxloader').remove(); 
44	$('div.spinner-container').remove();
45
46	// Add failure message
47	$('#columnOne').append('<div class="results"><p><strong>We\'re sorry, there was a problem with your search. Please try your search again.</strong></p></div>');
48	
49	// Set the current AJAX object to null
50	storeSearchAjax = null;
51}
52
53// Create feedback animation
54function showFeedback() {
55	if (!$('img.ajaxloader').length)
56	{	
57        // Removed for updated spinner
58		//$('#columnOne').append('<img src="../display/common/images/icons/ajax-loader.gif" alt="Loading" class="ajaxloader" />');
59
60        $('#columnOne').append('<div class="spinner-container"><span class="spinner">Loading…</span> Loading Results</div>');
61	}
62}
63
64//format result count
65function formatNumber(nStr)
66{
67	nStr += '';
68	x = nStr.split('.');
69	x1 = x[0];
70	x2 = x.length > 1 ? '.' + x[1] : '';
71	var rgx = /(\d+)(\d{3})/;
72	while (rgx.test(x1)) {
73		x1 = x1.replace(rgx, '$1' + ',' + '$2');
74	}
75	return x1 + x2;
76}
77
78function StoreSearchFormatToggleChildren(parent)
79{
80	if (parent.checked)
81	{
82		// Check and show children
83		$('input[id^="' + parent.id + '"]').attr('checked', true);
84		$('#more_' + parent.id).show();
85	}
86	else
87	{
88		// Uncheck and hide children
89		$('input[id^="' + parent.id + '"]').attr('checked', false);
90		$('#more_' + parent.id).hide();
91	}
92}
93
94function StoreSearchFormatClearParent(child)
95{
96	if (!child.checked)
97	{
98		var indexOf_ = child.id.indexOf('_');
99
100		// Are all children of this parent unchecked?
101		var childArray = $('input[id^="' + child.id.substring(0, indexOf_+1) + '"]').get();
102		var allUnchecked = true;
103		for (var i = 0; i < childArray.length; i++)
104		{
105			if (childArray[i].checked)
106				allUnchecked = false;
107		}
108
109		// If all children are unchecked, uncheck parent and hide children
110		if (allUnchecked)
111		{
112			var parentId = child.id.substring(0, indexOf_);
113			$('#' + parentId).attr('checked', false);
114			$('#more_' + parentId).hide();
115		}
116	}
117}
118
119function StoreSearchRefresh(objectClicked, environment, partnerIntId, userTierId, page, sort) {
120
121    // First we need to take care of hidding, unhiding children
122    // If a format parent was clicked
123    if (objectClicked.id.indexOf('format') != -1 && objectClicked.id.indexOf('_') == -1)
124    {
125	StoreSearchFormatToggleChildren(objectClicked);
126    }
127    // Are we a format child?
128    else if(objectClicked.id.indexOf('format') != -1 && objectClicked.id.indexOf('_') > 4)
129    {
130        StoreSearchFormatClearParent(objectClicked)
131    }
132
133    // Search service base URL (hosted on InformIT)
134    var searchUrl;
135    if (environment == 'PROD')
136        searchUrl = 'https://informit.com/';
137    else if (environment == 'PQA')
138        searchUrl = 'https://informit.qaprod.ecollege.net/';
139    else if (environment == 'DEV')
140        searchUrl = 'https://informit.local/';
141    else if (environment == 'DESIGN')
142        searchUrl = 'https://informit.design.ecollegeqa.net/';
143	else if (environment == 'CORE')
144        searchUrl = 'https://informit.core.ecollege.net/';
145    else
146        searchUrl = 'https://informit.qa.ecollegeqa.net/';
147    searchUrl += 'search/search.svc/UnifiedProductSearch';
148
149    // Add partner int ID, user tier ID, and query
150    searchUrl += '?partner=' + partnerIntId + '&tierId=' + userTierId + '&query=' + encodeURIComponent($('#searchQuery').val());
151
152    // Add page if it exists
153    if (page != null)
154        searchUrl += '&page=' + page;
155    
156    // Sorting
157    if (sort != null) {
158        var sortOptions = '';
159        
160        switch (sort) {
161            case 'PubDate':
162                searchUrl += '&sort=PubDate';
163                sortOptions = '<li><a href="" onClick="return StoreSearchRefresh(this, \'' + environment + '\', \'' + partnerIntId + '\', \'' + userTierId + '\', null, \'Relevance\')">Relevance</a></li>';
164                sortOptions += '<li>&#187; Publication Date</li>';
165                sortOptions += '<li><a href="" onClick="return StoreSearchRefresh(this, \'' + environment + '\', \'' + partnerIntId + '\', \'' + userTierId + '\', null, \'Title\')">Title</a></li>';
166                break;
167            case 'Relevance':
168                searchUrl += '&sort=Relevance';
169                sortOptions = '<li>&#187; Relevance</li>';
170                sortOptions += '<li><a href="" onClick="return StoreSearchRefresh(this, \'' + environment + '\', \'' + partnerIntId + '\', \'' + userTierId + '\', null, \'PubDate\')">Publication Date</a></li>';
171                sortOptions += '<li><a href="" onClick="return StoreSearchRefresh(this, \'' + environment + '\', \'' + partnerIntId + '\', \'' + userTierId + '\', null, \'Title\')">Title</a></li>';
172                break;
173            case 'Title':
174                searchUrl += '&sort=Title';
175                sortOptions = '<li><a href="" onClick="return StoreSearchRefresh(this, \'' + environment + '\', \'' + partnerIntId + '\', \'' + userTierId + '\', null, \'Relevance\')">Relevance</a></li>';
176                sortOptions += '<li><a href="" onClick="return StoreSearchRefresh(this, \'' + environment + '\', \'' + partnerIntId + '\', \'' + userTierId + '\', null, \'PubDate\')">Publication Date</a></li>';
177                sortOptions += '<li>&#187; Title</li>';
178                break;
179        }
180
181        $('#sort-options').html(sortOptions);
182    }
183
184    // Subjects
185    var subjectCheckboxArray = $('input[id^="subject"]').get();
186    var subjectList = '';
187    for (var i = 0; i < subjectCheckboxArray.length; i++) {
188        var subjectCheckbox = subjectCheckboxArray[i];
189
190        if (subjectCheckbox.checked)
191            subjectList += encodeURIComponent(subjectCheckbox.value) + '^';
192    }
193    if (subjectList.length > 0)
194        searchUrl += '&subjects=' + subjectList.substring(0, subjectList.length-1);
195    
196    // Formats
197    var inputArray = $('input[id^="format"]').get();
198    var formatList = '';
199    for (var i = 0; i < inputArray.length; i++) {
200        var input = inputArray[i];
201
202	if (input.checked)
203            formatList += encodeURIComponent(input.value) + '^';
204    }
205    if (formatList.length > 0)
206        searchUrl += '&formats=' + formatList.substring(0, formatList.length-1);
207
208    // Publishers (A.K.A. - Imprints)
209    var imprintCheckboxArray = $('input[id^="imprint"]').get();
210    var imprintList = '';
211    for (var i = 0; i < imprintCheckboxArray.length; i++) {
212        var imprintCheckbox = imprintCheckboxArray[i];
213
214        if (imprintCheckbox.checked)
215            imprintList += encodeURIComponent(imprintCheckbox.value) + '^';
216    }
217    if (imprintList.length > 0)
218        searchUrl += '&publishers=' + imprintList.substring(0, imprintList.length-1);
219
220    // Perform service call to get new results
221    // Cancel current request if it exists and isn't done
222    if (storeSearchAjax != null)
223        storeSearchAjax.abort();
224    storeSearchAjax = $.ajax({
225        url: searchUrl,
226        dataType: 'jsonp',
227        success: InjectStoreSearchResults,
228	error: InjectSearchResultsFail
229    });
230
231	// Before we remove anything, we want to set the min-height of #columnOne so that we can scroll properly
232	$('#columnOne').css('min-height', $('#columnOne').height());
233	
234	// Load feedback animation
235	showFeedback();
236
237    // Remove current result set (doing this before the return helps use see that it is updated)
238    $('#pager-header-top').remove();
239    $('#columnOne > div.results').remove();
240    $('#pagination').remove();
241
242    // Make sure we are scrolling to the top (only if a paging action)
243    if (page != null)
244   	$('html, body').scrollTop($('#searchform').offset().top + $('#searchform').height());
245
246    return false;
247}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.