summaryrefslogtreecommitdiff
path: root/htdocs/streeteditor.js
blob: ced38ea926da0ad9f6f5a1f6d5c227cdefeacd15 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
editor_mode = urlParams.has('editor');

if (editor_mode) {
	var draw_layer;
	var polyline;

	var edit_active = 0;

	function resolve_latlng(latlng, recenter = 0) {
		latlng.lng = Math.round(latlng.lng);
		latlng.lat = Math.round(latlng.lat);
		return latlng;
	}

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

	function start_reverse_editing() {
		start_editing(0);
	}

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

	function onDragEnd(e) {
		if (!polyline)
			return;
		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));
			if (!str)
				return;
		} else {
			str = window.location.hash.slice(1);
		}

		if (polyline) {
				polyline.remove(mymap);
				polyline = undefined;
		}

		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 editor_onHashChange() {
		if (("#" + get_location_string()) != window.location.hash) {
			onLoad(0);
		}
	}

	window.removeEventListener("hashchange", onHashChange, false);
	window.addEventListener("hashchange", editor_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);

	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) + "," + (latlng.lat) + "]";
		}

		return str;
	}

	function show_location_string(e) {
		prompt("Copy this string back into the Wiki and wait for the server to refresh the maps:", get_location_string());
	}

	function reset_path(e) {
		onLoad(1);
	}

	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.ResetPathControl = L.EditControl.extend({
		options: {
			position: 'topleft',
			callback: reset_path,
			title: 'Enter path coordinates',
			html: '📂'
		}
	});

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

	L.StartReverseEditControl = L.EditControl.extend({
		options: {
			position: 'topleft',
			callback: start_reverse_editing,
			title: 'Start editing (backward)',
			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.ResetPathControl());
	mymap.addControl(new L.StartEditControl());
	mymap.addControl(new L.StartReverseEditControl());

	onLoad();
}