2014-10-02 16:05:44 -04:00
|
|
|
'use strict';
|
|
|
|
|
2014-11-20 09:14:39 -05:00
|
|
|
var ngModule = angular.module('woUtil');
|
|
|
|
ngModule.service('notification', Notif);
|
|
|
|
module.exports = Notif;
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2015-04-28 04:13:13 -04:00
|
|
|
function Notif(appConfig, axe) {
|
2014-11-20 09:14:39 -05:00
|
|
|
this._appConfig = appConfig;
|
2015-04-28 04:13:13 -04:00
|
|
|
this._axe = axe;
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-11-20 09:14:39 -05: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
|
|
|
|
*/
|
2014-11-20 09:14:39 -05:00
|
|
|
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;
|
|
|
|
}
|
2014-04-01 07:16:39 -04:00
|
|
|
|
2014-11-20 09:14:39 -05:00
|
|
|
if (!self.hasPermission) {
|
2014-10-02 16:05:44 -04:00
|
|
|
// don't wait until callback returns
|
|
|
|
Notification.requestPermission(function(permission) {
|
|
|
|
if (permission === "granted") {
|
2014-11-20 09:14:39 -05:00
|
|
|
self.hasPermission = true;
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-04-01 07:16:39 -04:00
|
|
|
|
2015-04-28 04:13:13 -04:00
|
|
|
var notification;
|
|
|
|
try {
|
|
|
|
notification = new Notification(options.title, {
|
|
|
|
body: options.message,
|
|
|
|
icon: self._appConfig.config.iconPath
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
self._axe.error('Displaying notification failed: ' + err.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
notification.onclick = function() {
|
|
|
|
window.focus();
|
|
|
|
options.onClick();
|
|
|
|
};
|
2014-04-01 07:16:39 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
if (options.timeout > 0) {
|
|
|
|
setTimeout(function() {
|
|
|
|
notification.close();
|
|
|
|
}, options.timeout);
|
2014-09-16 13:01:49 -04:00
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
return notification;
|
|
|
|
};
|
2014-09-16 13:01:49 -04:00
|
|
|
|
2014-11-20 09:14:39 -05:00
|
|
|
Notif.prototype.close = function(notification) {
|
2014-12-19 08:22:46 -05:00
|
|
|
if (notification) {
|
|
|
|
notification.close();
|
|
|
|
}
|
2014-11-20 09:14:39 -05:00
|
|
|
};
|