gitlab-org--gitlab-foss/app/assets/javascripts/lib/utils/notify.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

51 lines
1.3 KiB
JavaScript
Raw Normal View History

/* eslint-disable consistent-return, no-return-assign */
2017-05-26 14:26:06 +00:00
function notificationGranted(message, opts, onclick) {
const notification = new Notification(message, opts);
setTimeout(
() =>
// Hide the notification after X amount of seconds
notification.close(),
8000,
);
2017-05-26 14:26:06 +00:00
2018-10-10 06:25:43 +00:00
return (notification.onclick = onclick || notification.close);
2017-05-26 14:26:06 +00:00
}
2016-07-24 20:45:11 +00:00
2017-05-26 14:26:06 +00:00
function notifyPermissions() {
/* eslint-disable-next-line @gitlab/require-i18n-strings */
2017-05-26 14:26:06 +00:00
if ('Notification' in window) {
return Notification.requestPermission();
}
}
function notifyMe(message, body, icon, onclick) {
const opts = {
body,
icon,
2017-05-26 14:26:06 +00:00
};
// Let's check if the browser supports notifications
/* eslint-disable-next-line @gitlab/require-i18n-strings */
2017-05-26 14:26:06 +00:00
if (!('Notification' in window)) {
// do nothing
} else if (Notification.permission === 'granted') {
// If it's okay let's create a notification
return notificationGranted(message, opts, onclick);
} else if (Notification.permission !== 'denied') {
return Notification.requestPermission((permission) => {
2017-05-26 14:26:06 +00:00
// If the user accepts, let's create a notification
if (permission === 'granted') {
2016-07-24 20:45:11 +00:00
return notificationGranted(message, opts, onclick);
}
2017-05-26 14:26:06 +00:00
});
}
}
const notify = {
notificationGranted,
notifyPermissions,
notifyMe,
};
export default notify;