Files
FarmMapsLib/projects/common-map/src/fm-map/components/if-zoom-to-show/if-zoom-to-show.directive.ts
Willem Dantuma fa146f2c6e
All checks were successful
FarmMaps.Develop/FarmMapsLib/pipeline/head This commit looks good
Refactor fmMapIfZoomToShow
2021-03-20 18:26:01 +01:00

59 lines
1.7 KiB
TypeScript

import { Directive, ViewContainerRef,TemplateRef,OnInit,Input, OnChanges } from '@angular/core';
import { layer } from 'ol';
import { MapComponent } from 'ngx-openlayers';
@Directive({
selector: '[fmMapIfZoomToShow]',
})
export class ifZoomToShowDirective implements OnInit {
@Input()
set fmMapIfZoomToShow(layer:layer) {
this.layer$=layer;
this.checkZoom();
}
@Input()
set fmMapIfZoomToShowThen(templateRef: TemplateRef<any>) {
this.thenTemplate$ = templateRef;
this.checkZoom();
}
@Input()
set fmMapIfZoomToShowElse(templateRef: TemplateRef<any>) {
this.elseTemplate$ = templateRef;
this.checkZoom();
}
private layer$:layer;
private thenTemplate$:TemplateRef<any>;
private elseTemplate$:TemplateRef<any>;
private showView = false;
constructor(private templateRef$: TemplateRef<any>,private viewContainerRef$: ViewContainerRef,private map$: MapComponent) {
this.thenTemplate$ = templateRef$;
this.checkZoom();
}
ngOnInit() {
this.map$.instance.on('moveend', (e) => {
this.checkZoom();
});
}
checkZoom() {
if(this.layer$ && this.map$.instance) {
let minZoom = this.layer$.getMinZoom();
let currentZoom = this.map$.instance.getView().getZoom();
let view = currentZoom < minZoom;
if(view!= this.showView) {
this.viewContainerRef$.clear();
this.showView=view;
if (this.showView && this.thenTemplate$ ) {
this.viewContainerRef$.createEmbeddedView(this.templateRef$);
} else if(this.elseTemplate$ ) {
this.viewContainerRef$.createEmbeddedView(this.elseTemplate$);
}
}
}
}
}