summaryrefslogtreecommitdiff
path: root/htdocs/streeteditor.js
blob: 392bc2233439eec6ecb9f306ffc21cf759d272d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
if (urlParams.has('editor')) {
	var draw_layer;
	var polyline;

	var edit_active = 0;

	function resolve_latlng(latlng, recenter = 0) {
		var corr;
		if (recenter)
			corr = 0.5;
		else
			corr = 0;
		latlng.lng = Math.round(latlng.lng - 0.5) + corr;
		latlng.lat = Math.round(latlng.lat - 0.5) + corr;
		return latlng;
	}

	function start_editing(e) {
		// 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
		if (polyline)
			polyline.editor.continueForward();
		else
			polyline = mymap.editTools.startPolyline();
	}

	function strToPoints(str) {
		var temp = JSON.parse("[" + str + "]"); // TODO: add .5 everwhere
		return temp;
	}

	function onDragEnd(e) {
		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" +
			"* Double click last waypoint to stop.\n" +
			"* Double click a waypoint to delete it.\n" +
			"* 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));
		} else {
			str = window.location.hash.slice(1);
		}

		if (str) {
			coords = strToPoints(str);
			for (var i = 0; i < coords.length; i++) {
				coords[i] = [coords[i][1], coords[i][0]];
			}
			polyline = L.polyline(coords).addTo(mymap);
			// polyline.on('dragend', onDragEnd); // TODO: Doesn't work, see "workaround" below
			polyline.enableEdit();
		}
	}

	function onHashChange() {
		if (("#" + get_location_string()) != window.location.hash) {
			polyline.remove(mymap);
			onLoad(0);
		}
	}

	window.addEventListener("hashchange", onHashChange, false);
	window.addEventListener("mouseup", onDragEnd, false); // Workaround as polyline.on(dragend, ) doesn't seem to work

	// Configure map for better editing
	document.getElementById('mapid').classList.add("no-aa");
	mymap.setMaxZoom(14);
	mymap.off('click', onMapClick);
	//mymap.setOpacity(0.7);

	onLoad();

	function get_location_string() {
		var latlngs = polyline.getLatLngs();
		var str = "";

		for (var i = 0; i < latlngs.length; i++) {
			latlng = resolve_latlng(latlngs[i], 1);
			if (i != 0)
				str += ",";
			str += "[" + (latlng.lng - 0.5) + "," + (latlng.lat - 0.5) + "]";
		}

		return str;
	}

	function show_location_string(e) {
		prompt("Copy this string back into the Wiki and wait for a few hours:", get_location_string());
	}

	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;
			L.DomEvent.on(link, 'click', L.DomEvent.stop)
			.on(link, 'click', function () {
			window.LAYER = this.options.callback.call(map.editTools);
		}, this);
		return container;
		}
	});

	L.StartEditControl = L.EditControl.extend({
		options: {
			position: 'topleft',
			callback: start_editing,
			title: 'Start editing',
			html: '\\/\\'
		}
	});

	L.NewLineControl = L.EditControl.extend({
		options: {
			position: 'topleft',
			callback: show_location_string,
			title: 'Get location string',
			html: '💾'
		}
	});

	mymap.addControl(new L.NewLineControl());
	mymap.addControl(new L.StartEditControl());
}