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

Enable search box geocoder provider selection #2224

Merged
merged 3 commits into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 0 additions & 5 deletions src/api/create-vis.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ var VisView = require('../vis/vis-view');
var VisModel = require('../vis/vis');
var Loader = require('../core/loader');
var VizJSON = require('./vizjson');
var config = require('../cdb.config');

var DEFAULT_OPTIONS = {
tiles_loader: true,
Expand Down Expand Up @@ -58,10 +57,6 @@ var createVis = function (el, vizjson, options) {
loadVizJSON(el, visModel, vizjson, options);
}

if (options.mapzenApiKey) {
config.set('mapzenApiKey', options.mapzenApiKey);
}

return visModel;
};

Expand Down
22 changes: 20 additions & 2 deletions src/geo/ui/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ var Search = View.extend({
},

_initializeGeocoder: function () {
this.geocoderService = this.options.geocoderService || DEFAULT_GEOCODER;
const injectedGeocoderConfig = this._getGeocodingInfoFromConfig();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing that tingles my spidey sense is the mix of configurations.

I can get the provider from the injected config but the token can be undefined and then I go directly for the window property... those combinations.

That won't break the code but if a configuration is wrong perhaps it makes it harder to debug.


this.geocoderService = this.options.geocoderService || injectedGeocoderConfig.provider || DEFAULT_GEOCODER;
this.geocoder = GEOCODERS[this.geocoderService];

const windowApiKey = GEOCODERS_WINDOW_API_KEYS[this.geocoderService];
this.token = this.options.token || window[windowApiKey];
this.token = this.options.token || injectedGeocoderConfig.token || window[windowApiKey];
},

render: function () {
Expand Down Expand Up @@ -209,6 +211,22 @@ var Search = View.extend({
this.mapView.unbind('click', this._destroySearchPin, this);
},

_getGeocodingInfoFromConfig () {
if (!window.geocoderConfiguration) {
return {};
}

const provider = window.geocoderConfiguration.provider;
let token;

if (provider) {
token = window.geocoderConfiguration[provider] &&
window.geocoderConfiguration[provider].search_bar_api_key;
}

return { provider, token };
},

clean: function () {
this._unbindEvents();
this._destroySearchPin();
Expand Down
81 changes: 81 additions & 0 deletions test/spec/geo/ui/search.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,87 @@ describe('geo/ui/search', function () {
});
});

describe('_initializeGeocoder', function () {
it('should set geocoder from options if available', function () {
this.view = new Search({
model: this.map,
mapView: this.mapView,
geocoderService: 'mapbox',
token: 'token_from_options'
});

expect(this.view.geocoderService).toBe('mapbox');
expect(this.view.token).toBe('token_from_options');
});

it('should set geocoder from injected configuration if available', function () {
spyOn(Search.prototype, '_getGeocodingInfoFromConfig').and.returnValue({
provider: 'tomtom_fake',
token: 'fakeAPIKey'
});

this.view = new Search({
model: this.map,
mapView: this.mapView
});

expect(this.view.geocoderService).toBe('tomtom_fake');
expect(this.view.token).toBe('fakeAPIKey');
});

it('should set default geocoder if no configurations are available', function () {
Object.defineProperty(window, 'tomtomApiKey', {
value: 'fake_tomtom_key',
writable: true
});

this.view = new Search({
model: this.map,
mapView: this.mapView
});

expect(this.view.geocoderService).toBe('tomtom');
expect(this.view.token).toBe('fake_tomtom_key');
});
});

describe('_getGeocodingInfoFromConfig', function () {
it('should return an empty object if there is no geocoderConfiguration', function () {
expect(this.view._getGeocodingInfoFromConfig()).toEqual({});
});

it('should return provider and token from geocoderConfiguration', function () {
var geocoderConfiguration = {
provider: 'tomtom',
tomtom: {
search_bar_api_key: 'fakeAPIKey'
}
};

Object.defineProperty(window, 'geocoderConfiguration', {
value: geocoderConfiguration,
writable: true
});

expect(this.view._getGeocodingInfoFromConfig()).toEqual({
provider: 'tomtom', token: 'fakeAPIKey'
});
});

it('should return undefined provider and token if no provider found in geocoderConfiguration', function () {
var geocoderConfiguration = {};

Object.defineProperty(window, 'geocoderConfiguration', {
value: geocoderConfiguration,
writable: true
});

expect(this.view._getGeocodingInfoFromConfig()).toEqual({
provider: undefined, token: undefined
});
});
});

afterEach(function () {
this.$el.remove();
});
Expand Down