-
Notifications
You must be signed in to change notification settings - Fork 58
/
multiple_types.html
69 lines (61 loc) · 3.17 KB
/
multiple_types.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript' src="https://cdnjs.cloudflare.com/ajax/libs/hogan.js/3.0.2/hogan.min.js"></script>
<script type='text/javascript' src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type='text/javascript' src='jquery.swiftype.search.js'></script>
<link type='text/css' rel='stylesheet' href='search.css' media='all' />
<script type='text/javascript'>
var resultTemplateOne = Hogan.compile('<div class="st-result"><h2 class="title"><a href="{{url}}" class="st-search-result-link">{{{title}}}</a></h2><div class="st-metadata"><span class="st-snippet">{{{body}}}</span></div><div><br style="clear:both;"/></div></div><br/>');
var resultTemplateTwo = Hogan.compile('<div class="st-result"><h2 class="title"><a href="{{url}}" class="st-search-result-link">{{{title}}}</a></h2><div class="st-metadata">{{{body}}}</div></div>');
var renderPagination = function (ctx, resultInfo) {
var currentPage = resultInfo['books'].current_page,
totalPages = resultInfo['books'].num_pages;
$(renderPaginationForType('books', currentPage, totalPages)).appendTo(ctx.resultContainer);
};
var renderPaginationForType = function (type, currentPage, totalPages) {
var pages = '<div class="st-page">',
previousPage, nextPage;
if (currentPage != 1) {
previousPage = currentPage - 1;
pages = pages + '<a href="#" class="st-prev" data-hash="true" data-page="' + previousPage + '">« previous</a>';
}
if (currentPage < totalPages) {
nextPage = currentPage + 1;
pages = pages + '<a href="#" class="st-next" data-hash="true" data-page="' + nextPage + '">next »</a>';
}
pages += '</div>';
return pages;
};
var customResultsRenderer = function(ctx, data) {
var $resultContainer = ctx.resultContainer,
config = ctx.config;
$resultContainer.html('');
if (data.info['books'].current_page == 1) {
var mgResult = data.records['magazines'][0];
if (mgResult) {
ctx.registerResult($(resultTemplateOne.render(mgResult)).appendTo($resultContainer), mgResult);
}
}
$.each(data.records['books'], function (idx, item) {
ctx.registerResult($(resultTemplateTwo.render(item)).appendTo($resultContainer), item);
});
renderPagination(ctx, data.info);
};
$(function() {
$('#st-search-input').swiftypeSearch({
resultContainingElement: '#st-results-container',
engineKey: 'RRsxcbroahbgrJmEXWe',
renderResultsFunction: customResultsRenderer
});
});
</script>
</head>
<body>
<form>
<h1>Example Swiftype Search installation (multiple document types)</h1>
Search your site: <input type='text' id='st-search-input' class='st-search-input' />
</form>
<div id="st-results-container" class="st-result-listing"></div>
</body>
</html>