From d709c8fd930c12f968d2e8fb9e8e6d6beff681f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Thu, 16 Oct 2014 17:12:48 +0200 Subject: [PATCH] Cancel all outstanding notifications when disconnecting / navigating away Fixes #296 --- js/glowingbear.js | 5 +++++ js/notifications.js | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/js/glowingbear.js b/js/glowingbear.js index 66b9730..5e057ef 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -221,6 +221,11 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', }); $rootScope.$on('relayDisconnect', function() { + // Reset title + $rootScope.pageTitle = ''; + $rootScope.notificationStatus = ''; + notifications.cancelAll(); + models.reinitialize(); $rootScope.$emit('notificationChanged'); $scope.connectbutton = 'Connect'; diff --git a/js/notifications.js b/js/notifications.js index fbeba09..0752f1b 100644 --- a/js/notifications.js +++ b/js/notifications.js @@ -1,6 +1,8 @@ var weechat = angular.module('weechat'); weechat.factory('notifications', ['$rootScope', '$log', 'models', function($rootScope, $log, models) { + var notifications = []; + // Ask for permission to display desktop notifications var requestNotificationPermission = function() { // Firefox @@ -109,6 +111,10 @@ weechat.factory('notifications', ['$rootScope', '$log', 'models', function($root icon: 'assets/img/favicon.png' }); + // Save notification, so we can close all outstanding ones when disconnecting + notification.id = notifications.length; + notifications.push(notification); + // Cancel notification automatically var timeout = 15*1000; notification.onshow = function() { @@ -124,6 +130,11 @@ weechat.factory('notifications', ['$rootScope', '$log', 'models', function($root notification.close(); }; + // Remove from list of active notifications + notification.onclose = function() { + delete notifications[this.id]; + }; + if ($rootScope.soundnotification) { // TODO fill in a sound file var audioFile = "assets/audio/sonar"; @@ -132,10 +143,20 @@ weechat.factory('notifications', ['$rootScope', '$log', 'models', function($root } }; + var cancelAll = function() { + while (notifications.length > 0) { + var notification = notifications.pop(); + if (notification !== undefined) { + notification.close(); + } + } + }; + return { requestNotificationPermission: requestNotificationPermission, updateTitle: updateTitle, updateFavico: updateFavico, createHighlight: createHighlight, + cancelAll: cancelAll, }; }]);