"use strict"; angular.module("iisland").factory('notification_service', ["$timeout", function($timeout) { var notifications = []; var subscribers = []; var sounds = { "POKE_SOUND" : { "path" : "js/POKE SOUND.mp3", "volume" : 0.05 } }; var notify = function() { //console.warn("send notifications", angular.copy(notifications), subscribers.length); angular.forEach(subscribers, function(fn) { fn(angular.copy(notifications)); }); }; var check_messages = function() { var tmp = []; angular.forEach(notifications, function(notification) { if (notification.until > Date.now()) { tmp.push(notification); } }); notifications = tmp; notify(); }; return { "notify" : function(message) {// {'msg' : 'tekst', 'delay' : sekunde za koliko se prikaže, default je 5} //console.warn("got notification", message); if (message.delay === undefined) { message.delay = 5; } if (message.until!=0) { var audio = new Audio("/"+sounds["POKE_SOUND"].path); audio.volume=sounds["POKE_SOUND"].volume; audio.play(); } message.until = Date.now() + message.delay * 1000; $timeout(function() { check_messages(); }, message.delay * 1100); notifications.push(message); notify(); }, "recheck" : check_messages, "notifications_subscribe" : function(fn) { subscribers.push(fn); } }; }]);