import { AfterContentInit, Directive, EventEmitter, Input, OnDestroy, Output, } from "@angular/core"; import { Feature as GeoJSONFeature, Point as GeoJSONPoint } from "geojson"; import { CircleMarker, CircleMarkerOptions, FillRule, LatLng, LatLngLiteral, LatLngTuple, LeafletEvent, LeafletMouseEvent, LineCapShape, LineJoinShape, Map, PathOptions, PopupEvent, TooltipEvent, } from "leaflet"; import { LayerGroupProvider } from "./layer-group.provider"; import { LayerProvider } from "./layer.provider"; import { lng2lat } from "./lng2lat"; import { MapComponent } from "./map.component"; /** * Angular2 directive for circle-markers of Leaflet. * * *You can use this directive in an Angular2 template after importing `YagaModule`.* * * How to use in a template: * ```html * * * * * ``` * * @link http://leafletjs.com/reference-1.2.0.html#circlemarker Original Leaflet documentation * @link https://leaflet-ng2.yagajs.org/latest/browser-test?grep=Circle-Marker%20Directive Unit-Test * @link https://leaflet-ng2.yagajs.org/latest/coverage/lcov-report/lib/circle-marker.directive.js.html * Test coverage * @link https://leaflet-ng2.yagajs.org/latest/typedoc/classes/circle-marker.directive.js.html API documentation * @example https://leaflet-ng2.yagajs.org/latest/examples/circle-marker-directive/ */ @Directive({ providers: [ LayerProvider ], selector: "yaga-circle-marker", }) export class CircleMarkerDirective extends CircleMarker implements OnDestroy, AfterContentInit { /** * Two-Way bound property for the display status of the geometry. * Use it with `` * or `` */ @Output() public displayChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the stroke state of the geometry. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-stroke Original Leaflet documentation */ @Output() public strokeChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the line-color of the geometry. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-color Original Leaflet documentation */ @Output() public colorChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the weight of the geometry. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-weight Original Leaflet documentation */ @Output() public weightChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the opacity of the geometry. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-opacity Original Leaflet documentation */ @Output() public opacityChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the lineCap of the geometry. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-linecap Original Leaflet documentation */ @Output() public lineCapChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the lineJoin of the geometry. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-linejoin Original Leaflet documentation */ @Output() public lineJoinChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the dashArray of the geometry. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-dasharray Original Leaflet documentation */ @Output() public dashArrayChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the dashOffset of the geometry. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-dashoffset Original Leaflet documentation */ @Output() public dashOffsetChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the fill state of the geometry. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-fill Original Leaflet documentation */ @Output() public fillChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the fill-color of the geometry. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-fillcolor Original Leaflet documentation */ @Output() public fillColorChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the fill-opacity of the geometry. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-fillopacity Original Leaflet documentation */ @Output() public fillOpacityChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the fill-rule of the geometry. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-fillrule Original Leaflet documentation */ @Output() public fillRuleChange: EventEmitter = new EventEmitter(); // @Output() public rendererChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the className of the geometry. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-classname Original Leaflet documentation */ @Output() public classNameChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the css-style of the geometry. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-setstyle Original Leaflet documentation */ @Output() public styleChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the latlng-position of the geometry. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-setlatlng Original Leaflet documentation */ @Output() public positionChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the latitude of the geometry. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-setlatlng Original Leaflet documentation */ @Output() public latChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the longitude of the geometry. * Use it with `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-setlatlng Original Leaflet documentation * or `` */ @Output() public lngChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the radius of the geometry. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-setradius Original Leaflet documentation */ @Output() public radiusChange: EventEmitter = new EventEmitter(); /** * Two-Way bound property for the geometry represented as GeoJSON. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-togeojson Original Leaflet documentation */ @Output() public geoJSONChange: EventEmitter> = new EventEmitter(); /** * From leaflet fired add event. * Use it with `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-add Original Leaflet documentation */ @Output("add") public addEvent: EventEmitter = new EventEmitter(); /** * From leaflet fired remove event. * Use it with `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-remove Original Leaflet documentation */ @Output("remove") public removeEvent: EventEmitter = new EventEmitter(); /** * From leaflet fired popupopen event. * Use it with `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-popupopen Original Leaflet documentation */ @Output("popupopen") public popupopenEvent: EventEmitter = new EventEmitter(); /** * From leaflet fired popupclose event. * Use it with `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-popupclose Original Leaflet documentation */ @Output("popupclose") public popupcloseEvent: EventEmitter = new EventEmitter(); /** * From leaflet fired tooltipopen event. * Use it with `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-tooltipopen Original Leaflet documentation */ @Output("tooltipopen") public tooltipopenEvent: EventEmitter = new EventEmitter(); /** * From leaflet fired tooltipclose event. * Use it with `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-tooltipclose Original Leaflet documentation */ @Output("tooltipclose") public tooltipcloseEvent: EventEmitter = new EventEmitter(); /** * From leaflet fired click event. * Use it with `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-click Original Leaflet documentation */ @Output("click") public clickEvent: EventEmitter = new EventEmitter(); /** * From leaflet fired dblclick event. * Use it with `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-dblclick Original Leaflet documentation */ @Output("dblclick") public dblclickEvent: EventEmitter = new EventEmitter(); /** * From leaflet fired mousedown event. * Use it with `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-mousedown Original Leaflet documentation */ @Output("mousedown") public mousedownEvent: EventEmitter = new EventEmitter(); /** * From leaflet fired mouseover event. * Use it with `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-mouseover Original Leaflet documentation */ @Output("mouseover") public mouseoverEvent: EventEmitter = new EventEmitter(); /** * From leaflet fired mouseout event. * Use it with `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-mouseout Original Leaflet documentation */ @Output("mouseout") public mouseoutEvent: EventEmitter = new EventEmitter(); /** * From leaflet fired contextmenu event. * Use it with `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-contextmenu Original Leaflet documentation */ @Output("contextmenu") public contextmenuEvent: EventEmitter = new EventEmitter(); public feature: GeoJSONFeature; // This is not optional in this class private initialized: boolean = false; constructor( protected layerGroupProvider: LayerGroupProvider, layerProvider: LayerProvider, ) { super([0, 0]); layerProvider.ref = this; this.feature = (this as any).feature || { geometry: {type: "Point", coordinates: []}, properties: {}, type: "Feature", }; this.feature.properties = this.feature.properties || {}; this.on("remove", () => { this.displayChange.emit(false); }); this.on("add", () => { this.displayChange.emit(true); }); this.layerGroupProvider.ref!.addLayer(this); // Events this.on("add", (event: LeafletEvent) => { this.addEvent.emit(event); }); this.on("remove", (event: LeafletEvent) => { this.removeEvent.emit(event); }); this.on("popupopen", (event: LeafletEvent) => { this.popupopenEvent.emit(event as PopupEvent); }); this.on("popupclose", (event: LeafletEvent) => { this.popupcloseEvent.emit(event as PopupEvent); }); this.on("tooltipopen", (event: LeafletEvent) => { this.tooltipopenEvent.emit(event as TooltipEvent); }); this.on("tooltipclose", (event: LeafletEvent) => { this.tooltipcloseEvent.emit(event as TooltipEvent); }); this.on("click", (event: LeafletEvent) => { this.clickEvent.emit(event as LeafletMouseEvent); }); this.on("dblclick", (event: LeafletEvent) => { this.dblclickEvent.emit(event as LeafletMouseEvent); }); this.on("mousedown", (event: LeafletEvent) => { this.mousedownEvent.emit(event as LeafletMouseEvent); }); this.on("mouseover", (event: LeafletEvent) => { this.mouseoverEvent.emit(event as LeafletMouseEvent); }); this.on("mouseout", (event: LeafletEvent) => { this.mouseoutEvent.emit(event as LeafletMouseEvent); }); this.on("contextmenu", (event: LeafletEvent) => { this.contextmenuEvent.emit(event as LeafletMouseEvent); }); } /** * Internal method that provides the initialization of the child popup or tooltip */ public ngAfterContentInit(): void { this.initialized = true; } /** * Internal method to provide the removal of the layer in Leaflet, when removing it from the Angular template */ public ngOnDestroy(): void { this.removeFrom(this.layerGroupProvider.ref as Map); } /** * Derived method of the original setLatLng. * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-setlatlng Original Leaflet documentation */ public setLatLng(val: LatLng | LatLngTuple | LatLngLiteral): this { super.setLatLng((val as any)); if (!this.initialized) { return this; } this.positionChange.emit((this as any)._latlng); this.latChange.emit((this as any)._latlng.lat); this.lngChange.emit((this as any)._latlng.lng); this.geoJSONChange.emit(this.geoJSON); return this; } /** * Two-Way bound property for the position of the circle. * Use it with `` or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-l-circlemarker Original Leaflet documentation */ @Input() public set position(val: LatLng | LatLngTuple | LatLngLiteral) { this.setLatLng(val); } public get position(): LatLng | LatLngTuple | LatLngLiteral { // it is always a LatLng! return (this as any)._latlng; } /** * Two-Way bound property for the latitude (position) of the circle. * Use it with `` or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-l-circlemarker Original Leaflet documentation */ @Input() public set lat(val: number) { this.setLatLng([val, this.lng]); } public get lat(): number { return (this as any)._latlng.lat; } /** * Two-Way bound property for the longitude (position) of the circle. * Use it with `` or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-l-circlemarker Original Leaflet documentation */ @Input() public set lng(val: number) { this.setLatLng([this.lat, val]); } public get lng(): number { return (this as any)._latlng.lng; } /** * Derived method of the original setRadius. * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-setradius Original Leaflet documentation */ public setRadius(val: number): this { super.setRadius(val); this.radiusChange.emit(val); return this; } /** * Two-Way bound property for the radius of the circle. * Use it with `` or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-radius Original Leaflet documentation */ @Input() public set radius(val: number) { this.setRadius(val); } public get radius(): number { return this.getRadius(); } /** * Two-Way bound property for the geoJSON data. * Use it with `` or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-togeojson Original Leaflet documentation */ @Input() public set geoJSON(val: GeoJSONFeature) { this.feature.properties = val.properties; const geomType: any = val.geometry.type; // Normally "Point" /* istanbul ignore if */ if (geomType !== "Point") { throw new Error("Unsupported geometry type: " + geomType ); } this.setLatLng(lng2lat(val.geometry.coordinates) as any); } public get geoJSON(): GeoJSONFeature { return (this.toGeoJSON() as GeoJSONFeature); } /** * Derived method of the original setStyle. * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-setstyle Original Leaflet documentation */ public setStyle(style: PathOptions): this { super.setStyle(style); if (style.hasOwnProperty("stroke")) { this.strokeChange.emit(style.stroke); } if (style.hasOwnProperty("color")) { this.colorChange.emit(style.color); } if (style.hasOwnProperty("weight")) { this.weightChange.emit(style.weight); } if (style.hasOwnProperty("opacity")) { this.opacityChange.emit(style.opacity); } if (style.hasOwnProperty("lineCap")) { this.lineCapChange.emit(style.lineCap); } if (style.hasOwnProperty("lineJoin")) { this.lineJoinChange.emit(style.lineJoin); } if (style.hasOwnProperty("dashArray")) { this.dashArrayChange.emit(style.dashArray); } if (style.hasOwnProperty("dashOffset")) { this.dashOffsetChange.emit(style.dashOffset); } if (style.hasOwnProperty("fill")) { this.fillChange.emit(style.fill); } if (style.hasOwnProperty("fillColor")) { this.fillColorChange.emit(style.fillColor); } if (style.hasOwnProperty("fillOpacity")) { this.fillOpacityChange.emit(style.fillOpacity); } if (style.hasOwnProperty("fillRule")) { this.fillRuleChange.emit(style.fillRule); } if (style.hasOwnProperty("className")) { this.classNameChange.emit(style.className); } this.styleChange.emit(style); return this; } /** * Two-Way bound property for the opacity. * Use it with `` or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-opacity Original Leaflet documentation */ @Input() public set opacity(val: number | undefined) { this.setStyle({opacity: val}); } public get opacity(): number | undefined { return this.options.opacity; } /** * Two-Way bound property for the stroke. * Use it with `` or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-stroke Original Leaflet documentation */ @Input() public set stroke(val: boolean) { this.setStyle({stroke: val}); } public get stroke(): boolean { if (this.options.hasOwnProperty("stroke") && this.options.stroke !== undefined) { return this.options.stroke; } return true; } /** * Two-Way bound property for the color. * Use it with `` or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-color Original Leaflet documentation */ @Input() public set color(val: string | undefined) { this.setStyle({color: val}); } public get color(): string | undefined { return this.options.color; } /** * Two-Way bound property for the weight. * Use it with `` or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-weight Original Leaflet documentation */ @Input() public set weight(val: number | undefined) { this.setStyle({weight: val}); } public get weight(): number | undefined { return this.options.weight; } /** * Two-Way bound property for the lineCap. * Use it with `` or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-linecap Original Leaflet documentation */ @Input() public set lineCap(val: LineCapShape | undefined) { this.setStyle({lineCap: val}); } public get lineCap(): LineCapShape | undefined { return this.options.lineCap; } /** * Two-Way bound property for the lineJoin. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-linejoin Original Leaflet documentation */ @Input() public set lineJoin(val: LineJoinShape | undefined) { this.setStyle({lineJoin: val}); } public get lineJoin(): LineJoinShape | undefined { return this.options.lineJoin; } /** * Two-Way bound property for the dashArray. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-dasharray Original Leaflet documentation */ @Input() public set dashArray(val: string | undefined) { this.setStyle({dashArray: val}); } public get dashArray(): string | undefined { return this.options.dashArray; } /** * Two-Way bound property for the dashOffset. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-dashoffset Original Leaflet documentation */ @Input() public set dashOffset(val: string | undefined) { this.setStyle({dashOffset: val}); } public get dashOffset(): string | undefined { return this.options.dashOffset; } /** * Two-Way bound property for the fill. * Use it with `` or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-fill Original Leaflet documentation */ @Input() public set fill(val: boolean) { this.setStyle({fill: val}); } public get fill(): boolean { return this.options.fill || false; } /** * Two-Way bound property for the fillColor. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-fillcolor Original Leaflet documentation */ @Input() public set fillColor(val: string | undefined) { this.setStyle({fillColor: val}); } public get fillColor(): string | undefined { return this.options.fillColor; } /** * Two-Way bound property for the fillOpacity. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-fillopacity Original Leaflet documentation */ @Input() public set fillOpacity(val: number | undefined) { this.setStyle({fillOpacity: val}); } public get fillOpacity(): number | undefined { return this.options.fillOpacity; } /** * Two-Way bound property for the fillRule. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-fillrule Original Leaflet documentation */ @Input() public set fillRule(val: FillRule | undefined) { this.setStyle({fillRule: val}); } public get fillRule(): FillRule | undefined { return this.options.fillRule; } /** * Two-Way bound property for the className. * Use it with `` * or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-classname Original Leaflet documentation */ @Input() public set className(val: string | undefined) { this.setStyle({className: val}); } public get className(): string | undefined { return this.options.className; } /** * Two-Way bound property for the opacity. * Use it with `` or `` * @link http://leafletjs.com/reference-1.2.0.html#circlemarker-style Original Leaflet documentation */ @Input() public set style(val: CircleMarkerOptions) { this.setStyle(val); } public get style(): CircleMarkerOptions { return this.options; } /** * Two-Way bound property for the display state. * Use it with `` or `` */ @Input() public set display(val: boolean) { const isDisplayed: boolean = this.display; if (isDisplayed === val) { return; } let container: HTMLElement; try { container = this.getElement() as HTMLElement; } catch (err) { /* istanbul ignore next */ return; } this.displayChange.emit(val); container.style.display = val ? "" : "none"; } public get display(): boolean { let container: HTMLElement; try { container = this.getElement() as HTMLElement; } catch (err) { /* istanbul ignore next */ return false; } return container.style.display !== "none" && !!container.parentElement; } /** * Input for the GeoJSON properties. * Use it with `` * @link http://leafletjs.com/reference-1.2.0.html#tilelayer-interactive Original Leaflet documentation */ @Input() public set interactive(val: boolean) { const map: MapComponent = ((this as any)._map as MapComponent); this.options.interactive = val; this.onRemove(map); this.onAdd(map); } public get interactive(): boolean { return !!this.options.interactive; } @Input() public set properties(val: T) { this.feature.properties = val; this.geoJSONChange.emit(this.geoJSON); } public get properties(): T { return (this.feature.properties as T); } }