-
Notifications
You must be signed in to change notification settings - Fork 498
/
connectRefinementList.js
298 lines (269 loc) · 10.6 KB
/
connectRefinementList.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import {checkRendering} from '../../lib/utils.js';
const usage = `Usage:
var customRefinementList = connectRefinementList(function render(params) {
// params = {
// isFromSearch,
// createURL,
// items,
// refine,
// searchForItems,
// instantSearchInstance,
// canRefine,
// toggleShowMore,
// isShowingMore,
// widgetParams,
// }
});
search.addWidget(
customRefinementList({
attributeName,
[ operator = 'or' ],
[ limit ],
[ showMoreLimit ],
[ sortBy = ['isRefined', 'count:desc', 'name:asc']],
})
);
Full documentation available at https://community.algolia.com/instantsearch.js/connectors/connectRefinementList.html
`;
export const checkUsage = ({attributeName, operator, usageMessage, showMoreLimit, limit}) => {
const noAttributeName = attributeName === undefined;
const invalidOperator = !(/^(and|or)$/).test(operator);
const invalidShowMoreLimit = showMoreLimit !== undefined ?
isNaN(showMoreLimit) || showMoreLimit < limit : false;
if (noAttributeName || invalidOperator || invalidShowMoreLimit) {
throw new Error(usageMessage);
}
};
/**
* @typedef {Object} RefinementListItem
* @property {string} value The value of the refinement list item.
* @property {string} label Human-readable value of the refinement list item.
* @property {number} count Number of matched results after refinement is applied.
* @property {boolean} isRefined Indicates if the list item is refined.
*/
/**
* @typedef {Object} CustomRefinementListWidgetOptions
* @property {string} attributeName The name of the attribute in the records.
* @property {"and"|"or"} [operator = 'or'] How the filters are combined together.
* @property {number} [limit = undefined] The max number of items to display when
* `showMoreLimit` is not or if the widget is showing less value.
* @property {number} [showMoreLimit] The max number of items to display if the widget
* is showing more items.
* @property {string[]|function} [sortBy = ['isRefined', 'count:desc', 'name:asc']] How to sort refinements. Possible values: `count|isRefined|name:asc|name:desc`.
*/
/**
* @typedef {Object} RefinementListRenderingOptions
* @property {RefinementListItem[]} items The list of filtering values returned from Algolia API.
* @property {function(item.value): string} createURL Creates the next state url for a selected refinement.
* @property {function(item.value)} refine Action to apply selected refinements.
* @property {function} searchForItems Searches for values inside the list.
* @property {boolean} isFromSearch `true` if the values are from an index search.
* @property {boolean} canRefine `true` if a refinement can be applied.
* @property {Object} widgetParams All original `CustomRefinementListWidgetOptions` forwarded to the `renderFn`.
* @property {boolean} isShowingMore True if the menu is displaying all the menu items.
* @property {function} toggleShowMore Toggles the number of values displayed between `limit` and `showMoreLimit`.
*/
/**
* **RefinementList** connector provides the logic to build a custom widget that will let the
* user filter the results based on the values of a specific facet.
*
* This connector provides a `toggleShowMore()` function to display more or less items and a `refine()`
* function to select an item.
* @type {Connector}
* @param {function(RefinementListRenderingOptions, boolean)} renderFn Rendering function for the custom **RefinementList** widget.
* @return {function(CustomRefinementListWidgetOptions)} Re-usable widget factory for a custom **RefinementList** widget.
* @example
* // custom `renderFn` to render the custom RefinementList widget
* function renderFn(RefinementListRenderingOptions, isFirstRendering) {
* if (isFirstRendering) {
* RefinementListRenderingOptions.widgetParams.containerNode
* .html('<ul></ul>')
* }
*
* RefinementListRenderingOptions.widgetParams.containerNode
* .find('li[data-refine-value]')
* .each(function() { $(this).off('click'); });
*
* if (RefinementListRenderingOptions.canRefine) {
* var list = RefinementListRenderingOptions.items.map(function(item) {
* return '<li data-refine-value="' + item.value + '">' +
* '<input type="checkbox" value="' + item.value + '"' + item.isRefined ? ' checked' : '' + '/>' +
* '<a href="' + RefinementListRenderingOptions.createURL(item.value) + '">' + item.label + '</a>' +
* '</li>';
* });
*
* RefinementListRenderingOptions.widgetParams.containerNode.find('ul').html(list);
* RefinementListRenderingOptions.widgetParams.containerNode
* .find('li[data-refine-value]')
* .each(function() {
* $(this).on('click', function(event) {
* event.stopPropagation();
* event.preventDefault();
*
* RefinementListRenderingOptions.refine($(this).data('refine-value'));
* });
* });
* } else {
* RefinementListRenderingOptions.widgetParams.containerNode.find('ul').html('');
* }
*
* // connect `renderFn` to RefinementList logic
* var customRefinementList = instantsearch.connectors.connectRefinementList(renderFn);
*
* // mount widget on the page
* search.addWidget(
* customRefinementList({
* containerNode: $('#custom-refinement-list-container'),
* attributeName: 'categories',
* })
* );
*/
export default function connectRefinementList(renderFn) {
checkRendering(renderFn, usage);
return (widgetParams = {}) => {
const {
attributeName,
operator = 'or',
limit,
showMoreLimit,
sortBy = ['isRefined', 'count:desc', 'name:asc'],
} = widgetParams;
checkUsage({attributeName, operator, usage, limit, showMoreLimit});
const formatItems = ({name: label, ...item}) =>
({...item, label, value: label, highlighted: label});
const render = ({items, state, createURL,
helperSpecializedSearchFacetValues,
refine, isFromSearch, isFirstSearch,
isShowingMore, toggleShowMore,
instantSearchInstance}) => {
// Compute a specific createURL method able to link to any facet value state change
const _createURL = facetValue => createURL(state.toggleRefinement(attributeName, facetValue));
// Do not mistake searchForFacetValues and searchFacetValues which is the actual search
// function
const searchFacetValues = helperSpecializedSearchFacetValues &&
helperSpecializedSearchFacetValues(
state,
createURL,
helperSpecializedSearchFacetValues,
refine,
instantSearchInstance,
);
renderFn({
createURL: _createURL,
items,
refine,
searchForItems: searchFacetValues,
instantSearchInstance,
isFromSearch,
canRefine: isFromSearch || items.length > 0,
widgetParams,
isShowingMore,
toggleShowMore,
}, isFirstSearch);
};
let lastResultsFromMainSearch;
let searchForFacetValues;
let refine;
const createSearchForFacetValues = helper =>
(state, createURL, helperSpecializedSearchFacetValues, toggleRefinement, instantSearchInstance) =>
query => {
if (query === '' && lastResultsFromMainSearch) {
// render with previous data from the helper.
render({
items: lastResultsFromMainSearch,
state,
createURL,
helperSpecializedSearchFacetValues,
refine: toggleRefinement,
isFromSearch: false,
isFirstSearch: false,
instantSearchInstance,
});
} else {
helper.searchForFacetValues(attributeName, query).then(results => {
const facetValues = results.facetHits;
render({
items: facetValues,
state,
createURL,
helperSpecializedSearchFacetValues,
refine: toggleRefinement,
isFromSearch: true,
isFirstSearch: false,
instantSearchInstance,
});
});
}
};
return {
isShowingMore: false,
// Provide the same function to the `renderFn` so that way the user
// has to only bind it once when `isFirstRendering` for instance
toggleShowMore() {},
cachedToggleShowMore() { this.toggleShowMore(); },
createToggleShowMore(renderOptions) {
return () => {
this.isShowingMore = !this.isShowingMore;
this.render(renderOptions);
};
},
getLimit() {
return this.isShowingMore ? showMoreLimit : limit;
},
getConfiguration: (configuration = {}) => {
const widgetConfiguration = {
[operator === 'and' ? 'facets' : 'disjunctiveFacets']: [attributeName],
};
if (limit !== undefined) {
const currentMaxValuesPerFacet = configuration.maxValuesPerFacet || 0;
if (showMoreLimit === undefined) {
widgetConfiguration.maxValuesPerFacet = Math.max(currentMaxValuesPerFacet, limit);
} else {
widgetConfiguration.maxValuesPerFacet = Math.max(currentMaxValuesPerFacet, limit, showMoreLimit);
}
}
return widgetConfiguration;
},
init({helper, createURL, instantSearchInstance}) {
this.cachedToggleShowMore = this.cachedToggleShowMore.bind(this);
refine = facetValue => helper
.toggleRefinement(attributeName, facetValue)
.search();
searchForFacetValues = createSearchForFacetValues(helper);
render({
items: [],
state: helper.state,
createURL,
helperSpecializedSearchFacetValues: searchForFacetValues,
refine,
isFromSearch: false,
isFirstSearch: true,
instantSearchInstance,
isShowingMore: this.isShowingMore,
toggleShowMore: this.cachedToggleShowMore,
});
},
render(renderOptions) {
const {results, state, createURL, instantSearchInstance} = renderOptions;
const items = results
.getFacetValues(attributeName, {sortBy})
.slice(0, this.getLimit())
.map(formatItems);
lastResultsFromMainSearch = items;
this.toggleShowMore = this.createToggleShowMore(renderOptions);
render({
items,
state,
createURL,
helperSpecializedSearchFacetValues: searchForFacetValues,
refine,
isFromSearch: false,
isFirstSearch: false,
instantSearchInstance,
isShowingMore: this.isShowingMore,
toggleShowMore: this.cachedToggleShowMore,
});
},
};
};
}