2014-06-22 09:04:07 +00:00
|
|
|
'use strict';
|
|
|
|
|
2014-06-22 10:02:22 +00:00
|
|
|
angular.module('app').service('notify', ['$window', '$timeout', '$location', function($window, $timeout, $location) {
|
2014-06-22 09:04:07 +00:00
|
|
|
|
2014-06-22 10:02:22 +00:00
|
|
|
// returns true if the HTML5 Notifications API is supported.
|
2014-06-22 09:04:07 +00:00
|
|
|
this.supported = function() {
|
|
|
|
return ("Notification" in $window)
|
|
|
|
}
|
|
|
|
|
2014-06-22 10:02:22 +00:00
|
|
|
// returns true if the user has granted permission to
|
|
|
|
// display HTML5 notifications.
|
2014-06-22 09:04:07 +00:00
|
|
|
this.granted = function() {
|
|
|
|
return ("Notification" in $window) && Notification.permission === "granted";
|
|
|
|
}
|
|
|
|
|
2014-06-22 10:02:22 +00:00
|
|
|
// instructs the browser to request permission to
|
|
|
|
// display HTML5 notifications.
|
2014-06-22 09:04:07 +00:00
|
|
|
this.requestPermission = function() {
|
|
|
|
Notification.requestPermission();
|
|
|
|
}
|
|
|
|
|
2014-06-22 10:02:22 +00:00
|
|
|
// sends an HTML5 desktop notification using the specified
|
|
|
|
// title and notification options (optional).
|
2014-06-22 09:04:07 +00:00
|
|
|
this.send = function(title, opts) {
|
|
|
|
if ("Notification" in $window) {
|
2014-06-22 10:02:22 +00:00
|
|
|
var notification = new Notification(title, opts);
|
|
|
|
|
|
|
|
// automatically close the notification after
|
|
|
|
// 10 seconds of being open.
|
|
|
|
$timeout(function() {
|
|
|
|
notification.close();
|
|
|
|
}, 5000);
|
|
|
|
|
|
|
|
// if a hyperlink was specified, open the link
|
|
|
|
// when the notification is clicked.
|
|
|
|
notification.onclick = function() {
|
|
|
|
if (opts.href == undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// not exactly sure why this is even necessary:
|
|
|
|
// http://stackoverflow.com/questions/11784656/angularjs-location-not-changing-the-path
|
|
|
|
$timeout(function(){
|
|
|
|
$location.path(opts.href);
|
|
|
|
}, 1);
|
|
|
|
};
|
2014-06-22 09:04:07 +00:00
|
|
|
}
|
|
|
|
};
|
2014-06-22 10:02:22 +00:00
|
|
|
|
|
|
|
// sends an HTML5 desktop notification for a Commit.
|
|
|
|
this.sendCommit = function(repo, commit) {
|
|
|
|
// ignore 'Pending' messages since they are (usually) immediately
|
|
|
|
// followed-up by a 'Started' message, and we don't want to flood
|
|
|
|
// the user with notifications.
|
|
|
|
if (commit.status == 'Pending') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var title = repo.owner+'/'+repo.name;
|
|
|
|
var url = '/'+repo.host+'/'+repo.owner+'/'+repo.name+'/'+commit.branch+'/'+commit.sha;
|
|
|
|
|
|
|
|
this.send(title, {
|
|
|
|
icon: 'https://secure.gravatar.com/avatar/'+commit.gravatar,
|
|
|
|
body: commit.message,
|
|
|
|
href: url,
|
|
|
|
});
|
|
|
|
}
|
2014-06-22 09:04:07 +00:00
|
|
|
}]);
|