Skip to content

Commit 3f0d18b

Browse files
committed
Code cleaned up
1 parent 087b8a1 commit 3f0d18b

File tree

3 files changed

+27
-178
lines changed

3 files changed

+27
-178
lines changed

src/GeocoderControl.js

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { mglControlMixin } from "vue-mapbox";
1+
import MapboxGeocoder from "@mapbox/mapbox-gl-geocoder";
2+
import { $helpers } from "vue-mapbox";
23

34
const geocoderEvents = {
5+
clear: "clear",
46
loading: "loading",
57
results: "results",
68
result: "result",
@@ -9,7 +11,7 @@ const geocoderEvents = {
911

1012
export default {
1113
name: "GeocoderControl",
12-
mixins: [mglControlMixin],
14+
mixins: [$helpers.asControl],
1315

1416
inject: ["mapbox", "map"],
1517

@@ -102,45 +104,44 @@ export default {
102104
},
103105

104106
created() {
105-
if (this.accessToken && !this.mapbox.accessToken)
107+
if (this.accessToken && !this.mapbox.accessToken) {
106108
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);
109+
}
110+
this.control = new MapboxGeocoder(this.$props);
111111
this.control.on("results", this.$_updateInput);
112+
113+
this.$_deferredMount();
112114
},
113115

114116
beforeDestroy() {
115117
this.control.off("results", this.$_updateInput);
116118
},
117119

118120
methods: {
119-
$_deferredMount(payload) {
120-
this.map = payload.map;
121+
$_deferredMount() {
121122
this.map.addControl(this.control);
122123
if (this.input) {
123124
this.control.setInput(this.input);
124125
}
125126
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);
127+
this.$_bindSelfEvents(Object.keys(geocoderEvents));
141128
this.initial = false;
142129
},
143130

131+
$_bindSelfEvents(events) {
132+
const vm = this;
133+
Object.keys(this.$listeners).forEach(eventName => {
134+
if (events.includes(eventName)) {
135+
this.control.on(eventName, eventData => {
136+
return vm.$_emitSelfEvent(
137+
{ type: eventName },
138+
{ geocoderData: eventData }
139+
);
140+
});
141+
}
142+
});
143+
},
144+
144145
$_updateInput(results) {
145146
if (!this.initial) {
146147
const input = results.query ? results.query.join("") : "";

src/MglGeocoderControl.vue

Lines changed: 0 additions & 152 deletions
This file was deleted.

src/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import MglGeocoderControl from './MglGeocoderControl.vue'
1+
import GeocoderControl from "./GeocoderControl";
22

3-
export default MglGeocoderControl
3+
export default GeocoderControl;

0 commit comments

Comments
 (0)