-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmpl_index.html
412 lines (370 loc) · 11.9 KB
/
tmpl_index.html
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<!doctype html>
<html lang=sv>
<head>
<meta charset=utf-8>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Historik - Fruktkartan</title>
<style>
:root {
--red-color: #f20505;
--orange-color: #dd8213;
--green-color: #53c45e;
--light-green-color: color-mix(in srgb, white 82%, var(--green-color));
--light-red-color: color-mix(in srgb, white 82%, var(--red-color));
}
body {
font-family: sans;
background-color: #fafafa;
max-width: 34em;
}
.change, .flagged {
padding: 0.5em 0 0.5em 0;
margin: 0;
border-top: 1px solid grey;
}
.change:nth-child(odd), .flagged:nth-child(odd) {
background-color: #f8f0e3;
}
.flagtime, .changetime, .op, .flagname {
font-family: monospace, monospace;
}
.op {
font-weight: bold;
}
.delete {
color: var(--red-color);
}
.insert {
color: var(--green-color);
}
.update {
color: var(--orange-color);
}
.old {
text-decoration: line-through;
text-decoration-color: var(--red-color);
text-decoration-thickness: 2px;
}
.desc ins {
/* !important to override DiffPrettyHtml's hardcoded style on element */
background-color: var(--light-green-color) !important;
}
.desc del {
/* !important to override DiffPrettyHtml's hardcoded style on element */
background-color: var(--light-red-color) !important;
}
.photo img {
border: 2px solid black;
border-radius: 2px
}
.photo.added img {
border: 4px solid var(--green-color);
border-radius: 4px;
}
.photo.removed img {
border: 4px solid var(--red-color);
border-radius: 3px
}
.photo.flagged img {
height: auto;
width: auto;
max-width: 130px;
max-height: 130px;
}
ins, del {
/* get rid of default underline/overstrike for these (we have colors) */
text-decoration: none;
}
</style>
<script>
const apiBase = 'https://fruktkartan-api.herokuapp.com'
window.onload = async () => {
loadFlagged()
}
function formatDate(dateStr) {
const date = new Date(dateStr)
const yyyy = String(date.getFullYear())
const mm = String(date.getMonth() + 1).padStart(2, '0')
const dd = String(date.getDate()).padStart(2, '0')
const hh = String(date.getHours()).padStart(2, '0')
const mi = String(date.getMinutes()).padStart(2, '0')
return `${yyyy}-${mm}-${dd} ${hh}:${mi}`
}
// Replaces the HTML generated when fruktsam was run
async function loadFlagged() {
document.getElementById('flagged').innerHTML = 'Laddar från databasen...'
const flags = await http('GET', `${apiBase}/flags`)
if ('error' in flags) {
document.getElementById('flagged').innerHTML = `fel: ${JSON.stringify(flags)}`
return
}
if (flags.length == 0) {
document.getElementById('flagged').innerHTML = `Inga flaggade träd`
return
}
let html = ''
flags.sort((a, b) => {
if (a.flagged_at == b.flagged_at) {
return 0
}
if (a.flagged_at < b.flagged_at) {
// Sort a after b
return 1
}
return -1
})
for (const flagged of flags) {
const tree = await http('GET', `${apiBase}/tree/${flagged.tree}`)
if ('error' in tree) {
html += `<p class="flagged">fel vid hämtning av träd '${JSON.stringify(flagged.tree)}':\n\n${tree.error}</p>`
continue
}
const flaggedAt = formatDate(flagged.flagged_at)
const lastChange = formatDate(tree.added)
let photoHtml = ''
if (tree.file !== '') {
photoHtml = `
<br/>
<span class="photo flagged">
<a href="https://fruktkartan-thumbs.s3.eu-north-1.amazonaws.com/${tree.file}_1200.jpg" target="_blank" rel="noopener">
<img src="https://fruktkartan-thumbs.s3.eu-north-1.amazonaws.com/${tree.file}_1200.jpg" width="130px" />
</a>
</span>
`
}
// If we have more than just a few flagged trees, then fetching this
// is too slow/hangs.
// var reverseURL = new URL('https://nominatim.openstreetmap.org/reverse')
// reverseURL.searchParams.set('lat', tree.lat)
// reverseURL.searchParams.set('lon', tree.lon)
// reverseURL.searchParams.set('format', 'json')
// const reverse = await http('GET', reverseURL.href, { 'Accept-Language': 'sv,en-US,en' })
// if (('error' in reverse) || (!('display_name' in reverse))) {
// html += `<p class="flagged">fel vid hämtning av nominatim-reverse för träd '${JSON.stringify(flagged.tree)}':\n\n${reverse.error}</p>`
// continue
// }
// — ${reverse.display_name}
// <br/>
html += `
<p class="flagged">
<span class="flagtime">${flaggedAt}</span>
<span class="type">
<a href="https://fruktkartan.se/#/t/${flagged.tree}" target="_blank" rel="noopener">${tree.type}</a>
</span>
<em>Flagga: </em><span class="flagname">${flagged.flag}</span>
<br/>
<span><em>Anledning: </em>${flagged.reason}</span>
<br/>
<span><em>Flaggat av: </em>${flagged.flagged_by}</span>
<br/>
<span class="desc"><em>Beskrivning: </em>${tree.desc}</span>
${photoHtml}
<br/>
<span class="lastchange"><em>Senast redigerat: </em>${lastChange}</span>
<br/>
<span>
<button onclick='deleteFlag("${flagged.tree}", "${flagged.flag}")'>ta bort flaggan</button>
<button style="float: right;" onclick='deleteTree("${flagged.tree}", "${flagged.flag}")'>ta bort trädet</button>
</span>
</p>
`
}
document.getElementById('flagged').innerHTML = html
}
async function http(method, url, headers) {
let opts = {
method: method,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
...headers
}
}
if ((method !== 'GET') && (method !== 'HEAD')) {
opts.body = '{}'
}
return await (await fetch(url, opts)
.catch(handleError))
.json()
.then((data) => {
// Is this then() needed?
return data
})
.catch((err) => {
// I think we end up here when json() cannot parse empty body
if ('error' in err) {
return err
}
return {}
})
}
function handleError(err) {
return new Response(JSON.stringify({
statusCode: 418,
error: 'synthetic teapot error',
message: `err: ${err}`
}))
}
async function deleteFlag(key, flag) {
if ((typeof(key) !== 'string') || (typeof(flag) !== 'string') ||
(key.length < 2) || (flag !== 'delete') ) {
window.alert(`dålig input! key:${key} flag:${flag}`)
return
}
const tree = await http('GET', `${apiBase}/tree/${key}`)
if ('error' in tree) {
window.alert(`fel:\n\n${JSON.stringify(tree)}`)
return
}
if (!window.confirm(`Träd: ${tree.type} [${key}]\n\nTa bort flaggan "${flag}" från detta träd?`)) {
loadFlagged()
return
}
const res = await http('DELETE', `${apiBase}/flag/${key}/${flag}`)
if ('error' in res) {
window.alert(`fel:\n\n${JSON.stringify(res)}`)
loadFlagged()
return
}
if (!isEmpty(res)) {
window.alert(`Kanske gick något snett?\n\nresultat: ${JSON.stringify(res)}`)
}
loadFlagged()
}
async function deleteTree(key, flag) {
if ((typeof(key) !== 'string') || (typeof(flag) !== 'string') ||
(key.length < 2) || (flag !== 'delete') ) {
window.alert(`dålig input! key:${key} flag:${flag}`)
return
}
const tree = await http('GET', `${apiBase}/tree/${key}`)
if ('error' in tree) {
window.alert(`fel:\n\n${JSON.stringify(tree)}`)
loadFlagged()
return
}
if (!window.confirm(`Träd: ${tree.type} [${key}]\n\nTa bort detta träd? (inkl. alla flaggor)`)) {
loadFlagged()
return
}
const res = await http('DELETE', `${apiBase}/tree/${key}`)
if ('error' in res) {
window.alert(`fel:\n\n${JSON.stringify(res)}`)
loadFlagged()
return
}
if (!isEmpty(res)) {
window.alert(`Kanske gick något snett?\n\nresultat: ${JSON.stringify(res)}`)
}
loadFlagged()
}
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
</script>
</head>
<body>
<p>
Sidan genererades {{ .Now }} från databas med namnet {{ .DatabaseName }}
</p>
<p>
Det finns {{ .Trees.Count }} träd på <a href="https://fruktkartan.se/">fruktkartan.se</a>.
Under de senaste {{ .History.SinceDays }} dagarna hände följande:
<ul>
<li>{{ .History.Inserts }} träd lades till</li>
<li>{{ .History.Deletes }} träd togs bort</li>
<li>netto {{ .History.Net }} träd</li>
<li>{{ .History.Updates }} redigeringar gjordes</li>
</ul>
</p>
<h2>Flaggade träd</h2>
<div id="flagged"></div>
{{ $lastDate := "" }}
{{ range .History.Entries }}
{{ if ne $lastDate .ChangeAt.Date }}
<h2>{{ .ChangeAt.Date }} v{{ .ChangeAt.WeekNumber }}</h2>
{{ $lastDate = .ChangeAt.Date }}
{{ end }}
<p class="change">
<span class="changetime">{{ .ChangeAt.TimeStr }}</span>
{{ if eq .ChangeOp "DELETE" }}
<span class="op delete">bort</span>
<span class="type">{{ .Type }}</span>
<span class="key">[{{ .Key }}]</span>
<span>— nära {{ .Address }}
<a href="{{ .Pos.OSMURL }}" target="_blank" rel="noopener">osm</a>
· <a href="{{ .Pos.GoogmapsURL }}" target="_blank" rel="noopener">gm</a>
· <a href="{{ .Pos.GeoURL }}" target="_blank" rel="noopener">geo</a>
</span>
<br/>
<span class="desc"><span class="old"><em>Beskrivning:</em></span> {{ .Desc }}</span>
{{ if ne .Img.String "" }}
<br/>
<span class="photo removed">
<a href="{{ .ImgURL }}" target="_blank" rel="noopener"><img src="{{ .ImgFile }}" /></a>
</span>
{{ end }}
<br/>
<span class="lastchange"><em>Senast redigerat:</em> {{ .At }}</span>
{{ end }}
{{ if eq .ChangeOp "INSERT" }}
<span class="op insert">nytt</span>
<span class="type">
<a href="https://fruktkartan.se/#/t/{{ .KeyNew }}" target="_blank" rel="noopener">{{ .TypeNew }}</a>
</span>
<span>— nära {{ .AddressNew }}
<a href="{{ .PosNew.OSMURL }}" target="_blank" rel="noopener">osm</a>
· <a href="{{ .PosNew.GoogmapsURL }}" target="_blank" rel="noopener">gm</a>
· <a href="{{ .PosNew.GeoURL }}" target="_blank" rel="noopener">geo</a>
</span>
<br/>
<span><em>Tillagt av:</em> {{ .ByNew }}</span>
<br/>
<span class="desc"><em>Beskrivning:</em> {{ .DescNew }}</span>
{{ if ne .ImgNew.String "" }}
<br/>
<span class="photo added">
<a href="{{ .ImgURLNew }}" target="_blank" rel="noopener"><img src="{{ .ImgFileNew }}" /></a>
</span>
{{ end }}
{{ end }}
{{ if eq .ChangeOp "UPDATE" }}
<span class="op update">red.</span>
<span class="type">
<a href="https://fruktkartan.se/#/t/{{ .KeyNew }}" target="_blank" rel="noopener">{{ .TypeNew }}</a>
</span>
{{ if ne .Type .TypeNew }}<span class="old">{{ .Type }}</span>{{ end }}
<span>— nära {{ .AddressNew }}
<a href="{{ .PosNew.OSMURL }}" target="_blank" rel="noopener">osm</a>
· <a href="{{ .PosNew.GoogmapsURL }}" target="_blank" rel="noopener">gm</a>
· <a href="{{ .PosNew.GeoURL }}" target="_blank" rel="noopener">geo</a>
</span>
<br/>
<span><em>Redigerat av:</em> {{ .ByNew }}</span>
<br/>
<span class="desc"><em>Beskrivning:</em> {{ .DescDiff }}</span>
{{ if or (ne .Img.String "") (ne .ImgNew.String "") }}
<br/>
{{ end }}
{{ if ne .Img.String "" }}
{{ if ne .Img.String .ImgNew.String }}
<span class="photo removed">
{{ else }}
<span class="photo">
{{ end }}
<a href="{{ .ImgURL }}" target="_blank" rel="noopener"><img src="{{ .ImgFile }}" /></a>
</span>
{{ end }}
{{ if and (ne .ImgNew.String "") (ne .Img.String .ImgNew.String) }}
<span class="photo added">
<a href="{{ .ImgURLNew }}" target="_blank" rel="noopener"><img src="{{ .ImgFileNew }}" /></a>
</span>
{{ end }}
<br/>
<span class="lastchange"><em>Tidigare redigerat:</em> {{ .At }}</span>
{{ if .UpdateIsEmpty }}<br/><span><strong>Ingen förändring, konstigt nog!</strong></span>{{ end }}
{{ end }}
</p>
{{ end }}
</body>
</html>