notifications.js
47 lines
| 1.4 KiB
| application/javascript
|
JavascriptLexer
r526 | "use strict"; | |||
function notifySystem(data) { | ||||
var notification = new Notification(data.message.level + ': ' + data.message.message); | ||||
}; | ||||
function notifyToaster(data){ | ||||
r699 | var notifications = document.getElementById('notifications'); | |||
notifications.push('toasts', | ||||
{ level: data.message.level, | ||||
message: data.message.message | ||||
}); | ||||
notifications.open(); | ||||
r526 | } | |||
function handleNotifications(data) { | ||||
r691 | if (!templateContext.rhodecode_user.notification_status && !data.message.testMessage) { | |||
r526 | // do not act if notifications are disabled | |||
return | ||||
} | ||||
// use only js notifications for now | ||||
var onlyJS = true; | ||||
if (!("Notification" in window) || onlyJS) { | ||||
// use legacy notificartion | ||||
notifyToaster(data); | ||||
} | ||||
else { | ||||
// Let's check whether notification permissions have already been granted | ||||
if (Notification.permission === "granted") { | ||||
notifySystem(data); | ||||
} | ||||
// Otherwise, we need to ask the user for permission | ||||
else if (Notification.permission !== 'denied') { | ||||
Notification.requestPermission(function (permission) { | ||||
if (permission === "granted") { | ||||
notifySystem(data); | ||||
} | ||||
}); | ||||
} | ||||
else{ | ||||
notifyToaster(data); | ||||
} | ||||
} | ||||
}; | ||||
$.Topic('/notifications').subscribe(handleNotifications); | ||||