fpaste/public/js/main.js

46 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2018-02-20 08:40:37 +00:00
window.onload = () => {
2018-02-20 08:22:57 +00:00
const menu = document.querySelector("menu");
2018-02-19 10:15:33 +00:00
switch(window.location.pathname.split(/\//)[1]) {
2018-02-20 08:22:57 +00:00
case "v": menu.querySelector("li:nth-child(2)").classList.add("active"); break;
2018-02-20 06:26:06 +00:00
case "u":
2018-02-20 08:22:57 +00:00
menu.querySelector("li:nth-child(4)").classList.add("active");
menu.querySelector("li:nth-child(4) > ul").classList.remove("hidden");
2018-02-20 06:26:06 +00:00
break;
2018-02-19 10:15:33 +00:00
case "a":
2018-02-20 08:22:57 +00:00
menu.querySelector("li:nth-child(6)").classList.add("active");
menu.querySelector("li:nth-child(6) > ul").classList.remove("hidden");
2018-02-19 10:15:33 +00:00
break;
default:
2018-02-20 08:22:57 +00:00
menu.querySelector("li:nth-child(1)").classList.add("active");
2018-02-19 10:15:33 +00:00
break;
}
2018-02-20 08:22:57 +00:00
// timeago
const epochs = [
["year", 31536000],
["month", 2592000],
["day", 86400],
["hour", 3600],
["minute", 60],
["second", 1]
];
const getDuration = timeAgoInSeconds => {
for (let [name, seconds] of epochs) {
const interval = ~~(timeAgoInSeconds / seconds);
if (interval >= 1) {
return {
interval: interval,
epoch: name
};
}
}
};
const timeAgo = date => {
const timeAgoInSeconds = ~~((new Date() - new Date(date)) / 1000);
const {interval, epoch} = getDuration(timeAgoInSeconds);
const suffix = interval === 1 ? "" : "s";
return `${interval} ${epoch}${suffix} ago`;
};
document.querySelectorAll("time").forEach(e => e.innerHTML = timeAgo(e.title));
2018-02-20 08:40:37 +00:00
};