summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/lib/notify.js.coffee
blob: 9e28353ac34e615fce0c1b3ea495f56d71e4e9e1 (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
((w) ->
  notificationGranted = (message, opts, onclick) ->
    notification = new Notification(message, opts)

    # Hide the notification after X amount of seconds
    setTimeout ->
      notification.close()
    , 8000

    if onclick
      notification.onclick = onclick

  notifyPermissions = ->
    if 'Notification' of window
      Notification.requestPermission()

  notifyMe = (message, body, icon, onclick) ->
    opts =
      body: body
      icon: icon
    # Let's check if the browser supports notifications
    if !('Notification' of window)
      # do nothing
    else if Notification.permission == 'granted'
      # If it's okay let's create a notification
      notificationGranted message, opts, onclick
    else if Notification.permission != 'denied'
      Notification.requestPermission (permission) ->
        # If the user accepts, let's create a notification
        if permission == 'granted'
          notificationGranted message, opts, onclick

  w.notify = notifyMe
  w.notifyPermissions = notifyPermissions
) window