Skip to content

Instantly share code, notes, and snippets.

@Hexagon
Last active August 29, 2015 14:06
Show Gist options
  • Save Hexagon/8faad2c74a85d2c7e365 to your computer and use it in GitHub Desktop.
Save Hexagon/8faad2c74a85d2c7e365 to your computer and use it in GitHub Desktop.
Notification module
/*
Usage
// Send an notification
channel.emit('notification:send',{
title: 'Woop',
body: 'Woop woop',
icon: 'gfx/icon.png'
});
// Turn notifications on
channel.emit('notification:on');
// Turn notifications off
channel.emit('notification:off');
*/
define(['mediator'],function (mediator){
var exports = {},
enabled = true,
window_active,
blur_delay_timer,
native_supported = false,
new_title,
original_title,
blink_timer,
interval,
channel = mediator(),
now = function () {
return performance.now() || Date.now();
},
on = function () {
requestNativeSupport();
enabled = true;
},
off = function () {
enabled = false;
},
focusCallback = function() {
/* Reset everything after regaining focus */
resetState();
},
resetState = function() {
clearTimeout(blur_delay_timer);
clearTimeout(blink_timer);
if (original_title !== undefined) setTitle(original_title);
original_title = undefined;
new_title = undefined;
window_active = true;
},
blurCallback = function() {
/* Apply a slight delay to prevent notifications from popping when the notifications
cause the windows to lose focus */
clearTimeout(blur_delay_timer);
blur_delay_timer = setTimeout(function() { window_active = false; },1000);
},
setTitle = function(t) { document.title = t; },
getTitle = function() { return document.title; },
doBlink = function() {
if(!window_active) {
if( getTitle() == original_title )
setTitle( new_title );
else
setTitle( original_title);
blink_timer = setTimeout(doBlink,interval);
} else {
resetState();
}
},
enableNative = function() {
if( native_supported && Notification.permission !== 'denied' ) {
Notification.requestPermission(function (status) {
Notification.permission = status;
});
}
},
blinkTitleUntilFocus = function(t,i) {
interval = (i == undefined) ? 1000 : i;
if (!window_active && enabled) {
new_title = t;
original_title = getTitle();
doBlink();
}
},
notify = function(title,body,icon,fallback) {
// Only notify while in background
if( !window_active && enabled) {
// Set default value for fallback parameter
if ( fallback === undefined) fallback = false;
if ( native_supported && Notification.permission === "granted") {
// Create notification
var n = new Notification(title, {body: body, icon:icon});
// Handle on show event
n.onshow = function () {
// Automatically close the notification after 5000ms
setTimeout(function(){n.close();},3000);
}
} else if ( fallback ) {
exports.blinkTitleUntilFocus("Attention",1000);
}
}
},
windowActive = function() {
return window_active;
};
native_supported = (window.Notification !== undefined);
// Keep track of document focus/blur
if (window.addEventListener){
// Normal browsers
window.addEventListener("focus", focusCallback, true);
window.addEventListener("blur", blurCallback, true);
} else {
// IE
window.observe("focusin", focusCallback);
window.observe("focusout", blurCallback);
}
channel.on('notification:send',function(data) { notify(data.title,data.body,data.icon,true); });
channel.on('notification:on',function() { on(); });
channel.on('notification:off',function() { off(); });
// Make sure we are at square one
resetState();
return exports;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment