@@ -33,6 +33,7 @@ export class PopupComponent implements OnChanges, OnDestroy, AfterViewInit, OnIn
33
33
@Input ( ) marker ?: MarkerComponent ;
34
34
35
35
@Output ( ) close = new EventEmitter < void > ( ) ;
36
+ @Output ( ) open = new EventEmitter < void > ( ) ;
36
37
37
38
@ViewChild ( 'content' ) content : ElementRef ;
38
39
@@ -50,54 +51,62 @@ export class PopupComponent implements OnChanges, OnDestroy, AfterViewInit, OnIn
50
51
51
52
ngOnChanges ( changes : SimpleChanges ) {
52
53
if ( changes . lngLat && ! changes . lngLat . isFirstChange ( ) ) {
53
- this . MapService . removePopup ( this . popupInstance ! ) ;
54
+ this . MapService . removePopupFromMap ( this . popupInstance ! ) ;
54
55
const popupInstanceTmp = this . createPopup ( ) ;
55
- this . MapService . addPopup ( popupInstanceTmp ) ;
56
+ this . MapService . addPopupToMap ( popupInstanceTmp , changes . lngLat . currentValue ) ;
56
57
this . popupInstance = popupInstanceTmp ;
57
58
}
58
59
if ( changes . marker && ! changes . marker . isFirstChange ( ) ) {
59
60
const previousMarker : MarkerComponent = changes . marker . previousValue ;
60
61
if ( previousMarker . markerInstance ) {
61
- previousMarker . markerInstance . setPopup ( undefined ) ;
62
+ this . MapService . removePopupFromMarker ( previousMarker . markerInstance ) ;
62
63
}
63
- if ( this . marker && this . marker . markerInstance ) {
64
- this . marker . markerInstance . setPopup ( this . popupInstance ) ;
64
+ if ( this . marker && this . marker . markerInstance && this . popupInstance ) {
65
+ this . MapService . addPopupToMarker ( this . marker . markerInstance , this . popupInstance ) ;
65
66
}
66
67
}
67
68
}
68
69
69
70
ngAfterViewInit ( ) {
70
71
this . popupInstance = this . createPopup ( ) ;
72
+ this . addPopup ( this . popupInstance ) ;
71
73
}
72
74
73
75
ngOnDestroy ( ) {
74
- this . MapService . removePopup ( this . popupInstance ! ) ;
76
+ if ( this . popupInstance ) {
77
+ if ( this . lngLat ) {
78
+ this . MapService . removePopupFromMap ( this . popupInstance ) ;
79
+ } else if ( this . marker && this . marker . markerInstance ) {
80
+ this . MapService . removePopupFromMarker ( this . marker . markerInstance ) ;
81
+ }
82
+ }
75
83
this . popupInstance = undefined ;
76
84
}
77
85
78
86
private createPopup ( ) {
79
- const options = {
80
- closeButton : this . closeButton ,
81
- closeOnClick : this . closeOnClick ,
82
- anchor : this . anchor ,
83
- offset : this . offset
84
- } ;
85
- Object . keys ( options )
86
- . forEach ( ( key ) =>
87
- ( < any > options ) [ key ] === undefined && delete ( < any > options ) [ key ] ) ;
88
- const popupInstance = new Popup ( options ) ;
89
- popupInstance . once ( 'close' , ( ) => {
90
- this . close . emit ( ) ;
91
- } ) ;
92
- popupInstance . setDOMContent ( this . content . nativeElement ) ;
87
+ return this . MapService . createPopup ( {
88
+ popupOptions : {
89
+ closeButton : this . closeButton ,
90
+ closeOnClick : this . closeOnClick ,
91
+ anchor : this . anchor ,
92
+ offset : this . offset
93
+ } ,
94
+ popupEvents : {
95
+ open : this . open ,
96
+ close : this . close
97
+ }
98
+ } , this . content . nativeElement ) ;
99
+ }
100
+
101
+ private addPopup ( popup : Popup ) {
93
102
this . MapService . mapCreated$ . subscribe ( ( ) => {
94
103
if ( this . lngLat ) {
95
- popupInstance . setLngLat ( this . lngLat ) ;
96
- this . MapService . addPopup ( popupInstance ) ;
104
+ this . MapService . addPopupToMap ( popup , this . lngLat ) ;
97
105
} else if ( this . marker && this . marker . markerInstance ) {
98
- this . marker . markerInstance . setPopup ( popupInstance ) ;
106
+ this . MapService . addPopupToMarker ( this . marker . markerInstance , popup ) ;
107
+ } else {
108
+ throw new Error ( 'mgl-popup need either lngLat or marker to be set' ) ;
99
109
}
100
110
} ) ;
101
- return popupInstance ;
102
111
}
103
112
}
0 commit comments