mail/src/js/util/notification.js

64 lines
1.6 KiB
JavaScript
Raw Normal View History

2014-10-02 16:05:44 -04:00
'use strict';
var ngModule = angular.module('woUtil');
ngModule.service('notification', Notif);
module.exports = Notif;
2014-10-02 16:05:44 -04:00
function Notif(appConfig) {
this._appConfig = appConfig;
2014-10-02 16:05:44 -04:00
if (window.Notification) {
this.hasPermission = Notification.permission === "granted";
}
2014-10-02 16:05:44 -04:00
}
/**
* Creates a notification. Requests permission if not already granted
*
* @param {String} options.title The notification title
* @param {String} options.message The notification message
* @param {Number} options.timeout (optional) Timeout when the notification is closed in milliseconds
* @param {Function} options.onClick (optional) callback when the notification is clicked
* @returns {Notification} A notification instance
*/
Notif.prototype.create = function(options) {
var self = this;
2014-10-02 16:05:44 -04:00
options.onClick = options.onClick || function() {};
if (!window.Notification) {
return;
}
if (!self.hasPermission) {
2014-10-02 16:05:44 -04:00
// don't wait until callback returns
Notification.requestPermission(function(permission) {
if (permission === "granted") {
self.hasPermission = true;
2014-10-02 16:05:44 -04:00
}
});
}
2014-10-02 16:05:44 -04:00
var notification = new Notification(options.title, {
body: options.message,
icon: self._appConfig.config.iconPath
2014-10-02 16:05:44 -04:00
});
notification.onclick = function() {
window.focus();
options.onClick();
};
2014-10-02 16:05:44 -04:00
if (options.timeout > 0) {
setTimeout(function() {
notification.close();
}, options.timeout);
}
2014-10-02 16:05:44 -04:00
return notification;
};
Notif.prototype.close = function(notification) {
if (notification) {
notification.close();
}
};