// calls the switfttype engine to use the fetch new results
var reloadResults = function () {
Swiftype.reloadResults();
};
// the barebones search configuration
var searchConfig = {
page: {
category: undefined,
}
};
// passed into the swift type engine when the reloadResults is called
// doc source pages are of the page DocumentType. Category is a class applied through a header tag with a name specific to the filters applied.
$(document).ready(function () {
readFilters = function () {
return {
page: {
category: window.searchConfig.page.category
}
}
};
});
//defines the results which display in the results container
var defaultRenderFunction = function (document_type, item) {
return '
';
}
// returns the title/body of the item. Body will be a substring if over a certain number of characters
var customRenderFunction = function (document_type, item) {
//returns title of article with embedded url
var out = '' + item['title'] + '';
//defines body and limits
var body;
if (item.highlight['body']) {
body = item['body'].length > 300 ? item.highlight['body'].substr(0, 300) + '...' : item.highlight['body'];
} else {
body = item['body'].length > 300 ? item['body'].substr(0, 300) + '...' : item['body'];
};
return out.concat('' + body + '
');
};
//Custom paginator will present prev, next plus page numbers
//if less than 6 pages it will show all
//if greather than 6 it will show 1, elipsis, three pages centered on current, elipsis, then last
var customRenderPaginationForType = function (type, currentPage, totalPages) {
let active;
let pageCutLow = currentPage - 1;
let pageCutHigh = currentPage + 1;
var pages = '',
previousPage, nextPage;
// Show the Previous button only if you are on a page other than the first
if (currentPage != 1) {
previousPage = currentPage - 1;
pages = pages + '- « previous
';
}
// Show all the pagination elements if there are less than 6 pages total
if (totalPages < 6) {
for (let p = 1; p <= totalPages; p++) {
active = currentPage == p ? "active" : "no";
pages = pages + '';
}
}
// Use "..." to collapse page numbers outside of a certain range
else {
// Show the very first page followed by a "..." at the beginning of the
// pagination section (after the Previous button)
if (currentPage > 2) {
pages = pages + '';
if (currentPage > 3) {
pages = pages + '';
}
}
// Determine how many pages to show after the current currentPage index
if (currentPage === 1) {
pageCutHigh += 2;
} else if (currentPage === 2) {
pageCutHigh += 1;
}
// Determine how many pages to show before the current currentPage index
if (currentPage === totalPages) {
pageCutLow -= 2;
} else if (currentPage === totalPages - 1) {
pageCutLow -= 1;
}
// Output the indexes for pages that fall inside the range of pageCutLow
// and pageCutHigh
for (let p = pageCutLow; p <= pageCutHigh; p++) {
if (p === 0) {
p += 1;
}
if (p > totalPages) {
continue
}
active = currentPage == p ? "active" : "no";
pages = pages + '';
}
// Show the very last page preceded by a "..." at the end of the pagination
// section (before the Next button)
if (currentPage < totalPages - 1) {
if (currentPage < totalPages - 2) {
pages = pages + '';
}
pages = pages + '';
}
}
// Show the Next button only if you are on a page other than the last
if (currentPage < totalPages) {
nextPage = currentPage + 1;
pages = pages + '- next »';
}
pages = pages + '
';
return pages;
};
var customPostRenderFunction = function (data) {
var totalResultCount = 0;
var $resultContainer = this.getContext().resultContainer;
var spellingSuggestion = null;
if (data['info']) {
$.each(data['info'], function (index, value) {
totalResultCount += value['total_result_count'];
if (value['spelling_suggestion']) {
spellingSuggestion = value['spelling_suggestion']['text'];
}
});
}
if (totalResultCount === 0) {
$resultContainer.html("No results found.
");
}
if (spellingSuggestion !== null) {
$resultContainer.append('');
}
};
//filter click function
$(document).ready(function () {
$('.search-filter').on('click', function () {
// we capture the variable as the code underneath will change the value to false
var checked = this.checked;
// removes all the other checkboxes, each result only has one category
$('.search-filter').each(function () {
$(this).prop('checked', false);
});
// updates the UI to whether this checkbox should be checked or not
$(this).prop('checked', checked);
// if the checkbox is checked, we update the global variable to the `value` attribute so it filters by that
window.searchConfig.page.category = checked ? $(this).data('value') : undefined;
reloadResults();
});
});