Unify calculation of unread-count for notifications and use it in the title bar

Fixes trac #1004

(imported from commit 6f04d1d3ded198b46cc2ff1733b94b5c17beb581)
This commit is contained in:
Leo Franchi 2013-03-19 16:53:49 -04:00
parent 02554b28cb
commit 6263c9ba1e
5 changed files with 60 additions and 63 deletions

View File

@ -41,7 +41,7 @@ var globals =
+ ' home_unread_messages' + ' home_unread_messages'
+ ' maybe_scroll_to_selected recenter_pointer_on_display suppress_scroll_pointer_update' + ' maybe_scroll_to_selected recenter_pointer_on_display suppress_scroll_pointer_update'
+ ' process_visible_unread_messages message_range message_in_table process_loaded_for_unread' + ' process_visible_unread_messages message_range message_in_table process_loaded_for_unread'
+ ' mark_all_as_read message_unread process_read_messages' + ' mark_all_as_read message_unread process_read_messages unread_in_current_view'
; ;

View File

@ -4,7 +4,6 @@ var exports = {};
var notice_memory = {}; var notice_memory = {};
var window_has_focus = true; var window_has_focus = true;
var new_message_count = 0;
var asked_permission_already = false; var asked_permission_already = false;
var names; var names;
@ -16,18 +15,49 @@ function browser_desktop_notifications_on () {
window.webkitNotifications.checkPermission() === 0); window.webkitNotifications.checkPermission() === 0);
} }
function update_title_count(new_count) { exports.initialize = function () {
// Update window title and favicon to reflect new_message_count. names = fullname.toLowerCase().split(" ");
// names.push(email.split("@")[0].toLowerCase());
// If new_count is given, set new_message_count to that first. names.push("all");
var n; names.push("everyone");
names.push("<strong>" + fullname.toLowerCase() + "</strong>");
if (new_count !== undefined) { $(window).focus(function () {
if (new_message_count === new_count) window_has_focus = true;
return; exports.update_title_count();
new_message_count = new_count;
$.each(notice_memory, function (index, notice_mem_entry) {
notice_mem_entry.obj.cancel();
});
process_visible_unread_messages();
}).blur(function () {
window_has_focus = false;
}).mouseover(function () {
exports.update_title_count();
});
if (!window.webkitNotifications) {
return;
} }
$(document).click(function () {
if (!desktop_notifications_enabled || asked_permission_already) {
return;
}
if (window.webkitNotifications.checkPermission() !== 0) { // 0 is PERMISSION_ALLOWED
window.webkitNotifications.requestPermission(function () {});
asked_permission_already = true;
}
});
};
exports.update_title_count = function () {
// Update window title and favicon to reflect unread messages in current view
var n;
var new_message_count = unread_in_current_view();
document.title = (new_message_count ? ("(" + new_message_count + ") ") : "") document.title = (new_message_count ? ("(" + new_message_count + ") ") : "")
+ domain + " - Humbug"; + domain + " - Humbug";
@ -47,44 +77,6 @@ function update_title_count(new_count) {
util.set_favicon('/static/favicon.ico?v=2'); util.set_favicon('/static/favicon.ico?v=2');
} }
} }
}
exports.initialize = function () {
names = fullname.toLowerCase().split(" ");
names.push(email.split("@")[0].toLowerCase());
names.push("all");
names.push("everyone");
names.push("<strong>" + fullname.toLowerCase() + "</strong>");
$(window).focus(function () {
window_has_focus = true;
update_title_count(0);
$.each(notice_memory, function (index, notice_mem_entry) {
notice_mem_entry.obj.cancel();
});
process_visible_unread_messages();
}).blur(function () {
window_has_focus = false;
}).mouseover(function () {
update_title_count(0);
});
if (!window.webkitNotifications) {
return;
}
$(document).click(function () {
if (!desktop_notifications_enabled || asked_permission_already) {
return;
}
if (window.webkitNotifications.checkPermission() !== 0) { // 0 is PERMISSION_ALLOWED
window.webkitNotifications.requestPermission(function () {});
asked_permission_already = true;
}
});
}; };
exports.window_has_focus = function () { exports.window_has_focus = function () {
@ -208,7 +200,6 @@ exports.received_messages = function (messages) {
$.each(messages, function (index, message) { $.each(messages, function (index, message) {
if (message.sender_email !== email && narrow.message_in_home(message)) { if (message.sender_email !== email && narrow.message_in_home(message)) {
new_message_count++;
title_needs_update = true; title_needs_update = true;
if (desktop_notifications_enabled && if (desktop_notifications_enabled &&
@ -221,7 +212,7 @@ exports.received_messages = function (messages) {
}); });
if (title_needs_update) { if (title_needs_update) {
update_title_count(); exports.update_title_count();
} }
}; };

View File

@ -50,21 +50,11 @@ function hide() {
// If there's a custom message, or if the last message is off the bottom of the // If there's a custom message, or if the last message is off the bottom of the
// screen, then show the notifications bar. // screen, then show the notifications bar.
exports.update = function () { exports.update = function () {
var unread_messages_below = 0;
if (!narrow.active()) {
unread_messages_below = home_unread_messages;
} else {
$.each(current_msg_list.all(), function (idx, msg) {
if (message_unread(msg) && msg.id > current_msg_list.selected_id()) {
unread_messages_below += 1;
}
});
}
if (on_custom) if (on_custom)
show(custom_message); show(custom_message);
else if (rows.last_visible().offset() !== null // otherwise the next line will error else if (rows.last_visible().offset() !== null // otherwise the next line will error
&& rows.last_visible().offset().top + rows.last_visible().height() > viewport.scrollTop() + viewport.height() && rows.last_visible().offset().top + rows.last_visible().height() > viewport.scrollTop() + viewport.height()
&& unread_messages_below > 0) && unread_in_current_view() > 0)
show("More messages below"); show("More messages below");
else else
hide(); hide();

View File

@ -530,8 +530,10 @@ $(function () {
} else if (!have_scrolled_away_from_top) { } else if (!have_scrolled_away_from_top) {
have_scrolled_away_from_top = true; have_scrolled_away_from_top = true;
} }
// When the window scrolls, it may cause some messages to go off the screen // When the window scrolls, it may cause some messages to
// enter the screen and become read
notifications_bar.update(); notifications_bar.update();
notifications.update_title_count();
var new_selected = current_msg_list.selected_id(); var new_selected = current_msg_list.selected_id();
if (scroll_start_message === undefined) { if (scroll_start_message === undefined) {

View File

@ -192,6 +192,20 @@ function send_queued_flags() {
var unread_counts = {'stream': {}, 'private': {}}; var unread_counts = {'stream': {}, 'private': {}};
var home_unread_messages = 0; var home_unread_messages = 0;
function unread_in_current_view() {
var unread = 0;
if (!narrow.active()) {
unread = home_unread_messages;
} else {
$.each(current_msg_list.all(), function (idx, msg) {
if (message_unread(msg) && msg.id > current_msg_list.selected_id()) {
unread += 1;
}
});
}
return unread;
}
function message_unread(message) { function message_unread(message) {
if (message === undefined) { if (message === undefined) {
return false; return false;