summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/lib/utils/notify.js
blob: 6d5979603b91323e077d3bc83c1166a3d20789c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, one-var, one-var-declaration-per-line, consistent-return, prefer-arrow-callback, no-return-assign, object-shorthand, comma-dangle, no-param-reassign, max-len */

(function() {
  (function(w) {
    var notificationGranted, notifyMe, notifyPermissions;
    notificationGranted = function(message, opts, onclick) {
      var notification;
      notification = new Notification(message, opts);
      setTimeout(function() {
        return notification.close();
      // Hide the notification after X amount of seconds
      }, 8000);
      if (onclick) {
        return notification.onclick = onclick;
      }
    };
    notifyPermissions = function() {
      if ('Notification' in window) {
        return Notification.requestPermission();
      }
    };
    notifyMe = function(message, body, icon, onclick) {
      var opts;
      opts = {
        body: body,
        icon: icon
      };
      // Let's check if the browser supports notifications
      if (!('Notification' in window)) {

      // do nothing
      } else if (Notification.permission === 'granted') {
        // If it's okay let's create a notification
        return notificationGranted(message, opts, onclick);
      } else if (Notification.permission !== 'denied') {
        return Notification.requestPermission(function(permission) {
          // If the user accepts, let's create a notification
          if (permission === 'granted') {
            return notificationGranted(message, opts, onclick);
          }
        });
      }
    };
    w.notify = notifyMe;
    return w.notifyPermissions = notifyPermissions;
  })(window);
}).call(this);