-
Notifications
You must be signed in to change notification settings - Fork 1
/
instantsearch-showmore.js
75 lines (62 loc) · 2.22 KB
/
instantsearch-showmore.js
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
70
71
72
73
74
75
// instantsearch.js custom widget with vanilla js
// First, we add ourselves to the instantsearch.widgets namespace
instantsearch.widgets.hitsWithShowMore = function showMore(options) {
if (!options || !options.container) {
console.error('You should provide an option object with a container to the showMore widget');
return;
}
var widgetContainer;
var hitsContainer;
var hitsPerPage = 20;
var itemTemplate = '';
var emptyTemplate = '';
// container
widgetContainer = document.querySelector(options.container);
// templates
if (options.templates && options.templates.item) {
itemTemplate = options.templates.item;
Mustache.parse(itemTemplate);
}
if (options.templates && options.templates.empty) {
emptyTemplate = options.templates.empty;
Mustache.parse(emptyTemplate);
}
// hitsPerPage
if (options.hitsPerPage) {
hitsPerPage = options.hitsPerPage;
}
// See more details in our documentation:
// https://community.algolia.com/instantsearch.js/documentation/#custom-widgets
//
// This is the custom widget interface (just an object). You need to implement
// at least render OR init.
return {
init: function (params) {
// params = { state: {}, helper: {} }
params.helper.setQueryParameter('hitsPerPage', hitsPerPage);
widgetContainer.addEventListener('click', function(e) {
if (e.target.id && e.target.id === 'show-more') {
params.helper.nextPage().search();
}
});
widgetContainer.innerHTML = '<div id="rendered-hits"></div><p><button id="show-more">Show more</button></p>';
hitsContainer = document.getElementById('rendered-hits');
},
render: function (params) {
// params = { results: {}, state: {}, helper: {} }
if (params.state.page === 0) { // because '0' means we updated refinements
hitsContainer.innerHTML = '';
}
var rendering;
if (params.results.nbHits > 0) {
rendering = params.results.hits.map(function(hit) {
return Mustache.render(itemTemplate, hit);
});
}
else {
rendering = [Mustache.render(emptyTemplate, params.results)];
}
hitsContainer.insertAdjacentHTML('beforeend', rendering.join(''));
}
}
};