Show "how many messages have arrived while away" in the document title

(imported from commit 6e2a18be9953694048d107a56a03a67f8bc2bd5a)
This commit is contained in:
Jeff Arnold 2012-11-26 17:57:31 -05:00
parent 60789abd7f
commit 2f882b505a
4 changed files with 34 additions and 17 deletions

View File

@ -6,9 +6,9 @@
<head>
<meta charset="UTF-8">
{% if user_profile.realm.domain %}
<title>Humbug for {{user_profile.realm.domain}}</title>
<title>Humbug - {{user_profile.realm.domain}}</title>
{% else %}
<title>Humbug, from Humbug Inc.</title>
<title>Humbug</title>
{% endif %}
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>

View File

@ -62,6 +62,7 @@
var initial_pointer = {{ user_profile.pointer }};
var email = "{{ user_profile.user.email|escapejs }}";
var domain = "{{ user_profile.realm.domain|escapejs }}";
var have_initial_messages = {{ have_initial_messages|escapejs }};
var desktop_notifications_enabled = "{{ user_profile.enable_desktop_notifications|escapejs }}" === "True";

View File

@ -7,7 +7,7 @@ var globals =
// index.html
+ ' initial_pointer email stream_list people_list have_initial_messages'
+ ' desktop_notifications_enabled'
+ ' desktop_notifications_enabled domain'
// common.js
+ ' status_classes'

View File

@ -2,19 +2,29 @@ var notifications = (function () {
var exports = {};
var notice_memory = {};
var window_has_focus = true;
var new_message_count = 0;
function browser_desktop_notifications_on () {
return (window.webkitNotifications &&
// 0 is PERMISSION_ALLOWED
window.webkitNotifications.checkPermission() === 0);
}
exports.initialize = function () {
if (!window.webkitNotifications) {
return;
}
$(window).focus(function () {
window_has_focus = true;
new_message_count = 0;
document.title = "Humbug - " + domain;
}).blur(function () {
window_has_focus = false;
});
if (!window.webkitNotifications) {
return;
}
$(document).click(function () {
if (!desktop_notifications_enabled) {
return;
@ -30,9 +40,7 @@ function gravatar_url(message) {
"?d=identicon&s=30?stamp=" + ui.get_gravatar_stamp();
}
var notice_memory = {};
function process_message(message) {
function process_desktop_notification(message) {
var i, notification_object;
var key = message.display_reply_to;
var title = message.sender_full_name;
@ -93,19 +101,27 @@ function process_message(message) {
}
exports.received_messages = function (messages) {
var i;
if (!window.webkitNotifications ||
!desktop_notifications_enabled ||
window_has_focus) {
var i, title_needs_update = false;
if (window_has_focus) {
return;
}
$.each(messages, function (index, message) {
if (message.sender_email !== email &&
(message.type === "personal" || message.type === "huddle")) {
process_message(message);
if (message.sender_email !== email) {
new_message_count++;
title_needs_update = true;
if (desktop_notifications_enabled &&
browser_desktop_notifications_on() &&
(message.type === "personal" || message.type === "huddle")) {
process_desktop_notification(message);
}
}
});
if (title_needs_update) {
document.title = "(" + new_message_count + ") Humbug - " + domain;
}
};
return exports;