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

49 lines
1.7 KiB
JavaScript
Raw Normal View History

/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, one-var, one-var-declaration-per-line, consistent-return, prefer-arrow-callback, no-return-assign, object-shorthand, comma-dangle, no-param-reassign, padded-blocks, max-len */
2016-07-24 16:45:11 -04:00
(function() {
(function(w) {
var notificationGranted, notifyMe, notifyPermissions;
notificationGranted = function(message, opts, onclick) {
var notification;
notification = new Notification(message, opts);
setTimeout(function() {
return notification.close();
// Hide the notification after X amount of seconds
2016-07-24 16:45:11 -04:00
}, 8000);
if (onclick) {
return notification.onclick = onclick;
}
};
notifyPermissions = function() {
if ('Notification' in window) {
return Notification.requestPermission();
}
};
notifyMe = function(message, body, icon, onclick) {
var opts;
opts = {
body: body,
icon: icon
};
// Let's check if the browser supports notifications
2016-07-24 16:45:11 -04:00
if (!('Notification' in window)) {
// do nothing
2016-07-24 16:45:11 -04:00
} else if (Notification.permission === 'granted') {
// If it's okay let's create a notification
2016-07-24 16:45:11 -04:00
return notificationGranted(message, opts, onclick);
} else if (Notification.permission !== 'denied') {
return Notification.requestPermission(function(permission) {
// If the user accepts, let's create a notification
2016-07-24 16:45:11 -04:00
if (permission === 'granted') {
return notificationGranted(message, opts, onclick);
}
});
}
};
w.notify = notifyMe;
return w.notifyPermissions = notifyPermissions;
})(window);
}).call(this);