-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
407 lines (341 loc) · 14.4 KB
/
Copy pathscript.js
File metadata and controls
407 lines (341 loc) · 14.4 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
// script.js
document.addEventListener('DOMContentLoaded', function () {
// Elements
const driveButtonsDiv = document.getElementById('driveButtons');
const customDriveInputDiv = document.getElementById('customDriveInput');
const customButton = document.getElementById('customButton');
const numDrivesInput = document.getElementById('numDrives');
const driveSizesDiv = document.getElementById('driveSizes');
const parityButtonsDiv = document.getElementById('parityButtons');
const parityDrivesDiv = document.getElementById('parityDrives');
const resultsDiv = document.getElementById('results');
const updateAllDrivesBtn = document.getElementById('updateAllDrivesBtn');
const updateAllDrivesModal = document.getElementById('updateAllDrivesModal');
const applyNewSizeBtn = document.getElementById('applyNewSizeBtn');
const closeModalBtn = document.getElementById('closeModalBtn');
const newDriveSizeInput = document.getElementById('newDriveSize');
const newDriveUnitSelect = document.getElementById('newDriveUnit');
let numDrives = 1; // Default number of data drives
let numParityDrives = 0; // Default number of parity drives
// Arrays to store drive values
let driveValues = [];
let parityValues = [];
// Generate Data Drive Buttons
for (let i = 1; i <= 10; i++) {
const button = document.createElement('button');
button.className = 'btn-drive w-full sm:w-auto px-3 py-2 bg-gray-800 text-white rounded hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-orange-300';
button.dataset.value = i;
button.textContent = i;
if (i === 1) button.classList.add('active', 'bg-orange-700');
// Tooltip
button.setAttribute('data-tooltip', `Select ${i} data drives`);
driveButtonsDiv.appendChild(button);
}
// Handle Data Drive Selection
driveButtonsDiv.addEventListener('click', function (event) {
if (event.target.matches('.btn-drive')) {
const selectedValue = event.target.dataset.value;
document.querySelectorAll('.btn-drive').forEach(btn => btn.classList.remove('active', 'bg-orange-700'));
event.target.classList.add('active', 'bg-orange-700');
customDriveInputDiv.style.display = 'none';
numDrivesInput.value = '';
numDrivesInput.classList.remove('border-orange-300');
numDrives = parseInt(selectedValue);
updateDriveInputs();
}
});
// Handle Custom Number Button
customButton.addEventListener('click', function () {
document.querySelectorAll('.btn-drive').forEach(btn => btn.classList.remove('active', 'bg-orange-700'));
customButton.classList.add('active', 'bg-orange-700');
customDriveInputDiv.style.display = 'block';
numDrivesInput.focus();
numDrives = parseInt(numDrivesInput.value) || 11;
updateDriveInputs();
});
// Handle Custom Number of Drives Input
numDrivesInput.addEventListener('input', function () {
let value = parseInt(this.value);
if (value > 100) {
this.value = 100;
value = 100;
}
if (value >= 11 && value <= 100) {
numDrives = value;
updateDriveInputs();
this.classList.remove('border-orange-300');
} else {
this.classList.add('border-orange-300');
}
});
numDrivesInput.addEventListener('blur', function () {
let value = parseInt(this.value);
if (isNaN(value) || value < 11) {
this.value = 11;
numDrives = 11;
updateDriveInputs();
this.classList.remove('border-orange-300');
}
});
// Function to update drive values
function updateDriveValues() {
const sizeInputs = document.querySelectorAll('.drive-size');
const unitSelects = document.querySelectorAll('.drive-unit-select');
sizeInputs.forEach((input, index) => {
const size = input.value;
const unit = unitSelects[index].value;
driveValues[index] = { size, unit };
});
}
// Function to update drive inputs
function updateDriveInputs() {
// Update driveValues with current inputs
updateDriveValues();
driveSizesDiv.innerHTML = '';
for (let i = 1; i <= numDrives; i++) {
const formGroup = document.createElement('div');
formGroup.className = 'drive-input-group flex items-center gap-1 mb-2'; // Reduced gap between items
const labelDiv = document.createElement('div');
labelDiv.className = 'drive-label w-20';
labelDiv.textContent = `Drive ${i}:`;
const sizeInput = document.createElement('input');
sizeInput.type = 'number';
sizeInput.min = '0';
sizeInput.className = 'drive-size w-20 px-2 py-1 bg-gray-800 border border-gray-700 rounded text-white focus:outline-none focus:ring-2 focus:ring-orange-300';
sizeInput.placeholder = `Size`;
sizeInput.addEventListener('input', calculateTotalStorage);
const unitSelect = document.createElement('select');
unitSelect.className = 'drive-unit-select w-16 px-2 py-1 bg-gray-800 border border-gray-700 rounded text-white focus:outline-none focus:ring-2 focus:ring-orange-300';
const tbOption = document.createElement('option');
tbOption.value = 'TB';
tbOption.textContent = 'TB';
const gbOption = document.createElement('option');
gbOption.value = 'GB';
gbOption.textContent = 'GB';
unitSelect.appendChild(tbOption);
unitSelect.appendChild(gbOption);
unitSelect.addEventListener('change', calculateTotalStorage);
// Set existing value if available
if (driveValues[i - 1]) {
sizeInput.value = driveValues[i - 1].size;
unitSelect.value = driveValues[i - 1].unit;
} else {
sizeInput.value = '8';
unitSelect.value = 'TB';
driveValues[i - 1] = { size: '8', unit: 'TB' };
}
formGroup.appendChild(labelDiv);
formGroup.appendChild(sizeInput);
formGroup.appendChild(unitSelect);
driveSizesDiv.appendChild(formGroup);
}
calculateTotalStorage();
}
// Generate Parity Drive Buttons
for (let i = 0; i <= 2; i++) {
const button = document.createElement('button');
button.className = 'btn-parity w-full sm:w-auto px-3 py-2 bg-gray-800 text-white rounded hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-orange-300';
button.dataset.value = i;
button.textContent = i;
if (i === 0) button.classList.add('active', 'bg-orange-700');
// Tooltip
button.setAttribute('data-tooltip', `Select ${i} parity drives`);
parityButtonsDiv.appendChild(button);
}
// Handle Parity Drive Selection
parityButtonsDiv.addEventListener('click', function (event) {
if (event.target.matches('.btn-parity')) {
numParityDrives = parseInt(event.target.dataset.value);
document.querySelectorAll('.btn-parity').forEach(btn => btn.classList.remove('active', 'bg-orange-700'));
event.target.classList.add('active', 'bg-orange-700');
updateParityDrives();
}
});
// Function to update parity values
function updateParityValues() {
const sizeInputs = document.querySelectorAll('.parity-size');
const unitSelects = document.querySelectorAll('.parity-unit-select');
sizeInputs.forEach((input, index) => {
const size = input.value;
const unit = unitSelects[index].value;
parityValues[index] = { size, unit };
});
}
// Function to get largest data drive size
function getLargestDriveSize() {
let largestSize = 0;
const driveSizes = document.querySelectorAll('.drive-size');
const driveUnits = document.querySelectorAll('.drive-unit-select');
driveSizes.forEach((input, index) => {
let size = parseFloat(input.value) || 0;
const unit = driveUnits[index].value;
if (unit === 'GB') size = size / 1024; // Convert GB to TB
if (size > largestSize) largestSize = size;
});
return largestSize;
}
// Function to update parity drive inputs
function updateParityDrives() {
// Update parityValues with current inputs
updateParityValues();
const largestDriveSize = getLargestDriveSize() || 4;
parityDrivesDiv.innerHTML = '';
for (let i = 1; i <= numParityDrives; i++) {
const formGroup = document.createElement('div');
formGroup.className = 'drive-input-group flex items-center mb-2';
const labelDiv = document.createElement('div');
labelDiv.className = 'drive-label w-24 mr-2';
labelDiv.textContent = `Parity ${i}:`;
const sizeInput = document.createElement('input');
sizeInput.type = 'number';
sizeInput.min = '0';
sizeInput.className = 'parity-size w-24 px-2 py-1 bg-gray-800 border border-gray-700 rounded text-white focus:outline-none focus:ring-2 focus:ring-orange-300';
sizeInput.placeholder = `Size`;
sizeInput.addEventListener('input', calculateTotalStorage);
const unitSelect = document.createElement('select');
unitSelect.className = 'parity-unit-select w-20 ml-2 px-2 py-1 bg-gray-800 border border-gray-700 rounded text-white focus:outline-none focus:ring-2 focus:ring-orange-300';
const tbOption = document.createElement('option');
tbOption.value = 'TB';
tbOption.textContent = 'TB';
const gbOption = document.createElement('option');
gbOption.value = 'GB';
gbOption.textContent = 'GB';
unitSelect.appendChild(tbOption);
unitSelect.appendChild(gbOption);
unitSelect.addEventListener('change', calculateTotalStorage);
// Set existing value if available
if (parityValues[i - 1]) {
sizeInput.value = parityValues[i - 1].size;
unitSelect.value = parityValues[i - 1].unit;
} else {
sizeInput.value = largestDriveSize;
unitSelect.value = 'TB';
parityValues[i - 1] = { size: sizeInput.value, unit: 'TB' };
}
formGroup.appendChild(labelDiv);
formGroup.appendChild(sizeInput);
formGroup.appendChild(unitSelect);
parityDrivesDiv.appendChild(formGroup);
}
calculateTotalStorage();
}
// Function to calculate total storage
function calculateTotalStorage() {
const driveSizes = document.querySelectorAll('.drive-size');
const driveUnits = document.querySelectorAll('.drive-unit-select');
const paritySizes = document.querySelectorAll('.parity-size');
const parityUnits = document.querySelectorAll('.parity-unit-select');
let totalStorage = 0;
let largestDriveSize = 0;
let errorMessages = [];
driveSizes.forEach((input, index) => {
let size = parseFloat(input.value) || 0;
const unit = driveUnits[index].value;
if (unit === 'GB') size = size / 1024; // Convert GB to TB
totalStorage += size;
if (size > largestDriveSize) largestDriveSize = size;
});
paritySizes.forEach((input, index) => {
let size = parseFloat(input.value) || 0;
const unit = parityUnits[index].value;
if (unit === 'GB') size = size / 1024; // Convert GB to TB
// Check parity size against largest drive
if (size < largestDriveSize) {
errorMessages.push(
`Parity Drive ${index + 1} must be at least ${largestDriveSize.toFixed(2)} TB`
);
}
});
if (errorMessages.length > 0) {
resultsDiv.innerHTML = `<div class="text-red-300">${errorMessages.join('<br>')}</div>`;
} else {
const totalDrives = numDrives + numParityDrives;
const usableStorage = totalStorage.toFixed(2);
const faultTolerance = numParityDrives;
resultsDiv.innerHTML = `
<div class="bg-gray-800 p-4 rounded">
<p class="mb-2">
<i class="fa-solid fa-server text-green-400 mr-2"></i>
Usable Storage: <span class="text-green-400">${usableStorage} TB</span>
</p>
<p class="mb-2">
<i class="fa fa-shield-halved text-red-400 mr-2"></i>
Drive Fault Tolerance: <span class="text-red-400">${faultTolerance} drive(s)</span>
</p>
<p>
<i class="fa-solid fa-hard-drive text-indigo-400 mr-2"></i>
Total Drives (including parity): <span class="text-indigo-400">${totalDrives} Drive(s)</span>
</p>
</div>
`;
}
}
// Initialize inputs
updateDriveInputs();
updateParityDrives();
// Update All Drive Sizes functionality
updateAllDrivesBtn.addEventListener('click', function () {
// Show modal
updateAllDrivesModal.classList.remove('hidden');
});
applyNewSizeBtn.addEventListener('click', function () {
const newSize = parseFloat(newDriveSizeInput.value);
const newUnit = newDriveUnitSelect.value;
if (!isNaN(newSize) && newSize > 0) {
// Update all drive sizes
document.querySelectorAll('.drive-size').forEach((input, index) => {
input.value = newSize;
driveValues[index].size = newSize;
});
document.querySelectorAll('.drive-unit-select').forEach((select, index) => {
select.value = newUnit;
driveValues[index].unit = newUnit;
});
// Update parity drive sizes
document.querySelectorAll('.parity-size').forEach((input, index) => {
input.value = newSize;
parityValues[index].size = newSize;
});
document.querySelectorAll('.parity-unit-select').forEach((select, index) => {
select.value = newUnit;
parityValues[index].unit = newUnit;
});
calculateTotalStorage();
updateAllDrivesModal.classList.add('hidden');
newDriveSizeInput.value = '';
} else {
alert('Please enter a valid drive size.');
}
});
closeModalBtn.addEventListener('click', function () {
updateAllDrivesModal.classList.add('hidden');
});
// Tooltip Functionality
document.addEventListener('mouseover', function (e) {
if (e.target.dataset.tooltip) {
const tooltip = document.getElementById('tooltip');
let left = e.pageX;
let top = e.pageY + 20;
// Adjust to avoid going outside viewport boundaries
if (left + tooltip.clientWidth > window.innerWidth) {
left = window.innerWidth - tooltip.clientWidth - 10;
}
tooltip.style.left = `${left}px`;
tooltip.style.top = `${top}px`;
tooltip.textContent = e.target.dataset.tooltip;
tooltip.classList.remove('hidden');
}
});
document.addEventListener('mouseout', function (e) {
if (e.target.dataset.tooltip) {
const tooltip = document.getElementById('tooltip');
tooltip.classList.add('hidden');
}
});
// Accessibility: Keyboard Navigation for Modals
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && !updateAllDrivesModal.classList.contains('hidden')) {
updateAllDrivesModal.classList.add('hidden');
}
});
});