cryptalk/public/js/cryptalk_modules/notifications.js

118 lines
2.4 KiB
JavaScript
Raw Normal View History

2014-09-22 16:11:13 -04:00
/*
Usage
2014-09-23 13:54:36 -04:00
// 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');
2014-09-22 16:11:13 -04:00
*/
2014-09-23 14:16:02 -04:00
define(['mediator','win'],function (mediator,win){
2014-09-22 16:11:13 -04:00
2014-09-23 13:54:36 -04:00
var enabled = true,
2014-09-22 16:11:13 -04:00
native_supported = false,
new_title,
original_title,
blink_timer,
interval,
2014-09-23 13:54:36 -04:00
channel = mediator(),
now = function () {
return performance.now() || Date.now();
2014-09-22 16:11:13 -04:00
},
2014-09-23 13:54:36 -04:00
on = function () {
enabled = true;
},
off = function () {
enabled = false;
},
2014-09-22 16:11:13 -04:00
resetState = function() {
clearTimeout(blink_timer);
if (original_title !== undefined) setTitle(original_title);
original_title = undefined;
new_title = undefined;
window_active = true;
},
2014-09-23 13:54:36 -04:00
2014-09-22 16:11:13 -04:00
doBlink = function() {
2014-09-23 13:54:36 -04:00
if(enabled) {
if( win.getTitle() == original_title )
win.setTitle( new_title );
2014-09-22 16:11:13 -04:00
else
2014-09-23 13:54:36 -04:00
win.setTitle( original_title);
2014-09-22 16:11:13 -04:00
blink_timer = setTimeout(doBlink,interval);
} else {
resetState();
}
2014-09-23 13:54:36 -04:00
},
enableNative = function() {
if( native_supported && Notification.permission !== 'denied' ) {
Notification.requestPermission(function (status) {
Notification.permission = status;
});
}
},
2014-09-22 16:11:13 -04:00
2014-09-23 13:54:36 -04:00
blinkTitleUntilFocus = function(t,i) {
interval = (i == undefined) ? 1000 : i;
if ( enabled ) {
new_title = t;
original_title = getTitle();
doBlink();
}
},
2014-09-22 16:11:13 -04:00
2014-09-23 13:54:36 -04:00
notify = function(title,body,icon,fallback) {
// Only notify while in background
if( enabled) {
2014-09-22 16:11:13 -04:00
2014-09-23 13:54:36 -04:00
// Set default value for fallback parameter
if ( fallback === undefined) fallback = false;
2014-09-22 16:11:13 -04:00
2014-09-23 13:54:36 -04:00
if ( native_supported && Notification.permission === "granted") {
2014-09-22 16:11:13 -04:00
2014-09-23 13:54:36 -04:00
// Create notification
var n = new Notification(title, {body: body, icon:icon});
2014-09-22 16:11:13 -04:00
2014-09-23 13:54:36 -04:00
// Handle on show event
n.onshow = function () {
// Automatically close the notification after 5000ms
setTimeout(function(){n.close();},3000);
}
2014-09-22 16:11:13 -04:00
2014-09-23 13:54:36 -04:00
} else if ( fallback ) {
exports.blinkTitleUntilFocus("Attention",1000);
}
2014-09-22 16:11:13 -04:00
}
2014-09-23 13:54:36 -04:00
};
2014-09-22 16:11:13 -04:00
native_supported = (window.Notification !== undefined);
2014-09-23 13:54:36 -04:00
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(); });
enableNative();
off();
2014-09-23 14:16:02 -04:00
2014-09-22 16:11:13 -04:00
// Make sure we are at square one
resetState();
});