Skip to content

Commit 087b8a1

Browse files
committed
VueMapbox 0.2 support
1 parent a25a8e6 commit 087b8a1

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

src/GeocoderControl.js

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import { mglControlMixin } from "vue-mapbox";
2+
3+
const geocoderEvents = {
4+
loading: "loading",
5+
results: "results",
6+
result: "result",
7+
error: "error"
8+
};
9+
10+
export default {
11+
name: "GeocoderControl",
12+
mixins: [mglControlMixin],
13+
14+
inject: ["mapbox", "map"],
15+
16+
props: {
17+
// Mapbox-geocoder options
18+
accessToken: {
19+
type: String,
20+
required: true
21+
},
22+
zoom: {
23+
type: Number,
24+
default: 16
25+
},
26+
flyTo: {
27+
type: Boolean,
28+
default: true
29+
},
30+
placeholder: {
31+
type: String,
32+
default: "Search"
33+
},
34+
proximity: {
35+
type: Object,
36+
default: null
37+
},
38+
trackProximity: {
39+
type: Boolean,
40+
default: false
41+
},
42+
bbox: {
43+
type: Array,
44+
default: null
45+
},
46+
types: {
47+
type: String,
48+
default: null
49+
},
50+
country: {
51+
type: String,
52+
default: null
53+
},
54+
minLength: {
55+
type: Number,
56+
default: 2
57+
},
58+
limit: {
59+
type: Number,
60+
default: 5
61+
},
62+
language: {
63+
type: String,
64+
default: null
65+
},
66+
filter: {
67+
type: Function,
68+
default: null
69+
},
70+
localGeocoder: {
71+
type: Function,
72+
default: null
73+
},
74+
// Component options
75+
input: {
76+
type: String,
77+
default: null
78+
}
79+
},
80+
81+
data() {
82+
return {
83+
control: null,
84+
initial: true
85+
};
86+
},
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+
104+
created() {
105+
if (this.accessToken && !this.mapbox.accessToken)
106+
this.mapbox.accessToken = this.accessToken;
107+
const Geocoder = this.mapboxGeocoder;
108+
console.log(this.$props);
109+
this.control = new Geocoder(this.$props);
110+
console.log("CONTROL: ", this.control);
111+
this.control.on("results", this.$_updateInput);
112+
},
113+
114+
beforeDestroy() {
115+
this.control.off("results", this.$_updateInput);
116+
},
117+
118+
methods: {
119+
$_deferredMount(payload) {
120+
this.map = payload.map;
121+
this.map.addControl(this.control);
122+
if (this.input) {
123+
this.control.setInput(this.input);
124+
}
125+
this.$_emitEvent("added", { geocoder: this.control });
126+
// if (this.$options._parentListeners) {
127+
// const eventNames = Object.keys(geocoderEvents)
128+
// const eventsToListen = Object.keys(this.$options._parentListeners)
129+
// .filter(eventName =>
130+
// eventNames.indexOf(eventName) !== -1
131+
// )
132+
// this.$_bindSelfEvents(eventsToListen, this.control)
133+
// }
134+
// Object.keys(this.$listeners).forEach(eventName => {
135+
// if (geocoderEvents.includes(eventName)) {
136+
// this.map.on(eventName, this.layerId, this.$_emitLayerMapEvent);
137+
// }
138+
// });
139+
this.$_bindSelfEvents(geocoderEvents, this.control);
140+
payload.component.$off("load", this.$_deferredMount);
141+
this.initial = false;
142+
},
143+
144+
$_updateInput(results) {
145+
if (!this.initial) {
146+
const input = results.query ? results.query.join("") : "";
147+
this.$emit("update:input", input);
148+
}
149+
},
150+
151+
query(query) {
152+
if (this.control) {
153+
this.$emit("update:input", query);
154+
return this.contol.query(query);
155+
}
156+
return null;
157+
}
158+
}
159+
};

0 commit comments

Comments
 (0)