forked from Mechanical-Advantage/AdvantageScope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunitConversion.ts
131 lines (119 loc) · 4.19 KB
/
unitConversion.ts
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
import { evaluate } from "mathjs";
import { GROUPED_UNITS, UnitConversionPreset } from "./shared/units";
const UNIT_TYPE = document.getElementById("unitType") as HTMLInputElement;
const FROM_UNIT = document.getElementById("fromUnit") as HTMLInputElement;
const TO_UNIT = document.getElementById("toUnit") as HTMLInputElement;
const EXTRA_FACTOR = document.getElementById("extraFactor") as HTMLInputElement;
const EXIT_BUTTON = document.getElementById("exit") as HTMLInputElement;
const CONFIRM_BUTTON = document.getElementById("confirm") as HTMLInputElement;
/** Updates the list of options based on the unit type. */
function updateUnitOptions() {
// Clear old options
while (FROM_UNIT.firstElementChild) {
FROM_UNIT.removeChild(FROM_UNIT.firstElementChild);
}
while (TO_UNIT.firstElementChild) {
TO_UNIT.removeChild(TO_UNIT.firstElementChild);
}
// Create new options
let type = UNIT_TYPE.value;
if (type === "none") {
FROM_UNIT.disabled = true;
TO_UNIT.disabled = true;
let option = document.createElement("option");
option.innerText = "NA";
FROM_UNIT.appendChild(option);
option = document.createElement("option");
option.innerText = "NA";
TO_UNIT.appendChild(option);
} else {
FROM_UNIT.disabled = false;
TO_UNIT.disabled = false;
Object.keys(GROUPED_UNITS[type]).forEach((unit, index) => {
let option = document.createElement("option");
option.innerText = unit;
FROM_UNIT.appendChild(option);
option = document.createElement("option");
option.innerText = unit;
TO_UNIT.appendChild(option);
if (index === 1) TO_UNIT.value = unit;
});
}
}
window.addEventListener("message", (event) => {
if (event.source === window && event.data === "port") {
let messagePort = event.ports[0];
messagePort.onmessage = (event) => {
// Update button focus
if (typeof event.data === "object" && "isFocused" in event.data) {
Array.from(document.getElementsByTagName("button")).forEach((button) => {
if (event.data.isFocused) {
button.classList.remove("blurred");
} else {
button.classList.add("blurred");
}
});
return;
}
// Normal message
let originalConversion: UnitConversionPreset = event.data;
// Add type options
["none", ...Object.keys(GROUPED_UNITS)].forEach((unitType) => {
let option = document.createElement("option");
option.innerText = unitType;
UNIT_TYPE.appendChild(option);
});
UNIT_TYPE.addEventListener("change", () => updateUnitOptions());
// Update values
if (originalConversion.type === null) {
UNIT_TYPE.value = "none";
updateUnitOptions();
} else {
UNIT_TYPE.value = originalConversion.type;
updateUnitOptions();
FROM_UNIT.value = originalConversion.from!;
TO_UNIT.value = originalConversion.to!;
}
EXTRA_FACTOR.value = originalConversion.factor.toString();
// Close function
function confirm() {
// Get extra factor
let factor = 1;
let factorSuccess = true;
try {
let factorEvaluated = evaluate(EXTRA_FACTOR.value);
if (typeof factorEvaluated === "number") {
factor = factorEvaluated;
} else {
factorSuccess = false;
}
} catch {
factorSuccess = false;
}
if (!factorSuccess) {
alert("Failed to parse extra factor.");
return;
}
// Save data
let unitType = UNIT_TYPE.value === "none" ? null : UNIT_TYPE.value;
let conversion: UnitConversionPreset = {
type: unitType,
factor: factor
};
if (unitType !== null) {
conversion.from = FROM_UNIT.value;
conversion.to = TO_UNIT.value;
}
messagePort.postMessage(conversion);
}
// Set up exit triggers
EXIT_BUTTON.addEventListener("click", () => {
messagePort.postMessage(originalConversion);
});
CONFIRM_BUTTON.addEventListener("click", confirm);
window.addEventListener("keydown", (event) => {
if (event.code === "Enter") confirm();
});
};
}
});