From 557332e37a7f615bafb767d8ba388c5a3a729944 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Tue, 10 Apr 2012 11:16:13 +0200 Subject: Lafargue: Add desktop notification feature for WebKit-based browsers. --- static/js/lafargue.js | 45 ++++++++++++++++++++++++++++++++++++++++++++- static/style/benki.css | 14 +++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) (limited to 'static') diff --git a/static/js/lafargue.js b/static/js/lafargue.js index f1198cb..baf7112 100644 --- a/static/js/lafargue.js +++ b/static/js/lafargue.js @@ -1,10 +1,17 @@ jQuery(function($) { + var notificationsp = false; + if (WebSocket) { var websocket_base = $('head').attr('data-websocket-base'); var open_socket = function() { var socket = new WebSocket(websocket_base + '/lafargue/events'); socket.onmessage = function(event) { - $('.lafargue-list').prepend(event.data); + var message = JSON.parse(event.data); + $('.lafargue-list').prepend(message.html); + if (window.webkitNotifications && notificationsp) { + var notification = window.webkitNotifications.createNotification(null, '', 'Lafargue', 'New message by ' + message.first_name); + notification.show(); + } }; var reconnect = function() { window.setTimeout(function() { @@ -16,4 +23,40 @@ jQuery(function($) { }; open_socket(); } + + var updateNotificationStatus = function() { + var status = $('#notification-status'); + if (notificationsp) { + status.removeClass('bad'); + status.addClass('good'); + status.html('Notifications are ON.'); + } else { + status.addClass('bad'); + status.removeClass('good'); + status.html('Notifications are OFF.'); + } + }; + + var toggleNotifications = function() { + if (!notificationsp) { + if (!window.webkitNotifications.checkPermission() !== 0) { + window.webkitNotifications.requestPermission(function() { + updateNotificationStatus(); + }); + } + notificationsp = true; + } else { + notificationsp = false; + updateNotificationStatus(); + } + }; + + if (window.webkitNotifications) { + var notify_button = $('.notifications').append('
Notifications are OFF.
'); + notify_button.click(toggleNotifications); + if (window.webkitNotifications.checkPermission() === 0) { + notificationsp = true; + } + updateNotificationStatus(); + }; }); diff --git a/static/style/benki.css b/static/style/benki.css index 3904f47..c60e170 100644 --- a/static/style/benki.css +++ b/static/style/benki.css @@ -1,4 +1,4 @@ -#login-message { +#notifications { text-align: right; position: absolute; right: 0; @@ -17,3 +17,15 @@ padding: 0.5em 1em 0.5em 1em; background-color: yellow; } + +.bad { + background-color: yellow; +} + +.good { + background-color: green; +} + +.notification { + padding: 0.5em 1em 0.5em 1em; +} -- cgit v1.2.3