Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Owner 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
You can’t perform that action at this time.