Created
August 2, 2012 03:36
-
-
Save sishen/3233112 to your computer and use it in GitHub Desktop.
Web Notifications integration in Pragmatic.ly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about writing:
rather than:
This goes for all the other occurrences of the
if ... then return true else return false
antipattern.