Skip to content

Commit 7030daf

Browse files
authored
convert Geocoder to class, respect API option (#162)
1 parent 8e2f931 commit 7030daf

File tree

3 files changed

+67
-44
lines changed

3 files changed

+67
-44
lines changed

src/controls/geocoder.js

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,26 @@ import Typeahead from 'suggestions';
44
import debounce from 'lodash.debounce';
55
import extend from 'xtend';
66
import { EventEmitter } from 'events';
7-
import utils from '../utils'
8-
9-
// Mapbox Geocoder version
10-
var API = 'https://api.mapbox.com/geocoding/v5/mapbox.places/';
7+
import utils from '../utils';
118

129
// Geocoder - this slightly mimicks the mapboxl-gl-geocoder but isn't an exact replica.
1310
// Once gl-js plugins can be added to custom divs, we should be able to require mapbox-gl-geocoder
1411
// instead of including it here
15-
function Geocoder(options) {
16-
this._ev = new EventEmitter();
17-
this.options = extend({}, this.options, options);
18-
}
19-
20-
Geocoder.prototype = {
2112

22-
options: {
23-
placeholder: 'Search',
24-
zoom: 16,
25-
flyTo: true
26-
},
13+
export default class Geocoder {
14+
constructor(options) {
15+
var defaultOptions = {
16+
placeholder: 'Search',
17+
zoom: 16,
18+
flyTo: true,
19+
};
20+
21+
this._ev = new EventEmitter();
22+
this.options = extend({}, defaultOptions, options);
23+
this.api = options && options.api || 'https://api.mapbox.com/geocoding/v5/mapbox.places/';
24+
}
2725

28-
onAdd: function(map) {
26+
onAdd(map) {
2927
this._map = map;
3028

3129
this.request = new XMLHttpRequest();
@@ -95,9 +93,9 @@ Geocoder.prototype = {
9593
this._typeahead.getItemValue = function(item) { return item.place_name; };
9694

9795
return el;
98-
},
96+
}
9997

100-
_geocode: function(q, callback) {
98+
_geocode(q, callback) {
10199
this._loadingEl.classList.add('active');
102100
this.fire('loading');
103101

@@ -109,9 +107,8 @@ Geocoder.prototype = {
109107

110108
var accessToken = this.options.accessToken ? this.options.accessToken : mapboxgl.accessToken;
111109
options.push('access_token=' + accessToken);
112-
113110
this.request.abort();
114-
this.request.open('GET', API + encodeURIComponent(q.trim()) + '.json?' + options.join('&'), true);
111+
this.request.open('GET', this.api + encodeURIComponent(q.trim()) + '.json?' + options.join('&'), true);
115112
this.request.onload = function() {
116113
this._loadingEl.classList.remove('active');
117114
if (this.request.status >= 200 && this.request.status < 400) {
@@ -137,25 +134,25 @@ Geocoder.prototype = {
137134
}.bind(this);
138135

139136
this.request.send();
140-
},
137+
}
141138

142-
_queryFromInput: function(q) {
139+
_queryFromInput(q) {
143140
q = q.trim();
144141
if (!q) this._clear();
145142
if (q.length > 2) {
146143
this._geocode(q, function(results) {
147144
this._results = results;
148145
}.bind(this));
149146
}
150-
},
147+
}
151148

152-
_change: function() {
149+
_change() {
153150
var onChange = document.createEvent('HTMLEvents');
154151
onChange.initEvent('change', true, false);
155152
this._inputEl.dispatchEvent(onChange);
156-
},
153+
}
157154

158-
_query: function(input) {
155+
_query(input) {
159156
if (!input) return;
160157
if (typeof input === 'object' && input.length) {
161158
input = [
@@ -172,9 +169,9 @@ Geocoder.prototype = {
172169
this._inputEl.value = result.place_name;
173170
this._change();
174171
}.bind(this));
175-
},
172+
}
176173

177-
_setInput: function(input) {
174+
_setInput(input) {
178175
if (!input) return;
179176
if (typeof input === 'object' && input.length) {
180177
input = [
@@ -189,9 +186,9 @@ Geocoder.prototype = {
189186
this._typeahead.selected = null;
190187
this._typeahead.clear();
191188
this._change();
192-
},
189+
}
193190

194-
_clear: function() {
191+
_clear() {
195192
this._input = null;
196193
this._inputEl.value = '';
197194
this._typeahead.selected = null;
@@ -200,31 +197,31 @@ Geocoder.prototype = {
200197
this._inputEl.focus();
201198
this._clearEl.classList.remove('active');
202199
this.fire('clear');
203-
},
200+
}
204201

205-
getResult: function() {
202+
getResult() {
206203
return this._input;
207-
},
204+
}
208205

209206
/**
210207
* Set & query the input
211208
* @param {Array|String} query An array of coordinates [lng, lat] or location name as a string.
212209
* @returns {Geocoder} this
213210
*/
214-
query: function(query) {
211+
query(query) {
215212
this._query(query);
216213
return this;
217-
},
214+
}
218215

219216
/**
220217
* Set input
221218
* @param {Array|String} value An array of coordinates [lng, lat] or location name as a string. Calling this function just sets the input and does not trigger an API request.
222219
* @returns {Geocoder} this
223220
*/
224-
setInput: function(value) {
221+
setInput(value) {
225222
this._setInput(value);
226223
return this;
227-
},
224+
}
228225

229226
/**
230227
* Subscribe to events that happen within the plugin.
@@ -238,32 +235,30 @@ Geocoder.prototype = {
238235
* @param {Function} fn function that's called when the event is emitted.
239236
* @returns {Geocoder} this;
240237
*/
241-
on: function(type, fn) {
238+
on(type, fn) {
242239
this._ev.on(type, fn);
243240
return this;
244-
},
241+
}
245242

246243
/**
247244
* Fire an event
248245
* @param {String} type event name.
249246
* @param {Object} data event data to pass to the function subscribed.
250247
* @returns {Geocoder} this
251248
*/
252-
fire: function(type, data) {
249+
fire(type, data) {
253250
this._ev.emit(type, data);
254251
return this;
255-
},
252+
}
256253

257254
/**
258255
* Remove an event
259256
* @returns {Geocoder} this
260257
* @param {String} type Event name.
261258
* @param {Function} fn Function that should unsubscribe to the event emitted.
262259
*/
263-
off: function(type, fn) {
260+
off(type, fn) {
264261
this._ev.removeListener(type, fn);
265262
return this;
266263
}
267264
};
268-
269-
module.exports = Geocoder;

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require('./test.directions');
1111
// require('./test.options');
1212
require('./test.inputs');
1313
require('./test.instructions');
14+
require('./test.geocoder');
1415
require('./test.utils');
1516

1617
// close the smokestack window once tests are complete

test/test.geocoder.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const test = require('tape');
4+
import Geocoder from '../src/controls/geocoder';
5+
6+
test('Geocoder#constructor', t =>{
7+
t.test('default options', t =>{
8+
const geocoder = new Geocoder();
9+
10+
t.equal(geocoder.api, 'https://api.mapbox.com/geocoding/v5/mapbox.places/');
11+
t.deepEqual(geocoder.options, {
12+
placeholder: 'Search',
13+
zoom: 16,
14+
flyTo: true,
15+
});
16+
t.end();
17+
});
18+
19+
20+
t.test('Geocoder#api', t => {
21+
const geocoder = new Geocoder({api: 'https://fake-geocoder.pizza'});
22+
t.equal(geocoder.api, 'https://fake-geocoder.pizza');
23+
t.end();
24+
});
25+
26+
t.end();
27+
})

0 commit comments

Comments
 (0)