-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfissionCalculator_controller.js
More file actions
188 lines (153 loc) · 7.07 KB
/
fissionCalculator_controller.js
File metadata and controls
188 lines (153 loc) · 7.07 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
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 fissionElementSelect = document.getElementById('fissionElementSelect');
const fissionIsotopeSelect = document.getElementById('fissionIsotopeSelect');
const fissionDecaySelect = document.getElementById('fissionDecaySelect');
const fissionReactionButton = document.getElementById('attemptFissionReaction');
const fissionContinueButton = document.getElementById("continueFissionReaction");
const fissionClearHistoryButton = document.getElementById("clearFissionHistory");
// ========[ Fill the element dropdowns ]========
atomData.forEach(element => {
const option = document.createElement('option');
option.value = element.protonCount;
option.textContent = `${element.elementName} (${element.protonCount})`;
fissionElementSelect.appendChild(option);
});
// ========[ Disable other input elements ]========
fissionIsotopeSelect.disabled = true;
fissionDecaySelect.disabled = true;
fissionReactionButton.disabled = true;
// ========[ Handle change to element dropdown ]========
fissionElementSelect.addEventListener('change', updateIsotopeDropdown);
function updateIsotopeDropdown() {
fissionIsotopeSelect.innerHTML = '<option value="base">-- Select Isotope --</option>';
fissionIsotopeSelect.disabled = true;
fissionDecaySelect.innerHTML = '<option value="base">-- Select Decay Type --</option>';
fissionDecaySelect.disabled = true;
fissionReactionButton.disabled = true;
const selectedElement = fissionElementSelect.value;
if (selectedElement == "base") return;
const elementObject = findElementObject(selectedElement);
const elementName = atomData[selectedElement-1].elementName;
const isotopeArray = elementObject.isotopes
isotopeArray.forEach(isotope => {
const option = document.createElement('option');
option.value = isotope.nucleonCount;
option.textContent = `${elementName}-${isotope.nucleonCount}`;
fissionIsotopeSelect.appendChild(option);
});
fissionIsotopeSelect.disabled = false;
localStorage.setItem("continuedFission", false);
};
// ========[ Handle change to isotope dropdown ]========
fissionIsotopeSelect.addEventListener('change', updateDecayDropdown);
function updateDecayDropdown() {
fissionDecaySelect.innerHTML = '<option value="base">-- Select Decay Type --</option>';
fissionDecaySelect.disabled = true;
fissionReactionButton.disabled = true;
const selectedElement = fissionElementSelect.value;
const selectedIsotope = fissionIsotopeSelect.value;
if (selectedIsotope == "base") return;
const isotopeObject = findNucleodeObject(selectedElement, selectedIsotope);
const decayArray = isotopeObject.decay;
if(decayArray == 'stable') {
const option = document.createElement('option');
option.value = 'stable';
option.textContent = `Stable`;
fissionDecaySelect.appendChild(option);
} else {
decayArray.forEach(decay => {
const option = document.createElement('option');
const type = decay.decayType;
option.value = type;
option.textContent = `${type}`;
fissionDecaySelect.appendChild(option);
});
}
fissionDecaySelect.disabled = false;
localStorage.setItem("continuedFission", false);
};
// ========[ Handle change to decay dropdown ]========
fissionDecaySelect.addEventListener('change', updateDecayButton);
function updateDecayButton() {
fissionReactionButton.disabled = true;
if (fissionDecaySelect.value == "base") return
fissionReactionButton.disabled = false;
};
// ========[ Execute decay reaction ]========
fissionReactionButton.addEventListener('click', executeDecayAction);
function executeDecayAction() {
const selectedElement = fissionElementSelect.value;
const selectedIsotope = fissionIsotopeSelect.value;
const selectedDecayType = fissionDecaySelect.value;
const newIsotopeArray = decayOperation(selectedDecayType, selectedElement, selectedIsotope);
const protonCount = newIsotopeArray[0];
const nucleonCount = newIsotopeArray[1];
const elementName = atomData[protonCount-1].elementName;
if(selectedElement == protonCount && selectedIsotope == nucleonCount) {
updateDecayHistory(`${selectedElement}-${selectedIsotope}>Stable`);
} else {
updateDecayHistory(`${selectedElement}-${selectedIsotope}>${selectedDecayType}>${protonCount}-${nucleonCount}`);
}
localStorage.setItem("latestFissionCalculatorIsotope", `${protonCount}-${nucleonCount}`);
changeElementText("fissionResult", `${elementName}-${nucleonCount}`);
};
// ========[ Continue decay of new isotope ]========
fissionContinueButton.addEventListener('click', setLatestProduct)
function setLatestProduct() {
const latestProduct = localStorage.getItem("latestFissionCalculatorIsotope");
if (!latestProduct) return
const isotopeArray = latestProduct.split('-');
const protonCount = isotopeArray[0], nucleonCount = isotopeArray[1];
const nucleodeCheck = findNucleodeObject(protonCount, nucleonCount);
if (!nucleodeCheck) {
console.log('nucleode not found');
return
}
fissionElementSelect.value = protonCount;
updateIsotopeDropdown();
fissionIsotopeSelect.value = nucleonCount;
updateDecayDropdown();
localStorage.setItem("continuedFission", true);
};
// ========[ decay history ]========
function updateDecayHistory(addition) {
const currentHistory = localStorage.getItem("fissionCalculatorHistory");
const continued = localStorage.getItem("continuedFission");
/*
* 92-235>a>90-231|90-231>b->91-231|91-231>b->92-239
* input>decayType>output
* | is a continued reaction, / is a new reaction chain
*/
let newHistory = '';
if (currentHistory) {
newHistory += `${currentHistory}${continued == "true" ? '|' : '/'}${addition}`;
} else {
newHistory += addition;
}
localStorage.setItem("fissionCalculatorHistory", newHistory);
updateHistory();
};
function updateHistory() {
const historyData = localStorage.getItem("fissionCalculatorHistory");
if (!historyData) {changeElementText("fissionCalculatorHistory", ''); return}
const historyPartArray = historyData.split('/');
let historyText = '';
historyPartArray.forEach(chain => {
const reactionArray = chain.split('|');
reactionArray.forEach(reaction => {
historyText += `${reaction}<br>`;
});
historyText += '<br><br>';
});
changeElementText("fissionCalculatorHistory", historyText);
};
fissionClearHistoryButton.addEventListener('click', resetHistory);
function resetHistory() {
// localStorage.clear() // Clears compconste local storage
localStorage.removeItem("latestFissionCalculatorIsotope");
localStorage.removeItem("fissionCalculatorHistory");
updateHistory();
};
setLatestProduct();