BangleApps/apps/sleeplog/interface.html

151 lines
4.6 KiB
HTML

<html>
<head>
<link rel="stylesheet" href="../../css/spectre.min.css">
<link rel="stylesheet" href="../../css/spectre-icons.min.css">
</head>
<body>
<div id="table"></div>
<script src="../../core/lib/interface.js"></script>
<script>
var domTable = document.getElementById("table");
function viewLog(logData, title) {
var html = `
<thead>
<tr>
<th>Date</th>
<th>Status</th>
</tr>
</thead>
<tbody>`;
logData.forEach(entry => html += `
<tr>
<td>` +
new Date(entry[0] * 6E5).toLocaleString(undefined) + `
</td>
<td>` +
"unknown ,non consec.,consecutive".split(",")[entry[2]] + " " +
"unknown,not worn,awake,light sleep,deep sleep".split(",")[entry[1]] + `
</td>
</tr>`);
html = `
</tbody>`;
document.getElementById("view").innerHTML = html;
}
function saveCSV(logData, title) {
var timeFormat = document.getElementById("timeFormat").selectedIndex;
logData = logData.map(entry => {
entry[0] *= 6E5;
if (timeFormat === 1) entry[0] /= 1E3;
if (timeFormat === 2) entry[0] = entry[0] / 864E5 + 25569;
return entry.join(",");
}).join("\n");
Util.saveCSV(title, "time,sleep,consecutive\n" + logData);
}
function readLog(date, callback) {
Util.showModal("Downloading logged data...");
Puck.eval(`require("sleeplog").readLog(` + date + `, ` + date + ` + 12096E5)`, logData => {
Util.hideModal();
callback(logData);
});
}
function getFnList() {
Util.showModal("Loading...");
domTable.innerHTML = "";
Puck.eval(`require("Storage").list(/^sleeplog_\\d\\d\\d\\d\\.log$/)`, files => {
// add this fortnight
files.push("" + Math.floor(Date.now() / 12096E5 - 0.25));
files = files.map(file => {
var ret = {
bName: file,
date: (parseInt(file.match(/\d{4}/)[0]) + 0.25) * 12096E5,
};
var thisDates = [new Date(ret.date), new Date(ret.date + 12096E5)];
ret.dName = thisDates[0].toISOString().substr(0, 10) + "_" + thisDates[1].toISOString().substr(5, 5);
ret.dateA = thisDates[0].toLocaleDateString(undefined);
ret.dateB = thisDates[1].toLocaleDateString(undefined);
return ret;
});
files = files.sort((a, b) => a.fortnigt - b.fortnigt);
var html = `
<table class="table table-striped table-hover">
<thead>
<tr>
<th>File</th>
<th>from</th>
<th>to</th>
<th>Actions</th>
</tr>
</thead>
<tbody>`;
files.forEach(file => {
html += `
<tr>
<td>${file.bName.endsWith(".log") ? file.bName : "active log"}</td>
<td>${file.dateA}</td>
<td>${file.dateB}</td>
<td>
<button class="btn" task="view" date="${file.date}" dName="${file.dName}"><i class="icon icon-menu"></i></button>
<button class="btn btn-primary" task="csv" date="${file.date}" dName="${file.dName}">CSV</button>`;
if (file.bName.endsWith(".log")) html += `
<button class="btn" task="del" bName="${file.bName}"><i class="icon icon-delete"></i></button>`;
html += `
</td>
</tr>`;
});
html += `
</tbody>
</table>
<p>csv time format:
<select id="timeFormat">
<option>JavaScript (msec since 1970)</option>
<option>UNIX (sec since 1970)</option>
<option>Office (days since 1900)</option>
</select>
</p>
<table id="view">
</table>`;
domTable.innerHTML = html;
Util.hideModal();
var buttons = domTable.querySelectorAll("button");
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", event => {
var button = event.currentTarget;
var task = button.getAttribute("task");
if (!task) return;
if (task=="view") {
var date = button.getAttribute("date");
var dName = button.getAttribute("dName");
if (!date || !dName) return;
readLog(date, logData => viewLog(logData, dName));
} else if (task=="csv") {
var date = button.getAttribute("date");
var dName = button.getAttribute("dName");
if (!date || !dName) return;
readLog(date, logData => saveCSV(logData, dName));
} else if (task=="delete") {
var bName = button.getAttribute("bName");
if (!bName) return;
Util.showModal("Deleting...");
Util.eraseStorage(bName, () => {
Util.hideModal();
getTrackList();
});
}
});
}
});
}
function onInit() {
getFnList();
}
</script>
</body>
</html>