-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Migrate decorators to signal queries (#178)
* feat: migrate Output ViewChild ContentChild decorators to signal queries * feat: replace @input decorator * fix: code review * refactor: source components * refector: change composition instead of inheritance for the sources
- Loading branch information
Showing
45 changed files
with
2,268 additions
and
2,253 deletions.
There are no files selected for viewing
65 changes: 34 additions & 31 deletions
65
projects/ngx-maplibre-gl/src/lib/control/attribution-control.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,54 @@ | ||
import { AfterContentInit, Directive, Host, Input } from '@angular/core'; | ||
import { AttributionControl } from 'maplibre-gl'; | ||
import { Directive, afterNextRender, inject, input } from '@angular/core'; | ||
import { | ||
AttributionControl, | ||
type AttributionControlOptions, | ||
} from 'maplibre-gl'; | ||
import { MapService } from '../map/map.service'; | ||
import { ControlComponent } from './control.component'; | ||
import { keepAvailableObjectValues } from '../shared/utils/functions/object.fn'; | ||
|
||
/** | ||
* `mglAttribution` - an attribution control directive | ||
* | ||
* | ||
* @category Directives | ||
* | ||
* | ||
* @see [Add custom attribution](https://maplibre.org/ngx-maplibre-gl/demo/custom-attribution) | ||
* @see [AttributionControl](https://maplibre.org/maplibre-gl-js/docs/API/classes/AttributionControl) | ||
*/ | ||
@Directive({ | ||
selector: '[mglAttribution]', | ||
standalone: true, | ||
}) | ||
export class AttributionControlDirective implements AfterContentInit { | ||
export class AttributionControlDirective { | ||
/* Init injection */ | ||
private readonly mapService = inject(MapService); | ||
private readonly controlComponent = inject< | ||
ControlComponent<AttributionControl> | ||
>(ControlComponent, { host: true }); | ||
|
||
/** Init input */ | ||
@Input() compact?: boolean; | ||
readonly compact = input<boolean>(); | ||
/** Init input */ | ||
@Input() customAttribution?: string | string[]; | ||
readonly customAttribution = input<string | string[]>(); | ||
|
||
constructor() { | ||
afterNextRender(() => { | ||
this.mapService.mapCreated$.subscribe(() => { | ||
if (this.controlComponent.control) { | ||
throw new Error('Another control is already set for this control'); | ||
} | ||
|
||
constructor( | ||
private mapService: MapService, | ||
@Host() private controlComponent: ControlComponent<AttributionControl> | ||
) {} | ||
const options = keepAvailableObjectValues<AttributionControlOptions>({ | ||
compact: this.compact(), | ||
customAttribution: this.customAttribution(), | ||
}); | ||
|
||
ngAfterContentInit() { | ||
this.mapService.mapCreated$.subscribe(() => { | ||
if (this.controlComponent.control) { | ||
throw new Error('Another control is already set for this control'); | ||
} | ||
const options: { | ||
compact?: boolean; | ||
customAttribution?: string | string[]; | ||
} = {}; | ||
if (this.compact !== undefined) { | ||
options.compact = this.compact; | ||
} | ||
if (this.customAttribution !== undefined) { | ||
options.customAttribution = this.customAttribution; | ||
} | ||
this.controlComponent.control = new AttributionControl(options); | ||
this.mapService.addControl( | ||
this.controlComponent.control, | ||
this.controlComponent.position | ||
); | ||
this.controlComponent.control = new AttributionControl(options); | ||
this.mapService.addControl( | ||
this.controlComponent.control, | ||
this.controlComponent.position() | ||
); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 31 additions & 30 deletions
61
projects/ngx-maplibre-gl/src/lib/control/fullscreen-control.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,49 @@ | ||
import { | ||
AfterContentInit, | ||
Directive, | ||
Host, | ||
HostListener, | ||
Input, | ||
} from '@angular/core'; | ||
import { Directive, afterNextRender, inject, input } from '@angular/core'; | ||
import { FullscreenControl } from 'maplibre-gl'; | ||
import { MapService } from '../map/map.service'; | ||
import { ControlComponent } from './control.component'; | ||
|
||
/** | ||
* `mglFullscreen` - a fullscreen control directive | ||
* | ||
* | ||
* @category Directives | ||
*/ | ||
@Directive({ | ||
selector: '[mglFullscreen]', | ||
standalone: true, | ||
host: { | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
'(window:webkitfullscreenchange)': 'onFullscreen()', | ||
}, | ||
}) | ||
export class FullscreenControlDirective implements AfterContentInit { | ||
/* Init inputs */ | ||
@Input() container?: HTMLElement; | ||
@HostListener('window:webkitfullscreenchange', ['$event.target']) | ||
onFullscreen() { | ||
this.mapService.mapInstance.resize(); | ||
} | ||
export class FullscreenControlDirective { | ||
/* Init injection */ | ||
private readonly mapService = inject(MapService); | ||
private readonly controlComponent = inject< | ||
ControlComponent<FullscreenControl> | ||
>(ControlComponent, { host: true }); | ||
|
||
constructor( | ||
private mapService: MapService, | ||
@Host() private controlComponent: ControlComponent<FullscreenControl> | ||
) {} | ||
/* Init inputs */ | ||
readonly container = input<HTMLElement>(); | ||
|
||
ngAfterContentInit() { | ||
this.mapService.mapCreated$.subscribe(() => { | ||
if (this.controlComponent.control) { | ||
throw new Error('Another control is already set for this control'); | ||
} | ||
this.controlComponent.control = new FullscreenControl({ | ||
container: this.container, | ||
constructor() { | ||
afterNextRender(() => { | ||
this.mapService.mapCreated$.subscribe(() => { | ||
if (this.controlComponent.control) { | ||
throw new Error('Another control is already set for this control'); | ||
} | ||
this.controlComponent.control = new FullscreenControl({ | ||
container: this.container(), | ||
}); | ||
this.mapService.addControl( | ||
this.controlComponent.control, | ||
this.controlComponent.position() | ||
); | ||
}); | ||
this.mapService.addControl( | ||
this.controlComponent.control, | ||
this.controlComponent.position | ||
); | ||
}); | ||
} | ||
|
||
onFullscreen() { | ||
this.mapService.mapInstance.resize(); | ||
} | ||
} |
92 changes: 48 additions & 44 deletions
92
projects/ngx-maplibre-gl/src/lib/control/geolocate-control.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,73 @@ | ||
import { | ||
AfterContentInit, | ||
Directive, | ||
EventEmitter, | ||
Host, | ||
Input, | ||
Output, | ||
afterNextRender, | ||
inject, | ||
input, | ||
output, | ||
} from '@angular/core'; | ||
import { FitBoundsOptions, GeolocateControl } from 'maplibre-gl'; | ||
import { | ||
type FitBoundsOptions, | ||
GeolocateControl, | ||
GeolocateControlOptions, | ||
} from 'maplibre-gl'; | ||
import { MapService } from '../map/map.service'; | ||
import { ControlComponent } from './control.component'; | ||
import { Position } from '../map/map.types'; | ||
import type { Position } from '../map/map.types'; | ||
import { keepAvailableObjectValues } from '../shared/utils/functions/object.fn'; | ||
|
||
/** | ||
* `mglGeolocate` - a geolocate control directive | ||
* | ||
* | ||
* @category Directives | ||
* | ||
* | ||
* @see [Locate user](https://maplibre.org/ngx-maplibre-gl/demo/locate-user) | ||
* @see [GeolocateControl](https://maplibre.org/maplibre-gl-js/docs/API/classes/GeolocateControl) | ||
*/ | ||
@Directive({ | ||
selector: '[mglGeolocate]', | ||
standalone: true, | ||
}) | ||
export class GeolocateControlDirective implements AfterContentInit { | ||
export class GeolocateControlDirective { | ||
/* Init injection */ | ||
private readonly mapService = inject(MapService); | ||
private readonly controlComponent = inject< | ||
ControlComponent<GeolocateControl> | ||
>(ControlComponent, { host: true }); | ||
|
||
/* Init inputs */ | ||
readonly positionOptions = input<PositionOptions>(); | ||
/* Init inputs */ | ||
readonly fitBoundsOptions = input<FitBoundsOptions>(); | ||
/* Init inputs */ | ||
@Input() positionOptions?: PositionOptions; | ||
@Input() fitBoundsOptions?: FitBoundsOptions; | ||
@Input() trackUserLocation?: boolean; | ||
@Input() showUserLocation?: boolean; | ||
readonly trackUserLocation = input<boolean>(); | ||
/* Init inputs */ | ||
readonly showUserLocation = input<boolean>(); | ||
|
||
@Output() | ||
geolocate: EventEmitter<Position> = new EventEmitter<Position>(); | ||
readonly geolocate = output<Position>(); | ||
|
||
constructor( | ||
private mapService: MapService, | ||
@Host() private controlComponent: ControlComponent<GeolocateControl> | ||
) {} | ||
constructor() { | ||
afterNextRender(() => { | ||
this.mapService.mapCreated$.subscribe(() => { | ||
if (this.controlComponent.control) { | ||
throw new Error('Another control is already set for this control'); | ||
} | ||
|
||
ngAfterContentInit() { | ||
this.mapService.mapCreated$.subscribe(() => { | ||
if (this.controlComponent.control) { | ||
throw new Error('Another control is already set for this control'); | ||
} | ||
const options = { | ||
positionOptions: this.positionOptions, | ||
fitBoundsOptions: this.fitBoundsOptions, | ||
trackUserLocation: this.trackUserLocation, | ||
showUserLocation: this.showUserLocation, | ||
}; | ||
const options = keepAvailableObjectValues<GeolocateControlOptions>({ | ||
positionOptions: this.positionOptions(), | ||
fitBoundsOptions: this.fitBoundsOptions(), | ||
trackUserLocation: this.trackUserLocation(), | ||
showUserLocation: this.showUserLocation(), | ||
}); | ||
|
||
Object.keys(options).forEach((key: string) => { | ||
const tkey = <keyof typeof options>key; | ||
if (options[tkey] === undefined) { | ||
delete options[tkey]; | ||
} | ||
}); | ||
this.controlComponent.control = new GeolocateControl(options); | ||
this.controlComponent.control.on('geolocate', (data: Position) => { | ||
this.geolocate.emit(data); | ||
this.controlComponent.control = new GeolocateControl(options); | ||
this.controlComponent.control.on('geolocate', (data: Position) => { | ||
this.geolocate.emit(data); | ||
}); | ||
this.mapService.addControl( | ||
this.controlComponent.control, | ||
this.controlComponent.position() | ||
); | ||
}); | ||
this.mapService.addControl( | ||
this.controlComponent.control, | ||
this.controlComponent.position | ||
); | ||
}); | ||
} | ||
} |
Oops, something went wrong.