/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ (function() { 'use strict'; /** * @name Alarm * @constructor */ function Alarm() { this.currentAlarm = null; } /** * @name getAlarms * @desc Fetch the list of alarms from the server and use the last one */ Alarm.getAlarms = function() { var _this = this; var now = new Date(); var browserTime = Math.floor(now.getTime()/1000); this.$$resource.fetch('', 'alarmslist?browserTime=' + browserTime).then(function(data) { var alarms = data.alarms.sort(function reverseSortByAlarmTime(a, b) { var x = parseInt(a[2]); var y = parseInt(b[2]); return (y - x); }); if (alarms.length > 0) { var next = alarms.pop(); var now = new Date(); var utc = Math.floor(now.getTime()/1000); var url = next[0] + '/' + next[1]; var alarmTime = parseInt(next[2]); var delay = alarmTime; if (alarmTime > 0) delay -= utc; var d = new Date(alarmTime*1000); //console.log ("now = " + now.toUTCString()); //console.log ("next event " + url + " in " + delay + " seconds (on " + d.toUTCString() + ")"); var f = angular.bind(_this, Alarm.showAlarm, url); if (_this.currentAlarm) _this.$timeout.cancel(_this.currentAlarm); _this.currentAlarm = _this.$timeout(f, delay*1000); } }); }; /** * @name showAlarm * @desc Show the latest alarm using a toast * @param url The URL of the calendar component for snoozing */ Alarm.showAlarm = function(url) { var _this = this; this.$$resource.fetch(url, '?resetAlarm=yes').then(function(data) { _this.$toast.show({ position: 'top right', hideDelay: 0, template: [ '', '
', ' ', ' ', ' ', ' ', l('5 minutes'), ' ', ' ', l('10 minutes'), ' ', ' ', l('15 minutes'), ' ', ' ', l('30 minutes'), ' ', ' ', l('45 minutes'), ' ', ' ', l('1 hour'), ' ', ' ', l('1 day'), ' ', ' ', ' ', ' ', l('Cancel'), ' ', ' ', l('Ok'), ' ', '
', '
' ].join(''), locals: { url: url }, controller: AlarmController }); /** * @ngInject */ AlarmController.$inject = ['scope', '$mdToast', 'url']; function AlarmController(scope, $mdToast, url) { scope.reminder = '10'; scope.cancel = function() { $mdToast.hide(); }; scope.ok = function() { _this.$$resource.fetch(url, 'view?snoozeAlarm=' + scope.reminder); $mdToast.hide(); }; } }); }; /** * @memberof Alarm * @desc The factory we'll register as Alarm in the Angular module SOGo.Common * @ngInject */ AlarmService.$inject = ['$timeout', 'sgSettings', 'Resource', '$mdToast']; function AlarmService($timeout, Settings, Resource, $mdToast) { angular.extend(Alarm, { $timeout: $timeout, $$resource: new Resource(Settings.activeUser('folderURL') + 'Calendar', Settings.activeUser()), $toast: $mdToast }); return Alarm; // return constructor } /* Factory registration in Angular module */ angular .module('SOGo.Common') .factory('Alarm', AlarmService); })();