From 157917d40c519d4c4b087c6308e186a7e0188771 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Thu, 20 Feb 2014 23:26:50 +0000 Subject: [PATCH] Rework notifications * Improve content of notifications * More descriptive title * The bug that caused the need for manually assembling the message has been fixed (#161 or c5e548c8ab41435dd22e7d56089fcec767bdf945) * Permission for displaying notifications can only be requested upon user interaction in Chrome, not on page load. Bind to connect button * Take user to g-b tab and buffer causing the notification on click --- js/glowingbear.js | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/js/glowingbear.js b/js/glowingbear.js index 2160e04..787b928 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -507,12 +507,16 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', console.log(watchers.length); }; - if (window.webkitNotifications !== undefined) { - if (window.webkitNotifications.checkPermission() === 0) { // 0 is PERMISSION_ALLOWED - $log.info('Notification permission status:', window.webkitNotifications.checkPermission() === 0); - window.webkitNotifications.requestPermission(); + $scope.requestWebkitNotificationPermission = function() { + if (window.webkitNotifications !== undefined) { + var havePermission = window.webkitNotifications.checkPermission(); + if (havePermission !== 0) { // 0 is PERMISSION_ALLOWED + $log.info('Notification permission status:', havePermission === 0); + window.webkitNotifications.requestPermission(); + } } - } + }; + // Check for firefox & app installed if (navigator.mozApps !== undefined) { navigator.mozApps.getSelf().onsuccess = function _onAppReady(evt) { @@ -721,6 +725,7 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', $scope.connect = function() { + $scope.requestWebkitNotificationPermission(); connection.connect($scope.host, $scope.port, $scope.password, $scope.ssl); }; $scope.disconnect = function() { @@ -749,25 +754,34 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', /* Function gets called from bufferLineAdded code if user should be notified */ $rootScope.createHighlight = function(buffer, message) { - var messages = ""; - message.content.forEach(function(part) { - if (part.text !== undefined) { - messages += part.text + " "; - } + var title = ''; + if (buffer.shortName.charAt(0) === '#') { + title = 'Highlight in '; + } else { + title = 'Private message from '; + } + title += buffer.shortName; + title += buffer.fullName.replace(/irc.([^\.]+)\..+/, " ($1)"); + + var notification = new Notification(title, { + body: message.text, + icon: 'img/favicon.png' }); - var title = buffer.fullName; - var content = messages; - - var timeout = 15*1000; - $log.info('Displaying notification:buffer:',buffer,',message:',message,',with timeout:',timeout); - var notification = new Notification(title, {body:content, icon:'img/favicon.png'}); // Cancel notification automatically + var timeout = 15*1000; notification.onshow = function() { setTimeout(function() { notification.close(); }, timeout); }; + + // Click takes the user to the buffer + notification.onclick = function() { + models.setActiveBuffer(buffer.id); + window.focus(); + notification.close(); + }; }; $scope.hasUnread = function(buffer) {