Skip to content

Commit f869885

Browse files
committed
feat(url-sync): use the new mapping option
Closes #838
1 parent 3d3804a commit f869885

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

dev/app.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ var search = instantsearch({
55
appId: 'latency',
66
apiKey: '6be0576ff61c053d5f9a3225e2a90f76',
77
indexName: 'instant_search',
8-
urlSync: window.history && 'pushState' in window.history ? true : {useHash: true}
8+
urlSync: {
9+
useHash: !(window.history && 'pushState' in window.history),
10+
mapping: {
11+
q: 'query',
12+
hPP: 'hits',
13+
hFR: 'hierarchical'
14+
}
15+
}
916
});
1017

1118
search.addWidget(

src/lib/InstantSearch.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,26 @@ function defaultCreateURL() { return '#'; }
1818
* @param {string} options.indexName The name of the main index
1919
* @param {string} [options.numberLocale] The locale used to display numbers. This will be passed
2020
* to Number.prototype.toLocaleString()
21+
* @param {function} [options.searchFunction] A hook that will be called each time a search needs to be done, with the
22+
* helper as a parameter. It's your responsibility to call helper.search(). This option allows you to avoid doing
23+
* searches at page load for example.
2124
* @param {Object} [options.searchParameters] Additional parameters to pass to
2225
* the Algolia API.
2326
* [Full documentation](https://community.algolia.com/algoliasearch-helper-js/docs/SearchParameters.html)
2427
* @param {Object|boolean} [options.urlSync] Url synchronization configuration.
2528
* Setting to `true` will synchronize the needed search parameters with the browser url.
29+
* @param {Object} [options.urlSync.mapping] Object used to define replacement query
30+
* parameter to use in place of another. Keys are current query parameters
31+
* and value the new value, e.g. `{ q: 'query' }`.
32+
* @param {number} [options.urlSync.threshold] Time in ms after which a new
33+
* state is created in the browser history. The default value is 700.
2634
* @param {string[]} [options.urlSync.trackedParameters] Parameters that will
2735
* be synchronized in the URL. By default, it will track the query, all the
2836
* refinable attribute (facets and numeric filters), the index and the page.
2937
* [Full documentation](https://community.algolia.com/algoliasearch-helper-js/docs/SearchParameters.html)
3038
* @param {boolean} [options.urlSync.useHash] If set to true, the url will be
3139
* hash based. Otherwise, it'll use the query parameters using the modern
3240
* history API.
33-
* @param {number} [options.urlSync.threshold] Time in ms after which a new
34-
* state is created in the browser history. The default value is 700.
35-
* @param {function} [options.searchFunction] A hook that will be called each time a search needs to be done, with the
36-
* helper as a parameter. It's your responsibility to call helper.search(). This option allows you to avoid doing
37-
* searches at page load for example.
3841
* @return {Object} the instantsearch instance
3942
*/
4043
class InstantSearch extends EventEmitter {

src/lib/url-sync.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,15 @@ class URLSync {
107107
this.urlUtils = urlUtils;
108108
this.originalConfig = null;
109109
this.timer = timerMaker(Date.now());
110+
this.mapping = options.mapping || {};
110111
this.threshold = options.threshold || 700;
111112
this.trackedParameters = options.trackedParameters || ['query', 'attribute:*', 'index', 'page', 'hitsPerPage'];
112113
}
113114

114115
getConfiguration(currentConfiguration) {
115116
this.originalConfig = currentConfiguration;
116117
let queryString = this.urlUtils.readUrl();
117-
let config = AlgoliaSearchHelper.getConfigurationFromQueryString(queryString);
118+
let config = AlgoliaSearchHelper.getConfigurationFromQueryString(queryString, {mapping: this.mapping});
118119
return config;
119120
}
120121

@@ -137,12 +138,15 @@ class URLSync {
137138

138139
renderURLFromState(state) {
139140
let currentQueryString = this.urlUtils.readUrl();
140-
let foreignConfig = AlgoliaSearchHelper.getForeignConfigurationInQueryString(currentQueryString);
141+
let foreignConfig = AlgoliaSearchHelper.getForeignConfigurationInQueryString(currentQueryString, {mapping: this.mapping});
141142
foreignConfig.is_v = majorVersionNumber;
142143

143144
let qs = urlHelper.getQueryStringFromState(
144145
state.filter(this.trackedParameters),
145-
{moreAttributes: foreignConfig}
146+
{
147+
moreAttributes: foreignConfig,
148+
mapping: this.mapping
149+
}
146150
);
147151

148152
if (this.timer() < this.threshold) {
@@ -157,17 +161,17 @@ class URLSync {
157161
createURL(state) {
158162
let currentQueryString = this.urlUtils.readUrl();
159163
let filteredState = state.filter(this.trackedParameters);
160-
let foreignConfig = algoliasearchHelper.url.getUnrecognizedParametersInQueryString(currentQueryString);
164+
let foreignConfig = algoliasearchHelper.url.getUnrecognizedParametersInQueryString(currentQueryString, {mapping: this.mapping});
161165
// Add instantsearch version to reconciliate old url with newer versions
162166
foreignConfig.is_v = majorVersionNumber;
163167

164-
return this.urlUtils.createURL(algoliasearchHelper.url.getQueryStringFromState(filteredState));
168+
return this.urlUtils.createURL(algoliasearchHelper.url.getQueryStringFromState(filteredState, {mapping: this.mapping}));
165169
}
166170

167171
onHistoryChange(fn) {
168172
this.urlUtils.onpopstate(() => {
169173
let qs = this.urlUtils.readUrl();
170-
let partialState = AlgoliaSearchHelper.getConfigurationFromQueryString(qs);
174+
let partialState = AlgoliaSearchHelper.getConfigurationFromQueryString(qs, {mapping: this.mapping});
171175
let fullState = merge({}, this.originalConfig, partialState);
172176
fn(fullState);
173177
});

0 commit comments

Comments
 (0)