Date change: refactoring
This commit is contained in:
parent
b90a0f763d
commit
da7a7f85e8
128
js/handlers.js
128
js/handlers.js
|
@ -19,75 +19,69 @@ 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) {
|
||||||
var old_date_plus_one = old_date;
|
|
||||||
old_date_plus_one.setDate(old_date.getDate() + 1);
|
|
||||||
|
|
||||||
new_date.setHours(0, 0, 0, 0);
|
|
||||||
old_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
|
new_date.setHours(0, 0, 0, 0);
|
||||||
// plus one day will be time 00:00:00
|
// Check if the date changed
|
||||||
old_date_plus_one.setHours(0, 0, 0, 0);
|
if (old_date.valueOf() !== new_date.valueOf()) {
|
||||||
|
var old_date_plus_one = old_date;
|
||||||
|
old_date_plus_one.setDate(old_date.getDate() + 1);
|
||||||
|
// it's not always true that a date with time 00:00:00
|
||||||
|
// plus one day will be time 00:00:00
|
||||||
|
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
|
||||||
content += new_date.toLocaleDateString(window.navigator.language,
|
// Add day of the week
|
||||||
{weekday: "long"});
|
content += new_date.toLocaleDateString(window.navigator.language,
|
||||||
// if you're testing different date formats,
|
{weekday: "long"});
|
||||||
// make sure to test different locales such as "en-US",
|
// if you're testing different date formats,
|
||||||
// "en-US-u-ca-persian" (which has different weekdays, year 0, and an ERA)
|
// make sure to test different locales such as "en-US",
|
||||||
// "ja-JP-u-ca-persian-n-thai" (above, diff numbering, diff text)
|
// "en-US-u-ca-persian" (which has different weekdays, year 0, and an ERA)
|
||||||
var extra_date_format = {
|
// "ja-JP-u-ca-persian-n-thai" (above, diff numbering, diff text)
|
||||||
day: "numeric",
|
var extra_date_format = {
|
||||||
month: "long"
|
day: "numeric",
|
||||||
};
|
month: "long"
|
||||||
if (new_date.getYear() !== old_date.getYear()) {
|
};
|
||||||
extra_date_format.year = "numeric";
|
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)) + 1;
|
|
||||||
if (date_diff < 0) {
|
|
||||||
date_diff = -1*(date_diff);
|
|
||||||
if (date_diff === 1) {
|
|
||||||
content += ", 1 day before";
|
|
||||||
} else {
|
|
||||||
content += ", " + date_diff + " days before";
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
content += ", " + date_diff + " days later";
|
|
||||||
}
|
}
|
||||||
// Result: Friday (November 27, 5 days later)
|
content += " (";
|
||||||
}
|
content += new_date.toLocaleDateString(window.navigator.language,
|
||||||
content += ")";
|
extra_date_format);
|
||||||
|
// Result should be something like
|
||||||
|
// Friday (November 27)
|
||||||
|
// or if the year is different,
|
||||||
|
// Friday (November 27, 2015)
|
||||||
|
|
||||||
var line = {
|
// Comparing dates in javascript is beyond tedious
|
||||||
buffer: buffer,
|
if (old_date_plus_one.valueOf() !== new_date.valueOf()) {
|
||||||
date: new_date,
|
var date_diff = Math.round((new_date - old_date)/(24*60*60*1000)) + 1;
|
||||||
prefix: '\u001943\u2500',
|
if (date_diff < 0) {
|
||||||
tags_array: [],
|
date_diff = -1*(date_diff);
|
||||||
displayed: true,
|
if (date_diff === 1) {
|
||||||
highlight: 0,
|
content += ", 1 day before";
|
||||||
message: content
|
} else {
|
||||||
};
|
content += ", " + date_diff + " days before";
|
||||||
var new_message = new models.BufferLine(line);
|
}
|
||||||
buffer.addLine(new_message);
|
} else {
|
||||||
};
|
content += ", " + date_diff + " days later";
|
||||||
|
}
|
||||||
|
// Result: Friday (November 27, 5 days later)
|
||||||
|
}
|
||||||
|
content += ")";
|
||||||
|
|
||||||
// wrapper to do the logic checking
|
var line = {
|
||||||
var injectDateChangeMessageIfNeeded = function(buffer, previous_date, current_date) {
|
buffer: buffer,
|
||||||
previous_date.setHours(0, 0, 0, 0);
|
date: new_date,
|
||||||
current_date.setHours(0, 0, 0, 0);
|
prefix: '\u001943\u2500',
|
||||||
if (previous_date.valueOf() !== current_date.valueOf()) {
|
tags_array: [],
|
||||||
injectDateChangeMessage(buffer, previous_date, current_date);
|
displayed: true,
|
||||||
|
highlight: 0,
|
||||||
|
message: content
|
||||||
|
};
|
||||||
|
var new_message = new models.BufferLine(line);
|
||||||
|
buffer.addLine(new_message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
|
Loading…
Reference in New Issue