Skip to content

Instantly share code, notes, and snippets.

@sishen
Created August 2, 2012 03:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sishen/3233112 to your computer and use it in GitHub Desktop.
Save sishen/3233112 to your computer and use it in GitHub Desktop.
Web Notifications integration in Pragmatic.ly
class Notifier
constructor: ->
@enableNotification = false
@checkOrRequirePermission()
hasSupport: ->
window.webkitNotifications?
requestPermission: (cb) ->
window.webkitNotifications.requestPermission (cb)
setPermission: =>
if @hasPermission()
$('body .notification-alert a.close').click()
@enableNotification = true
else if window.webkitNotifications.checkPermission() is 2
$('body .notification-alert a.close').click()
hasPermission: ->
if window.webkitNotifications.checkPermission() is 0
return true
else
return false
checkOrRequirePermission: =>
if @hasSupport()
if @hasPermission()
@enableNotification = true
else
if window.webkitNotifications.checkPermission() isnt 2
@showTooltip()
else
console.log("Desktop notifications are not supported for this Browser/OS version yet.")
showTooltip: ->
$('body').append(JST["app/views/lib/notification_info"]())
$('body .notification-alert').on 'click', 'a#link_enable_notifications', (e) =>
e.preventDefault()
@requestPermission(@setPermission)
visitUrl: (url) ->
Spine.Route.navigate(url)
notify: (avatar, title, content, url = null) ->
if @enableNotification
popup = window.webkitNotifications.createNotification(avatar, title, content)
if url
popup.onclick = ->
window.parent.focus()
window.parent.notifier.visitUrl(url)
popup.show()
setTimeout ( => popup.cancel() ), 12000
window.Notifier = Notifier
@friggeri
Copy link

friggeri commented Aug 2, 2012

How about writing:

hasPermission: -> window.webkitNotifications?

rather than:

hasPermission: ->
  if window.webkitNotifications
    return true
  else
    return false

This goes for all the other occurrences of the if ... then return true else return false antipattern.

@sishen
Copy link
Author

sishen commented Aug 2, 2012

Thanks @afriggeri. It's better, :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment