2013-01-14 17:26:50 +01:00
|
|
|
var timerender = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2013-02-12 23:26:25 +01:00
|
|
|
var next_timerender_id = 0;
|
|
|
|
|
|
|
|
var set_to_start_of_day = function (time) {
|
|
|
|
return time.setMilliseconds(0).setSeconds(0).setMinutes(0).setHours(0);
|
|
|
|
};
|
|
|
|
|
2016-12-05 07:02:18 +01:00
|
|
|
function now() { return new XDate(); }
|
2013-02-12 23:26:25 +01:00
|
|
|
|
|
|
|
// Given an XDate object 'time', return a two-element list containing
|
|
|
|
// - a string for the current human-formatted version
|
2013-06-25 16:22:14 +02:00
|
|
|
// - a boolean for if it will need to be updated when the day changes
|
2016-07-26 20:35:03 +02:00
|
|
|
exports.render_now = function (time) {
|
2013-02-12 23:26:25 +01:00
|
|
|
var start_of_today = set_to_start_of_day(now());
|
|
|
|
var start_of_other_day = set_to_start_of_day(time.clone());
|
|
|
|
|
|
|
|
// How many days old is 'time'? 0 = today, 1 = yesterday, 7 = a
|
|
|
|
// week ago, -1 = tomorrow, etc.
|
|
|
|
|
|
|
|
// Presumably the result of diffDays will be an integer in this
|
2013-02-26 20:32:49 +01:00
|
|
|
// case, but round it to be sure before comparing to integer
|
|
|
|
// constants.
|
2013-02-12 23:26:25 +01:00
|
|
|
var days_old = Math.round(start_of_other_day.diffDays(start_of_today));
|
|
|
|
|
2013-06-25 16:22:14 +02:00
|
|
|
if (days_old === 0) {
|
|
|
|
return ["Today", true];
|
|
|
|
} else if (days_old === 1) {
|
|
|
|
return ["Yesterday", true];
|
2016-07-13 00:43:19 +02:00
|
|
|
} else if (days_old >= 365) {
|
|
|
|
// For long running servers, searching backlog can get ambiguous
|
|
|
|
// without a year stamp. Only show year if message is over a year old.
|
|
|
|
return [time.toString("MMM\xa0dd,\xa0yyyy"), false];
|
2013-02-12 23:26:25 +01:00
|
|
|
}
|
2016-12-02 21:34:35 +01:00
|
|
|
// For now, if we get a message from tomorrow, we don't bother
|
|
|
|
// rewriting the timestamp when it gets to be tomorrow.
|
|
|
|
|
|
|
|
// "\xa0" is U+00A0 NO-BREAK SPACE.
|
|
|
|
// Can't use as that represents the literal string " ".
|
|
|
|
return [time.toString("MMM\xa0dd"), false];
|
2016-07-26 20:35:03 +02:00
|
|
|
};
|
2013-02-12 23:26:25 +01:00
|
|
|
|
2013-06-25 16:22:14 +02:00
|
|
|
// List of the dates that need to be updated when the day changes.
|
2013-02-12 23:26:25 +01:00
|
|
|
// Each timestamp is represented as a list of length 2:
|
|
|
|
// [id of the span element, XDate representing the time]
|
2013-06-25 16:22:14 +02:00
|
|
|
var update_list = [];
|
2013-02-12 23:26:25 +01:00
|
|
|
|
2013-06-25 16:22:14 +02:00
|
|
|
// The time at the beginning of the next day, when the timestamps are updated.
|
2013-02-12 23:26:25 +01:00
|
|
|
// Represented as an XDate with hour, minute, second, millisecond 0.
|
2013-06-25 16:22:14 +02:00
|
|
|
var next_update;
|
2013-05-16 22:47:53 +02:00
|
|
|
$(function () {
|
2013-06-25 16:22:14 +02:00
|
|
|
next_update = set_to_start_of_day(now()).addDays(1);
|
2013-05-16 22:47:53 +02:00
|
|
|
});
|
2013-02-12 23:26:25 +01:00
|
|
|
|
2013-08-29 01:34:10 +02:00
|
|
|
// time_above is an optional argument, to support dates that look like:
|
|
|
|
// --- ▲ Yesterday ▲ ------ ▼ Today ▼ ---
|
2016-12-05 07:02:18 +01:00
|
|
|
function maybe_add_update_list_entry(needs_update, id, time, time_above) {
|
2013-06-25 16:22:14 +02:00
|
|
|
if (needs_update) {
|
2013-08-29 01:34:10 +02:00
|
|
|
if (time_above !== undefined) {
|
|
|
|
update_list.push([id, time, time_above]);
|
|
|
|
} else {
|
|
|
|
update_list.push([id, time]);
|
|
|
|
}
|
2013-06-25 16:22:14 +02:00
|
|
|
}
|
2013-02-12 23:26:25 +01:00
|
|
|
}
|
|
|
|
|
2013-08-29 01:34:10 +02:00
|
|
|
function render_date_span(elem, time_str, time_above_str) {
|
|
|
|
elem.text("");
|
|
|
|
if (time_above_str !== undefined) {
|
2013-09-10 18:40:31 +02:00
|
|
|
return elem.append('<i class="date-direction icon-vector-caret-up"></i>' +
|
|
|
|
time_above_str).append($('<hr class="date-line">')).append('<i class="date-direction icon-vector-caret-down"></i>'
|
|
|
|
+ time_str);
|
2013-08-29 01:34:10 +02:00
|
|
|
}
|
2016-12-02 21:34:35 +01:00
|
|
|
return elem.append(time_str);
|
2013-08-29 01:34:10 +02:00
|
|
|
}
|
|
|
|
|
2013-01-14 17:26:50 +01:00
|
|
|
// Given an XDate object 'time', return a DOM node that initially
|
2013-07-01 20:51:39 +02:00
|
|
|
// displays the human-formatted date, and is updated automatically as
|
|
|
|
// necessary (e.g. changing "Today" to "Yesterday" to "Jul 1").
|
2013-08-29 01:34:10 +02:00
|
|
|
// If two dates are given, it renders them as:
|
|
|
|
// --- ▲ Yesterday ▲ ------ ▼ Today ▼ ---
|
2013-01-14 17:26:50 +01:00
|
|
|
|
2013-02-12 23:26:25 +01:00
|
|
|
// (What's actually spliced into the message template is the contents
|
|
|
|
// of this DOM node as HTML, so effectively a copy of the node. That's
|
|
|
|
// okay since to update the time later we look up the node by its id.)
|
2013-08-29 01:34:10 +02:00
|
|
|
exports.render_date = function (time, time_above) {
|
2013-02-12 23:26:25 +01:00
|
|
|
var id = "timerender" + next_timerender_id;
|
2016-11-30 19:05:04 +01:00
|
|
|
next_timerender_id += 1;
|
2016-07-26 20:35:03 +02:00
|
|
|
var rendered_time = exports.render_now(time);
|
2013-08-29 01:34:10 +02:00
|
|
|
var node = $("<span />").attr('id', id);
|
|
|
|
if (time_above !== undefined) {
|
2016-07-26 20:35:03 +02:00
|
|
|
var rendered_time_above = exports.render_now(time_above);
|
2013-08-29 01:34:10 +02:00
|
|
|
node = render_date_span(node, rendered_time[0], rendered_time_above[0]);
|
|
|
|
} else {
|
|
|
|
node = render_date_span(node, rendered_time[0]);
|
|
|
|
}
|
|
|
|
maybe_add_update_list_entry(rendered_time[1], id, time, time_above);
|
2013-02-12 23:26:25 +01:00
|
|
|
return node;
|
2013-01-14 17:26:50 +01:00
|
|
|
};
|
|
|
|
|
2013-02-26 20:44:44 +01:00
|
|
|
// This isn't expected to be called externally except manually for
|
|
|
|
// testing purposes.
|
|
|
|
exports.update_timestamps = function () {
|
2013-06-25 16:22:14 +02:00
|
|
|
var time = now();
|
|
|
|
if (time >= next_update) {
|
|
|
|
var to_process = update_list;
|
|
|
|
update_list = [];
|
|
|
|
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(to_process, function (elem) {
|
2013-06-25 16:22:14 +02:00
|
|
|
var id = elem[0];
|
|
|
|
var element = document.getElementById(id);
|
|
|
|
// The element might not exist any more (because it
|
|
|
|
// was in the zfilt table, or because we added
|
|
|
|
// messages above it and re-collapsed).
|
|
|
|
if (element !== null) {
|
|
|
|
var time = elem[1];
|
2013-08-29 01:34:10 +02:00
|
|
|
var time_above;
|
2016-07-26 20:35:03 +02:00
|
|
|
var rendered_time = exports.render_now(time);
|
2013-08-29 01:34:10 +02:00
|
|
|
if (elem.length === 3) {
|
|
|
|
time_above = elem[2];
|
2016-07-26 20:35:03 +02:00
|
|
|
var rendered_time_above = exports.render_now(time_above);
|
2013-08-29 01:34:10 +02:00
|
|
|
render_date_span($(element), rendered_time[0], rendered_time_above[0]);
|
|
|
|
} else {
|
|
|
|
render_date_span($(element), rendered_time[0]);
|
|
|
|
}
|
|
|
|
maybe_add_update_list_entry(rendered_time[1], id, time, time_above);
|
2013-06-25 16:22:14 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
next_update = set_to_start_of_day(time.clone().addDays(1));
|
2013-02-12 23:26:25 +01:00
|
|
|
}
|
2013-02-26 20:44:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
setInterval(exports.update_timestamps, 60 * 1000);
|
2013-02-12 23:26:25 +01:00
|
|
|
|
2013-03-05 00:18:04 +01:00
|
|
|
// XDate.toLocaleDateString and XDate.toLocaleTimeString are
|
|
|
|
// expensive, so we delay running the following code until we need
|
|
|
|
// the full date and time strings.
|
|
|
|
exports.set_full_datetime = function timerender_set_full_datetime(message, time_elem) {
|
|
|
|
if (message.full_date_str !== undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var time = new XDate(message.timestamp * 1000);
|
|
|
|
// Convert to number of hours ahead/behind UTC.
|
|
|
|
// The sign of getTimezoneOffset() is reversed wrt
|
|
|
|
// the conventional meaning of UTC+n / UTC-n
|
|
|
|
var tz_offset = -time.getTimezoneOffset() / 60;
|
|
|
|
|
|
|
|
message.full_date_str = time.toLocaleDateString();
|
|
|
|
message.full_time_str = time.toLocaleTimeString() +
|
|
|
|
' (UTC' + ((tz_offset < 0) ? '' : '+') + tz_offset + ')';
|
|
|
|
|
|
|
|
time_elem.attr('title', message.full_date_str + ' ' + message.full_time_str);
|
|
|
|
};
|
|
|
|
|
2013-01-14 17:26:50 +01:00
|
|
|
return exports;
|
|
|
|
}());
|