-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfusionCalculator_controller.js
More file actions
106 lines (87 loc) · 4.98 KB
/
fusionCalculator_controller.js
File metadata and controls
106 lines (87 loc) · 4.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
import { timePrefixDict, atomData, findElementObject, findNucleodeObject, decayOperation, fusionOperation } from "./reaction_calculator.js";
import { changeElementText, changeButtonText, giveState, setState, switchState, getInput, fillDropDown } from "./utils/base_utils.js";
// ========[ Declare Buttons ]========
const fusionCalculatorFirstElementSelect = document.getElementById('fusionCalculatorFirstElementSelect');
const fusionCalculatorFirstNucleonEntry = document.getElementById('fusionCalculatorFirstNucleonEntry');
const fusionCalculatorSecondElementSelect = document.getElementById('fusionCalculatorSecondElementSelect');
const fusionCalculatorSecondNucleonEntry = document.getElementById('fusionCalculatorSecondNucleonEntry');
const attemptFusionCalculatorReaction = document.getElementById('attemptFusionCalculatorReaction');
const continueFusionCalculatorReaction = document.getElementById('continueFusionCalculatorReaction');
// ========[ Fill the element dropdowns ]========
atomData.forEach(element => {
const option = document.createElement('option');
option.value = element.protonCount;
option.textContent = `${element.elementName} (${element.protonCount})`;
fusionCalculatorFirstElementSelect.appendChild(option);
});
atomData.forEach(element => {
const option = document.createElement('option');
option.value = element.protonCount;
option.textContent = `${element.elementName} (${element.protonCount})`;
fusionCalculatorSecondElementSelect.appendChild(option);
});
// ========[ Disable other input elements ]========
fusionCalculatorFirstNucleonEntry.disabled = true;
fusionCalculatorSecondNucleonEntry.disabled = true;
attemptFusionCalculatorReaction.disabled = true;
// ========[ Handle change to element dropdowns ]========
fusionCalculatorFirstElementSelect.addEventListener('change', updateIsotopeEntries);
fusionCalculatorSecondElementSelect.addEventListener('change', updateIsotopeEntries);
function updateIsotopeEntries() {const firstSelectedElement = fusionCalculatorFirstElementSelect.value;
const secondSelectedElement = fusionCalculatorSecondElementSelect.value;
let firstEntry = false;
let secondEntry = false;
fusionCalculatorFirstNucleonEntry.disabled = true;
fusionCalculatorSecondNucleonEntry.disabled = true;
attemptFusionCalculatorReaction.disabled = true;
if (firstSelectedElement != "base") {
fusionCalculatorFirstNucleonEntry.disabled = false;
firstEntry = true
}
if (secondSelectedElement != "base") {
fusionCalculatorSecondNucleonEntry.disabled = false;
secondEntry = true
}
if (firstEntry && secondEntry) attemptFusionCalculatorReaction.disabled = false;
};
// ========[ Execute fusion reaction ]========
attemptFusionCalculatorReaction.addEventListener('click', executeFusionAction);
function executeFusionAction() {
const firstSelectedElement = fusionCalculatorFirstElementSelect.value;
const firstSelectedNucleon = fusionCalculatorFirstNucleonEntry.value;
const secondSelectedElement = fusionCalculatorSecondElementSelect.value;
const secondSelectedNucleon = fusionCalculatorSecondNucleonEntry.value;
if(!/[0-9]+/.test(firstSelectedNucleon)) {
changeElementText("fusionCalculatorResult", 'Invalid first nucleode count');
}
if(!/[0-9]+/.test(secondSelectedNucleon)) {
changeElementText("fusionCalculatorResult", 'Invalid second nucleode count');
}
const newIsotopeArray = fusionOperation(Number(firstSelectedElement), Number(firstSelectedNucleon), Number(secondSelectedElement), Number(secondSelectedNucleon));
const protonCount = newIsotopeArray[0], nucleonCount = newIsotopeArray[1];
if (nucleonCount > 0 && protonCount > 0 && protonCount < nucleonCount) {
changeElementText("fusionCalculatorResult", `${protonCount <= 118 ? atomData[protonCount-1].elementName : protonCount}-${newIsotopeArray[1]}`);
localStorage.setItem("latestFusionCalculatorIsotope", `${protonCount}-${newIsotopeArray[1]}`);
} else {
changeElementText("fusionCalculatorResult", 'An unknown element');
localStorage.removeItem("latestFusionCalculatorIsotope");
}
};
// ========[ Continue fusion with new isotope ]========
continueFusionCalculatorReaction.addEventListener('click', setLatestProduct)
function setLatestProduct() {
const latestProduct = localStorage.getItem("latestFusionCalculatorIsotope");
if (!latestProduct) return
const isotopeArray = latestProduct.split('-');
const protonCount = isotopeArray[0], nucleonCount = isotopeArray[1];
if (protonCount <= 118 || protonCount < 0 || nucleonCount < 0 || nucleonCount < protonCount) {
fusionCalculatorFirstElementSelect.value = protonCount;
fusionCalculatorFirstNucleonEntry.value = nucleonCount;
fusionCalculatorSecondElementSelect.value = "base";
fusionCalculatorSecondNucleonEntry.value = "";
} else {
changeElementText("fusionCalculatorResult", 'An unknown element');
}
updateIsotopeEntries();
};
setLatestProduct()