[templates] Add JS-calculated hovertext for date ages

On torrent listing and torrent view, mouse over the times to see "X hours Y minutes Z seconds ago" etc
This commit is contained in:
TheAMM
2017-05-13 02:30:39 +03:00
parent 517d3e8e32
commit 39230e1f39
4 changed files with 62 additions and 5 deletions

View File

@@ -31,6 +31,51 @@ $(document).ready(function() {
});
});
function _format_time_difference(seconds) {
var units = [
["year", 365*24*60*60],
["month", 30*24*60*60],
["week", 7*24*60*60],
["day", 24*60*60],
["hour", 60*60],
["minute", 60],
["second", 1]
];
var suffix = " ago";
var prefix = "";
if (seconds < 0) {
suffix = "";
prefix = "After ";
}
var parts = [];
for (var i = 0; i < units.length; i++) {
var scale = units[i];
var m = (seconds / scale[1]) | 0;
if (m > 0) {
// N unit(s)
parts.push( m.toString() + " " + scale[0] + (m == 1 ? "" : "s") );
seconds -= m*scale[1];
}
}
return prefix + parts.join(" ") + suffix;
}
document.addEventListener("DOMContentLoaded", function(event) {
var now_timestamp = (Date.now() / 1000) | 0; // UTC timestamp in seconds
var timestamp_targets = document.querySelectorAll('[data-timestamp]');
for (var i = 0; i < timestamp_targets.length; i++) {
var target = timestamp_targets[i];
var torrent_timestamp = parseInt(target.getAttribute('data-timestamp'));
if (torrent_timestamp) {
var timedelta = now_timestamp - torrent_timestamp;
target.setAttribute('title', _format_time_difference(timedelta));
}
};
});
//
// This is the unminified version of the theme changer script in the layout.html @ line: 21