Date change: refactoring

This commit is contained in:
Lorenz Hübschle-Schneider 2015-11-28 13:23:41 +01:00
parent b90a0f763d
commit da7a7f85e8
1 changed files with 61 additions and 67 deletions

View File

@ -19,18 +19,20 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific
models.closeBuffer(bufferId); models.closeBuffer(bufferId);
}; };
// inject a fake buffer line for date change // inject a fake buffer line for date change if needed
var injectDateChangeMessage = function(buffer, old_date, new_date) { var injectDateChangeMessageIfNeeded = function(buffer, old_date, new_date) {
old_date.setHours(0, 0, 0, 0);
new_date.setHours(0, 0, 0, 0);
// Check if the date changed
if (old_date.valueOf() !== new_date.valueOf()) {
var old_date_plus_one = old_date; var old_date_plus_one = old_date;
old_date_plus_one.setDate(old_date.getDate() + 1); old_date_plus_one.setDate(old_date.getDate() + 1);
new_date.setHours(0, 0, 0, 0);
old_date.setHours(0, 0, 0, 0);
// it's not always true that a date with time 00:00:00 // it's not always true that a date with time 00:00:00
// plus one day will be time 00:00:00 // plus one day will be time 00:00:00
old_date_plus_one.setHours(0, 0, 0, 0); old_date_plus_one.setHours(0, 0, 0, 0);
var content = "\u001943"; // this colour corresponds to chat_day_change var content = "\u001943"; // this colour corresponds to chat_day_change
// Add day of the week
content += new_date.toLocaleDateString(window.navigator.language, content += new_date.toLocaleDateString(window.navigator.language,
{weekday: "long"}); {weekday: "long"});
// if you're testing different date formats, // if you're testing different date formats,
@ -80,14 +82,6 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific
}; };
var new_message = new models.BufferLine(line); var new_message = new models.BufferLine(line);
buffer.addLine(new_message); 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);
} }
}; };
@ -99,9 +93,9 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific
if (message.displayed) { if (message.displayed) {
// Check for date change // Check for date change
if (buffer.lines.length > 0) { if (buffer.lines.length > 0) {
var previous_date = new Date(buffer.lines[buffer.lines.length - 1].date), var old_date = new Date(buffer.lines[buffer.lines.length - 1].date),
current_date = new Date(message.date); new_date = new Date(message.date);
injectDateChangeMessageIfNeeded(buffer, previous_date, current_date); injectDateChangeMessageIfNeeded(buffer, old_date, new_date);
} }
message = plugins.PluginManager.contentForMessage(message); message = plugins.PluginManager.contentForMessage(message);