Skip to content

Commit 8a4cab5

Browse files
committed
Added events support
1 parent 744d911 commit 8a4cab5

File tree

2 files changed

+100
-12
lines changed

2 files changed

+100
-12
lines changed

dist/vue-mapbox-geocoder.umd.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/MglGeocoderControl.vue

Lines changed: 99 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,79 @@
33
<script>
44
import { mglBaseMixin, mglControlMixin } from 'vue-mapbox'
55
6+
const geocoderEvents = {
7+
loading: 'loading',
8+
results: 'results',
9+
result: 'result',
10+
error: 'error'
11+
}
12+
613
export default {
714
name: 'GeocoderControl',
815
mixins: [mglBaseMixin, mglControlMixin],
916
1017
props: {
18+
// Mapbox-geocoder options
1119
accessToken: {
1220
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,
1337
default: null
1438
},
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: {
1652
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
1879
}
1980
},
2081
@@ -24,26 +85,53 @@
2485
}
2586
},
2687
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+
27104
created() {
28105
if (this.accessToken) this.mapbox.accessToken = this.accessToken
29106
const Geocoder = this.mapboxGeocoder
30107
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-
})
38108
},
39109
40110
methods: {
41111
$_deferredMount(payload) {
42112
this.map = payload.map
43-
console.log('MOUNTING!', this.map)
44113
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+
}
46126
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
47135
}
48136
}
49137
}

0 commit comments

Comments
 (0)