Files
fnx_web/res/include/script/dependencies/util.js

105 lines
3.2 KiB
JavaScript
Raw Normal View History

2020-01-20 12:43:43 +01:00
function addUploadHistory(fileID) {
// Make sure the user is not logged in, for privacy. This keeps the
// files uploaded while logged in and anonymously uploaded files
// separated
if (document.cookie.includes("pd_auth_key")) { return; }
let uploads = localStorage.getItem("uploaded_files");
if (uploads === null) { uploads = ""; }
// Check if there are not too many values stored
if (uploads.length > 3600) {
// 3600 characters is enough to store 400 file IDs. If we exceed that
// number we'll drop the last two items
uploads = uploads.substring(
uploads.indexOf(",") + 1
).substring(
uploads.indexOf(",") + 1
);
}
// Save the new ID
localStorage.setItem("uploaded_files", fileID + "," + uploads);
}
2020-01-20 19:55:51 +01:00
2020-02-04 19:37:46 +01:00
function printDate(date, hours, minutes, seconds) {
let dateStr = date.getFullYear()
2021-01-18 13:40:28 +01:00
+ "-" + ("00" + (date.getMonth() + 1)).slice(-2)
+ "-" + ("00" + date.getDate()).slice(-2)
2020-02-04 19:37:46 +01:00
2021-01-18 13:40:28 +01:00
if (hours) { dateStr += " " + ("00" + date.getHours()).slice(-2) }
if (minutes) { dateStr += ":" + ("00" + date.getMinutes()).slice(-2) }
if (seconds) { dateStr += ":" + ("00" + date.getMinutes()).slice(-2) }
2020-02-04 19:37:46 +01:00
return dateStr
}
2020-01-20 19:55:51 +01:00
function copyText(text) {
// Create a textarea to copy the text from
let ta = document.createElement("textarea");
ta.setAttribute("readonly", "readonly")
ta.style.position = "absolute";
ta.style.left = "-9999px";
ta.value = text; // Put the text in the textarea
// Add the textarea to the DOM so it can be seleted by the user
document.body.appendChild(ta);
ta.select() // Select the contents of the textarea
let success = document.execCommand("copy"); // Copy the selected text
document.body.removeChild(ta); // Remove the textarea
return success;
}
2020-01-21 16:10:45 +01:00
function domainURL() {
2021-01-18 13:40:28 +01:00
let url = window.location.protocol + "//" + window.location.hostname;
2020-01-21 16:10:45 +01:00
if (window.location.port != "") {
2021-01-18 13:40:28 +01:00
url = url + ":" + window.location.port;
2020-01-21 16:10:45 +01:00
}
return url;
}
2020-01-21 17:01:26 +01:00
2020-06-08 16:30:37 +02:00
function formatNumber(amt, precision) {
if (precision < 3) { precision = 3; }
if (amt >= 1e6) {
2021-01-18 13:40:28 +01:00
return (amt / 1e6).toPrecision(precision) + "M";
2020-06-08 16:30:37 +02:00
} else if (amt >= 1e3) {
2021-01-18 13:40:28 +01:00
return (amt / 1e3).toPrecision(precision) + "k";
2020-06-08 16:30:37 +02:00
}
return amt
}
2020-06-14 16:02:15 +02:00
function formatThousands(x) {
2021-01-18 13:40:28 +01:00
// Inject a space every three digits
2020-06-14 16:02:15 +02:00
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
}
2020-01-22 19:48:58 +01:00
function formatDataVolume(amt, precision) {
2020-01-21 17:01:26 +01:00
if (precision < 3) { precision = 3; }
2020-07-19 11:40:11 +02:00
if (amt >= 1e18) {
2021-01-18 13:40:28 +01:00
return (amt / 1e18).toPrecision(precision) + " EB";
} else if (amt >= 1e15) {
return (amt / 1e15).toPrecision(precision) + " PB";
} else if (amt >= 1e12) {
return (amt / 1e12).toPrecision(precision) + " TB";
2020-01-21 17:01:26 +01:00
} else if (amt >= 1e9) {
2021-01-18 13:40:28 +01:00
return (amt / 1e9).toPrecision(precision) + " GB";
2020-01-21 17:01:26 +01:00
} else if (amt >= 1e6) {
2021-01-18 13:40:28 +01:00
return (amt / 1e6).toPrecision(precision) + " MB";
2020-01-21 17:01:26 +01:00
} else if (amt >= 1e3) {
2021-01-18 13:40:28 +01:00
return (amt / 1e3).toPrecision(precision) + " kB";
2020-01-21 17:01:26 +01:00
}
2021-01-18 13:40:28 +01:00
return Math.floor(amt) + " B"
2020-01-21 17:01:26 +01:00
}
2020-05-25 20:40:16 +02:00
const second = 1000
2021-01-18 13:40:28 +01:00
const minute = second * 60
const hour = minute * 60
const day = hour * 24
2020-05-25 20:40:16 +02:00
function formatDuration(ms) {
let res = ""
2021-01-18 13:40:28 +01:00
if (ms >= day) { res += Math.floor(ms / day) + "d " }
if (ms >= hour) { res += Math.floor((ms % day) / hour) + "h " }
if (ms >= minute) { res += Math.floor((ms % hour) / minute) + "m " }
return res + ((ms % minute) / second).toFixed(3) + "s"
2020-05-25 20:40:16 +02:00
}