-
Notifications
You must be signed in to change notification settings - Fork 0
/
unitconverter.html
251 lines (223 loc) · 8.2 KB
/
unitconverter.html
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
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<title>Unit converters</title>
</head>
<body onload="initConverters()">
<div class="container">
<div class="row py-4">
<div class="col">
<h1 class="display-6 text-center" id="caption">Unit converters</h1>
</div>
</div>
<div class="row justify-content-center align-items-center">
<div class="col-auto">
<form>
<div class="row mb-3">
<label class="form-label" for="degreesCentigrade">Temperature:</label>
<div class="col">
<div class="input-group">
<input type="number" class="form-control" id="degreesCentigrade">
<span class="input-group-text">°C</span>
</div>
</div>
<div class="col">
<div class="input-group">
<input type="number" class="form-control" id="degreesFahrenheit">
<span class="input-group-text">°F</span>
</div>
</div>
</div>
</form>
<form>
<div class="row mb-3">
<label class="form-label" for="hectoPascal">Pressure:</label>
<div class="col">
<div class="input-group">
<input type="number" class="form-control" id="hectoPascal">
<span class="input-group-text">hPa</span>
</div>
</div>
<div class="col">
<div class="input-group">
<input type="number" class="form-control" id="inchesHg">
<span class="input-group-text">in Hg</span>
</div>
</div>
</div>
</form>
<form>
<div class="row mb-3">
<label class="form-label" for="liter">Volume:</label>
<div class="col">
<div class="input-group">
<input type="number" class="form-control" id="liter">
<span class="input-group-text">l</span>
</div>
</div>
<div class="col">
<div class="input-group">
<input type="number" class="form-control" id="usGallons">
<span class="input-group-text">US-gal</span>
</div>
</div>
</div>
</form>
<form>
<div class="row mb-3">
<label class="form-label" for="kilogram">Mass:</label>
<div class="col">
<div class="input-group">
<input type="number" class="form-control" id="kilogram">
<span class="input-group-text">kg</span>
</div>
</div>
<div class="col">
<div class="input-group">
<input type="number" class="form-control" id="pounds">
<span class="input-group-text">lbs</span>
</div>
</div>
</div>
</form>
<form>
<div class="row mb-3">
<label class="form-label" for="kilometers">Distance:</label>
<div class="col">
<div class="input-group">
<input type="number" class="form-control" id="kilometers">
<span class="input-group-text">km</span>
</div>
</div>
<div class="col">
<div class="input-group">
<input type="number" class="form-control" id="nauticalMiles">
<span class="input-group-text">NM</span>
</div>
</div>
</div>
</form>
<form>
<div class="row mb-3">
<label class="form-label" for="meters">Height:</label>
<div class="col">
<div class="input-group">
<input type="number" class="form-control" id="meters">
<span class="input-group-text">m</span>
</div>
</div>
<div class="col">
<div class="input-group">
<input type="number" class="form-control" id="feet">
<span class="input-group-text">ft</span>
</div>
</div>
</div>
</form>
<form>
<div class="row mb-3">
<label class="form-label" for="kilometersPerHour">Speed:</label>
<div class="col">
<div class="input-group">
<input type="number" class="form-control" id="kilometersPerHour">
<span class="input-group-text">km/h</span>
</div>
</div>
<div class="col">
<div class="input-group">
<input type="number" class="form-control" id="knots">
<span class="input-group-text">kts</span>
</div>
</div>
</div>
</form>
<form>
<div class="row mb-3">
<label class="form-label" for="metersPerSecond">Vertical speed:</label>
<div class="col">
<div class="input-group">
<input type="number" class="form-control" id="metersPerSecond">
<span class="input-group-text">m/s</span>
</div>
</div>
<div class="col">
<div class="input-group">
<input type="number" class="form-control" id="feetPerMinute">
<span class="input-group-text">ft/min</span>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript">
"use strict";
function initConverters() {
const converters = [
new UnitConverter("degreesCentigrade", "degreesFahrenheit", UnitConverter.C_to_F, UnitConverter.F_to_C, 1, 1),
new UnitConverter("hectoPascal", "inchesHg", UnitConverter.hPa_to_inHg, UnitConverter.inHg_to_hPa),
new UnitConverter("liter", "usGallons", UnitConverter.l_to_usgal, UnitConverter.usgal_to_l, 1, 1),
new UnitConverter("kilogram", "pounds", UnitConverter.kg_to_lbs, UnitConverter.lbs_to_kg, 1, 1),
new UnitConverter("kilometers", "nauticalMiles", UnitConverter.km_to_NM, UnitConverter.NM_to_km),
new UnitConverter("meters", "feet", UnitConverter.m_to_ft, UnitConverter.ft_to_m),
new UnitConverter("kilometersPerHour", "knots", UnitConverter.km_to_NM, UnitConverter.NM_to_km, 0, 0),
new UnitConverter("metersPerSecond", "feetPerMinute", UnitConverter.ms_to_fpm, UnitConverter.fpm_to_ms, 2, 0),
];
}
Object.prototype.getElementValue = function() {
const invalidClassName = "is-invalid";
if (this.checkValidity()) {
this.classList.remove(invalidClassName);
}
else {
this.classList.add(invalidClassName);
}
const value = parseFloat(this.value);
return isNaN(value) ? 0.0 : value;
}
Object.prototype.setElementValue = function(value, decimalPlaces) {
const x = Math.pow(10, decimalPlaces);
this.value = Math.round(x * value) / x;
}
class UnitConverter {
constructor(domElementIdA, domElementIdB, conversionA2B, conversionB2A, precisionA, precisionB) {
const _defaultPrecision = 2;
const _precisionA = (precisionA == null) ? _defaultPrecision : precisionA;
const _precisionB = (precisionB == null) ? _defaultPrecision : precisionB;
this.elementA = document.getElementById(domElementIdA);
this.elementB = document.getElementById(domElementIdB);
if (conversionA2B == null || conversionB2A == null || this.elementA == null || this.elementB == null) {
console.log("initialization error");
return;
}
this.elementA.oninput = () => {
const a = this.elementA.getElementValue();
this.elementB.setElementValue(conversionA2B(a), _precisionB);
}
this.elementB.oninput = () => {
const b = this.elementB.getElementValue();
this.elementA.setElementValue(conversionB2A(b), _precisionA);
}
}
static C_to_F(x) { return x * 1.8 + 32; }
static F_to_C(x) { return (x - 32) / 1.8; }
static hPa_to_inHg(x) { return x * (76000 / 25.4) / 101325; }
static inHg_to_hPa(x) { return x / (76000 / 25.4) * 101325; }
static l_to_usgal(x) { return x * 0.2642; }
static usgal_to_l(x) { return x * 3.785; }
static kg_to_lbs(x) { return x * 2.2046; }
static lbs_to_kg(x) { return x * 0.4536; }
static km_to_NM(x) { return x / 1.852; }
static NM_to_km(x) { return x * 1.852; }
static m_to_ft(x) { return x / 0.3048; }
static ft_to_m(x) { return x * 0.3048; }
static ms_to_fpm(x) { return 60 * x / 0.3048; }
static fpm_to_ms(x) { return x / 60 * 0.3048; }
}
</script>
</body>
</html>