This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(instantsearch): Provide an instantsearch.js widget for places
- Loading branch information
Alexandre Stanislawski
committed
Jun 15, 2016
1 parent
a9c68d4
commit e574433
Showing
8 changed files
with
153 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<input type="search" id="input-map-instantsearch" class="form-control" placeholder="Where are you looking for a coffee?" /> | ||
<div id="map-instantsearch-container"></div> | ||
|
||
<style> | ||
#map-instantsearch-container {height: 300px}; | ||
</style> | ||
|
||
<%= javascript_include_tag config[:instantsearch_lib_url] %> | ||
<%= javascript_include_tag config[:places_instantsearch_widget_lib_url] %> | ||
<%= javascript_include_tag config[:instantsearch_googlemaps_lib_url] %> | ||
<%= javascript_include_tag config[:google_maps_lib_url] %> | ||
<script> | ||
(function() { | ||
var search = instantsearch({ | ||
appId: 'latency', | ||
apiKey: 'ffc36feb6e9df06e1c3c4549b5af2b31', | ||
indexName: 'starbucks', | ||
searchParameters: { | ||
hitsPerPage: 50 | ||
} | ||
}); | ||
|
||
var searchBox = placesInstantsearchWidget({ | ||
container: document.querySelector('#input-map-instantsearch') | ||
}); | ||
|
||
var map = instantsearchGoogleMaps({ | ||
container: document.querySelector('#map-instantsearch-container'), | ||
prepareMarkerData: ({Brand, Name, 'Street Combined': street}) => { | ||
return { | ||
title: `${Brand} - ${Name}` | ||
}; | ||
}, | ||
refineOnMapInteraction: true | ||
}); | ||
|
||
search.addWidget(searchBox); | ||
search.addWidget(map); | ||
search.start(); | ||
})(); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import widget from './src/instantsearch/widget.js'; | ||
module.exports = widget; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import places from '../places.js'; | ||
|
||
/** | ||
* The underlying structure for the Algolia Places instantsearch widget. | ||
*/ | ||
class AlgoliaPlacesWidget { | ||
constructor({ | ||
defaultPosition = [0, 0], | ||
...placesOptions | ||
}) { | ||
this.defaultPosition = defaultPosition.join(','); | ||
this.placesOptions = placesOptions; | ||
} | ||
init({helper}) { | ||
const placesAutocomplete = places(this.placesOptions); | ||
|
||
helper.setQueryParameter('aroundLatLng', this.defaultPosition); | ||
|
||
placesAutocomplete.on('change', (opts) => { | ||
const {suggestion: {latlng: {lat, lng}}} = opts; | ||
helper.setQueryParameter('aroundLatLng', `${lat},${lng}`) | ||
.search(); | ||
}); | ||
|
||
placesAutocomplete.on('clear', () => { | ||
helper.setQueryParameter('aroundLatLng', this.defaultPosition) | ||
.search(); | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Creates a new instance of the Algolia Places widget. This widget | ||
* sets the geolocation value for the search based on the seelected | ||
* result in the Algolia Places autocomplete. If the input is cleared, | ||
* the position is result to the default position. | ||
* @function | ||
* @param {object} opts configuration object | ||
* @param {number[]} opts.defaultPosition=[0,0] default position as an array of the form [lat, lng] | ||
* @returns {Widget} the algolia places widget | ||
*/ | ||
export default function makeAlgoliaPlacesWidget(opts) { | ||
return new AlgoliaPlacesWidget(opts); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters