-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
150 lines (132 loc) · 4.13 KB
/
script.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict';
// prettier-ignore
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const form = document.querySelector('.form');
const containerAlerts = document.querySelector('.warnings');
const inputRiskType = document.querySelector('.form__input--type');
const inputTime = document.querySelector('.form__input--time');
const inputIssue = document.querySelector('.form__input--issue');
const resetDataBtn = document.querySelector('#resetData');
const today = new Date();
let map, mapPosition;
const mapZoomLevel = 17;
const dataStore = [];
const loadMap = () => {
navigator.geolocation.getCurrentPosition(
function (position) {
const coords = [position.coords.latitude, position.coords.longitude];
map = L.map('map').setView(coords, mapZoomLevel);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution:
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).addTo(map);
map.on('click', function (mapPos) {
mapPosition = mapPos;
form.classList.remove('hidden');
});
const isLocalStrDataExist = _getAlertListFromLocalStr();
if (isLocalStrDataExist) {
dataStore.push(...isLocalStrDataExist);
dataStore.forEach(data => {
renderAlertsOnSideBar(data);
createSpotOnMap(data);
});
}
},
function () {
console.log('Could not get position');
}
);
};
const createSpotOnMap = alertData => {
inputTime.value = inputIssue.value = '';
inputTime.value = Date.now().toString();
L.marker([...alertData.coords])
.addTo(map)
.bindPopup(
L.popup({
maxWidth: 250,
minWidth: 100,
autoClose: false,
closeOnClick: false,
className: alertData.risk === 'high' ? 'high-popup' : 'mediate-popup',
})
)
.setPopupContent(
`
${alertData.risk} - ${alertData.timeSlot}
Description: ${alertData.issue}
`
)
.openPopup();
};
const renderAlertsOnSideBar = alert => {
let html = `
<li class="warning warning--${alert.risk}" data-id="${alert.id}">
<h2 class="warning__title">👉${alert.issue}</h2>
<div class="warning__details">
<span class="warning__icon">${
alert.risk === 'high' ? '🔴 High' : '🚫Mid'
}</span>
<span class="warning__value">⏱${alert.timeSlot}</span>
</div>
</li>
`;
form.insertAdjacentHTML('afterend', html);
};
const moveToWarningPointer = e => {
const warningEl = e.target.closest('.warning');
if (warningEl === null) return;
const warning = dataStore.find(w => w.id == warningEl.dataset.id);
map.setView(warning.coords, mapZoomLevel, {
animate: true,
pan: {
duration: 1,
},
});
};
const _storeDataInLocalStr = alertList => {
localStorage.setItem('alertList', JSON.stringify(alertList));
};
const _getAlertListFromLocalStr = () => {
const data = JSON.parse(localStorage.getItem('alertList'));
if (!data) return;
return data;
};
const _resetData = () => {
localStorage.removeItem('alertList');
window.location.reload();
};
form.addEventListener('submit', function (event) {
event.preventDefault();
inputTime.value = `${today.getDate()}/${today.getMonth()}/${today.getFullYear()}`;
if (
inputRiskType.value === '' ||
inputIssue.value === '' ||
inputTime.value === ''
) {
alert('Please fill all info');
return;
}
const locationData = {
id: Date.now(),
risk: inputRiskType.value,
timeSlot: `${today.getDate()}/${today.getMonth()}/${today.getFullYear()}`,
issue: inputIssue.value,
dateAndTime: Date.now(),
coords: [mapPosition.latlng.lat, mapPosition.latlng.lng],
};
dataStore.push(locationData);
_storeDataInLocalStr(dataStore);
createSpotOnMap(locationData);
renderAlertsOnSideBar(locationData);
form.classList.add('hidden');
});
containerAlerts.addEventListener('click', moveToWarningPointer);
resetDataBtn.addEventListener('click', _resetData);
const runApplication = () => {
if (navigator.geolocation) {
loadMap();
}
};
runApplication();