Skip to content

Commit 91f9890

Browse files
committed
instantsearch.js: Fix groups
1 parent d07e973 commit 91f9890

File tree

1 file changed

+76
-18
lines changed

1 file changed

+76
-18
lines changed

docs/js/instantsearch.js

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
instantsearch.widgets.stats({
6060
container: '#stats',
6161
templates: {
62-
text: `<h1>
62+
text: `<h1>
6363
Search results ({{#helpers.formatNumber}}{{nbHits}}{{/helpers.formatNumber}})
6464
</h1>`,
6565
},
@@ -78,14 +78,14 @@
7878
const groupNameKey = breadcrumbsKeys.shift();
7979
const entryNameKey = breadcrumbsKeys.pop();
8080
const newItem = {
81+
groupNameKey,
8182
entryNameKey,
8283
breadcrumbsKeys,
8384
item: getClearedItem(item),
8485
};
8586
const groupChildren = outputItemsMap[groupNameKey]?.children ?? [];
8687

8788
outputItemsMap[groupNameKey] = {
88-
groupNameKey,
8989
children: [...groupChildren, newItem],
9090
};
9191
});
@@ -94,31 +94,41 @@
9494
},
9595
templates: {
9696
item: (hit) => {
97-
const { groupNameKey, children } = hit;
98-
const groupItem = children[0].item;
99-
const groupHeaderHTML = `<h2 class="instantsearch__group-header">
100-
${instantsearch.highlight({ attribute: `hierarchy.${groupNameKey}`, highlightedTagName: 'mark', hit: groupItem })}
101-
(${children.length})
102-
</h2>`;
97+
const {children} = hit;
98+
let resultHTML = '';
99+
let previousGroupName = null;
100+
let groupChildCount = 0;
103101
let groupContentHTML = '';
104102

105-
children.forEach((childHit) => {
106-
const { breadcrumbsKeys, entryNameKey, item: entryItem } = childHit;
103+
children.forEach((childHit, index) => {
104+
const {groupNameKey, breadcrumbsKeys, entryNameKey, item: entryItem} = childHit;
107105
const headerHTML = `<h3 class="instantsearch__entry-header">
108-
${instantsearch.highlight({ attribute: `hierarchy.${entryNameKey}`, highlightedTagName: 'mark', hit: entryItem })}
106+
${instantsearch.highlight({
107+
attribute: `hierarchy.${entryNameKey}`,
108+
highlightedTagName: 'mark',
109+
hit: entryItem
110+
})}
109111
</h3>`;
110112
let breadcrumbsHTML = '';
111113
let contentHTML = '';
112114

113115
if (entryItem.content) {
114116
contentHTML = `<div class="instantsearch__entry-content">
115-
${instantsearch.highlight({ attribute: `content`, highlightedTagName: 'mark', hit: entryItem })}
117+
${instantsearch.highlight({
118+
attribute: `content`,
119+
highlightedTagName: 'mark',
120+
hit: entryItem
121+
})}
116122
</div>`;
117123
}
118124

119125
breadcrumbsKeys?.forEach((breadcrumbKey) => {
120126
breadcrumbsHTML += `<span class="instantsearch__entry-breadcrumbs-item">
121-
${instantsearch.highlight({ attribute: `hierarchy.${breadcrumbKey}`, highlightedTagName: 'mark', hit: entryItem })}
127+
${instantsearch.highlight({
128+
attribute: `hierarchy.${breadcrumbKey}`,
129+
highlightedTagName: 'mark',
130+
hit: entryItem
131+
})}
122132
</span>`
123133
});
124134

@@ -130,14 +140,62 @@
130140
</div>
131141
</a>`;
132142

133-
groupContentHTML += childHTML;
143+
let groupName = childHit.item.hierarchy[groupNameKey];
144+
if (index && groupName != previousGroupName) {
145+
// Not first and in a new group
146+
if (children.length === index + 1) {
147+
// Not first, last and in a new group: close previous group, close current group
148+
let groupHeaderHTML = `<h2 class="instantsearch__group-header">
149+
${previousGroupName}
150+
(${groupChildCount})
151+
</h2>`;
152+
resultHTML += `${groupHeaderHTML}
153+
<div class="instantsearch__group">
154+
${groupContentHTML}
155+
</div>`;
156+
groupHeaderHTML = `<h2 class="instantsearch__group-header">
157+
${groupName}
158+
(1)
159+
</h2>`;
160+
resultHTML += `${groupHeaderHTML}
161+
<div class="instantsearch__group">
162+
${childHTML}
163+
</div>`;
164+
} else {
165+
// Not first, not last and in new group: close previous group, add to next group content
166+
let groupHeaderHTML = `<h2 class="instantsearch__group-header">
167+
${previousGroupName}
168+
(${groupChildCount})
169+
</h2>`;
170+
resultHTML += `${groupHeaderHTML}
171+
<div class="instantsearch__group">
172+
${groupContentHTML}
173+
</div>`;
174+
groupContentHTML = childHTML;
175+
groupChildCount = 1;
176+
}
177+
} else if (children.length === index + 1) {
178+
// Last and in previous group: add to previous group and close previous group
179+
groupContentHTML += childHTML;
180+
groupChildCount++;
181+
let groupHeaderHTML = `<h2 class="instantsearch__group-header">
182+
${groupName}
183+
(${groupChildCount})
184+
</h2>`;
185+
resultHTML += `${groupHeaderHTML}
186+
<div class="instantsearch__group">
187+
${groupContentHTML}
188+
</div>`;
189+
} else {
190+
// Not last and in previous group: add to previous group
191+
groupContentHTML += childHTML;
192+
groupChildCount++;
193+
}
194+
previousGroupName = groupName;
134195
});
135196

136197
return `<div class="instantsearch">
137-
${groupHeaderHTML}
138-
<div class="instantsearch__group">
139-
${groupContentHTML}
140-
</div>
198+
${resultHTML}
141199
</div>`;
142200
},
143201
},

0 commit comments

Comments
 (0)