Merge pull request #164 from lorenzhs/notifications

Rework notifications
This commit is contained in:
David Cormier 2014-02-20 19:31:32 -05:00
commit f10e44efe4

View file

@ -507,12 +507,16 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
console.log(watchers.length); console.log(watchers.length);
}; };
$scope.requestWebkitNotificationPermission = function() {
if (window.webkitNotifications !== undefined) { if (window.webkitNotifications !== undefined) {
if (window.webkitNotifications.checkPermission() === 0) { // 0 is PERMISSION_ALLOWED var havePermission = window.webkitNotifications.checkPermission();
$log.info('Notification permission status:', window.webkitNotifications.checkPermission() === 0); if (havePermission !== 0) { // 0 is PERMISSION_ALLOWED
$log.info('Notification permission status:', havePermission === 0);
window.webkitNotifications.requestPermission(); window.webkitNotifications.requestPermission();
} }
} }
};
// Check for firefox & app installed // Check for firefox & app installed
if (navigator.mozApps !== undefined) { if (navigator.mozApps !== undefined) {
navigator.mozApps.getSelf().onsuccess = function _onAppReady(evt) { navigator.mozApps.getSelf().onsuccess = function _onAppReady(evt) {
@ -721,6 +725,7 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
$scope.connect = function() { $scope.connect = function() {
$scope.requestWebkitNotificationPermission();
connection.connect($scope.host, $scope.port, $scope.password, $scope.ssl); connection.connect($scope.host, $scope.port, $scope.password, $scope.ssl);
}; };
$scope.disconnect = function() { $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 */ /* Function gets called from bufferLineAdded code if user should be notified */
$rootScope.createHighlight = function(buffer, message) { $rootScope.createHighlight = function(buffer, message) {
var messages = ""; var title = '';
message.content.forEach(function(part) { if (buffer.shortName.charAt(0) === '#') {
if (part.text !== undefined) { title = 'Highlight in ';
messages += part.text + " "; } 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 // Cancel notification automatically
var timeout = 15*1000;
notification.onshow = function() { notification.onshow = function() {
setTimeout(function() { setTimeout(function() {
notification.close(); notification.close();
}, timeout); }, timeout);
}; };
// Click takes the user to the buffer
notification.onclick = function() {
models.setActiveBuffer(buffer.id);
window.focus();
notification.close();
};
}; };
$scope.hasUnread = function(buffer) { $scope.hasUnread = function(buffer) {