123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- /* Small set of function for Notification handling */
-
- /* Stores Notifications instance associated with alarm name */
- var _notifications = {};
- var _requested = false;
-
- /* Return True if the browser supports Notifications
- * Note : Return False in case user denied the permission to notify
- */
- function support() {
- return "Notification" in window && Notification.permission !== 'denied'
- }
-
- /* Return True if Notifications are supported and permission granted */
- function granted() {
- if(!support()) {
- return false;
- }
- return Notification.permission === 'granted'
- }
-
- /* Request permission for notifications */
- function requestPermission() {
- if(_requested) { return; }
- _requested = true
- Notification.requestPermission()
- }
-
- /* Given an alarm name and a ringtime, create a new notification
- * for the ringing alarm if none existing
- */
- function set_notif_alrm(name:string, time_str:string) {
- if(!support()) { return; }
- if(!granted()) {
- if(_requested) { return; } // allready requested
- _requested = true
- Notification.requestPermission().then(function (perm) {
- set_notif_alrm(name)
- })
- }
- if(name in _notifications) { return; }
-
- let notif = new Notification(time_str+" '"+name+"'",
- {body:time_str+' alarm "'+name+'" is ringing.'})
-
- notif.addEventListener('close', () => {
- if(!name in _notifications) {
- delete _notifications[name];
- }
- });
-
- notif.addEventListener('error', () => {
- if(!name in _notifications) {
- delete _notifications[name];
- }
- });
-
- _notifications[name] = notif;
- }
-
- /* Given an alarm name, delete & close associated Notification if one exists */
- function unset_notif_alrm(name:string) {
- if(!support()) { return; }
- if(name in _notifications) {
- _notifications[name].close()
- delete _notifications[name]
- }
- }
-
- const Notif = {
-
- set: set_notif_alrm,
- unset: unset_notif_alrm,
- requestPermission: requestPermission,
-
- };
-
- export default Notif;
|