@@ -16,21 +16,17 @@ export class MaptilerProjectionControl implements IControl {
1616 projectionButton ! : HTMLButtonElement ;
1717 private externalProjection ?: HTMLElement ;
1818 private options : MaptilerProjectionControlOptions ;
19- private boundToggleProjection : ( e : Event ) => void ;
2019
2120 constructor ( options : MaptilerProjectionControlOptions = { } ) {
2221 this . options = options ;
2322 this . externalProjection = options . projectionElement ;
24- // Bind the method once in constructor to maintain reference
25- this . boundToggleProjection = this . toggleProjection . bind ( this ) ;
26- if ( options . removeDefaultDOM ) {
27- this . setupExternalElements ( ) ;
28- }
2923 }
3024
3125 onAdd ( map : SDKMap ) : HTMLElement {
3226 this . map = map ;
27+
3328 if ( this . options . removeDefaultDOM ) {
29+ this . setupExternalElements ( ) ;
3430 this . container = DOMcreate ( "div" ) ;
3531 return this . container ;
3632 }
@@ -47,41 +43,39 @@ export class MaptilerProjectionControl implements IControl {
4743 this . projectionButton ,
4844 ) . setAttribute ( "aria-hidden" , "true" ) ;
4945 this . projectionButton . type = "button" ;
50- this . projectionButton . addEventListener ( "click" , this . boundToggleProjection ) ;
46+ this . projectionButton . addEventListener ( "click" , this . toggleProjection ) ;
5147
5248 map . on ( "projectiontransition" , this . updateProjectionIcon . bind ( this ) ) ;
5349
5450 this . updateProjectionIcon ( ) ;
51+
5552 return this . container ;
5653 }
5754
5855 onRemove ( ) : void {
5956 if ( this . externalProjection ) {
6057 this . externalProjection . removeEventListener (
6158 "click" ,
62- this . boundToggleProjection ,
59+ this . toggleProjection ,
6360 ) ;
6461 }
6562 DOMRemove ( this . container ) ;
66- this . map . off ( "projectiontransition" , this . updateProjectionIcon ) ;
63+ this . map . off ( "projectiontransition" , this . updateProjectionIcon . bind ( this ) ) ;
6764 // @ts -expect-error: map will only be undefined on remove
6865 this . map = undefined ;
6966 }
7067
7168 private setupExternalElements ( ) : void {
7269 if ( this . externalProjection ) {
73- this . externalProjection . addEventListener (
74- "click" ,
75- this . boundToggleProjection ,
76- ) ;
70+ this . externalProjection . addEventListener ( "click" , this . toggleProjection ) ;
7771 // Set initial title
7872 this . updateExternalTitle ( ) ;
7973 // Listen for projection changes
8074 this . map . on ( "projectiontransition" , this . updateExternalTitle . bind ( this ) ) ;
8175 }
8276 }
8377
84- private toggleProjection ( ) : void {
78+ private toggleProjection = ( ) => {
8579 if ( this . map . isGlobeProjection ( ) ) {
8680 this . map . enableMercatorProjection ( ) ;
8781 } else {
@@ -90,7 +84,7 @@ export class MaptilerProjectionControl implements IControl {
9084 if ( ! this . options . removeDefaultDOM ) {
9185 this . updateProjectionIcon ( ) ;
9286 }
93- }
87+ } ;
9488
9589 private updateExternalTitle ( ) : void {
9690 if ( this . externalProjection ) {
0 commit comments