1
|
/* global L */
|
2
|
/* global $ */
|
3
|
|
4
|
var mymap
|
5
|
var heatmapLayer = null
|
6
|
var marksLayer = null
|
7
|
|
8
|
// values for arrow keys
|
9
|
const arrowKeyLEFT = 37
|
10
|
const arrowKeyRIGHT = 39
|
11
|
|
12
|
var startX = 49.7248
|
13
|
var startY = 13.3521
|
14
|
var startZoom = 17
|
15
|
|
16
|
var dataSourceRoute
|
17
|
let positionsSourceRoute
|
18
|
|
19
|
let currentTime
|
20
|
|
21
|
let currentDate
|
22
|
|
23
|
var timer
|
24
|
var isAnimationRunning = false
|
25
|
var data = []
|
26
|
|
27
|
//
|
28
|
// info = {
|
29
|
// DATASETNAME: {
|
30
|
// items: Array,
|
31
|
// number: Number,
|
32
|
// datasetName: String
|
33
|
// }
|
34
|
// }
|
35
|
//
|
36
|
var info = []
|
37
|
let currentPageInPopup = 0
|
38
|
|
39
|
// dictionary for names of datasets
|
40
|
const datasetDictNameDisplayName = {}
|
41
|
var datasetSelected = []
|
42
|
|
43
|
// data only for one day
|
44
|
let lockedDay = false
|
45
|
|
46
|
// loading information for async operations
|
47
|
let loading = 0
|
48
|
|
49
|
// default loader showup delay
|
50
|
const defaultLoaderDelay = 1000
|
51
|
|
52
|
// marks for all datasets
|
53
|
const dataSourceMarks = {}
|
54
|
|
55
|
const globalMarkersHolder = {}
|
56
|
// all marker from which popup was removed
|
57
|
// contains: {key:[L.circle,L.pupup]}
|
58
|
// key: x and y, x + '' + y string
|
59
|
let globalMarkersChanged = {}
|
60
|
|
61
|
const globalPopup = {
|
62
|
coord: {
|
63
|
lat: 0,
|
64
|
lng: 0
|
65
|
},
|
66
|
_popup: null
|
67
|
}
|
68
|
|
69
|
const setGlobalPopupContent = (content) => {
|
70
|
globalPopup._popup.setContent(content)
|
71
|
globalPopup._popup.openOn(mymap)
|
72
|
}
|
73
|
|
74
|
const disablePopupControls = () => {
|
75
|
$('#btn-previous-page').prop('disabled', true)
|
76
|
$('#btn-next-page').prop('disabled', true)
|
77
|
$('.popup-controls').hide()
|
78
|
}
|
79
|
|
80
|
const areSameCoord = (first, second) => {
|
81
|
return first.lat === second.lat && first.lng === second.lng
|
82
|
}
|
83
|
|
84
|
const loadingCallbackNested = (func, delay) => {
|
85
|
setTimeout(() => {
|
86
|
func(loading)
|
87
|
if (loading) {
|
88
|
loadingCallbackNested(func, delay)
|
89
|
}
|
90
|
}, delay)
|
91
|
}
|
92
|
|
93
|
const loadingY = (delay = defaultLoaderDelay) => {
|
94
|
loading++
|
95
|
// check after nms if there is something that is loading
|
96
|
loadingCallbackNested(() => loadingCallbackNested((isLoading) => loadingTimeline(isLoading), delay))
|
97
|
}
|
98
|
|
99
|
const loadingN = (delay = defaultLoaderDelay) => {
|
100
|
loading--
|
101
|
loadingCallbackNested(() => loadingCallbackNested((isLoading) => loadingTimeline(isLoading)), delay)
|
102
|
}
|
103
|
|
104
|
const changeCurrentTime = (time = null) => {
|
105
|
if (time !== null) {
|
106
|
currentTime = time
|
107
|
} else {
|
108
|
currentTime = parseInt($('#dropdown-time input[type="radio"]:checked').val())
|
109
|
}
|
110
|
}
|
111
|
|
112
|
const changeCurrentDate = (date = null) => {
|
113
|
const dateInput = $('#date')
|
114
|
currentDate = new Date(date ? date : dateInput.val())
|
115
|
|
116
|
dateInput.val(currentDateToString())
|
117
|
$('#player-date span').html(`${currentDate.getDate()}. ${currentDate.getMonth() + 1}. ${currentDate.getFullYear()}`)
|
118
|
|
119
|
data = []
|
120
|
}
|
121
|
|
122
|
const currentDayToString = () => {
|
123
|
const day = currentDate.getDate()
|
124
|
return day > 9 ? `${day}` : `0${day}`
|
125
|
}
|
126
|
|
127
|
const currentMonthToString = () => {
|
128
|
const month = currentDate.getMonth() + 1
|
129
|
return month > 9 ? `${month}` : `0${month}`
|
130
|
}
|
131
|
|
132
|
const currentDateToString = () => `${currentDate.getFullYear()}-${currentMonthToString()}-${currentDayToString()}`
|
133
|
|
134
|
const addDayToCurrentDate = (day) => {
|
135
|
currentDate.setDate(currentDate.getDate() + day)
|
136
|
changeCurrentDate(currentDate)
|
137
|
}
|
138
|
|
139
|
const toggleDayLock = () => {
|
140
|
lockedDay = !lockedDay
|
141
|
$('#player-date').toggleClass('lock')
|
142
|
}
|
143
|
|
144
|
const fetchByNameDate = async (baseRoute, name, date, currentTime) => {
|
145
|
const headers = new Headers()
|
146
|
const myRequest = new Request(baseRoute + '/' + name + '/' + date + '/' + currentTime, {
|
147
|
method: 'GET',
|
148
|
headers: headers
|
149
|
})
|
150
|
const beforeJson = await fetch(myRequest)
|
151
|
|
152
|
return beforeJson.json()
|
153
|
}
|
154
|
|
155
|
const fetchDataSourceMarks = async (positionRoute, datasetName) => {
|
156
|
const headers = new Headers()
|
157
|
const myRequest = new Request(positionRoute + '/' + datasetName, {
|
158
|
method: 'GET',
|
159
|
headers: headers
|
160
|
})
|
161
|
const beforeJson = await fetch(myRequest)
|
162
|
|
163
|
return beforeJson.json()
|
164
|
}
|
165
|
|
166
|
const getPaginationButtonsInPopup = (currentPage, countPages) => ({
|
167
|
previousButton: '<button id="btn-previous-page" class="circle-button" onclick="setPreviousPageInPopup()"></button>',
|
168
|
pagesList: `<div id="pages">${currentPage} z ${countPages}</div>`,
|
169
|
nextButton: '<button id="btn-next-page" class="circle-button next" onclick="setNextPageInPopup()"></button>'
|
170
|
})
|
171
|
|
172
|
const genPopUpControls = (controls) => {
|
173
|
return `<div class="popup-controls">${controls ? controls.reduce((sum, item) => sum + item, '') : ''}</div>`
|
174
|
}
|
175
|
|
176
|
const genMultipleDatasetsPopUp = (sum, currentPage, countPages, datasetName) => {
|
177
|
const popupHeader = `<strong id="dataset-name">${datasetName}</strong>`
|
178
|
const popupData = `<div id="number-info"><span id="current-number">${sum}</span></div>`
|
179
|
const { previousButton, nextButton, pagesList } = getPaginationButtonsInPopup(currentPage, countPages)
|
180
|
|
181
|
return `
|
182
|
${popupHeader}
|
183
|
${popupData}
|
184
|
${genPopUpControls([previousButton, pagesList, nextButton])}
|
185
|
`
|
186
|
}
|
187
|
|
188
|
const prepareLayerPopUp = (lat, lng, num, className) => L.popup({
|
189
|
autoPan: false,
|
190
|
className: className
|
191
|
}).setLatLng([lat / num, lng / num])
|
192
|
|
193
|
const getPopupContent = (datasetName, placeName, currentCount, sum, currentPage, countPages) => {
|
194
|
const popupHeader = `
|
195
|
<strong>${datasetName}</strong>
|
196
|
<div id="place-name">${placeName}</div>`
|
197
|
const popupData = `
|
198
|
<div id="number-info">
|
199
|
<span id="current-number">${currentCount}</span>
|
200
|
<span id="part-info">${(sum && sum !== Number(currentCount)) ? '/' + sum : ''}</span>
|
201
|
</div>`
|
202
|
const { previousButton, nextButton, pagesList } = getPaginationButtonsInPopup(currentPage, countPages)
|
203
|
|
204
|
return `
|
205
|
${popupHeader}
|
206
|
${popupData}
|
207
|
${genPopUpControls(countPages > 1 ? [previousButton, pagesList, nextButton] : null)}
|
208
|
`
|
209
|
}
|
210
|
|
211
|
const onCheckboxClicked = async (checkbox) => {
|
212
|
if ($(checkbox).prop('checked')) {
|
213
|
await loadCurrentTimeHeatmap(dataSourceRoute, positionsSourceRoute, 0)
|
214
|
} else {
|
215
|
loadCheckboxDatasetNameData()
|
216
|
|
217
|
data.forEach((item, index) => {
|
218
|
Object.keys(item).forEach((datasetName) => {
|
219
|
if (datasetName === $(checkbox).val()) {
|
220
|
delete data[index][datasetName]
|
221
|
}
|
222
|
})
|
223
|
|
224
|
drawHeatmap(data[currentTime])
|
225
|
})
|
226
|
}
|
227
|
|
228
|
updatePopup()
|
229
|
changeUrlParameters()
|
230
|
}
|
231
|
|
232
|
const onArrowLeftRightKeysDownRegister = () => {
|
233
|
$(document).keydown(function (e) {
|
234
|
const { which } = e
|
235
|
|
236
|
if (which === arrowKeyLEFT) {
|
237
|
previous()
|
238
|
e.preventDefault()
|
239
|
} else if (which === arrowKeyRIGHT) {
|
240
|
next()
|
241
|
e.preventDefault()
|
242
|
}
|
243
|
})
|
244
|
}
|
245
|
|
246
|
const debounce = (func, delay) => {
|
247
|
let inDebounce
|
248
|
return function () {
|
249
|
const context = this
|
250
|
const args = arguments
|
251
|
clearTimeout(inDebounce)
|
252
|
inDebounce = setTimeout(() => func.apply(context, args), delay)
|
253
|
}
|
254
|
}
|
255
|
|
256
|
const onValueChangeRegister = () => {
|
257
|
$('#date').change(function () {
|
258
|
changeCurrentDate($(this).val())
|
259
|
loadCurrentTimeHeatmap(dataSourceRoute, positionsSourceRoute, 0)
|
260
|
changeUrlParameters()
|
261
|
})
|
262
|
|
263
|
$('#dropdown-time input[type="radio"]').each(function () {
|
264
|
$(this).change(function () {
|
265
|
changeHour(parseInt($(this).val()))
|
266
|
drawHeatmap(data[currentTime])
|
267
|
})
|
268
|
})
|
269
|
|
270
|
$('#dropdown-dataset input[type="checkbox"]').each(function () {
|
271
|
$(this).change(
|
272
|
debounce(() => onCheckboxClicked(this), 1000)
|
273
|
)
|
274
|
})
|
275
|
}
|
276
|
|
277
|
/**
|
278
|
* Initialize leaflet map on start position which can be default or set based on user action
|
279
|
*/
|
280
|
// eslint-disable-next-line no-unused-vars
|
281
|
function initMap () {
|
282
|
startX = localStorage.getItem('lat') || startX
|
283
|
startY = localStorage.getItem('lng') || startY
|
284
|
startZoom = localStorage.getItem('zoom') || startZoom
|
285
|
|
286
|
mymap = L.map('heatmap').setView([startX, startY], startZoom)
|
287
|
|
288
|
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
289
|
attribution: '',
|
290
|
maxZoom: 19
|
291
|
}).addTo(mymap)
|
292
|
|
293
|
mymap.on('click', function (e) { showInfo(e) })
|
294
|
}
|
295
|
|
296
|
const getCountPagesInPopup = () => {
|
297
|
const infoKeys = Object.keys(info)
|
298
|
if (infoKeys.length === 1) {
|
299
|
// return number of records in one dataset (one dataset in area)
|
300
|
return info[infoKeys[0]].items.length
|
301
|
}
|
302
|
// return number of datasets (agregation of all datasets in area)
|
303
|
return infoKeys.length
|
304
|
}
|
305
|
|
306
|
const getElFromObjectInfo = (position) => {
|
307
|
const keys = Object.keys(info)
|
308
|
return info[keys[position]]
|
309
|
}
|
310
|
|
311
|
const hasInfoMultipleDatasets = () => {
|
312
|
return Object.keys(info).length > 1
|
313
|
}
|
314
|
|
315
|
function showInfo (e) {
|
316
|
info = []
|
317
|
currentPageInPopup = 0
|
318
|
|
319
|
// https://wiki.openstreetmap.org/wiki/Zoom_levels
|
320
|
// Todo change to variable - it is used in heatmap init
|
321
|
const stile = 40075016.686 * Math.cos(startX) / Math.pow(2, mymap.getZoom())
|
322
|
const radius = 25 * stile / 256
|
323
|
|
324
|
let i = 0
|
325
|
let lat = 0
|
326
|
let lng = 0
|
327
|
|
328
|
let total = 0
|
329
|
|
330
|
const datasetsInRadius = {}
|
331
|
const eventCoord = {
|
332
|
lng: e.latlng.lng,
|
333
|
lat: e.latlng.lat
|
334
|
}
|
335
|
Object.keys(data[currentTime]).forEach((key) => {
|
336
|
const namedData = data[currentTime][key]
|
337
|
namedData.items.forEach(element => {
|
338
|
if (e.latlng.distanceTo(new L.LatLng(element.x, element.y)) < radius) {
|
339
|
lat += element.x
|
340
|
lng += element.y
|
341
|
info[i] = { place: element.place, number: element.number, datasetName: key }
|
342
|
total += parseInt(element.number)
|
343
|
i++
|
344
|
datasetsInRadius[key] = true
|
345
|
}
|
346
|
})
|
347
|
})
|
348
|
|
349
|
// Process info for more then one dataset
|
350
|
info = info.reduce((acc, item) => {
|
351
|
if (!acc[item.datasetName]) {
|
352
|
acc[item.datasetName] = {
|
353
|
items: [],
|
354
|
number: 0,
|
355
|
datasetName: item.datasetName
|
356
|
}
|
357
|
}
|
358
|
|
359
|
acc[item.datasetName].items.push(item)
|
360
|
acc[item.datasetName].number += Number(item.number)
|
361
|
return acc
|
362
|
}, {})
|
363
|
// There is one dataset
|
364
|
|
365
|
const numDatasets = Object.keys(datasetsInRadius).length
|
366
|
|
367
|
if (!numDatasets) {
|
368
|
if (mymap._popup) {
|
369
|
$('#part-info').text('')
|
370
|
$('#current-number').html(0)
|
371
|
disablePopupControls()
|
372
|
}
|
373
|
|
374
|
return
|
375
|
}
|
376
|
|
377
|
if (numDatasets === 1) {
|
378
|
const infoDict = getElFromObjectInfo(0)
|
379
|
const info_ = infoDict.items
|
380
|
const { place, number } = info_[currentPageInPopup]
|
381
|
|
382
|
if (!globalPopup._popup || !areSameCoord(globalPopup.coord, eventCoord)) {
|
383
|
globalPopup._popup = prepareLayerPopUp(lat, lng, i, `popup-${infoDict.datasetName}`)
|
384
|
globalPopup.coord = eventCoord
|
385
|
}
|
386
|
|
387
|
setGlobalPopupContent(getPopupContent(datasetDictNameDisplayName[infoDict.datasetName], place, number, total, 1, info_.length))
|
388
|
|
389
|
if (info_.length === 1) {
|
390
|
disablePopupControls()
|
391
|
}
|
392
|
} else {
|
393
|
const { datasetName, number } = getElFromObjectInfo(currentPageInPopup)
|
394
|
|
395
|
if (!globalPopup._popup || !areSameCoord(globalPopup.coord, eventCoord)) {
|
396
|
globalPopup._popup = prepareLayerPopUp(lat, lng, i, `popup-${datasetName}`)
|
397
|
globalPopup.coord = eventCoord
|
398
|
}
|
399
|
|
400
|
setGlobalPopupContent(genMultipleDatasetsPopUp(number, 1, getCountPagesInPopup(), datasetDictNameDisplayName[datasetName]))
|
401
|
}
|
402
|
}
|
403
|
|
404
|
// eslint-disable-next-line no-unused-vars
|
405
|
function setPreviousPageInPopup () {
|
406
|
const countPagesInPopup = getCountPagesInPopup()
|
407
|
const page = currentPageInPopup
|
408
|
|
409
|
currentPageInPopup = (currentPageInPopup + countPagesInPopup - 1) % countPagesInPopup
|
410
|
setPageContentInPopup(page)
|
411
|
}
|
412
|
|
413
|
// eslint-disable-next-line no-unused-vars
|
414
|
function setNextPageInPopup () {
|
415
|
const countPagesInPopup = getCountPagesInPopup()
|
416
|
const page = currentPageInPopup
|
417
|
|
418
|
currentPageInPopup = (currentPageInPopup + 1) % countPagesInPopup
|
419
|
setPageContentInPopup(page)
|
420
|
}
|
421
|
|
422
|
function setPageContentInPopup (page) {
|
423
|
const previousPageData = hasInfoMultipleDatasets() ? getElFromObjectInfo(page) : getElFromObjectInfo(0).items[page]
|
424
|
const currentPageData = hasInfoMultipleDatasets() ? getElFromObjectInfo(currentPageInPopup) : getElFromObjectInfo(0).items[currentPageInPopup]
|
425
|
const datasetName = $('#dataset-name')
|
426
|
|
427
|
if (datasetName) {
|
428
|
datasetName.html(datasetDictNameDisplayName[currentPageData.datasetName])
|
429
|
}
|
430
|
|
431
|
$('#place-name').html(currentPageData.place ? currentPageData.place : currentPageData.datasetName)
|
432
|
$('#current-number').html(currentPageData.number)
|
433
|
$('#pages').html(currentPageInPopup + 1 + ' z ' + getCountPagesInPopup())
|
434
|
|
435
|
$('.leaflet-popup').removeClass(`popup-${previousPageData.datasetName}`).addClass(`popup-${currentPageData.datasetName}`)
|
436
|
}
|
437
|
|
438
|
// eslint-disable-next-line no-unused-vars
|
439
|
function setMapView (latitude, longitude, zoom) {
|
440
|
localStorage.setItem('lat', latitude)
|
441
|
localStorage.setItem('lng', longitude)
|
442
|
localStorage.setItem('zoom', zoom)
|
443
|
|
444
|
mymap.setView([latitude, longitude], zoom)
|
445
|
}
|
446
|
|
447
|
/**
|
448
|
* Change animation start from playing to stopped or the other way round
|
449
|
*/
|
450
|
// eslint-disable-next-line no-unused-vars
|
451
|
function changeAnimationState () {
|
452
|
const btnAnimate = $('#animate-btn')
|
453
|
|
454
|
isAnimationRunning = !isAnimationRunning
|
455
|
|
456
|
if (isAnimationRunning) {
|
457
|
btnAnimate.removeClass('play').addClass('pause')
|
458
|
timer = setInterval(function () { next() }, 800)
|
459
|
} else {
|
460
|
clearTimeout(timer)
|
461
|
btnAnimate.removeClass('pause').addClass('play')
|
462
|
}
|
463
|
}
|
464
|
|
465
|
// eslint-disable-next-line no-unused-vars
|
466
|
async function previous () {
|
467
|
if (loading) {
|
468
|
return
|
469
|
}
|
470
|
|
471
|
currentTime = (currentTime + 23) % 24
|
472
|
changeHour(currentTime)
|
473
|
|
474
|
if (!lockedDay && currentTime === 23) {
|
475
|
addDayToCurrentDate(-1)
|
476
|
await loadCurrentTimeHeatmap(dataSourceRoute, positionsSourceRoute)
|
477
|
} else {
|
478
|
drawHeatmap(data[currentTime])
|
479
|
}
|
480
|
|
481
|
updatePopup()
|
482
|
}
|
483
|
|
484
|
async function next () {
|
485
|
if (loading) {
|
486
|
return
|
487
|
}
|
488
|
|
489
|
currentTime = (currentTime + 1) % 24
|
490
|
changeHour(currentTime)
|
491
|
|
492
|
if (!lockedDay && currentTime === 0) {
|
493
|
addDayToCurrentDate(1)
|
494
|
await loadCurrentTimeHeatmap(dataSourceRoute, positionsSourceRoute)
|
495
|
} else {
|
496
|
drawHeatmap(data[currentTime])
|
497
|
}
|
498
|
|
499
|
updatePopup()
|
500
|
}
|
501
|
|
502
|
/**
|
503
|
* Change browser url based on animation step.
|
504
|
*/
|
505
|
const changeUrlParameters = () => {
|
506
|
window.history.pushState(
|
507
|
'',
|
508
|
document.title,
|
509
|
window.location.origin + window.location.pathname + `?date=${currentDateToString()}&time=${currentTime}${datasetSelected.reduce((acc, current) => acc + '&type=' + current, '')}`
|
510
|
)
|
511
|
}
|
512
|
|
513
|
const formatTime = (hours, twoDigitsHours = false) => {
|
514
|
return ((twoDigitsHours && hours < 10) ? '0' : '') + hours + ':00';
|
515
|
}
|
516
|
|
517
|
const updateHeaderControls = () => {
|
518
|
$(`#time_${currentTime}`).prop('checked', true)
|
519
|
$('#dropdownMenuButtonTime').html(formatTime(currentTime, true))
|
520
|
}
|
521
|
|
522
|
const setTimeline = () => {
|
523
|
$('#player-time > span').text(formatTime(currentTime))
|
524
|
$('#player-time').attr('class', 'time hour-' + currentTime)
|
525
|
}
|
526
|
|
527
|
const loadingTimeline = (isLoading) => {
|
528
|
if (isLoading) {
|
529
|
loadingYTimeline()
|
530
|
} else {
|
531
|
loadingNTimeline()
|
532
|
}
|
533
|
}
|
534
|
|
535
|
const loadingYTimeline = () => {
|
536
|
$('#player-time > .spinner-border').removeClass('d-none')
|
537
|
$('#player-time > span').text('')
|
538
|
}
|
539
|
|
540
|
const loadingNTimeline = () => {
|
541
|
$('#player-time > .spinner-border').addClass('d-none')
|
542
|
setTimeline()
|
543
|
}
|
544
|
|
545
|
const onChangeHour = (hour) => {
|
546
|
changeHour(hour)
|
547
|
drawHeatmap(data[currentTime])
|
548
|
}
|
549
|
|
550
|
const changeHour = (hour) => {
|
551
|
$('#player-time').removeAttr('style')
|
552
|
changeCurrentTime(hour)
|
553
|
updateHeaderControls()
|
554
|
setTimeline()
|
555
|
changeUrlParameters()
|
556
|
updatePopup()
|
557
|
}
|
558
|
|
559
|
const updatePopup = () => {
|
560
|
const { _popup } = mymap
|
561
|
|
562
|
if (_popup) {
|
563
|
showInfo({
|
564
|
latlng: _popup.getLatLng()
|
565
|
})
|
566
|
}
|
567
|
}
|
568
|
|
569
|
/**
|
570
|
* Load and display heatmap layer for current data
|
571
|
* @param {string} opendataRoute route to dataset source
|
572
|
* @param {string} positionsRoute route to dataset postitions source
|
573
|
*/
|
574
|
// eslint-disable-next-line no-unused-vars
|
575
|
async function loadCurrentTimeHeatmap (opendataRoute, positionsRoute, loaderDelay = defaultLoaderDelay) {
|
576
|
loadCheckboxDatasetNameData()
|
577
|
|
578
|
dataSourceRoute = opendataRoute
|
579
|
positionsSourceRoute = positionsRoute
|
580
|
const allPromises = []
|
581
|
data[currentTime] = {}
|
582
|
|
583
|
const dataSelectedHandler = async (datasetName) => {
|
584
|
if (!(datasetName in dataSourceMarks)) {
|
585
|
dataSourceMarks[datasetName] = await fetchDataSourceMarks(positionsRoute, datasetName)
|
586
|
}
|
587
|
|
588
|
const datasetData = await fetchByNameDate(dataSourceRoute, datasetName, currentDateToString(), currentTime)
|
589
|
data[currentTime][datasetName] = datasetData
|
590
|
}
|
591
|
datasetSelected.forEach((datasetName) => {
|
592
|
allPromises.push(dataSelectedHandler(datasetName))
|
593
|
})
|
594
|
|
595
|
loadingY(loaderDelay)
|
596
|
|
597
|
await Promise.all(allPromises).then(
|
598
|
() => {
|
599
|
loadingN(0)
|
600
|
drawDataSourceMarks(dataSourceMarks)
|
601
|
drawHeatmap(data[currentTime])
|
602
|
preload(currentTime, 1, currentDateToString())
|
603
|
preload(currentTime, -1, currentDateToString())
|
604
|
}
|
605
|
)
|
606
|
}
|
607
|
|
608
|
function drawDataSourceMarks (data) {
|
609
|
if (marksLayer != null) {
|
610
|
mymap.removeLayer(marksLayer)
|
611
|
}
|
612
|
|
613
|
marksLayer = L.layerGroup()
|
614
|
|
615
|
Object.keys(data).forEach((key_) => {
|
616
|
for (var key in data[key_]) {
|
617
|
const { x, y, name } = data[key_][key]
|
618
|
const pop =
|
619
|
prepareLayerPopUp(x, y, 1, `popup-${key_}`)
|
620
|
.setContent(getPopupContent(datasetDictNameDisplayName[key_], name, 0, 0, 1, 1))
|
621
|
const newCircle =
|
622
|
L.circle([x, y], { radius: 2, fillOpacity: 0.8, color: '#004fb3', fillColor: '#004fb3', bubblingMouseEvents: true })
|
623
|
.bindPopup(pop)
|
624
|
globalMarkersHolder[x + '' + y] = [newCircle, pop] // add new marker to global holders
|
625
|
marksLayer.addLayer(
|
626
|
newCircle
|
627
|
)
|
628
|
}
|
629
|
})
|
630
|
|
631
|
marksLayer.setZIndex(-1).addTo(mymap)
|
632
|
}
|
633
|
|
634
|
async function preload (time, change, date) {
|
635
|
loadingY()
|
636
|
|
637
|
for (let nTime = time + change; nTime >= 0 && nTime <= 23; nTime = nTime + change) {
|
638
|
if (!data[nTime]) {
|
639
|
data[nTime] = {}
|
640
|
}
|
641
|
|
642
|
datasetSelected.forEach(async (datasetName) => {
|
643
|
if (!data[nTime][datasetName]) {
|
644
|
data[nTime][datasetName] = await fetchByNameDate(dataSourceRoute, datasetName, date, nTime)
|
645
|
}
|
646
|
})
|
647
|
}
|
648
|
|
649
|
loadingN()
|
650
|
}
|
651
|
|
652
|
function drawHeatmap (dataRaw) {
|
653
|
// Todo still switched
|
654
|
const dataDict = dataRaw
|
655
|
const mergedPoints = []
|
656
|
let max = 0
|
657
|
|
658
|
if (Object.keys(globalMarkersChanged).length) {
|
659
|
Object.keys(globalMarkersChanged).forEach(function (key) {
|
660
|
globalMarkersChanged[key][0].bindPopup(globalMarkersChanged[key][1])
|
661
|
})
|
662
|
globalMarkersChanged = {}
|
663
|
}
|
664
|
|
665
|
Object.keys(dataDict).forEach((key) => {
|
666
|
const data = dataDict[key]
|
667
|
max = Math.max(max, data.max)
|
668
|
|
669
|
if (data != null) {
|
670
|
// Bind back popups for markers (we dont know if there is any data for this marker or not)
|
671
|
const points = data.items.map((point) => {
|
672
|
const { x, y, number } = point
|
673
|
const key = x + '' + y
|
674
|
const holder = globalMarkersHolder[key]
|
675
|
if (!globalMarkersChanged[key] && number) {
|
676
|
// There is data for this marker => unbind popup with zero value
|
677
|
holder[0] = holder[0].unbindPopup()
|
678
|
globalMarkersChanged[key] = holder
|
679
|
}
|
680
|
|
681
|
return [x, y, number]
|
682
|
})
|
683
|
mergedPoints.push(...points)
|
684
|
} else {
|
685
|
if (heatmapLayer != null) {
|
686
|
mymap.removeLayer(heatmapLayer)
|
687
|
}
|
688
|
}
|
689
|
})
|
690
|
|
691
|
if (heatmapLayer != null) {
|
692
|
mymap.removeLayer(heatmapLayer)
|
693
|
}
|
694
|
|
695
|
if (mergedPoints.length) {
|
696
|
heatmapLayer = L.heatLayer(mergedPoints, { max: max, minOpacity: 0.5, radius: 35, blur: 30 }).addTo(mymap)
|
697
|
}
|
698
|
|
699
|
// timto vyresen bug #8191 - TODO: znamena to, ze muzeme smazat volani updatePopup() ve funkcich, kde se nejdriv vola drawHeatmap() a pak updatePopup()?
|
700
|
updatePopup()
|
701
|
}
|
702
|
|
703
|
/**
|
704
|
* Checks dataset availibility
|
705
|
* @param {string} route authority for datasets availibility checks
|
706
|
*/
|
707
|
// eslint-disable-next-line no-unused-vars
|
708
|
function checkDataSetsAvailability (route) {
|
709
|
$.ajax({
|
710
|
type: 'POST',
|
711
|
// Todo it might be good idea to change db collections format
|
712
|
url: route + '/' + currentDateToString(),
|
713
|
success: function (result) {
|
714
|
updateAvailableDataSets(result)
|
715
|
}
|
716
|
})
|
717
|
}
|
718
|
|
719
|
function updateAvailableDataSets (available) {
|
720
|
let leastOneOptionEnabled = false
|
721
|
|
722
|
$('#dropdown-dataset .dropdown-item').each(function () {
|
723
|
const input = $(this).find('input')
|
724
|
const inputVal = input[0].value
|
725
|
|
726
|
if (!(inputVal in available)) {
|
727
|
$(this).addClass('disabled')
|
728
|
$(input).prop('checked', false)
|
729
|
} else {
|
730
|
leastOneOptionEnabled = true
|
731
|
$(this).removeClass('disabled')
|
732
|
}
|
733
|
})
|
734
|
|
735
|
$('#btn-update-heatmap').prop('disabled', !leastOneOptionEnabled)
|
736
|
}
|
737
|
|
738
|
function formatDate (date) {
|
739
|
var day = String(date.getDate())
|
740
|
var month = String(date.getMonth() + 1)
|
741
|
|
742
|
if (day.length === 1) {
|
743
|
day = '0' + day
|
744
|
}
|
745
|
|
746
|
if (month.length === 1) {
|
747
|
month = '0' + month
|
748
|
}
|
749
|
|
750
|
return date.getFullYear() + '-' + month + '-' + day
|
751
|
}
|
752
|
|
753
|
// eslint-disable-next-line no-unused-vars
|
754
|
function initDatepicker (availableDatesSource) {
|
755
|
var availableDates = ''
|
756
|
|
757
|
$.ajax({
|
758
|
type: 'GET',
|
759
|
url: availableDatesSource,
|
760
|
success: function (result) {
|
761
|
availableDates = String(result).split(',')
|
762
|
}
|
763
|
}).then(function () {
|
764
|
$('#date').datepicker({
|
765
|
format: 'yyyy-mm-dd',
|
766
|
language: 'cs',
|
767
|
beforeShowDay: function (date) {
|
768
|
if (availableDates.indexOf(formatDate(date)) < 0) {
|
769
|
return { enabled: false, tooltip: 'Žádná data' }
|
770
|
} else {
|
771
|
return { enabled: true }
|
772
|
}
|
773
|
},
|
774
|
autoclose: true
|
775
|
})
|
776
|
})
|
777
|
}
|
778
|
|
779
|
function initLocationsMenu () {
|
780
|
const elmLocationsList = $('.locations')
|
781
|
const locationsDisplayClass = 'show'
|
782
|
|
783
|
if ($(window).width() <= 480) {
|
784
|
elmLocationsList.removeClass(locationsDisplayClass)
|
785
|
} else {
|
786
|
elmLocationsList.addClass(locationsDisplayClass)
|
787
|
}
|
788
|
}
|
789
|
|
790
|
function onDocumentReady () {
|
791
|
$('#dropdown-dataset').on('click', function (e) {
|
792
|
e.stopPropagation()
|
793
|
})
|
794
|
|
795
|
$('#btn-update-heatmap').prop('name', '')
|
796
|
changeCurrentTime()
|
797
|
changeCurrentDate()
|
798
|
onValueChangeRegister()
|
799
|
onArrowLeftRightKeysDownRegister()
|
800
|
}
|
801
|
|
802
|
const loadCheckboxDatasetNameData = () => {
|
803
|
datasetSelected = []
|
804
|
|
805
|
$('#dropdown-dataset .dropdown-item').each(function () {
|
806
|
const input = $(this).find('input')
|
807
|
const inputVal = input[0].value
|
808
|
|
809
|
if (input[0].checked) {
|
810
|
datasetSelected.push(inputVal)
|
811
|
}
|
812
|
|
813
|
datasetDictNameDisplayName[inputVal] = $(input).data('dataset-display-name')
|
814
|
})
|
815
|
}
|
816
|
|
817
|
const dragTimeline = () => {
|
818
|
const hourElemWidthPx = 26
|
819
|
|
820
|
const elem = $('#player-time')
|
821
|
const offset = elem.offset().left - elem.parent().offset().left
|
822
|
|
823
|
if (offset >= 0 && offset <= elem.parent().width()) {
|
824
|
const hour = Math.round(offset / hourElemWidthPx)
|
825
|
|
826
|
if (hour !== currentTime) {
|
827
|
elem.attr('class', 'time hour-' + hour)
|
828
|
$('#player-time span').html(formatTime(hour))
|
829
|
|
830
|
onChangeHour(hour)
|
831
|
}
|
832
|
}
|
833
|
}
|