Repository navigation
Expand file tree
/
Copy pathconverter.html
More file actions
703 lines (603 loc) · 23.2 KB
/
Copy pathconverter.html
File metadata and controls
703 lines (603 loc) · 23.2 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
<meta charset="UTF-8">
<title>Converter</title>
<style>
/* SYSTEM 7 AESTHETICS */
:root {
--bg-color: #ffffff;
--text-color: #000000;
--border: 2px solid #000000;
--shadow: 4px 4px 0px #000000;
--stripe-pattern: repeating-linear-gradient(0deg, transparent, transparent 2px, #000 3px, #000 4px);
}
body {
image-rendering: -webkit-crisp-edges;
image-rendering: -moz-crisp-edges;
image-rendering: -o-crisp-edges;
image-rendering: pixelated;
image-rendering: crisp-edges;
-ms-interpolation-mode: nearest-neighbor;
background-repeat: repeat;
background-position: top left;
image-rendering: -webkit-optimize-contrast;
image-rendering: pixelated;
image-rendering: crisp-edges;
background-repeat: repeat;
background-position: top left;
image-rendering: pixelated;
image-rendering: crisp-edges;
background-repeat: repeat;
background-position: top left;
font-family: "Geneva", "Verdana", sans-serif;
margin: 0;
padding: 20px;
height: 100vh;
overflow: hidden;
background-color: #e5e5e5;
/* Background managed by settings */
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
user-select: none;
box-sizing: border-box;
}
/* WINDOW FRAME */
.window {
background: white;
border: 2px solid black;
box-shadow: var(--shadow);
display: flex;
flex-direction: column;
min-height: auto;
max-min-height: 90vh;
width: 100%;
max-width: 500px;
/* Phone/Tablet friendly width */
position: relative;
}
/* TITLE BAR */
.title-bar {
height: 35px;
border-bottom: 2px solid black;
display: flex;
align-items: center;
justify-content: center;
background: white;
padding: 0 10px;
position: relative;
flex-shrink: 0;
}
.title-stripes {
position: absolute;
top: 4px;
bottom: 4px;
left: 4px;
right: 4px;
background-image: var(--stripe-pattern);
z-index: 0;
}
.title-text {
background: white;
padding: 0 15px;
font-weight: bold;
z-index: 1;
font-size: 1.1rem;
display: inline-flex;
align-items: center;
height: 100%;
box-sizing: border-box;
}
/* CLOSE BUTTON */
.close-box {
position: absolute;
left: 10px;
width: 18px;
height: 18px;
border: 2px solid black;
background: white;
z-index: 2;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
font-family: sans-serif;
font-size: 0.9rem;
line-height: 1;
box-shadow: 2px 2px 0 black;
}
.close-box:active {
background: black;
color: white;
transform: translate(1px, 1px);
box-shadow: none;
}
/* CATEGORY DROPDOWN (In Title Bar) */
.title-bar .custom-select-container {
position: absolute;
right: 8px;
top: 50%;
transform: translateY(-50%);
width: 120px;
z-index: 10;
height: 26px;
}
.title-bar .custom-select-trigger {
height: 26px;
line-height: 24px;
padding: 0 5px;
font-size: 0.8rem;
background: white;
border: 2px solid black;
box-shadow: 2px 2px 0 black;
}
.title-bar .custom-options {
top: 100%;
right: 0;
left: auto;
/* Align right */
width: 140px;
margin-top: 4px;
border: 2px solid black;
box-shadow: 4px 4px 0 black;
}
/* CONTENT AREA */
.window-content {
flex-grow: 1;
padding: 20px;
background: white;
display: flex;
flex-direction: column;
overflow-y: auto;
}
.window-content>*+* {
margin-top: 15px;
}
/* CONVERSION ROW */
.conv-row {
display: flex;
flex-direction: column;
background: #f5f5f5;
padding: 10px;
border: 2px solid black;
}
.conv-row>*+* {
margin-top: 5px;
}
.input-group {
display: flex;
}
.input-group>*+* {
margin-left: 10px;
}
.value-display {
flex: 1;
background: white;
border: 2px solid black;
padding: 10px;
font-family: "Courier New", monospace;
font-size: 1.5rem;
font-weight: bold;
text-align: right;
min-height: 24px;
overflow: hidden;
}
.value-display.result {
background: #eee;
/* Read-only look */
}
select {
flex: 1;
border: 2px solid black;
font-family: inherit;
font-weight: bold;
font-size: 1rem;
background: white;
padding: 5px;
border-radius: 0;
}
.swap-btn {
align-self: center;
background: white;
border: 2px solid black;
border-radius: 50%;
width: 30px;
height: 30px;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.2rem;
cursor: pointer;
box-shadow: 2px 2px 0 black;
margin: 5px 0;
}
.swap-btn:active {
transform: translate(1px, 1px);
box-shadow: none;
background: black;
color: white;
}
/* KEYPAD */
.keypad {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
margin-top: auto;
}
.key {
background: white;
border: 2px solid black;
height: 50px;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.5rem;
font-weight: bold;
cursor: pointer;
box-shadow: 3px 3px 0 black;
border-radius: 4px;
}
.key:active {
background: black;
color: white;
box-shadow: none;
transform: translate(2px, 2px);
}
.key.action {
background: #ddd;
}
.key.clear {
background: black;
color: white;
}
</style>
<link rel="stylesheet" href="css/custom-select.css">
<script src="js/i18n.js?v=3"></script>
<script src="theme.js?v=21"></script>
<script src="js/custom-select.js?v=2"></script>
</head>
<body>
<div class="window">
<div class="title-bar">
<div class="title-stripes"></div>
<div class="close-box" onclick="window.location.href='index'">X</div>
<span class="title-text" data-i18n="converter.title">Converter</span>
<select id="category-select" onchange="setCategory(this.value)">
<option value="length" data-i18n="converter.tab.length">Length</option>
<option value="mass" data-i18n="converter.tab.mass">Mass</option>
<option value="temp" data-i18n="converter.tab.temp">Temp</option>
<option value="speed" data-i18n="converter.tab.speed">Speed</option>
<option value="volume" data-i18n="converter.tab.volume">Volume</option>
<option value="currency" data-i18n="converter.tab.currency">Currency</option>
</select>
</div>
<div class="window-content">
<!-- INPUT -->
<div class="conv-row">
<div class="input-group">
<div class="value-display" id="input-display" onclick="focusInput()">0</div>
</div>
<div class="input-group">
<select id="unit-from" onchange="convert()"></select>
</div>
</div>
<div class="swap-btn" onclick="swapUnits()">⇅</div>
<!-- OUTPUT -->
<div class="conv-row">
<div class="input-group">
<div class="value-display result" id="output-display">0</div>
</div>
<div class="input-group">
<select id="unit-to" onchange="convert()"></select>
</div>
</div>
<!-- KEYPAD -->
<div class="keypad">
<div class="key" onclick="pressNum('7')">7</div>
<div class="key" onclick="pressNum('8')">8</div>
<div class="key" onclick="pressNum('9')">9</div>
<div class="key action" onclick="pressBack()">⌫</div>
<div class="key" onclick="pressNum('4')">4</div>
<div class="key" onclick="pressNum('5')">5</div>
<div class="key" onclick="pressNum('6')">6</div>
<div class="key clear" onclick="pressClear()">C</div>
<div class="key" onclick="pressNum('1')">1</div>
<div class="key" onclick="pressNum('2')">2</div>
<div class="key" onclick="pressNum('3')">3</div>
<div class="key action" onclick="toggleSign()">±</div>
<div class="key" onclick="pressNum('0')" style="grid-column: span 2;">0</div>
<div class="key" onclick="pressNum('.')">.</div>
</div>
</div>
</div>
<script>
// --- CONFIGURATION ---
const DEFINITIONS = {
length: {
base: 'm',
units: {
'mm': { name: 'Millimeters', factor: 0.001 },
'cm': { name: 'Centimeters', factor: 0.01 },
'm': { name: 'Meters', factor: 1 },
'km': { name: 'Kilometers', factor: 1000 },
'in': { name: 'Inches', factor: 0.0254 },
'ft': { name: 'Feet', factor: 0.3048 },
'yd': { name: 'Yards', factor: 0.9144 },
'mi': { name: 'Miles', factor: 1609.34 }
}
},
mass: {
base: 'g',
units: {
'mg': { name: 'Milligrams', factor: 0.001 },
'g': { name: 'Grams', factor: 1 },
'kg': { name: 'Kilograms', factor: 1000 },
'oz': { name: 'Ounces', factor: 28.3495 },
'lb': { name: 'Pounds', factor: 453.592 },
'st': { name: 'Stone', factor: 6350.29 }
}
},
temp: {
type: 'temp', // Special handling
units: {
'c': { name: 'Celsius' },
'f': { name: 'Fahrenheit' },
'k': { name: 'Kelvin' }
}
},
speed: {
base: 'mps',
units: {
'mps': { name: 'm/s', factor: 1 },
'kph': { name: 'km/h', factor: 0.277778 },
'mph': { name: 'mph', factor: 0.44704 },
'kn': { name: 'Knots', factor: 0.514444 }
}
},
volume: {
base: 'l',
units: {
'ml': { name: 'Milliliters', factor: 0.001 },
'l': { name: 'Liters', factor: 1 },
'tsp': { name: 'Teaspoons', factor: 0.00492892 },
'tbsp': { name: 'Tablespoons', factor: 0.0147868 },
'cup': { name: 'Cups', factor: 0.236588 },
'floz': { name: 'Fluid Oz', factor: 0.0295735 },
'gal': { name: 'Gallons', factor: 3.78541 }
}
},
currency: {
type: 'currency',
base: 'USD',
units: {
'USD': { name: 'US Dollar', factor: 1 },
'EUR': { name: 'Euro', factor: 0.85 }, // Default fallback
'GBP': { name: 'British Pound', factor: 0.75 },
'JPY': { name: 'Japanese Yen', factor: 110 },
'CAD': { name: 'Canadian Dollar', factor: 1.25 },
'AUD': { name: 'Australian Dollar', factor: 1.35 },
'CNY': { name: 'Chinese Yuan', factor: 6.45 },
'INR': { name: 'Indian Rupee', factor: 74.5 },
'MXN': { name: 'Mexican Peso', factor: 20 },
'BRL': { name: 'Brazilian Real', factor: 5.2 },
'RUB': { name: 'Russian Ruble', factor: 73 },
'KRW': { name: 'South Korean Won', factor: 1150 },
'CHF': { name: 'Swiss Franc', factor: 0.92 },
'SEK': { name: 'Swedish Krona', factor: 8.6 },
'NOK': { name: 'Norwegian Krone', factor: 8.8 },
'NZD': { name: 'New Zealand Dollar', factor: 1.4 },
'TRY': { name: 'Turkish Lira', factor: 8.5 },
'ZAR': { name: 'South African Rand', factor: 14.5 },
'HKD': { name: 'Hong Kong Dollar', factor: 7.78 },
'SGD': { name: 'Singapore Dollar', factor: 1.36 }
}
}
};
// --- STATE ---
let currentCategory = 'length';
let inputValue = "0";
// --- INIT ---
window.onload = () => {
if (window.rekindleApplyWallpaper) window.rekindleApplyWallpaper();
setCategory('length');
};
// --- LOGIC ---
function setCategory(cat) {
currentCategory = cat;
if (cat === 'currency') {
fetchRates();
}
// Update Dropdown (if changed programmatically)
const catSelect = document.getElementById('category-select');
if (catSelect.value !== cat) {
catSelect.value = cat;
// Dispatch change to update CustomSelect UI if it exists
catSelect.dispatchEvent(new Event('change'));
}
// Populate Dropdowns
const def = DEFINITIONS[cat];
const units = def.units;
const fromSel = document.getElementById('unit-from');
const toSel = document.getElementById('unit-to');
fromSel.innerHTML = '';
toSel.innerHTML = '';
let keys = Object.keys(units);
keys.forEach(key => {
const opt1 = document.createElement('option');
opt1.value = key;
opt1.innerText = window.t('converter.unit.' + key, units[key].name);
fromSel.appendChild(opt1);
const opt2 = document.createElement('option');
opt2.value = key;
opt2.innerText = window.t('converter.unit.' + key, units[key].name);
toSel.appendChild(opt2);
});
// Set defaults based on Global Unit System
let defFrom = '';
let defTo = '';
const isImperial = window.rekindleGetUnitSystem && window.rekindleGetUnitSystem() === 'imperial';
if (isImperial) {
if (cat === 'length') { defFrom = 'ft'; defTo = 'm'; }
else if (cat === 'mass') { defFrom = 'lb'; defTo = 'kg'; }
else if (cat === 'temp') { defFrom = 'f'; defTo = 'c'; }
else if (cat === 'speed') { defFrom = 'mph'; defTo = 'kph'; }
else if (cat === 'volume') { defFrom = 'gal'; defTo = 'l'; }
} else {
// Metric Defaults
if (cat === 'length') { defFrom = 'm'; defTo = 'ft'; }
else if (cat === 'mass') { defFrom = 'kg'; defTo = 'lb'; }
else if (cat === 'temp') { defFrom = 'c'; defTo = 'f'; }
else if (cat === 'speed') { defFrom = 'kph'; defTo = 'mph'; }
else if (cat === 'volume') { defFrom = 'l'; defTo = 'gal'; }
}
if (cat === 'currency') { defFrom = 'USD'; defTo = 'EUR'; }
// Fallback if specific units not found in category (though they should be there)
if (units[defFrom]) fromSel.value = defFrom;
else fromSel.selectedIndex = 0;
if (units[defTo]) toSel.value = defTo;
else toSel.selectedIndex = (keys.length > 1) ? 1 : 0;
convert();
}
function pressNum(num) {
if (inputValue === "0" && num !== ".") {
inputValue = num;
} else {
// Prevent multiple decimals
if (num === "." && inputValue.includes(".")) return;
// Max length check
if (inputValue.length > 10) return;
inputValue += num;
}
updateDisplay();
}
function pressBack() {
if (inputValue.length > 1) {
inputValue = inputValue.slice(0, -1);
} else {
inputValue = "0";
}
updateDisplay();
}
function pressClear() {
inputValue = "0";
updateDisplay();
}
function toggleSign() {
if (inputValue === "0") return;
if (inputValue.startsWith("-")) {
inputValue = inputValue.substring(1);
} else {
inputValue = "-" + inputValue;
}
updateDisplay();
}
function updateDisplay() {
document.getElementById('input-display').innerText = inputValue;
convert();
}
function convert() {
const val = parseFloat(inputValue);
if (isNaN(val)) return;
const fromUnit = document.getElementById('unit-from').value;
const toUnit = document.getElementById('unit-to').value;
let result = 0;
const def = DEFINITIONS[currentCategory];
if (def.type === 'temp') {
result = convertTemp(val, fromUnit, toUnit);
} else if (def.type === 'currency') {
// Currency Rates are "Units per Base"
// Formula: (Value / FromRate) * ToRate
const fromFactor = def.units[fromUnit].factor;
const toFactor = def.units[toUnit].factor;
result = (val / fromFactor) * toFactor;
} else {
// Standard factor conversion
// Convert to Base -> Convert to Target
const baseVal = val * def.units[fromUnit].factor;
result = baseVal / def.units[toUnit].factor;
}
// Formatting
let displayResult = parseFloat(result.toFixed(6));
// Remove trailing zeros if needed implicitly by parseFloat
document.getElementById('output-display').innerText = displayResult;
}
function convertTemp(val, from, to) {
if (from === to) return val;
// Convert to Celsius first
let c = val;
if (from === 'f') c = (val - 32) * 5 / 9;
if (from === 'k') c = val - 273.15;
// Convert from Celsius to Target
if (to === 'c') return c;
if (to === 'f') return (c * 9 / 5) + 32;
if (to === 'k') return c + 273.15;
return 0;
}
function swapUnits() {
const fromSel = document.getElementById('unit-from');
const toSel = document.getElementById('unit-to');
const temp = fromSel.value;
fromSel.value = toSel.value;
toSel.value = temp;
// Trigger change for UI updates (CustomSelect) and auto-conversion
fromSel.dispatchEvent(new Event('change'));
toSel.dispatchEvent(new Event('change'));
}
// --- CURRENCY FETCHING ---
async function fetchRates() {
const CACHE_KEY = 'rekindle_currency_rates';
const CACHE_TIME_KEY = 'rekindle_currency_time';
const CACHE_DURATION = 24 * 60 * 60 * 1000; // 24 hours
const now = Date.now();
const cachedTime = localStorage.getItem(CACHE_TIME_KEY);
const cachedRates = localStorage.getItem(CACHE_KEY);
if (cachedRates && cachedTime && (now - cachedTime < CACHE_DURATION)) {
console.log("Using cached currency rates");
updateCurrencyDefinitions(JSON.parse(cachedRates));
return;
}
console.log("Fetching fresh currency rates...");
try {
// Using open.er-api.com which is free and supports CORS
const response = await fetch('https://open.er-api.com/v6/latest/USD');
const data = await response.json();
if (data && data.rates) {
localStorage.setItem(CACHE_KEY, JSON.stringify(data.rates));
localStorage.setItem(CACHE_TIME_KEY, now);
updateCurrencyDefinitions(data.rates);
// Refresh if currently on currency tab
if (currentCategory === 'currency') {
convert();
}
}
} catch (e) {
console.error("Failed to fetch rates", e);
// Fallback to hardcoded defaults in DEFINITIONS
}
}
function updateCurrencyDefinitions(rates) {
const currencyDef = DEFINITIONS['currency'].units;
for (const [code, rate] of Object.entries(rates)) {
// If we have a definition for this code, update its factor
if (currencyDef[code]) {
currencyDef[code].factor = rate;
} else {
// Optional: Add new currencies dynamically if desired.
// For now, let's stick to the curated list to ensure we have names.
// Or we could add them with code as name:
// currencyDef[code] = { name: code, factor: rate };
}
}
}
</script>
<script>
</script>
<script src="https://cdn.counter.dev/script.js" data-id="4e551911-0e5d-4c48-91a6-b828c3301675"
data-utcoffset="11"></script>
</body>
</html>