Skip to content

Commit 040a7e9

Browse files
committed
add reset diffs and reset graph button
1 parent 691b52b commit 040a7e9

1 file changed

Lines changed: 65 additions & 7 deletions

File tree

nodejs-assets/nodejs-project/index.html

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,19 @@
200200
width: 100%;
201201
height: 500px;
202202
}
203+
.reset-buttons {
204+
display: flex;
205+
justify-content: center;
206+
gap: 6px;
207+
margin-top: 16px;
208+
}
209+
.reset-buttons button {
210+
padding: 6px;
211+
background-color: rgb(58, 58, 58);
212+
color: white;
213+
border-radius: 6px;
214+
border: none;
215+
}
203216
</style>
204217
</head>
205218
<body>
@@ -220,6 +233,10 @@
220233
</div>
221234
</div>
222235

236+
<div class="reset-buttons">
237+
<button id="resetOffset">Reset Diffs</button>
238+
<button id="resetGraph">Reset Graph</button>
239+
</div>
223240
<div id="sections"></div>
224241
<div id="cells"></div>
225242
<div id="charts">
@@ -398,9 +415,35 @@
398415
}),
399416
);
400417
}
418+
resetValues = () => {
419+
this.labels = [];
420+
this.data = {
421+
capacity: [],
422+
voltage: [],
423+
current: [],
424+
cellDiff: [],
425+
cells: [],
426+
};
427+
this.lastPlottedAt = -1;
428+
this.onAdd();
429+
this.saveToStorage();
430+
};
401431
}
402432

403433
const plotter = new Plotter();
434+
const [initialValues, setInitialValues] = cachedValue("initialValues", {});
435+
const [cellStartValues, setCellStartValues] = cachedValue('cellStartValues', []);
436+
437+
const resetGraphBtn = document.getElementById('resetGraph');
438+
resetGraphBtn.addEventListener('click', () => {
439+
plotter.resetValues();
440+
})
441+
const resetOffsetBtn = document.getElementById('resetOffset');
442+
resetOffsetBtn.addEventListener('click', () => {
443+
setCellStartValues([]);
444+
setInitialValues({});
445+
})
446+
404447

405448
function getIndicesOfLargestNumbers(arr) {
406449
if (!arr || arr.length === 0) {
@@ -501,12 +544,13 @@
501544
}
502545
}
503546

504-
const initialValues = {};
547+
548+
505549

506550
const createOrUpdateSection = (id, title, description, value, rawValue) => {
507-
let initVal = initialValues[id];
551+
let initVal = initialValues()[id];
508552
if (initVal === undefined) {
509-
initialValues[id] = rawValue;
553+
setInitialValues({...initialValues(), [id]: rawValue});
510554
initVal = rawValue;
511555
}
512556

@@ -558,7 +602,6 @@
558602
sections.innerHTML += element;
559603
};
560604

561-
let cellStartValues = [];
562605

563606
const estimateBalanceCells = [];
564607
function createCellsSection(balanceStatus, cellVolts) {
@@ -588,11 +631,11 @@
588631
{id: 'cell-avg', name: 'Avg', value: averageVoltage.toFixed(3)},
589632
];
590633

591-
if (!cellStartValues.length) {
592-
cellStartValues = cellVolts;
634+
if (!cellStartValues().length) {
635+
setCellStartValues(cellVolts);
593636
}
594637

595-
const cellDiffs = cellStartValues.map((v, i) => {
638+
const cellDiffs = cellStartValues().map((v, i) => {
596639
const diff = cellVolts[i] - v;
597640
return diff.toFixed(3);
598641
});
@@ -1019,5 +1062,20 @@
10191062
window.addEventListener('resize', () => {
10201063
myChart?.resize();
10211064
});
1065+
1066+
function cachedValue(key, initialValue) {
1067+
let value = JSON.parse(localStorage.getItem(key) || JSON.stringify(initialValue));
1068+
1069+
const set = (val) => {
1070+
value = val;
1071+
localStorage.setItem(key, JSON.stringify(val));
1072+
};
1073+
const get = () => value;
1074+
1075+
if (value === null) {
1076+
set(initialValue);
1077+
}
1078+
return [get, set];
1079+
}
10221080
</script>
10231081
</html>

0 commit comments

Comments
 (0)