2020-12-16 16:54:41 +00:00
|
|
|
/*
|
2021-07-25 14:12:40 +00:00
|
|
|
* Copyright 2021 James Lyne
|
2020-12-16 16:54:41 +00:00
|
|
|
*
|
|
|
|
* Some portions of this file were taken from https://github.com/webbukkit/dynmap.
|
|
|
|
* These portions are Copyright 2020 Dynmap Contributors.
|
|
|
|
*
|
2021-07-25 14:12:40 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2020-12-16 16:54:41 +00:00
|
|
|
*
|
2021-07-25 14:12:40 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2020-12-16 16:54:41 +00:00
|
|
|
*
|
2021-07-25 14:12:40 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2020-12-16 16:54:41 +00:00
|
|
|
*/
|
|
|
|
|
2021-07-23 21:28:06 +00:00
|
|
|
import LiveAtlasPolyline from "@/leaflet/vector/LiveAtlasPolyline";
|
2022-01-13 14:06:18 +00:00
|
|
|
import {Coordinate, LiveAtlasLineMarker} from "@/index";
|
2021-07-28 00:56:41 +00:00
|
|
|
import {LatLngExpression} from "leaflet";
|
2021-07-29 02:06:05 +00:00
|
|
|
import {createPopup, tooltipOptions} from "@/util/paths";
|
2020-12-11 15:28:51 +00:00
|
|
|
|
2022-01-13 14:06:18 +00:00
|
|
|
export const createLine = (options: LiveAtlasLineMarker, converter: Function): LiveAtlasPolyline => {
|
2021-07-28 01:14:20 +00:00
|
|
|
const points = options.points.map(projectPointsMapCallback, converter),
|
|
|
|
line = new LiveAtlasPolyline(points, options);
|
2020-12-11 15:28:51 +00:00
|
|
|
|
2022-01-13 16:07:27 +00:00
|
|
|
if(options.popup) {
|
2021-07-28 16:49:38 +00:00
|
|
|
line.bindPopup(() => createPopup(options, 'LinePopup'));
|
2020-12-11 15:28:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 16:07:27 +00:00
|
|
|
if (options.tooltip) {
|
2022-01-15 15:50:27 +00:00
|
|
|
line.bindTooltip(() => options.tooltipHTML || options.tooltip, tooltipOptions);
|
2021-07-29 02:06:05 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 15:28:51 +00:00
|
|
|
return line;
|
|
|
|
};
|
|
|
|
|
2022-01-13 14:06:18 +00:00
|
|
|
export const updateLine = (line: LiveAtlasPolyline | undefined, options: LiveAtlasLineMarker, converter: Function): LiveAtlasPolyline => {
|
2020-12-11 15:28:51 +00:00
|
|
|
if (!line) {
|
|
|
|
return createLine(options, converter);
|
|
|
|
}
|
|
|
|
|
2021-01-26 14:42:31 +00:00
|
|
|
line.closePopup();
|
2020-12-11 15:28:51 +00:00
|
|
|
line.unbindPopup();
|
2021-07-28 16:49:38 +00:00
|
|
|
line.bindPopup(() => createPopup(options, 'LinePopup'));
|
2020-12-11 15:28:51 +00:00
|
|
|
line.setStyle(options.style);
|
2021-07-28 01:14:20 +00:00
|
|
|
line.setLatLngs(options.points.map(projectPointsMapCallback, converter));
|
2020-12-11 15:28:51 +00:00
|
|
|
line.redraw();
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2021-07-28 00:56:41 +00:00
|
|
|
const projectPointsMapCallback = function(point: Coordinate): LatLngExpression {
|
|
|
|
if(Array.isArray(point)) {
|
|
|
|
return projectPointsMapCallback(point);
|
|
|
|
} else {
|
|
|
|
// @ts-ignore
|
|
|
|
return this(point);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getLinePoints = (x: number[], y: number[], z: number[]): Coordinate[] => {
|
2020-12-11 15:28:51 +00:00
|
|
|
const points = [];
|
|
|
|
|
2021-07-28 00:56:41 +00:00
|
|
|
for(let i = 0; i < x.length; i++) {
|
|
|
|
points.push({x: x[i], y: y[i], z: z[i]});
|
2020-12-11 15:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return points;
|
|
|
|
};
|