-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
259 lines (247 loc) · 8.51 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
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
"use strict";
// prettier-ignore
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const form = document.querySelector(".form");
const containerWorkouts = document.querySelector(".workouts");
const inputType = document.querySelector(".form__input--type");
const inputDistance = document.querySelector(".form__input--distance");
const inputDuration = document.querySelector(".form__input--duration");
const inputCadence = document.querySelector(".form__input--cadence");
const inputElevation = document.querySelector(".form__input--elevation");
const mapDiv = document.querySelector("#map");
let coords;
const styles = getComputedStyle(document.documentElement);
const colorDark1 = styles.getPropertyValue("--color-dark--1");
const colorDark2 = styles.getPropertyValue("--color-dark--2");
const popups = [];
const markers = [];
let activePopup = undefined;
inputType.addEventListener("change", function () {
// inputElevation.closest(".form__row").classList.toggle("form__row--hidden");
// inputCadence.closest(".form__row").classList.toggle("form__row--hidden");
});
class App {
constructor() {
this.workouts = [];
this.map = new L.map("map");
this.activeWorkout = undefined;
this._getPosition();
this.popups = [];
}
_getPosition() {
if (!navigator.geolocation) return false;
navigator.geolocation.getCurrentPosition(
this._loadMap.bind(this),
function () {
console.log("geoloc problem");
}
);
}
_loadMap(geoloc) {
const { latitude, longitude } = geoloc.coords;
const position = [latitude, longitude];
L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution:
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).addTo(this.map);
this.map.setView(position, 13);
// add new workout by clicking on map
this.map.on(
"click",
function (e) {
// get clicked location
const { lat, lng } = e.latlng;
this.activeWorkout = this._newWorkout([lat, lng]);
App._showForm();
}.bind(this)
);
this.map.on("popupopen", function (e) {
const popup = e.popup;
myApp._setActive.bind(popup)();
popup
.getElement()
.addEventListener(
"click",
myApp._setActive.bind(myApp.activeWorkout.popup)
);
});
this.map.on("popupclose", myApp._hideForm);
}
_hideForm() {
// clear input fields and go back to map
inputCadence.value =
inputDistance.value =
inputDuration.value =
inputElevation.value =
"";
form.classList.add("hidden");
mapDiv.focus();
}
_newWorkout(location) {
const tempPopUp = new L.popup({
maxWidth: 250,
autoClose: false,
closeOnClick: false,
interactive: true,
className: "running-popup",
});
// set init text
// const content = L.DomUtil.create("p", "content");
// content.innerText = "New Workout 💪";
tempPopUp
.setContent("New Workout 💪")
.setLatLng(new L.latLng(location[0], location[1]));
// bring to front on hover
tempPopUp.on("mouseover", function (e) {
e.target.bringToFront();
});
tempPopUp.on("mouseout", function (e) {
myApp.activeWorkout.popup.bringToFront();
});
const tempWorkout = new Workout();
tempWorkout.popup = tempPopUp;
this.workouts.push(tempWorkout);
const tempMarker = L.marker(location, { riseOnHover: true })
.addTo(this.map)
.bindPopup(tempPopUp);
return tempWorkout;
}
_setActive() {
const popup = this;
// console.log(popup.getElement().children[0]);
App._showForm();
myApp.activeWorkout = myApp.workouts.find(
(workout) => workout.popup === popup
);
myApp.workouts.forEach((workout) => {
const popup = workout.popup;
if (popup.getElement()) {
popup.getElement().children[0].style.background = colorDark1;
popup.getElement().children[1].children[0].style.background =
colorDark1;
}
});
myApp.activeWorkout.setPopupColor(1);
}
static _showForm() {
form.classList.remove("hidden");
inputDistance.focus();
}
}
class Workout {
constructor(distance, duration, coords, popup) {
this.distance = distance;
this.duration = duration;
this.coords = coords;
this.date = new Date();
this.popup = popup;
this.type = undefined;
this.id = Math.random();
}
setPopupColor(color) {
this.popup.getElement().children[0].style.background =
color === 0 ? colorDark1 : colorDark2;
this.popup.getElement().children[1].children[0].style.background =
color === 0 ? colorDark1 : colorDark2;
}
setType(type) {
this.type = type;
if (type === "🚴♂️") {
this.popup.options.className = "cycling-popup";
this.popup.getElement().classList.add("cycling-popup");
this.popup.getElement().classList.remove("running-popup");
} else {
this.popup.options.className = "running-popup";
this.popup.getElement().classList.add("running-popup");
this.popup.getElement().classList.remove("cycling-popup");
}
}
}
class Running extends Workout {
constructor(distance, duration, coords, popup, cadence) {
super(distance, duration, coords, popup);
this.cadence = cadence;
}
}
class Cycling extends Workout {
constructor(distance, duration, coords, popup, elevationGain) {
super(distance, duration, coords, popup);
this.elevationGain = elevationGain;
this.calcSpeed();
}
calcSpeed() {
this.speed = this.distance / (this.duration / 60);
return this.speed;
}
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
const myApp = new App();
function updateActive() {
const popup = this;
App._showForm();
myApp.activeWorkout = myApp.workouts.find(
(workout) => workout.popup === popup
);
myApp.workouts.forEach((workout) => {
const popup = workout.popup;
if (popup.getElement()) {
popup.getElement().children[0].style.background = colorDark1;
popup.getElement().children[1].children[0].style.background = colorDark1;
}
});
myApp.activeWorkout.setPopupColor(1);
}
// ADDING/EDITING a WORKOUT(popup content)
form.addEventListener("submit", function (e) {
e.preventDefault();
// GET TYPE OF WORKOUT
const woType = inputType.value === "running" ? "🏃♂️" : "🚴♂️";
if (!+inputDistance.value || !+inputDuration.value) return;
// create content html element and read input
const content = L.DomUtil.create("p", "content");
content.innerText = `${woType} ${inputDistance.value} km ${inputDuration.value} min`;
myApp.activeWorkout.distance = Number(inputDistance.value);
myApp.activeWorkout.duration = Number(inputDuration.value);
// show workout info
myApp.activeWorkout.popup.openOn(myApp.map);
myApp.activeWorkout.popup.setContent(content);
myApp.activeWorkout.setPopupColor(0);
myApp.activeWorkout.setType(woType);
// add popup click event for later editing
const [_, month, day] = myApp.activeWorkout.date.toString().split(" ");
// console.log(month, +day);
containerWorkouts.insertAdjacentHTML(
"afterbegin",
`<li class="workout workout--${inputType.value}" data-id= ${
myApp.activeWorkout.id
}>
<h2 class="workout__title">${
inputType.value.charAt(0).toUpperCase() + inputType.value.slice(1)
} on ${[month, +day].join(" ")}</h2>
<div class="workout__details">
<span class="workout__icon">${woType}</span>
<span class="workout__value">${inputDistance.value}</span>
<span class="workout__unit">km</span>
</div>
<div class="workout__details">
<span class="workout__icon">⏱</span>
<span class="workout__value">${inputDuration.value}</span>
<span class="workout__unit">min</span>
</div>`
// <div class="workout__details">
// <span class="workout__icon">⚡️</span>
// <span class="workout__value">4.6</span>
// <span class="workout__unit">min/km</span>
// </div>
// <div class="workout__details">
// <span class="workout__icon">🦶🏼</span>
// <span class="workout__value">178</span>
// <span class="workout__unit">spm</span>
// </div>
// </li>`
);
// clear input fields and go back to map
myApp._hideForm();
});