You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PackageHelper/www/assets/js/activitylog.js

108 lines
2.6 KiB
JavaScript

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
function appendActivityLog(title, subtitle, content, icon, timestamp) {
if (typeof timestamp == "undefined") {
timestamp = time();
}
let entry = {
title: title,
subtitle: subtitle,
content: content,
icon: icon,
timestamp: timestamp
};
let log = getStorage("activitylog");
if (log == null) {
log = [];
} else {
try {
log = JSON.parse(log);
} catch (ex) {
log = [];
}
}
let pushed = false;
let datestr = formatTimestamp("Y-m-d", timestamp);
for (var i = 0; i < log.length; i++) {
if (formatTimestamp("Y-m-d", log[i].date) == datestr) {
log[i].entries.push(entry);
pushed = true;
break;
}
}
if (!pushed) {
log.push({
date: timestamp,
entries: [
entry
]
});
}
console.log("Added activity log entry", entry);
console.log(log);
setStorage("activitylog", JSON.stringify(log));
// Trim the log soon but don't block for it
setTimeout(trimActivityLog, 100);
}
function clearActivityLog() {
setStorage("activitylog", "[]");
}
function trimActivityLog() {
let log = JSON.parse(getStorage("activitylog"));
log.sort(function (x, y) {
if (x.date < y.date) {
return 1;
} else if (x.date > y.date) {
return -1;
}
return 0;
});
let entries = 0;
let allowed = SETTINGS.activitylog_maxlength;
let newlog = [];
for (var i = 0; i < log.length; i++) {
let logdate = {
date: log[i].date,
entries: []
};
for (var j = 0; j < log[i].entries.length; j++) {
if (entries < allowed) {
logdate.entries.push(log[i].entries[j]);
}
entries++;
}
if (logdate.entries.length > 0) {
newlog.push(logdate);
}
}
if (entries - allowed > 0) {
newlog[newlog.length - 1].entries.push({
title: "Log Trimmed",
subtitle: "",
content: (entries - allowed) + " older " + ((entries - allowed) == 1 ? "entry was" : "entries were") + " removed from the log.",
icon: "fas fa-cut",
timestamp: newlog[newlog.length - 1].date
});
}
console.log(log);
console.log(newlog);
setStorage("activitylog", JSON.stringify(newlog));
}