1- import { mglControlMixin } from "vue-mapbox" ;
1+ import MapboxGeocoder from "@mapbox/mapbox-gl-geocoder" ;
2+ import { $helpers } from "vue-mapbox" ;
23
34const geocoderEvents = {
5+ clear : "clear" ,
46 loading : "loading" ,
57 results : "results" ,
68 result : "result" ,
@@ -9,7 +11,7 @@ const geocoderEvents = {
911
1012export 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 ( "" ) : "" ;
0 commit comments