diff --git a/src/demo-app/app/app.component.html b/src/demo-app/app/app.component.html index 005d987f1b..f9d59c7c95 100644 --- a/src/demo-app/app/app.component.html +++ b/src/demo-app/app/app.component.html @@ -86,6 +86,7 @@ igoOverlay igoMapContext igoLayerContext + igoMapBrowserBinding [map]="map"> diff --git a/src/lib/map/map-browser/index.ts b/src/lib/map/map-browser/index.ts index 23b9fdcaad..0fc04af099 100644 --- a/src/lib/map/map-browser/index.ts +++ b/src/lib/map/map-browser/index.ts @@ -1 +1,2 @@ export * from './map-browser.component'; +export * from './map-browser-binding.directive'; diff --git a/src/lib/map/map-browser/map-browser-binding.directive.ts b/src/lib/map/map-browser/map-browser-binding.directive.ts new file mode 100644 index 0000000000..8fdc5e89b0 --- /dev/null +++ b/src/lib/map/map-browser/map-browser-binding.directive.ts @@ -0,0 +1,31 @@ +import { Directive, Self, OnInit, OnDestroy } from '@angular/core'; + +import { MapService } from '../shared'; +import { MapBrowserComponent } from './map-browser.component'; + + +@Directive({ + selector: '[igoMapBrowserBinding]' +}) +export class MapBrowserBindingDirective implements OnInit, OnDestroy { + + private component: MapBrowserComponent; + + constructor(@Self() component: MapBrowserComponent, + private mapService: MapService) { + this.component = component; + } + + ngOnInit() { + if (this.mapService.getMap() !== undefined) { + throw new Error('No more than one map be binded to the map service.'); + } + + this.mapService.setMap(this.component.map); + } + + ngOnDestroy() { + this.mapService.setMap(undefined); + } + +} diff --git a/src/lib/map/map-browser/map-browser-binding.spec.ts b/src/lib/map/map-browser/map-browser-binding.spec.ts new file mode 100644 index 0000000000..79aa131ecf --- /dev/null +++ b/src/lib/map/map-browser/map-browser-binding.spec.ts @@ -0,0 +1,14 @@ +import { TestBed } from '@angular/core/testing'; + +describe('MapBrowserBindingDirective', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [], + providers: [] + }); + }); + + it('should create an instance', () => { + expect(true).toBeTruthy(); + }); +}); diff --git a/src/lib/map/map-browser/map-browser.component.spec.ts b/src/lib/map/map-browser/map-browser.component.spec.ts index 1b1dd9bcb4..6f758636d5 100644 --- a/src/lib/map/map-browser/map-browser.component.spec.ts +++ b/src/lib/map/map-browser/map-browser.component.spec.ts @@ -1,6 +1,6 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; -import { IgoMap, MapService } from '../shared'; +import { IgoMap } from '../shared'; import { MapBrowserComponent } from './map-browser.component'; @@ -13,9 +13,6 @@ describe('MapBrowserComponent', () => { imports: [], declarations: [ MapBrowserComponent - ], - providers: [ - MapService ] }) .compileComponents(); diff --git a/src/lib/map/map-browser/map-browser.component.ts b/src/lib/map/map-browser/map-browser.component.ts index aa7f7f0635..dbd9bc27ec 100644 --- a/src/lib/map/map-browser/map-browser.component.ts +++ b/src/lib/map/map-browser/map-browser.component.ts @@ -1,6 +1,6 @@ import { Component, Input, AfterViewInit } from '@angular/core'; -import { IgoMap, MapViewOptions, MapService } from '../shared'; +import { IgoMap, MapViewOptions } from '../shared'; @Component({ selector: 'igo-map-browser', @@ -26,10 +26,9 @@ export class MapBrowserComponent implements AfterViewInit { public id: string = 'igo-map-target'; - constructor(private mapService: MapService) {} + constructor() {} ngAfterViewInit(): any { this.map.olMap.setTarget(this.id); - this.mapService.setMap(this.map); } } diff --git a/src/lib/map/module.ts b/src/lib/map/module.ts index 74dd484722..d7285f04aa 100644 --- a/src/lib/map/module.ts +++ b/src/lib/map/module.ts @@ -3,7 +3,7 @@ import { NgModule, ModuleWithProviders } from '@angular/core'; import { IgoSharedModule } from '../shared'; import { MapService } from './shared'; -import { MapBrowserComponent } from './map-browser'; +import { MapBrowserComponent, MapBrowserBindingDirective } from './map-browser'; import { ZoomComponent } from './zoom'; @@ -13,10 +13,12 @@ import { ZoomComponent } from './zoom'; ], exports: [ MapBrowserComponent, + MapBrowserBindingDirective, ZoomComponent ], declarations: [ MapBrowserComponent, + MapBrowserBindingDirective, ZoomComponent ] })