-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
854 lines (699 loc) · 24.1 KB
/
script.js
File metadata and controls
854 lines (699 loc) · 24.1 KB
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
'use strict';
class Workout {
date = new Date();
id = (Date.now() + ``).slice(-10);
clicks = 0;
constructor(coords, distance, duration) {
this.coords = coords; //[lat,lng]
this.distance = distance; // in km
this.duration = duration; // in min
}
_setDescription(d) {
// prettier-ignore
if (d) {
this.date = d;
}
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
this.description = `${this.type[0].toUpperCase()}${this.type.slice(1)} on ${
months[this.date.getMonth()]
} ${this.date.getDate()}`;
}
click() {
this.clicks++;
}
}
class Running extends Workout {
type = `running`;
constructor(coords, distance, duration, cadence, desc = false) {
super(coords, distance, duration);
this.calcPace();
this._setDescription(desc);
this.cadence = cadence;
}
calcPace() {
// min/km
this.pace = this.duration / this.distance;
return this.pace;
}
}
class Cycling extends Workout {
type = `cycling`;
constructor(coords, distance, duration, elevationGain, desc = false) {
super(coords, distance, duration);
this.calcSpeed();
console.log(desc);
this._setDescription(desc);
this.elevationGain = elevationGain;
}
calcSpeed() {
// km/h
this.speed = this.distance / (this.duration / 60);
return this.speed;
}
}
// const run = new Running([35, -12], 5.2, 24, 178);
// const cycling = new Cycling([35, -12], 5.2, 24, 178);
// console.log(run, cycling);
//////////////////////////////////////////////////////////
// APPLICATION ARCHITECTURE
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 map = document.querySelector(`#map`);
const editForm = document.querySelector(`.editForm`);
const inputEditType = editForm.querySelector('.form__input--type');
const inputEditDistance = editForm.querySelector('.form__input--distance');
const inputEditDuration = editForm.querySelector('.form__input--duration');
const inputEditCadence = editForm.querySelector('.form__input--cadence');
const inputEditElevation = editForm.querySelector('.form__input--elevation');
const deletingForm = document.querySelector('.delete-confirmation');
const sortBy = document.querySelector('#sortBy');
const sortOptions = document.querySelector(`.sorting-options`);
class App {
#map;
#coords = [];
#mapZoomLevel = 13;
#mapEvent;
#workouts = [];
#workoutMarkers = [];
constructor() {
// Get user's position
this._getPosition();
// Get data from local storage
this._getLocalStorage();
this._checkWorkouts();
// Attach event handlers
form.addEventListener(`submit`, this._newWorkout.bind(this));
inputType.addEventListener(`change`, this._toggleElevationField);
editForm
.querySelector('.form__input--type')
.addEventListener(`change`, this._toggleEditFormElevationField);
editForm.addEventListener(`submit`, this._editWorkout.bind(this));
// Click event
document.body.addEventListener(`click`, this._clicksHandle.bind(this));
}
_getPosition() {
if (navigator.geolocation)
navigator.geolocation.getCurrentPosition(
this._loadMap.bind(this),
function () {
alert(`Could not get your position`);
}
);
}
_loadMap(position) {
const { latitude } = position.coords;
const { longitude } = position.coords;
// console.log(`https://www.google.com/maps/@${latitude},${longitude}`);
this.#coords = [latitude, longitude];
this.#map = L.map('map').setView(this.#coords, this.#mapZoomLevel);
L.tileLayer('https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png', {
attribution:
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).addTo(this.#map);
// Handling clicks on map
this.#map.on(`click`, this._showForm.bind(this));
// Render markers on map from local storage
this.#workouts.forEach(work => {
this._renderWorkoutMarker(work);
});
}
_showForm(mapE) {
this.#mapEvent = mapE;
form.classList.remove(`hidden`);
inputDistance.focus();
}
_hideForm() {
// Empty inputs
// prettier-ignore
inputDistance.value = inputDuration.value = inputCadence.value = inputElevation.value = ``;
form.style.display = `none`;
form.classList.add(`hidden`);
setTimeout(() => (form.style.display = `grid`), 1000);
}
_hideEditForm() {
// Empty inputs
// prettier-ignore
// inputDistance.value = inputDuration.value = inputCadence.value = inputElevation.value = ``;
editForm.style.display = `none`;
editForm.classList.add(`hidden`);
setTimeout(() => (editForm.style.display = `grid`), 1000);
}
_toggleElevationField() {
inputElevation.closest(`.form__row`).classList.toggle(`form__row--hidden`);
inputCadence.closest(`.form__row`).classList.toggle(`form__row--hidden`);
}
_toggleEditFormElevationField() {
editForm
.querySelector('.form__input--elevation')
.closest(`.form__row`)
.classList.toggle(`form__row--hidden`);
editForm
.querySelector('.form__input--cadence')
.closest(`.form__row`)
.classList.toggle(`form__row--hidden`);
}
//////////////////////////////////////////////////////////////
// When is toggle changing the value empty !!!!!!!!!!!!
//////////////////////////////////////////////////////////
_changeEditFormElevationField(e) {
if (e === `running`) {
editForm
.querySelector('.form__input--elevation')
.closest(`.form__row`)
.classList.add(`form__row--hidden`);
editForm
.querySelector('.form__input--cadence')
.closest(`.form__row`)
.classList.remove(`form__row--hidden`);
}
if (e === `cycling`) {
editForm
.querySelector('.form__input--elevation')
.closest(`.form__row`)
.classList.remove(`form__row--hidden`);
editForm
.querySelector('.form__input--cadence')
.closest(`.form__row`)
.classList.add(`form__row--hidden`);
}
}
// Instruction is displayed when is no workout or hid when is workout
_checkWorkouts() {
this.#workouts.length == 0
? document.querySelector(`.menu`).classList.add(`hidden`) &
document.querySelector(`.start`).classList.remove(`hidden`) &
document.querySelector(`.workouts`).classList.add(`hidden`)
: document.querySelector(`.menu`).classList.remove(`hidden`) &
document.querySelector(`.workouts`).classList.remove(`hidden`) &
document.querySelector(`.start`).classList.add(`hidden`);
if (sortBy.textContent != `date`) sortBy.textContent = `Date`;
}
_newWorkout(e) {
e.preventDefault();
const validInputs = (...inputs) =>
inputs.every(inp => Number.isFinite(inp));
const allPositive = (...inputs) => inputs.every(inp => inp > 0);
// Get data from form
const type = inputType.value;
const distance = +inputDistance.value;
const duration = +inputDuration.value;
const { lat, lng } = this.#mapEvent.latlng;
let workout;
// If workout running, create running object
if (type === `running`) {
const cadence = +inputCadence.value;
// Check if data is valid
if (
!validInputs(distance, duration, cadence) ||
!allPositive(distance, duration, cadence)
)
return alert(`Inputs have to be positive numbers!`);
workout = new Running([lat, lng], distance, duration, cadence);
}
// If workout cycling, create cycling object
if (type === `cycling`) {
const elevation = +inputElevation.value;
if (
!validInputs(distance, duration, elevation) ||
!allPositive(distance, duration)
)
return alert(`Inputs have to be positive numbers!`);
workout = new Cycling([lat, lng], distance, duration, elevation);
}
// Add new object to workout array
this.#workouts.push(workout);
// console.log(workout);
// Render workout on map as a marker
this._renderWorkoutMarker(workout);
// Render workout on list
this._renderWorkout(workout);
// Hide start description, form and clear input fields
this._checkWorkouts();
this._hideForm();
// Set local storage to all workout
this._setLocalStorage();
}
_renderWorkoutMarker(
workout,
autoPan = true,
newMarker = true,
editMarker = false
) {
// Display marker
const markerIcon = L.icon({
iconUrl: 'pin.png',
iconSize: [32, 32],
iconAnchor: [17, 3],
});
const mark = L.marker(workout.coords, { icon: markerIcon }).addTo(
this.#map
);
// console.log(mark);
const pop = L.popup({
maxWidth: 250,
minWidth: 100,
autoClose: false,
closeOnClick: false,
autoPan: autoPan,
className: `${workout.type}-popup`,
}).setContent(
`${workout.type === `running` ? `🏃♂️` : `🚴♀️`} ${workout.description}`
);
mark.bindPopup(pop).openPopup();
if (newMarker) this.#workoutMarkers.push(mark);
}
_renderWorkout(workout, addNew = true, update = false) {
let html = `
<li class="workout workout--${workout.type}" data-id="${workout.id}">
<h2 class="workout__title">${workout.description}</h2>
<div class="workout__head hidden">
<span class="edit__workout workout__icon">
✏
</span>
<span class="remove__workout workout__icon">
🗑
</span>
</div>
<div class="workout__details">
<span class="workout__icon">${
workout.type === `running` ? `🏃♂️` : `🚴♀️`
}</span>
<span class="workout__value">${workout.distance}</span>
<span class="workout__unit">km</span>
</div>
<div class="workout__details">
<span class="workout__icon">⏱</span>
<span class="workout__value">${workout.duration}</span>
<span class="workout__unit">min</span>
</div>
`;
if (workout.type === `running`)
html += `
<div class="workout__details">
<span class="workout__icon">⚡️</span>
<span class="workout__value">${workout.pace.toFixed(1)}</span>
<span class="workout__unit">min/km</span>
</div>
<div class="workout__details">
<span class="workout__icon">🦶🏼</span>
<span class="workout__value">${workout.cadence}</span>
<span class="workout__unit">spm</span>
</div>
`;
if (workout.type === `cycling`)
html += `
<div class="workout__details">
<span class="workout__icon">⚡️</span>
<span class="workout__value">${workout.speed.toFixed(1)}</span>
<span class="workout__unit">km/h</span>
</div>
<div class="workout__details">
<span class="workout__icon">⛰</span>
<span class="workout__value">${workout.elevationGain}</span>
<span class="workout__unit">m</span>
</div>
</li>
`;
if (addNew) {
containerWorkouts.insertAdjacentHTML('afterbegin', html);
}
if (update) {
// Remove the old workout and inserting the new one in the same position
const currentWorkout = document.querySelector(
`.workout[data-id="${workout.id}"]`
);
currentWorkout.style.display = 'none';
currentWorkout.insertAdjacentHTML('afterend', html);
currentWorkout.remove();
}
}
_renderEditForm(e) {
const workoutEl = e.target.closest(`.workout`);
// Identify workout
const workout = this.#workouts.find(
work => work.id === workoutEl.dataset.id
);
// Put in position the edit form and hid workout
editForm.style.top = `${
workoutEl.getBoundingClientRect().top -
containerWorkouts.getBoundingClientRect().top +
containerWorkouts.scrollTop
}px`;
if (workout.type === `running`) {
}
editForm.classList.remove(`hidden`);
workoutEl.classList.add(`edit`);
// Taking the attributes names from the object
const attributeKeys = Object.keys(workout);
attributeKeys.forEach(key => {
if (key == `type`) {
this._changeEditFormElevationField(workout[key]);
editForm.querySelector(`.form__input--${key}`).value = workout[key];
}
if (
key == `distance` ||
key == `duration` ||
key == `cadence` ||
key == `elevationGain`
) {
editForm
.querySelector(
`.form__input--${key == `elevationGain` ? `elevation` : key}`
)
.setAttribute(`value`, `${workout[key]}`);
}
});
setTimeout(() => editForm.classList.add('animated', 'active'), 0);
}
_editWorkout(e) {
e.preventDefault();
// Checking the inputs are valid
const validInputs = (...inputs) =>
inputs.every(inp => Number.isFinite(inp));
const allPositive = (...inputs) => inputs.every(inp => inp > 0);
let id;
function error() {
if (
!validInputs(distance, duration, cadence ? cadence : elevationGain) ||
cadence
? !allPositive(distance, duration, cadence)
: !allPositive(distance, duration)
)
return alert(`Inputs have to be positive numbers!`);
}
// Finding the workout what we editing
document.querySelectorAll(`.workout`).forEach(element => {
if (element.classList.contains(`edit`)) {
id = element.getAttribute(`data-id`);
}
element.classList.remove(`edit`);
});
const workoutIndex = this.#workouts.findIndex(workout => workout.id == id);
const workout = this.#workouts[workoutIndex];
// Get data from edit form
const type = inputEditType.value;
const distance = +inputEditDistance.value;
const duration = +inputEditDuration.value;
let cadence;
let elevationGain;
// Checking if the original type and the new type are the same and changing the values of the inputs
// if (workout.type == type) {
if (type == `running`) {
cadence = +inputEditCadence.value;
error();
this._updateWorkout(workout, distance, duration, cadence, false);
}
if (type == `cycling`) {
elevationGain = +inputEditElevation.value;
error();
this._updateWorkout(workout, distance, duration, false, elevationGain);
}
// }
// Keeping the date from previous workout
const newDate = new Date(Date.parse(workout.date));
let newWorkout;
// Checking if the original type and the new type are'nt the same and changing the values of the inputs
if (workout.type != type) {
if (workout.type === `running` && type === `cycling`) {
elevationGain = +inputEditElevation.value;
error();
newWorkout = new Cycling(
workout.coords,
distance,
duration,
elevationGain,
newDate
);
}
if (workout.type === `cycling` && type === `running`) {
cadence = +inputEditCadence.value;
error();
newWorkout = new Running(
workout.coords,
distance,
duration,
cadence,
newDate
);
}
// Keeping the id from previous workout
// newWorkout.date = workout.date;
newWorkout.id = workout.id;
console.log(newDate.getDate());
console.log(newWorkout);
// Replacing the old workout with the new workout in #workouts array
this.#workouts.splice(workoutIndex, 1, newWorkout);
// Replacing the old workout with the new values
this._renderWorkout(newWorkout, false, true);
// Updating the marker (markers array and marker element)
this._renderWorkoutMarker(newWorkout, true, false, true);
// Updating Local Storage
this._setLocalStorage();
}
this._hideEditForm();
}
_updateWorkout(workout, distance, duration, cadence, elevation) {
workout.distance = distance;
workout.duration = duration;
workout.cadence
? (workout.cadence = cadence)
: (workout.elevationGain = elevation);
workout.pace
? (workout.pace = duration / distance)
: (workout.speed = distance / (duration / 60));
// Replacing the old workout with the new values
this._renderWorkout(workout, false, true);
// Updating the marker (markers array and marker element)
this._renderWorkoutMarker(workout, true, false, true);
// Updating Local Storage
this._setLocalStorage();
}
_removeWorkout(e) {
// Identification of the workout that has to be deleted
const el = e.target.closest('.workout');
// Delete marker from Markers UI and workoutMarkers array
const workoutCoords = this.#workouts.find(
workout => workout.id === el.dataset.id
).coords;
const markerIndex = this.#workoutMarkers.findIndex(marker => {
return (
marker._latlng.lat === workoutCoords[0] &&
marker._latlng.lng === workoutCoords[1]
);
});
this.#map.removeLayer(this.#workoutMarkers[markerIndex]); // Delete from marker list
this.#workoutMarkers.splice(markerIndex, 1); // Delete from workoutMarkers Array
// Delete workout from workout Arrays
const index = this.#workouts.findIndex(
workout => workout.id === el.dataset.id
);
this.#workouts.splice(index, 1);
// Delete workout from list in UI
el.remove();
// Checking if the workout array is empty or not, If it is, this function will disable all menu links
this._checkWorkouts();
// Updating localStorage or resetting it if there are no more workouts
if (this.#workouts.length !== 0) {
this._setLocalStorage(); // Will overwrite the previous 'workout' item
} else {
localStorage.removeItem('workouts');
// Also, if we delete the last workout, the map should be positioned on user's initial coords
this.#map.setView(this.#coords, this.#mapZoomLevel, {
animate: true,
duration: 1.2,
});
}
}
_deleteAllWorkout() {
this.#workouts.splice(0);
// console.log(this.#workouts);
const deleteFn = function () {
// Delete all workouts from List
document
.querySelectorAll('.workout')
.forEach(workoutEl => workoutEl.remove());
// Delete all markers from Map
this.#workoutMarkers.forEach(marker => {
this.#map.removeLayer(marker);
});
// Delete all markers from workoutMarkers array
this.#workoutMarkers.splice(0);
// Positioning the map on the current location
this.#map.setView(this.#coords, this.#mapZoomLevel, {
animate: true,
duration: 1.2,
});
// After deleted all workouts the menu links should be disabled
this._checkWorkouts();
}.bind(this);
setTimeout(deleteFn, 600);
// Reset local Storage
localStorage.removeItem('workouts');
}
_sortOptions() {
document
.querySelector(`.sorting-menu__options`)
.querySelectorAll(`.menu__option`)
.forEach(element => {
element.classList.remove(`hide`);
});
if (sortBy.textContent == `Date`) {
document.querySelector(`#sort-date`).classList.add(`hide`);
}
if (sortBy.textContent == `Distant`) {
document.querySelector(`#sort-distant`).classList.add(`hide`);
}
if (sortBy.textContent == `Duration`) {
document.querySelector(`#sort-duration`).classList.add(`hide`);
}
sortOptions.classList.remove(`sorting-options--hidden`);
}
_sortWorkouts(e) {
document.querySelector(`#sort-${e}`).classList.add(`hide`);
sortOptions.classList.add(`sorting-options--hidden`);
}
_clicksHandle(e) {
// Selecting workout and move to popup on the map
if (e.target.closest(`.workout`)) {
e.preventDefault();
this._moveToPopup(e);
}
// Hiding option icons in workouts when click event is out of the list
if (!e.target.closest(`.workout`)) {
document.querySelectorAll(`.workout__head`).forEach(element => {
element.classList.add(`hidden`);
});
}
// Hiding form when it's open and if selecting a workout
if (!form.matches(`.hidden`)) {
if (
form !== e.target &&
!e.target.parentElement.closest(`.form`) &&
map !== e.target
) {
this._hideForm();
}
}
// Hiding editForm when click event happening outside of the form
if (!editForm.matches(`.hidden`)) {
if (
editForm !== e.target &&
!e.target.parentElement.closest(`.editForm`) &&
e.target == map
) {
document.querySelectorAll(`.workout`).forEach(element => {
element.classList.remove(`edit`);
});
this._hideEditForm();
}
}
// Handling menu functions
// Removing active class from the link if is active
document
.querySelectorAll('.menu__link.active')
.forEach(link => link.classList.remove('active'));
// Show All workout markers on the map from the menu
if (e.target.closest(`.menu__option--show`)) {
// e.preventDefault();
const group = new L.featureGroup(this.#workoutMarkers);
this.#map.fitBounds(group.getBounds());
}
// Adding active class to the link for highlighting the menu in use
if (e.target.closest('.menu__link'))
e.target.closest('.menu__link').classList.add('active');
// Reveal editing form
if (e.target.closest(`.edit__workout`)) {
e.preventDefault;
document.querySelectorAll(`.workout`).forEach(element => {
element.classList.remove(`edit`);
});
this._renderEditForm(e);
}
// Remove individual workout bottom
if (e.target.closest(`.remove__workout`)) {
this._removeWorkout(e);
}
// Showing the Delete confirmation form
if (e.target.closest('.menu__option--delete')) {
e.preventDefault();
deletingForm.classList.remove('delete-confirmation--hidden');
}
// Hiding the form if "Canceling" delete
if (e.target.closest(`.delete-confirmation__btn--no`)) {
deletingForm.classList.add('delete-confirmation--hidden');
}
// Hiding the form if "Yes" and delete the workouts
if (e.target.closest(`.delete-confirmation__btn--yes`)) {
deletingForm.classList.add('delete-confirmation--hidden');
this._deleteAllWorkout();
}
// Showing the Sorting options
if (e.target.closest('.menu__option--sort')) {
e.preventDefault();
this._sortOptions();
}
// Handling the Sorting options
if (
e.target.closest('#sort-date') ||
e.target.closest('#sort-distant') ||
e.target.closest('#sort-duration')
) {
e.preventDefault();
this._sortWorkouts(e.target.textContent.toLowerCase());
sortBy.textContent = e.target.textContent;
}
}
// Selecting workout
_moveToPopup(e) {
// Hiding option icons when click event is inside the list
e.target
.closest(`.workouts`)
.querySelectorAll(`.workout__head`)
.forEach(element => {
element.classList.add(`hidden`);
});
const workoutEl = e.target.closest(`.workout`);
// Unhide option icons when clicking on a unique workout
workoutEl.querySelector(`.workout__head`).classList.remove(`hidden`);
const workout = this.#workouts.find(
work => work.id === workoutEl.dataset.id
);
this.#map.setView(workout.coords, this.#mapZoomLevel, {
animate: true,
pan: { duration: 1 },
});
}
_setLocalStorage() {
localStorage.setItem(`workouts`, JSON.stringify(this.#workouts));
}
_getLocalStorage() {
const data = JSON.parse(localStorage.getItem(`workouts`));
if (!data) return;
this.#workouts = data;
this.#workouts.forEach(work => {
this._renderWorkout(work);
});
}
reset() {
localStorage.removeItem(`workouts`);
}
}
const app = new App();