Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 4 changes #245

Merged
merged 113 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
113 commits
Select commit Hold shift + click to select a range
0d6a0bd
add keystroke event
Mar 12, 2019
7391edc
add fields to selection event
Mar 12, 2019
84dfde5
obtain language from user's browser by default
Mar 13, 2019
d8bda5b
localize placeholder based on first language, #150
Mar 13, 2019
8be67be
sets trackProximity to true by default
Mar 13, 2019
8b23f7c
add documentation
Mar 14, 2019
3f3616a
Update changelog with unreleased changes
Mar 14, 2019
2a7306e
Add documentation
Mar 14, 2019
4e1b2a6
get placeholder is a private method
Mar 14, 2019
7ac2a10
fix tests
Mar 14, 2019
8ba23ea
update changelog with new changes
Mar 14, 2019
baa7109
Merge pull request #202 from mapbox/proximity-default
Mar 14, 2019
0ab7ecf
expose render and getItemValue functions as constructor options
Mar 15, 2019
a7bbf9c
add get/set functions
Mar 15, 2019
5ff3272
merge with version4 working branch
Mar 15, 2019
241f90c
merge with version4 working branch
Mar 15, 2019
ab3c5e0
Update jsdoc strings
Mar 15, 2019
2019e92
add events tests
Mar 15, 2019
4e364be
Merge branch 'version4' of github.com:mapbox/mapbox-gl-geocoder into …
Mar 15, 2019
60bf39d
change event method name
Mar 15, 2019
710fbc2
update changelog with proposed changes
Mar 15, 2019
301eb30
do not debounce keyevents
Mar 15, 2019
f2ccd56
batch events together to minimize network calls
Mar 15, 2019
17cd846
add and cleanup tests
Mar 15, 2019
67b7949
pass flyTo options to map upon selection
Mar 18, 2019
bf58172
update jsdoc and api docs
Mar 18, 2019
6e9f1ef
Update changelog
Mar 18, 2019
9997889
bump suggestions to v1.3.4
Mar 18, 2019
236e095
update changelog
Mar 18, 2019
2e625d9
Merge pull request #215 from mapbox/[email protected]
Mar 18, 2019
2cc5ebe
tests for options.flyTo
Mar 18, 2019
3e7abaa
Bump insecure dev dependencies
Mar 18, 2019
9393add
update changelog
Mar 18, 2019
f6a0cad
update outdated packages with npm update
Mar 18, 2019
c32841e
merge version4 upstream
Mar 18, 2019
6c6c98a
Merge pull request #201 from mapbox/localize-placeholder
Mar 18, 2019
0b73d85
remove use of hardcoded feature IDs
Mar 18, 2019
6baa496
update changelog
Mar 18, 2019
41a0d54
deduplicate result event emissions
Mar 18, 2019
1f0b6ba
update changelog
Mar 18, 2019
7c64490
Add marker to show selected result
Mar 18, 2019
4831816
generate documentation
Mar 19, 2019
69a21fb
Update changelog
Mar 19, 2019
5751660
respond to @katydecorah 's code review comments
Mar 19, 2019
1c44b72
Merge pull request #218 from mapbox/deduplicate-result-event
Mar 19, 2019
7e5309b
merge version4 upstream
Mar 19, 2019
8c6bd49
Ensure properties exist prior to checking id
katydecorah Mar 19, 2019
4cbc6ca
additional properties existance check
Mar 19, 2019
eff493c
Merge pull request #200 from mapbox/browser-locale
Mar 19, 2019
767d3c8
merge version4 upstream changes
Mar 19, 2019
6f6c71f
Merge pull request #217 from mapbox/no-hardcoded-ids
Mar 19, 2019
a427492
Fix typo in documentation
Mar 19, 2019
60feb9e
merge version4 upstream changes
Mar 19, 2019
36147da
Merge pull request #214 from mapbox/fly-to-options
Mar 19, 2019
8ba0600
expose event opt-out constructor option
Mar 19, 2019
b8104fd
Add documentation for event opt-out option
Mar 19, 2019
2991163
update tests to work with [email protected]
Mar 19, 2019
5a0b7b3
merge version4 upstream
Mar 19, 2019
24c8285
pull upstream changes from master
Mar 19, 2019
0fae824
pull upstream changes from version4
Mar 19, 2019
a96862d
add mapbox-gl-js as a peer dependency
Mar 19, 2019
008f98a
Update test
Mar 19, 2019
08b245a
sync with version4 upstream branch
Mar 19, 2019
bdc7d51
Merge pull request #216 from mapbox/bump-dev-deps
Mar 19, 2019
f454c4d
remove marker on geocoder.clear
Mar 20, 2019
e730470
Simplify marker options to a single constructor option
Mar 20, 2019
430e659
Make mapboxgl a constructor option so it is a real peer dependency
Mar 20, 2019
ff7346a
fix focus trap (#220)
katydecorah Mar 20, 2019
1be007b
[email protected]
Mar 20, 2019
35c3c84
merge version4 upstream changes
Mar 20, 2019
79ad3c2
add getter/setter functions for geocoder options
Mar 20, 2019
9902ad7
Merge pull request #208 from mapbox/custom-render
Mar 20, 2019
0535b27
tests for get and set functions
Mar 20, 2019
5cb9d6a
Update documentation for new get/set functions
Mar 20, 2019
c98c728
Update docs to indicate that flying options will be passed to both fl…
Mar 21, 2019
c405387
Update documentation with [email protected]
Mar 21, 2019
85364cb
Merge pull request #228 from mapbox/update-docs
Mar 21, 2019
2e03012
fixes double moveend event #131
Mar 21, 2019
b373667
Update documentation with new options.mapboxgl
Mar 21, 2019
5d98d0c
Remove marker on geocoder removal from map
Mar 21, 2019
ca51e90
[v4] create collapsed option (#222)
katydecorah Mar 21, 2019
95bc8d3
Merge pull request #229 from mapbox/fix-double-moveend
Mar 21, 2019
3447759
update changelog
Mar 21, 2019
dea449a
Merge version4 upstream changes
Mar 21, 2019
0825a3a
merge version4 upstream changes
Mar 21, 2019
172809c
Update changelog
Mar 21, 2019
a9998e9
Reformat changelog to highlight impact of each change
Mar 21, 2019
4c31226
[v4] style updates (#211)
katydecorah Mar 21, 2019
dcf3fdf
merge version4 upstream changes
Mar 21, 2019
acb62e2
add mobile styles
Mar 21, 2019
d8418cc
reset this.lastSelected on clear (#224)
andrewharvey Mar 22, 2019
e57b7ed
reduce minimum version of mapboxgljs
Mar 22, 2019
9e77770
Merge pull request #227 from mapbox/camera-options-on-fit-bounds
Mar 22, 2019
524f598
Documentation updates suggested by @katydecorah
Mar 22, 2019
5c6c339
Fix last outstanding comment
Mar 22, 2019
94e0031
Merge version4 upstream changes
Mar 22, 2019
ee926a2
Merge pull request #226 from mapbox/get-set-options
Mar 22, 2019
ec86cf4
pull version 4 upstream changes
Mar 22, 2019
41dc4f9
Merge pull request #219 from mapbox/map-marker
Mar 22, 2019
cd5682b
pull version4 upstream changes and resolve conflicts
Mar 25, 2019
f6ad56f
Change according to @ingalls review
Mar 25, 2019
4431b04
Merge pull request #212 from mapbox/[email protected]
Mar 25, 2019
d8a3958
merge version4 upstream changes
Mar 26, 2019
26997ca
Merge pull request #230 from mapbox/format-changelog
Mar 26, 2019
a06553d
increase input and icon size on mobile
yuletide Mar 27, 2019
184ba74
Make clear method public
Mar 27, 2019
48cc2ae
update changelog
Mar 27, 2019
16d774b
Update clear method documentation
Mar 27, 2019
895f569
Merge pull request #233 from mapbox/v4-mobile
Mar 27, 2019
6fa9eb8
Merge pull request #235 from mapbox/public-clear-method
Mar 27, 2019
0969ff7
Minor documentation updates to improve clarity
Apr 8, 2019
4adb843
code review changes
Apr 8, 2019
24dda61
Merge pull request #243 from mapbox/misc-doc-updates
Apr 8, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
449 changes: 403 additions & 46 deletions API.md

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
## Master

### Breaking Changes ⚠️
- Support for the Mapbox GL JS 0.47.0 API. This is compatible with 0.47.0 and later, and may not be compatible with earlier versions [#219](https://github.com/mapbox/mapbox-gl-geocoder/pull/219).
- Obtain language from user's browser settings [#195](https://github.com/mapbox/mapbox-gl-geocoder/issues/195)
- Localize placeholder based on language set in constructor options [#150](https://github.com/mapbox/mapbox-gl-geocoder/issues/150)
- `trackProximity` turned on by default [#195](https://github.com/mapbox/mapbox-gl-geocoder/issues/195)

### Features / Improvements 🚀
- Mapbox events upgraded to v0.2.0 for better handling [#212](https://github.com/mapbox/mapbox-gl-geocoder/pull/212)
- Pass `flyTo` options to the map on result selection on both map#flyTo and map#fitBounds operations [#214](https://github.com/mapbox/mapbox-gl-geocoder/pull/214) and [#227](https://github.com/mapbox/mapbox-gl-geocoder/pull/227)
- Bump `suggestions` dependency to v1.4.x
- Adds the `marker` constructor option that allows adding the selected result to the map as a [marker](https://docs.mapbox.com/mapbox-gl-js/api/#marker). Adding the marker to the map is now the default behavior. [#219](https://github.com/mapbox/mapbox-gl-geocoder/pull/219).
- Add `get` and `set` methods for constructor options [#226](https://github.com/mapbox/mapbox-gl-geocoder/pull/226)
- Add `collapsed` option to collapse the geocoder controller into a button until hovered or focused [#222](https://github.com/mapbox/mapbox-gl-geocoder/issues/222)
- Expose `clear` as public method [#115](https://github.com/mapbox/mapbox-gl-geocoder/issues/115)

### Bug Fixes 🐛
- Upgrade dev dependencies [#216](https://github.com/mapbox/mapbox-gl-geocoder/pull/216)
- Remove hardcoded IDs in bounding box exception list [#217](https://github.com/mapbox/mapbox-gl-geocoder/pull/217)
- Fix double map `moveend` event [#229](https://github.com/mapbox/mapbox-gl-geocoder/pull/229)
- Fix duplicate `result` event bug [#218](https://github.com/mapbox/mapbox-gl-geocoder/pull/218)
- Fix trapped focus bug [#220](https://github.com/mapbox/mapbox-gl-geocoder/issues/220)


## v3.1.6
- Resolve npm publish failure

Expand Down
7 changes: 4 additions & 3 deletions debug/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ var coordinatesGeocoder = function(query) {
return null;
}
function coordinateFeature(lng, lat) {
var lng = Number(lng);
var lat = Number(lat);
lng = Number(lng);
lat = Number(lat);
return {
center: [lng, lat],
geometry: {
Expand Down Expand Up @@ -76,7 +76,8 @@ var geocoder = new MapboxGeocoder({
trackProximity: true,
localGeocoder: function(query) {
return coordinatesGeocoder(query);
}
},
mapboxgl: mapboxgl
});

window.geocoder = geocoder;
Expand Down
165 changes: 118 additions & 47 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ var nanoid = require('nanoid')
* Construct a new mapbox event client to send interaction events to the mapbox event service
* @param {Object} options options with which to create the service
* @param {String} options.accessToken the mapbox access token to make requests
* @param {Number} [options.flushInterval=1000] the number of ms after which to flush the event queue
* @param {Number} [options.maxQueueSize=100] the number of events to queue before flushing
* @private
*/
function MapboxEventManager(options) {
this.origin = options.origin || 'https://api.mapbox.com';
this.endpoint = '/events/v2';
this.endpoint = 'events/v2';
this.access_token = options.accessToken;
this.version = '0.0.1'
this.version = '0.2.0'
this.sessionID = this.generateSessionID();
this.userAgent = this.getUserAgent();

this.options = options;
this.send = this.send.bind(this);


// parse global options to be sent with each request
this.countries = (options.countries) ? options.countries.split(",") : null;
this.types = (options.types) ? options.types.split(",") : null;
Expand All @@ -22,47 +29,71 @@ function MapboxEventManager(options) {
this.limit = (options.limit) ? +options.limit : null;
this.locale = navigator.language || null;
this.enableEventLogging = this.shouldEnableLogging(options);
this.eventQueue = new Array();
this.flushInterval = options.flushInterval || 1000;
this.maxQueueSize = options.maxQueueSize || 100;
this.timer = (this.flushInterval) ? setTimeout(this.flush.bind(this), this.flushInterval) : null;
// keep some state to deduplicate requests if necessary
this.lastSentInput = "";
this.lastSentIndex = 0;
}

MapboxEventManager.prototype = {

/**
* Send a search.select event to the mapbox events service
* This event marks the array index of the item selected by the user out of the array of possible options
* @private
* @param {Object} selected the geojson feature selected by the user
* @param {Object} geocoder a mapbox-gl-geocoder instance
* @param {Function} callback a callback function to invoke once the event has been sent (optional)
* @returns {Promise}
*/
select: function (selected, geocoder, callback) {
* Send a search.select event to the mapbox events service
* This event marks the array index of the item selected by the user out of the array of possible options
* @private
* @param {Object} selected the geojson feature selected by the user
* @param {Object} geocoder a mapbox-gl-geocoder instance
* @returns {Promise}
*/
select: function(selected, geocoder){
var resultIndex = this.getSelectedIndex(selected, geocoder);
var payload = this.getEventPayload('search.select', geocoder);
payload.resultIndex = resultIndex;
payload.resultPlaceName = selected.place_name;
payload.resultId = selected.id;
if ((resultIndex === this.lastSentIndex && payload.queryString === this.lastSentInput) || resultIndex == -1) {
// don't log duplicate events if the user re-selected the same feature on the same search
if (callback) return callback();
else return;
return;
}
this.lastSentIndex = resultIndex;
this.lastSentInput = payload.queryString;
return this.send(payload, callback)
if (!payload.queryString) return; // will be rejected
return this.push(payload)
},

/**
* Send a search-start event to the mapbox events service
* This turnstile event marks when a user starts a new search
* Send a search-start event to the mapbox events service
* This turnstile event marks when a user starts a new search
* @private
* @param {Object} geocoder a mapbox-gl-geocoder instance
* @returns {Promise}
*/
start: function(geocoder){
var payload = this.getEventPayload('search.start', geocoder);
if (!payload.queryString) return; // will be rejected
return this.push(payload);
},

/**
* Send a search-keyevent event to the mapbox events service
* This event records each keypress in sequence
* @private
* @param {Object} geocoder a mapbox-gl-geocoder instance
* @param {Function} callback
* @returns {Promise}
* @param {Object} keyEvent the keydown event to log
* @param {Obeject} geocoder a mapbox-gl-geocoder instance
*
*/
start: function (geocoder, callback) {
var payload = this.getEventPayload('search.start', geocoder);
return this.send(payload, callback);
keyevent: function(keyEvent, geocoder){
//pass invalid event
if (!keyEvent.key) return;
// don't send events for keys that don't change the input
// TAB, ESC, LEFT, RIGHT, ENTER, UP, DOWN
if (keyEvent.metaKey || [9, 27, 37, 39, 13, 38, 40].indexOf(keyEvent.keyCode) !== -1) return;
var payload = this.getEventPayload('search.keystroke', geocoder);
payload.lastAction = keyEvent.key;
if (!payload.queryString) return; // will be rejected
return this.push(payload);
},

/**
Expand All @@ -72,27 +103,29 @@ MapboxEventManager.prototype = {
*
* @private
* @param {Object} payload the http POST body of the event
* @param {Function} [callback] a callback function to invoke when the send has completed
* @returns {Promise}
*/
send: function (payload, callback) {
if (!callback) callback = function () {
return
};
if (!this.enableEventLogging) {
return callback();
if (callback) return callback();
return;
}
var options = this.getRequestOptions(payload);
this.request(options, function (err) {
this.request(options, function(err){
if (err) return this.handleError(err, callback);
if (callback) return callback();
})
if (callback) {
return callback();
}
}.bind(this))
},
/**
* Get http request options
* @private
* @param {*} payload
*/
getRequestOptions: function (payload) {
getRequestOptions: function(payload){
if (!Array.isArray(payload)) payload = [payload];
var options = {
// events must be sent with POST
method: "POST",
Expand All @@ -101,7 +134,7 @@ MapboxEventManager.prototype = {
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify([payload]) //events are arrays
body:JSON.stringify(payload) //events are arrays
}
return options
},
Expand Down Expand Up @@ -134,7 +167,7 @@ MapboxEventManager.prototype = {
proximity: proximity,
limit: geocoder.options.limit,
// routing: search.routing, //todo --> add to plugin
queryString: geocoder.inputString,
queryString: (geocoder._inputEl) ? geocoder._inputEl.value : geocoder.inputString,
mapZoom: zoom,
keyboardLocale: this.locale
}
Expand All @@ -159,7 +192,8 @@ MapboxEventManager.prototype = {
}
}
};
xhttp.open(opts.method, opts.host + opts.path, true);

xhttp.open(opts.method, opts.host + '/' + opts.path, true);
for (var header in opts.headers){
var headerValue = opts.headers[header];
xhttp.setRequestHeader(header, headerValue)
Expand Down Expand Up @@ -194,13 +228,14 @@ MapboxEventManager.prototype = {
},

/**
* Get the 0-based numeric index of the item that the user selected out of the list of options
* @private
* @param {Object} selected the geojson feature selected by the user
* @param {Object} geocoder a Mapbox-GL-Geocoder instance
* @returns {Number} the index of the selected result
*/
getSelectedIndex: function (selected, geocoder) {
* Get the 0-based numeric index of the item that the user selected out of the list of options
* @private
* @param {Object} selected the geojson feature selected by the user
* @param {Object} geocoder a Mapbox-GL-Geocoder instance
* @returns {Number} the index of the selected result
*/
getSelectedIndex: function(selected, geocoder){
if (!geocoder._typeahead) return;
var results = geocoder._typeahead.data;
var selectedID = selected.id;
var resultIDs = results.map(function (feature) {
Expand All @@ -211,17 +246,53 @@ MapboxEventManager.prototype = {
},

/**
* Check whether events should be logged
* Clients using a localGeocoder or an origin other than mapbox should not have events logged
* @private
*/
shouldEnableLogging: function (options) {
if (this.origin.indexOf('api.mapbox.com') == -1) return false;
* Check whether events should be logged
* Clients using a localGeocoder or an origin other than mapbox should not have events logged
* @private
*/
shouldEnableLogging: function(options){
if (options.enableEventLogging === false) return false;
if (options.origin && options.origin.indexOf('api.mapbox.com') == -1) return false;
// hard to make sense of events when a local instance is suplementing results from origin
if (options.localGeocoder) return false;
// hard to make sense of events when a custom filter is in use
if (options.filter) return false;
return true;
},

/**
* Flush out the event queue by sending events to the events service
* @private
*/
flush: function(){
if (this.eventQueue.length > 0){
this.send(this.eventQueue);
this.eventQueue = new Array();
}
// //reset the timer
if (this.timer) clearTimeout(this.timer);
if (this.flushInterval) this.timer = setTimeout(this.flush.bind(this), this.flushInterval)
},

/**
* Push event into the pending queue
* @param {Object} evt the event to send to the events service
* @param {Boolean} forceFlush indicates that the event queue should be flushed after adding this event regardless of size of the queue
* @private
*/
push: function(evt, forceFlush){
this.eventQueue.push(evt);
if (this.eventQueue.length >= this.maxQueueSize || forceFlush){
this.flush();
}
},

/**
* Flush any remaining events from the queue before it is removed
* @private
*/
remove: function(){
this.flush();
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/exceptions.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module.exports = {
'country.3148': {
'fr': {
'name': 'France',
'bbox': [[-4.59235, 41.380007], [9.560016, 51.148506]]
},
'country.3145': {
'us': {
'name': 'United States',
'bbox': [[-171.791111, 18.91619], [-66.96466, 71.357764]]
},
'country.330': {
'ru': {
'name': 'Russia',
'bbox': [[19.66064, 41.151416], [190.10042, 81.2504]]
},
'country.3179': {
'ca': {
'name': 'Canada',
'bbox': [[-140.99778, 41.675105], [-52.648099, 83.23324]]
}
Expand Down
3 changes: 0 additions & 3 deletions lib/icons/close.svg

This file was deleted.

1 change: 0 additions & 1 deletion lib/icons/loader.svg

This file was deleted.

3 changes: 0 additions & 3 deletions lib/icons/search.svg

This file was deleted.

Loading