Implemented activity timer in JS.
This commit is contained in:
parent
2ac0f934a7
commit
2aaeaf9b9b
4 changed files with 81 additions and 35 deletions
|
|
@ -59,7 +59,10 @@
|
||||||
# "(select creation from post where id = (select max(id) from post where thread = ?)))"), %threadId)
|
# "(select creation from post where id = (select max(id) from post where thread = ?)))"), %threadId)
|
||||||
#let timeStr = formatTimestamp(latestReplyDate.parseInt())
|
#let timeStr = formatTimestamp(latestReplyDate.parseInt())
|
||||||
<div class="activity">
|
<div class="activity">
|
||||||
<div>$timeStr</div>
|
<div>
|
||||||
|
<span class="isoDate">$timeStr</span>
|
||||||
|
$timeStr
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
# end for
|
# end for
|
||||||
|
|
|
||||||
35
forum.nim
35
forum.nim
|
|
@ -143,42 +143,9 @@ proc genButtons(c: var TForumData, btns: seq[TStyledButton]): string =
|
||||||
result.add(("""<a class="$3active button" href="$1$4">$2</a>""") % [
|
result.add(("""<a class="$3active button" href="$1$4">$2</a>""") % [
|
||||||
btns[i].link, btns[i].text, class, anchor])
|
btns[i].link, btns[i].text, class, anchor])
|
||||||
|
|
||||||
proc toInterval(diff: int64): TimeInterval =
|
|
||||||
var remaining = diff
|
|
||||||
let years = remaining div 31536000
|
|
||||||
remaining -= years * 31536000
|
|
||||||
let months = remaining div 2592000
|
|
||||||
remaining -= months * 2592000
|
|
||||||
let days = remaining div 86400
|
|
||||||
remaining -= days * 86400
|
|
||||||
let hours = remaining div 3600
|
|
||||||
remaining -= hours * 3600
|
|
||||||
let minutes = remaining div 60
|
|
||||||
remaining -= minutes * 60
|
|
||||||
#assert false
|
|
||||||
result = initInterval(0, remaining.int, minutes.int, hours.int, days.int,
|
|
||||||
months.int, years.int)
|
|
||||||
|
|
||||||
proc formatTimestamp(t: int): string =
|
proc formatTimestamp(t: int): string =
|
||||||
let t2 = TTime(t)
|
let t2 = TTime(t)
|
||||||
let now = getTime()
|
return t2.getGMTime().format("yyyy-MM-dd'T'HH':'mm':'ss'Z'")
|
||||||
let diff = (now - t2).toInterval()
|
|
||||||
if diff.years > 0:
|
|
||||||
return getGMTime(t2).format("MMMM d',' yyyy")
|
|
||||||
elif diff.months > 0:
|
|
||||||
return $diff.months & (if diff.months > 1: " months ago" else: " month ago")
|
|
||||||
elif diff.days > 0:
|
|
||||||
return $diff.days & (if diff.days > 1: " days ago" else: " day ago")
|
|
||||||
elif diff.hours > 0:
|
|
||||||
return $diff.hours & (if diff.hours > 1: " hours ago" else: " hour ago")
|
|
||||||
elif diff.minutes > 0:
|
|
||||||
return $diff.minutes &
|
|
||||||
(if diff.minutes > 1: " minutes ago" else: " minute ago")
|
|
||||||
elif diff.seconds > 0:
|
|
||||||
return $diff.seconds &
|
|
||||||
(if diff.seconds > 1: " seconds ago" else: " second ago")
|
|
||||||
else:
|
|
||||||
return "less than a second ago"
|
|
||||||
|
|
||||||
proc getGravatarUrl(email: string, size = 80): string =
|
proc getGravatarUrl(email: string, size = 80): string =
|
||||||
let emailMD5 = email.toLower.toMD5
|
let emailMD5 = email.toLower.toMD5
|
||||||
|
|
|
||||||
|
|
@ -496,3 +496,8 @@ hr
|
||||||
{
|
{
|
||||||
border: 1px solid #3D3D3D;
|
border: 1px solid #3D3D3D;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.activity .isoDate
|
||||||
|
{
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,4 +2,75 @@
|
||||||
|
|
||||||
window.onload = function() {
|
window.onload = function() {
|
||||||
positionGlowArrow();
|
positionGlowArrow();
|
||||||
|
processMainDates();
|
||||||
|
window.setTimeout(performProcessingMainDates, 1000);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function performProcessingMainDates()
|
||||||
|
{
|
||||||
|
if (processMainDates())
|
||||||
|
{
|
||||||
|
window.setTimeout(performProcessingMainDates, 1000);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
window.setTimeout(performProcessingMainDates, 60000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function timeSince(date) {
|
||||||
|
var result = ["", false];
|
||||||
|
var seconds = Math.floor((new Date() - date) / 1000);
|
||||||
|
|
||||||
|
var interval = Math.floor(seconds / 31536000);
|
||||||
|
|
||||||
|
if (interval >= 1) {
|
||||||
|
result[0] = interval + (interval == 1 ? " year ago" : " years ago");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
interval = Math.floor(seconds / 2592000);
|
||||||
|
if (interval >= 1) {
|
||||||
|
result[0] = interval + (interval == 1 ? " month ago" : " months ago");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
interval = Math.floor(seconds / 86400);
|
||||||
|
if (interval >= 1) {
|
||||||
|
result[0] = interval + (interval == 1 ? " day ago" : " days ago");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
interval = Math.floor(seconds / 3600);
|
||||||
|
if (interval >= 1) {
|
||||||
|
result[0] = interval + (interval == 1 ? " hour ago" : " hours ago");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
interval = Math.floor(seconds / 60);
|
||||||
|
if (interval >= 1) {
|
||||||
|
result[0] = interval + (interval == 1 ? " minute ago" : " minutes ago");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
if (seconds >= 1) {
|
||||||
|
result[0] = Math.floor(seconds) +
|
||||||
|
(seconds == 1 ? " second ago" : " seconds ago");
|
||||||
|
result[1] = true;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return ["less than a second ago", true];
|
||||||
|
}
|
||||||
|
|
||||||
|
function processMainDates() {
|
||||||
|
var result = false;
|
||||||
|
var threads = document.getElementById("talk-threads").children;
|
||||||
|
for (var i = 0; i < threads.length; i++)
|
||||||
|
{
|
||||||
|
var activity = threads[i].getElementsByClassName("activity")[0];
|
||||||
|
var activityDiv = activity.children[0];
|
||||||
|
var isoDate = activityDiv.children[0].innerHTML;
|
||||||
|
var parsed = Date.parse(isoDate);
|
||||||
|
var timeS = timeSince(parsed);
|
||||||
|
|
||||||
|
activityDiv.innerHTML = activityDiv.children[0].outerHTML +
|
||||||
|
timeS[0];
|
||||||
|
if (timeS[1]) { result = timeS[1] }
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue