2020-04-17 21:47:12 +02:00
|
|
|
const queryString = window.location.search;
|
|
|
|
const urlParams = new URLSearchParams(queryString);
|
2020-04-19 09:12:17 +02:00
|
|
|
editor_mode = urlParams.has('editor');
|
2020-04-19 20:54:57 +02:00
|
|
|
|
2020-04-19 09:12:17 +02:00
|
|
|
if (editor_mode) {
|
2020-04-20 18:51:10 +02:00
|
|
|
var editor_mode_polygon = urlParams.has('polygon');
|
|
|
|
|
2020-04-17 21:47:12 +02:00
|
|
|
var draw_layer;
|
|
|
|
var polyline;
|
|
|
|
|
|
|
|
var edit_active = 0;
|
2020-04-25 18:08:02 +02:00
|
|
|
|
|
|
|
var editor_style = {
|
|
|
|
radius: 8,
|
|
|
|
fillColor: "#00ff00",
|
|
|
|
color: "red",
|
|
|
|
opacity: 1.0,
|
|
|
|
fillOpacity: 0.5
|
|
|
|
};
|
2020-04-17 21:47:12 +02:00
|
|
|
|
2020-04-19 20:54:57 +02:00
|
|
|
function start_editing(dir = 1) {
|
2020-04-17 21:47:12 +02:00
|
|
|
// TODO: Check whether we already are in edit mode
|
|
|
|
// TODO: Detect whether we are cloner to the tail or the head, and issue Fwd or Bwd accordingly
|
2020-04-19 20:54:57 +02:00
|
|
|
if (polyline) {
|
|
|
|
if (dir)
|
|
|
|
polyline.editor.continueForward();
|
|
|
|
else
|
|
|
|
polyline.editor.continueBackward();
|
|
|
|
} else {
|
2020-04-20 18:51:10 +02:00
|
|
|
if (editor_mode_polygon)
|
|
|
|
polyline = mymap.editTools.startPolygon();
|
|
|
|
else
|
|
|
|
polyline = mymap.editTools.startPolyline();
|
2020-04-19 20:54:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function start_reverse_editing() {
|
|
|
|
start_editing(0);
|
2020-04-17 21:47:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function strToPoints(str) {
|
|
|
|
var temp = JSON.parse("[" + str + "]"); // TODO: add .5 everwhere
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onDragEnd(e) {
|
2020-04-19 09:14:19 +02:00
|
|
|
if (!polyline)
|
|
|
|
return;
|
2020-04-17 21:47:12 +02:00
|
|
|
var latlngs = polyline.getLatLngs();
|
|
|
|
|
|
|
|
for (var i = 0; i < latlngs.length; i++) {
|
|
|
|
latlngs[i] = resolve_latlng(latlngs[i], 1);
|
|
|
|
}
|
|
|
|
polyline.editor.refresh();
|
|
|
|
polyline.editor.refreshVertexMarkers();
|
|
|
|
location.hash = get_location_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onLoad(interactive = 1) {
|
|
|
|
if (interactive) {
|
|
|
|
str = prompt("Instructions: \n" +
|
|
|
|
"* Click the scribble-icon in the top left to start or continue drawing.\n" +
|
2020-04-25 18:16:16 +02:00
|
|
|
"* Click last waypoint to stop drawing.\n" +
|
|
|
|
"* Click a waypoint to delete it.\n" +
|
2020-04-17 21:47:12 +02:00
|
|
|
"* Click save in the top right to get the new string.\n\n" +
|
|
|
|
"Enter existing waypoints in the following format: [x,y],[x,y]:", window.location.hash.slice(1));
|
2020-04-25 18:14:20 +02:00
|
|
|
if (str == undefined)
|
2020-04-19 20:54:57 +02:00
|
|
|
return;
|
2020-04-17 21:47:12 +02:00
|
|
|
} else {
|
|
|
|
str = window.location.hash.slice(1);
|
|
|
|
}
|
|
|
|
|
2020-04-19 20:54:57 +02:00
|
|
|
if (polyline) {
|
|
|
|
polyline.remove(mymap);
|
|
|
|
polyline = undefined;
|
|
|
|
}
|
|
|
|
|
2020-04-17 21:47:12 +02:00
|
|
|
if (str) {
|
|
|
|
coords = strToPoints(str);
|
|
|
|
for (var i = 0; i < coords.length; i++) {
|
|
|
|
coords[i] = [coords[i][1], coords[i][0]];
|
|
|
|
}
|
2020-04-20 18:51:10 +02:00
|
|
|
if (editor_mode_polygon)
|
2020-04-25 18:08:02 +02:00
|
|
|
polyline = L.polygon([coords], editor_style).addTo(mymap);
|
2020-04-20 18:51:10 +02:00
|
|
|
else
|
2020-04-25 18:08:02 +02:00
|
|
|
polyline = L.polyline(coords, editor_style).addTo(mymap);
|
2020-04-17 21:47:12 +02:00
|
|
|
// polyline.on('dragend', onDragEnd); // TODO: Doesn't work, see "workaround" below
|
|
|
|
polyline.enableEdit();
|
2020-04-19 22:26:35 +02:00
|
|
|
if (interactive) {
|
2020-04-19 23:09:26 +02:00
|
|
|
latlng = L.latLng(polyline_get_middle_coords(coords));
|
2020-04-19 22:26:35 +02:00
|
|
|
jump_to(latlng, 8);
|
|
|
|
}
|
2020-04-17 21:47:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 09:12:17 +02:00
|
|
|
function editor_onHashChange() {
|
2020-04-17 21:47:12 +02:00
|
|
|
if (("#" + get_location_string()) != window.location.hash) {
|
|
|
|
onLoad(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 09:12:17 +02:00
|
|
|
window.removeEventListener("hashchange", onHashChange, false);
|
|
|
|
window.addEventListener("hashchange", editor_onHashChange, false);
|
2020-04-17 21:47:12 +02:00
|
|
|
window.addEventListener("mouseup", onDragEnd, false); // Workaround as polyline.on(dragend, ) doesn't seem to work
|
|
|
|
|
2020-04-25 18:08:23 +02:00
|
|
|
function onMapKeydown(e) {
|
|
|
|
if (e.originalEvent.key == "Escape") {
|
|
|
|
if (polyline)
|
|
|
|
polyline.editor.cancelDrawing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 21:47:12 +02:00
|
|
|
// Configure map for better editing
|
|
|
|
mymap.setMaxZoom(14);
|
|
|
|
mymap.off('click', onMapClick);
|
2020-04-25 18:08:23 +02:00
|
|
|
mymap.on('keydown', onMapKeydown);
|
2020-04-17 21:47:12 +02:00
|
|
|
|
|
|
|
function get_location_string() {
|
2020-04-20 18:51:10 +02:00
|
|
|
var latlngs;
|
|
|
|
if (editor_mode_polygon)
|
|
|
|
latlngs = polyline.getLatLngs()[0];
|
|
|
|
else
|
|
|
|
latlngs = polyline.getLatLngs();
|
2020-04-17 21:47:12 +02:00
|
|
|
var str = "";
|
|
|
|
|
|
|
|
for (var i = 0; i < latlngs.length; i++) {
|
|
|
|
latlng = resolve_latlng(latlngs[i], 1);
|
|
|
|
if (i != 0)
|
|
|
|
str += ",";
|
2020-04-19 07:38:41 +02:00
|
|
|
str += "[" + (latlng.lng) + "," + (latlng.lat) + "]";
|
2020-04-17 21:47:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
function show_location_string(e) {
|
2020-05-08 17:18:10 +02:00
|
|
|
if (polyline)
|
|
|
|
polyline.editor.cancelDrawing();
|
2020-04-19 20:54:57 +02:00
|
|
|
prompt("Copy this string back into the Wiki and wait for the server to refresh the maps:", get_location_string());
|
2020-04-17 21:47:12 +02:00
|
|
|
}
|
|
|
|
|
2020-04-19 20:36:47 +02:00
|
|
|
function reset_path(e) {
|
|
|
|
onLoad(1);
|
|
|
|
}
|
|
|
|
|
2020-04-17 21:47:12 +02:00
|
|
|
L.EditControl = L.Control.extend({
|
|
|
|
options: {
|
|
|
|
position: 'topleft',
|
|
|
|
callback: null,
|
|
|
|
kind: '',
|
|
|
|
html: ''
|
|
|
|
},
|
|
|
|
onAdd: function (map) {
|
|
|
|
var container = L.DomUtil.create('div', 'leaflet-control leaflet-bar'),
|
|
|
|
link = L.DomUtil.create('a', '', container);
|
|
|
|
|
|
|
|
link.href = '#';
|
|
|
|
link.title = this.options.title;
|
|
|
|
link.innerHTML = this.options.html;
|
2020-05-08 17:18:10 +02:00
|
|
|
L.DomEvent.on(link, 'mousedown', L.DomEvent.stop);
|
|
|
|
L.DomEvent.on(link, 'mouseup', L.DomEvent.stop);
|
2020-04-17 21:47:12 +02:00
|
|
|
L.DomEvent.on(link, 'click', L.DomEvent.stop)
|
2020-05-08 17:18:10 +02:00
|
|
|
.on(link, 'click', function (e) {
|
|
|
|
window.LAYER = this.options.callback.call(map.editTools);
|
|
|
|
}, this);
|
2020-04-17 21:47:12 +02:00
|
|
|
return container;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-19 20:36:47 +02:00
|
|
|
L.ResetPathControl = L.EditControl.extend({
|
|
|
|
options: {
|
|
|
|
position: 'topleft',
|
|
|
|
callback: reset_path,
|
|
|
|
title: 'Enter path coordinates',
|
2020-04-19 20:54:57 +02:00
|
|
|
html: '📂'
|
2020-04-19 20:36:47 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-17 21:47:12 +02:00
|
|
|
L.StartEditControl = L.EditControl.extend({
|
|
|
|
options: {
|
|
|
|
position: 'topleft',
|
|
|
|
callback: start_editing,
|
2020-04-19 20:54:57 +02:00
|
|
|
title: 'Start editing (forward)',
|
2020-04-17 21:47:12 +02:00
|
|
|
html: '\\/\\'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-19 20:54:57 +02:00
|
|
|
L.StartReverseEditControl = L.EditControl.extend({
|
|
|
|
options: {
|
|
|
|
position: 'topleft',
|
|
|
|
callback: start_reverse_editing,
|
|
|
|
title: 'Start editing (backward)',
|
|
|
|
html: '↺'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-17 21:47:12 +02:00
|
|
|
L.NewLineControl = L.EditControl.extend({
|
|
|
|
options: {
|
|
|
|
position: 'topleft',
|
|
|
|
callback: show_location_string,
|
|
|
|
title: 'Get location string',
|
|
|
|
html: '💾'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mymap.addControl(new L.NewLineControl());
|
2020-04-19 20:36:47 +02:00
|
|
|
mymap.addControl(new L.ResetPathControl());
|
2020-04-17 21:47:12 +02:00
|
|
|
mymap.addControl(new L.StartEditControl());
|
2020-04-19 20:54:57 +02:00
|
|
|
mymap.addControl(new L.StartReverseEditControl());
|
2020-04-19 20:36:47 +02:00
|
|
|
|
|
|
|
onLoad();
|
2020-04-17 21:47:12 +02:00
|
|
|
}
|