facilmap/leaflet/src/lines/route-layer.ts

104 wiersze
2.6 KiB
TypeScript
Czysty Zwykły widok Historia

2021-03-04 15:45:34 +00:00
import Client from "facilmap-client";
2021-02-13 21:56:10 +00:00
import { Map, PathOptions, PolylineOptions } from "leaflet";
import { HighlightableLayerOptions, HighlightablePolyline } from "leaflet-highlightable-layers";
import { trackPointsToLatLngArray } from "../utils/leaflet";
import DraggableLines from "leaflet-draggable-lines";
interface RouteLayerOptions extends PolylineOptions {
}
export default class RouteLayer extends HighlightablePolyline {
realOptions!: RouteLayerOptions;
2021-03-04 15:45:34 +00:00
client: Client;
2021-02-13 21:56:10 +00:00
draggable?: DraggableLines;
2021-03-04 15:45:34 +00:00
constructor(client: Client, options?: RouteLayerOptions) {
2021-02-13 21:56:10 +00:00
super([], options);
this.client = client;
}
2021-02-28 22:17:26 +00:00
onAdd(map: Map): this {
2021-02-13 21:56:10 +00:00
super.onAdd(map);
this.draggable = new DraggableLines(map, { enableForLayer: false });
this.draggable.enable();
this.draggable.on("dragend remove insert", this.handleDrag);
this.updateDraggableStyle();
this.client.on("route", this.handleRoute);
this.client.on("routePoints", this.handleRoutePoints);
this.updateLine(true);
return this;
}
2021-02-28 22:17:26 +00:00
onRemove(map: Map): this {
2021-02-13 21:56:10 +00:00
super.onRemove(map);
this.client.removeListener("route", this.handleRoute);
this.client.removeListener("routePoints", this.handleRoutePoints);
this.draggable!.off("dragend remove insert", this.handleDrag);
this.draggable!.disableForLayer(this);
this.draggable!.disable();
return this;
}
2021-02-28 22:17:26 +00:00
handleDrag = (): void => {
2021-02-13 21:56:10 +00:00
this.updateRoute();
};
2021-02-28 22:17:26 +00:00
handleRoute = (): void => {
2021-02-13 21:56:10 +00:00
this.updateLine(true);
};
2021-02-28 22:17:26 +00:00
handleRoutePoints = (): void => {
2021-02-13 21:56:10 +00:00
this.updateLine(false);
};
2021-02-28 22:17:26 +00:00
updateRoute(): void {
2021-02-13 21:56:10 +00:00
if (this.client.route) {
this.client.setRoute({
...this.client.route,
routePoints: this.getDraggableLinesRoutePoints()!.map((p) => ({ lat: p.lat, lon: p.lng }))
});
}
}
2021-02-28 22:17:26 +00:00
updateLine(updateRoutePoints: boolean): void {
2021-02-13 21:56:10 +00:00
if (this.client.route) {
if (updateRoutePoints)
this.setDraggableLinesRoutePoints(this.client.route.routePoints.map((p) => [p.lat, p.lon]));
const trackPoints = trackPointsToLatLngArray(this.client.route.trackPoints);
this.setLatLngs(trackPoints);
this.draggable!.enableForLayer(this);
} else {
this.setLatLngs([]);
this.draggable!.disableForLayer(this);
}
}
2021-02-28 22:17:26 +00:00
updateDraggableStyle(): void {
2021-02-13 21:56:10 +00:00
if (this.draggable) {
Object.assign(this.draggable.options, {
dragMarkerOptions: () => ({ pane: "fm-raised-marker" }),
tempMarkerOptions: () => ({ pane: "fm-raised-marker" }),
plusTempMarkerOptions: () => ({ pane: "fm-raised-marker" })
});
this.draggable.redraw();
}
}
2021-02-28 22:17:26 +00:00
setStyle(style: HighlightableLayerOptions<PathOptions>): this {
2021-02-13 21:56:10 +00:00
super.setStyle(style);
this.updateDraggableStyle();
return this;
}
}