-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
191 lines (150 loc) · 5.98 KB
/
script.js
File metadata and controls
191 lines (150 loc) · 5.98 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
'use strict';
// prettier-ignore
//GLOBAL VARIABLES
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
//-------------------------------
//DOM Elements
//-------------------------------
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');
//-------------------------------
// Managing workout data
//-------------------------------
class Workout {
date = new Date();
//Usually we would generate id using a library but for simplicity we will use this method
id = (Date.now() + Math.random().toString().slice(2, 6) + '').slice(-10);
constructor(coords, distance, duration) {
this.coords = coords; // [lat, lng]
this.distance = distance; // in km
this.duration = duration; // in min
}
}
class Running extends Workout {
constructor(coords, distance, duration, cadence) {
super(coords, distance, duration); //initializing the parent class or this keyword
this.cadence = cadence;
this.calcPace(); //We call the method here to set the pace property when a new object is created
}
calcPace() {
//min/km
this.pace = this.duration / this.distance;
return this.pace;
}
};
class Cycling extends Workout {
constructor(coords, distance, duration, elevationGain) {
super(coords, distance, duration);
this.elevationGain = elevationGain;
this.calcSpeed();
}
calcSpeed() {
// km/h
this.speed = this.distance / (this.duration / 60); // convert min to hours (dividing by 60)
return this.speed;
}
};
//-------------------------------
// App Architecture
//-------------------------------
class App {
#map;
#mapEvent;
#workouts = [];
constructor() {
//Get user's position
this._getPosition();
//Attach event handlers
form.addEventListener('submit', this._newWorkout.bind(this));
inputType.addEventListener('change', this._toggleElevationField.bind(this));
};
_getPosition() {
if (navigator.geolocation) {
//We have to bind this to the _loadMap method because otherwise this would point to the global object. Returning undefined in strict mode...
navigator.geolocation.getCurrentPosition(
this._loadMap.bind(this), function () {
alert('Could not get your position');
})
};
};
_loadMap(position) {
const { latitude } = position.coords;
const { longitude } = position.coords;
const coords = [latitude, longitude];
this.#map = L.map('map').setView(coords, 13);
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);
//Displaying a Map Marker
//We are adding an event listener to the map object
this.#map.on('click', this._showForm.bind(this));
};
_showForm(mapE) {
this.#mapEvent = mapE;
//Display form
form.classList.remove('hidden');
inputDistance.focus();
};
_toggleElevationField() {
//We are using closest to go up in the DOM tree to find the parent element with the class form__row
inputCadence.closest('.form__row').classList.toggle('form__row--hidden');
inputElevation.closest('.form__row').classList.toggle('form__row--hidden');
};
_newWorkout(e) {
//Helpers functions
const validInputs = (...inputs) =>
inputs.every(inp => Number.isFinite(inp));
const allPositive = (...inputs) => inputs.every(inp => inp > 0);
e.preventDefault();
// Get data from form
const type = inputType.value;
const distance = +inputDistance.value; //The + operator converts string to number since input values are always strings
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 (
// !Number.isFinite(distance) ||
// !Number.isFinite(duration) ||
// !Number.isFinite(cadence)
!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);
// Render workout on map as marker
this._renderWorkoutMarker(workout);
// Render workout on list
this._renderWorkout(workout);
// Hide form + clear input fields
this._hideForm();
// Set local storage to all workouts
this._setLocalStorage();
}
}
//-------------------------------
//Create and object of the App class
//This will automatically call the constructor method
//-------------------------------
const app = new App();