-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.module.ts
55 lines (53 loc) · 2.37 KB
/
map.module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { CommonModule } from '@angular/common';
import { Inject, ModuleWithProviders, NgModule, Optional, Type } from '@angular/core';
import { LeafletModule } from '@asymmetrik/ngx-leaflet';
import { MapComponent } from './map.component';
import {
ZOOM_LEVEL_TOKEN,
ZoomLevelConfiguration,
MARKER_TYPE_TOKEN,
MarkerTypeConfiguration,
GEOMETRY_POLYGON_TOKEN,
GeometryPolygonConfiguration,
INITIAL_POSITION_TOKEN,
InitialPositionConfiguration
} from '../../configurations';
import { GEO_JSON_TOKEN, GeoJsonRepository } from '../repositories';
@NgModule({
declarations: [MapComponent],
exports: [MapComponent],
imports: [CommonModule, LeafletModule]
})
export class MapModule {
public static forRoot(
geometryPolygonConfiguration: GeometryPolygonConfiguration,
zoomLevelConfiguration: ZoomLevelConfiguration,
initialPositionConfiguration: InitialPositionConfiguration,
markerTypeConfiguration: MarkerTypeConfiguration,
geoJsonRepository: Type<GeoJsonRepository>
): ModuleWithProviders<MapModule> {
return {
ngModule: MapModule,
providers: [
{ provide: GEO_JSON_TOKEN, useClass: geoJsonRepository },
{ provide: GEOMETRY_POLYGON_TOKEN, useValue: geometryPolygonConfiguration },
{ provide: MARKER_TYPE_TOKEN, useValue: markerTypeConfiguration },
{ provide: ZOOM_LEVEL_TOKEN, useValue: zoomLevelConfiguration },
{ provide: INITIAL_POSITION_TOKEN, useValue: initialPositionConfiguration }
]
};
}
public constructor(
@Optional() @Inject(GEOMETRY_POLYGON_TOKEN) private readonly geometryPolygonConfiguration: GeometryPolygonConfiguration,
@Optional() @Inject(MARKER_TYPE_TOKEN) private readonly markerType: MarkerTypeConfiguration,
@Optional() @Inject(ZOOM_LEVEL_TOKEN) private readonly zoomLevel: ZoomLevelConfiguration,
@Optional() @Inject(INITIAL_POSITION_TOKEN) private readonly initialPosition: InitialPositionConfiguration,
@Optional() @Inject(GEO_JSON_TOKEN) private readonly geoJsonRepository: GeoJsonRepository
) {
if ([geometryPolygonConfiguration, markerType, zoomLevel, initialPosition, geoJsonRepository].includes(null)) {
throw new Error(
'Cannot import `MapModule` without calling `forRoot` with valid parameters: you must provide defined `geometryPolygonConfiguration`, `markerType`, `zoomLevel`, `initialPosition` and `geoJsonRepository`.'
);
}
}
}