2014-04-01 07:16:39 -04:00
|
|
|
define(function(require) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var cfg = require('js/app-config').config;
|
|
|
|
|
|
|
|
var self = {};
|
|
|
|
|
2014-09-16 13:01:49 -04:00
|
|
|
if (window.Notification) {
|
|
|
|
self.hasPermission = Notification.permission === "granted";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
self.create = function(options) {
|
|
|
|
options.onClick = options.onClick || function() {};
|
|
|
|
|
|
|
|
if (!window.Notification) {
|
|
|
|
return;
|
2014-04-01 07:16:39 -04:00
|
|
|
}
|
|
|
|
|
2014-09-16 13:01:49 -04:00
|
|
|
if (!self.hasPermission) {
|
|
|
|
// don't wait until callback returns
|
|
|
|
Notification.requestPermission(function(permission) {
|
|
|
|
if (permission === "granted") {
|
|
|
|
self.hasPermission = true;
|
|
|
|
}
|
|
|
|
});
|
2014-04-01 07:16:39 -04:00
|
|
|
}
|
2014-09-16 13:01:49 -04:00
|
|
|
|
|
|
|
var notification = new Notification(options.title, {
|
|
|
|
body: options.message,
|
|
|
|
icon: cfg.iconPath
|
|
|
|
});
|
|
|
|
notification.onclick = options.onClick;
|
|
|
|
|
|
|
|
if (options.timeout > 0) {
|
|
|
|
setTimeout(function() {
|
|
|
|
notification.close();
|
|
|
|
}, options.timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
return notification;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.close = function(notification) {
|
|
|
|
notification.close();
|
2014-04-01 07:16:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
|
|
|
});
|