|
3 | 3 | <script> |
4 | 4 | import { mglBaseMixin, mglControlMixin } from 'vue-mapbox' |
5 | 5 |
|
| 6 | + const geocoderEvents = { |
| 7 | + loading: 'loading', |
| 8 | + results: 'results', |
| 9 | + result: 'result', |
| 10 | + error: 'error' |
| 11 | + } |
| 12 | +
|
6 | 13 | export default { |
7 | 14 | name: 'GeocoderControl', |
8 | 15 | mixins: [mglBaseMixin, mglControlMixin], |
9 | 16 |
|
10 | 17 | props: { |
| 18 | + // Mapbox-geocoder options |
11 | 19 | accessToken: { |
12 | 20 | type: String, |
| 21 | + required: true |
| 22 | + }, |
| 23 | + zoom: { |
| 24 | + type: Number, |
| 25 | + default: 16 |
| 26 | + }, |
| 27 | + flyTo: { |
| 28 | + type: Boolean, |
| 29 | + default: true |
| 30 | + }, |
| 31 | + placeholder: { |
| 32 | + type: String, |
| 33 | + default: 'Search' |
| 34 | + }, |
| 35 | + proximity: { |
| 36 | + type: Object, |
13 | 37 | default: null |
14 | 38 | }, |
15 | | - position: { |
| 39 | + trackProximity: { |
| 40 | + type: Boolean, |
| 41 | + default: false |
| 42 | + }, |
| 43 | + bbox: { |
| 44 | + type: Array, |
| 45 | + default: null |
| 46 | + }, |
| 47 | + types: { |
| 48 | + type: String, |
| 49 | + default: null |
| 50 | + }, |
| 51 | + country: { |
16 | 52 | type: String, |
17 | | - default: 'top-right' |
| 53 | + default: null |
| 54 | + }, |
| 55 | + minLength: { |
| 56 | + type: Number, |
| 57 | + default: 2 |
| 58 | + }, |
| 59 | + limit: { |
| 60 | + type: Number, |
| 61 | + default: 5 |
| 62 | + }, |
| 63 | + language: { |
| 64 | + type: String, |
| 65 | + default: null |
| 66 | + }, |
| 67 | + filter: { |
| 68 | + type: Function, |
| 69 | + default: null |
| 70 | + }, |
| 71 | + localGeocoder: { |
| 72 | + type: Function, |
| 73 | + default: null |
| 74 | + }, |
| 75 | + // Component options |
| 76 | + input: { |
| 77 | + type: String, |
| 78 | + default: null |
18 | 79 | } |
19 | 80 | }, |
20 | 81 |
|
|
24 | 85 | } |
25 | 86 | }, |
26 | 87 |
|
| 88 | + watch: { |
| 89 | + input: { |
| 90 | + handler(next, prev) { |
| 91 | + if (this.control && next !== prev) { |
| 92 | + this.control.setInput(next) |
| 93 | + } |
| 94 | + }, |
| 95 | + immediate: true |
| 96 | + }, |
| 97 | + proximity(next, prev) { |
| 98 | + if (this.control && next !== prev) { |
| 99 | + this.control.setProximity(next) |
| 100 | + } |
| 101 | + } |
| 102 | + }, |
| 103 | +
|
27 | 104 | created() { |
28 | 105 | if (this.accessToken) this.mapbox.accessToken = this.accessToken |
29 | 106 | const Geocoder = this.mapboxGeocoder |
30 | 107 | this.control = new Geocoder(this._props) |
31 | | -
|
32 | | - this.control.on('error', error => { |
33 | | - this.$_emitMapEvent('geocoder-error', { error }) |
34 | | - }) |
35 | | - this.control.on('result', event => { |
36 | | - this.$_emitMapEvent('geocoder-result', { event }) |
37 | | - }) |
38 | 108 | }, |
39 | 109 |
|
40 | 110 | methods: { |
41 | 111 | $_deferredMount(payload) { |
42 | 112 | this.map = payload.map |
43 | | - console.log('MOUNTING!', this.map) |
44 | 113 | this.map.addControl(this.control) |
45 | | - this.$emit('added', this.control) |
| 114 | + if (this.input) { |
| 115 | + this.control.setInput(this.input) |
| 116 | + } |
| 117 | + this.$_emitMapEvent('added', { geocoder: this.control }) |
| 118 | + if (this.$options._parentListeners) { |
| 119 | + const eventNames = Object.keys(geocoderEvents) |
| 120 | + const eventsToListen = Object.keys(this.$options._parentListeners) |
| 121 | + .filter(eventName => |
| 122 | + eventNames.indexOf(eventName) !== -1 |
| 123 | + ) |
| 124 | + this.$_bindSelfEvents(eventsToListen, this.control) |
| 125 | + } |
46 | 126 | payload.component.$off('load', this.$_deferredMount) |
| 127 | + }, |
| 128 | +
|
| 129 | + query(query) { |
| 130 | + if (this.control) { |
| 131 | + this.$emit('update:input', query) |
| 132 | + return this.contol.query(query) |
| 133 | + } |
| 134 | + return null |
47 | 135 | } |
48 | 136 | } |
49 | 137 | } |
|
0 commit comments