-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
136 lines (123 loc) · 4.05 KB
/
script.js
File metadata and controls
136 lines (123 loc) · 4.05 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
const form = document.querySelector('form');
const storage = window.localStorage;
let batteries = [];
if(storage.getItem('batteries')){
batteries = JSON.parse(storage.getItem('batteries'));
}
form.addEventListener("submit", function(event){
event.preventDefault();
let battery = {};
data = new FormData(form);
for (const [key, value] of data.entries()) {
battery[key] = value;
}
batteries.push(battery);
storage.setItem('batteries', JSON.stringify(batteries));
form.reset();
displayData();
document.getElementById('date').valueAsDate = new Date();
})
function displayData(){
const display = document.querySelector('#battery-data-display');
display.innerHTML = '';
batteries.forEach(battery => {
const div = document.createElement('div');
div.innerHTML = `
<p>Battery ID: ${battery.Batteryid}</p>
<p>Date: ${battery.date}</p>
<p>In/Out: ${battery['in-out']}</p>
<p>Status: ${battery.status}</p>
<p>Charge Level: ${battery.chargelevel}</p>
<p>Rint: ${battery.rint} Ohms</p>
<hr>
`;
display.appendChild(div);
})
}
displayData();
function jsonToCsv(jsonData) {
let csv = '';
// Extract headers
const headers = Object.keys(jsonData[0]);
csv += headers.join(',') + '\n';
// Extract values
jsonData.forEach(obj => {
const values = headers.map(header => obj[header]);
csv += values.join(',') + '\n';
});
return csv;
}
document.getElementById('download-csv').addEventListener('click', function() {
if (batteries.length === 0) {
alert('No data to download');
return;
}
const csv = jsonToCsv(batteries);
const blob = new Blob([csv], { type: 'text/csv' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'batteries.csv';
a.click();
URL.revokeObjectURL(url);
a.remove();
});
document.getElementById('download-json').addEventListener('click', function() {
if (batteries.length === 0) {
alert('No data to download');
return;
}
const json = JSON.stringify(batteries, null, 2);
const blob = new Blob([json], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'batteries.json';
a.click();
URL.revokeObjectURL(url);
a.remove();
});
document.getElementById('import-json').addEventListener('change', function(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
const jsonData = JSON.parse(e.target.result);
batteries = jsonData;
storage.setItem('batteries', JSON.stringify(batteries));
displayData();
};
reader.readAsText(file);
}
});
document.getElementById('clear-data').addEventListener('click', function() {
if (confirm('Are you sure you want to clear all data?')) {
batteries = [];
storage.removeItem('batteries');
displayData();
}
});
document.getElementById('date').valueAsDate = new Date();
document.getElementById('fill-130').addEventListener('click', function() {
document.getElementById('chargelevel').value = 130;
});
document.onscroll = function() {
if (window.scrollY > 0) {
document.getElementById('scroll-to-top').style.display = 'block';
document.getElementById('scroll-to-top').style.animation = 'fadeIn 0.3s';
} else {
document.getElementById('scroll-to-top').style.animation = 'fadeOut 0.3s';
setTimeout(() => {
document.getElementById('scroll-to-top').style.display = 'none';
}, 300);
}
}
document.getElementById('scroll-to-top').addEventListener('click', function() {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
function validateOhms(input) {
const value = input.value;
if (value.startsWith('.')) {
input.value = value.replace('.', '0.');
}
}