Jamstash/app/common/notification-service.js
Hyzual 392ef74eba Rewrites notification service
- showNotification takes a song as parameter and picks the information it needs itself. That way we avoid player-directive.js having utils as a dependency.
- shortened the names of the methods a bit. Had to rename them in settings.js
- Used $interval rather than window.setInterval in player-directive.js and in notification-service.js. It's easier to unit test them that way !
2015-01-17 19:15:08 +01:00

54 lines
1.8 KiB
JavaScript

/**
* jamstash.notifications Module
*
* Provides access to the notification UI.
*/
angular.module('jamstash.notifications', ['jamstash.player.service', 'jamstash.utils'])
.service('notifications', ['$rootScope', '$window', '$interval', 'globals', 'player', 'utils',
function($rootScope, $window, $interval, globals, player, utils) {
'use strict';
this.updateMessage = function (msg, autohide) {
if (msg !== '') {
var id = $rootScope.Messages.push(msg) - 1;
$('#messages').fadeIn();
if (autohide) {
setTimeout(function () {
$('#msg_' + id).fadeOut(function () { $(this).remove(); });
}, globals.settings.Timeout);
}
}
};
this.requestPermissionIfRequired = function () {
if (this.isSupported() && !this.hasPermission()) {
window.Notify.requestPermission();
}
};
this.hasPermission = function () {
return !$window.Notify.needsPermission();
};
this.isSupported = function () {
return window.Notify.isSupported();
};
this.showNotification = function (song) {
if (this.hasPermission()) {
var notification = new Notify(utils.toHTML.un(song.name), {
body: utils.toHTML.un(song.artist + " - " + song.album),
icon: song.coverartthumb,
notifyClick: function () {
player.nextTrack();
this.close();
$rootScope.$apply();
}
});
$interval(function() {
notification.close();
}, globals.settings.Timeout);
notification.show();
} else {
console.log("showNotification: No Permission");
}
};
}]);