-
Notifications
You must be signed in to change notification settings - Fork 0
/
lines.js
128 lines (101 loc) · 3.34 KB
/
lines.js
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
const linesContainer = document.querySelector('.lines-container');
const line = document.querySelector('.line');
const template = line.cloneNode(true);
const addButton = linesContainer.querySelector('.add-line');
const lineColors = ['#a83232', '#34a832', '#3236a8', '#a832a6', '#a89932', '#a85632', '#329da8', '#525c69'];
const layers = [];
function persistState(lines) {
history.pushState(null, '', `#${encodeURIComponent(JSON.stringify(lines))}`);
}
function render(lines, fitToBounds) {
// reset all layers
while (layers.length) {
const layer = layers.pop();
layer.remove();
}
const allBounds = []; // We use this to zoom the map after all lines are plotted
lines.forEach((line, i) => {
if (!line.shape || line.hide) return;
const mul = line.polyline6 ? 1e6 : 1e5;
const shape = line.unescape ? line.shape.replace(/\\\\/g, '\\') : line.shape;
const decoded = decode(shape, mul);
layers.push(
L.polyline(decoded, {
color: lineColors[i % lineColors.length],
opacity: '0.6',
}).addTo(map),
);
const icon = L.icon({
iconUrl: 'round_marker.svg',
iconSize: [8, 8],
});
const start = L.icon({
iconUrl: 'marker.svg',
iconSize: [36, 36],
iconAnchor: [18, 36],
});
// render markers
decoded.forEach((x) => {
layers.push(
L.marker(x, {
icon,
title: `${x[0]},${x[1]}`,
}).addTo(map),
);
allBounds.push(x);
});
// render Start
layers.push(
L.marker(decoded[0], {
icon: start,
}).addTo(map),
);
//fit it in view
if (fitToBounds) {
map.fitBounds(allBounds);
}
});
}
function addLineContainer() {
linesContainer.insertBefore(template.cloneNode(true), addButton);
}
function keyClickHandler(e) {
if (e.target.classList.contains('add-line')) {
addLineContainer();
return;
}
if (e.target.classList.contains('remove-line')) {
linesContainer.removeChild(e.target.closest('.line'));
}
const lines = Array.from(linesContainer.querySelectorAll('.line')).map((x) => ({
unescape: x.querySelector('[name=unescape]').checked,
polyline6: x.querySelector('[name=polyline6]').checked,
hide: x.querySelector('[name=hide]').checked,
shape: x.querySelector('[name=shape]').value,
}));
render(lines, false);
persistState(lines);
}
function parseState() {
const state = JSON.parse(decodeURIComponent(location.hash.substring(1)));
state.forEach((line, i) => {
if (i > 0) {
linesContainer.insertBefore(template.cloneNode(true), addButton);
}
const lastLineElement = linesContainer.querySelector('.line:last-of-type');
lastLineElement.querySelector('[name=unescape]').checked = line.unescape;
lastLineElement.querySelector('[name=polyline6]').checked = line.polyline6;
lastLineElement.querySelector('[name=hide]').checked = line.hide;
lastLineElement.querySelector('[name=shape]').value = line.shape;
});
if (state.length) render(state, true);
}
linesContainer.addEventListener('keyup', keyClickHandler);
linesContainer.addEventListener('click', keyClickHandler);
// read current state
parseState();
window.addEventListener('hashchange', () => {
const removable = Array.from(linesContainer.querySelectorAll('.line')).slice(1);
removable.forEach(x => linesContainer.removeChild(x));
parseState();
})