Merge pull request #682 from pmelanson/date-change
Date change at the end of the buffer when changing buffers, more HR dates
This commit is contained in:
commit
b90a0f763d
|
@ -20,7 +20,7 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific
|
|||
};
|
||||
|
||||
// inject a fake buffer line for date change
|
||||
var injectDateChangeMessage = function(message, buffer, old_date, new_date) {
|
||||
var injectDateChangeMessage = function(buffer, old_date, new_date) {
|
||||
var old_date_plus_one = old_date;
|
||||
old_date_plus_one.setDate(old_date.getDate() + 1);
|
||||
|
||||
|
@ -30,23 +30,49 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific
|
|||
// plus one day will be time 00:00:00
|
||||
old_date_plus_one.setHours(0, 0, 0, 0);
|
||||
|
||||
var content = "\u001943" + // styling
|
||||
"Date changed to " + new_date.toDateString();
|
||||
var content = "\u001943"; // this colour corresponds to chat_day_change
|
||||
content += new_date.toLocaleDateString(window.navigator.language,
|
||||
{weekday: "long"});
|
||||
// if you're testing different date formats,
|
||||
// make sure to test different locales such as "en-US",
|
||||
// "en-US-u-ca-persian" (which has different weekdays, year 0, and an ERA)
|
||||
// "ja-JP-u-ca-persian-n-thai" (above, diff numbering, diff text)
|
||||
var extra_date_format = {
|
||||
day: "numeric",
|
||||
month: "long"
|
||||
};
|
||||
if (new_date.getYear() !== old_date.getYear()) {
|
||||
extra_date_format.year = "numeric";
|
||||
}
|
||||
content += " (";
|
||||
content += new_date.toLocaleDateString(window.navigator.language,
|
||||
extra_date_format);
|
||||
// Result should be something like
|
||||
// Friday (November 27)
|
||||
// or if the year is different,
|
||||
// Friday (November 27, 2015)
|
||||
|
||||
// Comparing dates in javascript is beyond tedious
|
||||
if (old_date_plus_one.valueOf() !== new_date.valueOf()) {
|
||||
var date_diff = Math.round((new_date - old_date)/(24*60*60*1000));
|
||||
var date_diff = Math.round((new_date - old_date)/(24*60*60*1000)) + 1;
|
||||
if (date_diff < 0) {
|
||||
date_diff = -1*(date_diff + 1);
|
||||
content += " (" + date_diff + " days before " + old_date.toDateString() + ")";
|
||||
date_diff = -1*(date_diff);
|
||||
if (date_diff === 1) {
|
||||
content += ", 1 day before";
|
||||
} else {
|
||||
content += " (" + date_diff + " days have passed since " + old_date.toDateString() + ")";
|
||||
content += ", " + date_diff + " days before";
|
||||
}
|
||||
} else {
|
||||
content += ", " + date_diff + " days later";
|
||||
}
|
||||
// Result: Friday (November 27, 5 days later)
|
||||
}
|
||||
content += ")";
|
||||
|
||||
var line = {
|
||||
buffer: buffer,
|
||||
date: new_date,
|
||||
prefix: '\u001943\u2500\u2500',
|
||||
prefix: '\u001943\u2500',
|
||||
tags_array: [],
|
||||
displayed: true,
|
||||
highlight: 0,
|
||||
|
@ -56,6 +82,15 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific
|
|||
buffer.addLine(new_message);
|
||||
};
|
||||
|
||||
// wrapper to do the logic checking
|
||||
var injectDateChangeMessageIfNeeded = function(buffer, previous_date, current_date) {
|
||||
previous_date.setHours(0, 0, 0, 0);
|
||||
current_date.setHours(0, 0, 0, 0);
|
||||
if (previous_date.valueOf() !== current_date.valueOf()) {
|
||||
injectDateChangeMessage(buffer, previous_date, current_date);
|
||||
}
|
||||
};
|
||||
|
||||
var handleLine = function(line, manually) {
|
||||
var message = new models.BufferLine(line);
|
||||
var buffer = models.getBuffer(message.buffer);
|
||||
|
@ -66,11 +101,7 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific
|
|||
if (buffer.lines.length > 0) {
|
||||
var previous_date = new Date(buffer.lines[buffer.lines.length - 1].date),
|
||||
current_date = new Date(message.date);
|
||||
previous_date.setHours(0, 0, 0, 0);
|
||||
current_date.setHours(0, 0, 0, 0);
|
||||
if (previous_date.valueOf() !== current_date.valueOf()) {
|
||||
injectDateChangeMessage(message, buffer, previous_date, current_date);
|
||||
}
|
||||
injectDateChangeMessageIfNeeded(buffer, previous_date, current_date);
|
||||
}
|
||||
|
||||
message = plugins.PluginManager.contentForMessage(message);
|
||||
|
@ -235,6 +266,13 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific
|
|||
lines.forEach(function(l) {
|
||||
handleLine(l, manually);
|
||||
});
|
||||
if (message.objects[0].content.length > 0) {
|
||||
var last_line =
|
||||
message.objects[0].content[message.objects[0].content.length-1];
|
||||
var last_message = new models.BufferLine(last_line);
|
||||
var buffer = models.getBuffer(last_message.buffer);
|
||||
injectDateChangeMessageIfNeeded(buffer, last_message.date, new Date());
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
|
|
Loading…
Reference in New Issue