-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
117 lines (96 loc) · 4.09 KB
/
script.js
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
document.addEventListener('DOMContentLoaded', () =>
{
rerender()
})
function round(number)
{
return Math.round(number * 100) / 100
}
function rerender()
{
const tree = document.getElementById("tree-container");
const amount = parseInt(document.getElementById("global-modifier").value);
tree.innerHTML = '';
fetch('file.json')
.then(response => response.json())
.then(data =>
{
const treeContainer = document.getElementById('tree-container');
renderTree(data, treeContainer, amount)
}
)
}
function renderTree(data, container, modifier) {
data["chems"].forEach(element => {
const outer = document.createElement('div');
outer.className = 'outer';
const thing = document.createElement('div');
thing.className = 'tree-item';
thing.textContent = "> " + element["name"] + " " + (element["amount"] * modifier).toFixed(0);
thing.id = element["name"]
// Create a container for the nested tree
const nestedContainer = document.createElement('div');
nestedContainer.className = 'nested-tree';
nestedContainer.style.display = 'none'; // Initially hidden
// Add click event to toggle visibility
thing.addEventListener('click', () => {
const isHidden = nestedContainer.style.display === 'none';
nestedContainer.style.display = isHidden ? 'block' : 'none';
if (isHidden)
{
thing.style.backgroundColor = '#940000'; // Open trees will have red background
}
else
{
thing.style.backgroundColor = ''; // Reset background color when hidden
}
});
outer.appendChild(thing);
outer.appendChild(nestedContainer);
container.appendChild(outer);
// Render the nested tree if there are reactants
if (element.reactants && element.reactants.length > 0) {
renderNestedTree(element.reactants, nestedContainer, modifier);
}
});
}
function renderNestedTree(reactants, container, modifier) {
reactants.forEach(reactant => {
const reactantItem = document.createElement('div');
reactantItem.className = 'tree-item';
reactantItem.id = reactant["name"].replace(/\s+/g, '-').toLowerCase(); // Set the ID
// Create a link to this reactant IF it's not a base
if(reactant["type"] == "base")
{
reactantItem.className = reactantItem.className + " base"
reactantItem.textContent = reactant["name"] + " " + round(reactant["amount"] * modifier).toFixed(1);
}
else
reactantItem.textContent = "> " + reactant["name"] + " " + round(reactant["amount"] * modifier).toFixed(1);
// Create a container for the nested reactant tree
const nestedReactantContainer = document.createElement('div');
nestedReactantContainer.className = 'nested-tree';
nestedReactantContainer.style.display = 'none'; // Initially hidden
// Add click event to toggle visibility
reactantItem.addEventListener('click', (e) => {
e.stopPropagation(); // Prevent event bubbling
const isHidden = nestedReactantContainer.style.display === 'none';
nestedReactantContainer.style.display = isHidden ? 'block' : 'none';
if(reactant["type"] == "base") return;
if (isHidden)
{
reactantItem.style.backgroundColor = '#940000'; // Open trees will have red background
}
else
{
reactantItem.style.backgroundColor = ''; // Reset background color when hidden
}
});
container.appendChild(reactantItem);
container.appendChild(nestedReactantContainer);
// Recursively render the nested reactant tree
if (reactant.reactants && reactant.reactants.length > 0) {
renderNestedTree(reactant.reactants, nestedReactantContainer, modifier);
}
});
}