cryptalk/public/js/cryptalk_modules/notifications.js

127 lines
2.7 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
2014-09-24 11:20:20 -04:00
mediator.emit('notification:send',{
2014-09-23 13:54:36 -04:00
title: 'Woop',
body: 'Woop woop',
icon: 'gfx/icon.png'
});
// Turn notifications on
2014-09-24 11:20:20 -04:00
mediator.emit('notification:on');
2014-09-23 13:54:36 -04:00
// Turn notifications off
2014-09-24 11:20:20 -04:00
mediator.emit('notification:off');
2014-09-23 13:54:36 -04:00
2014-09-22 16:11:13 -04:00
*/
define(['mediator','window','settings'], function (mediator, win, settings) {
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,
last,
2014-09-23 13:54:36 -04:00
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) win.setTitle(original_title);
2014-09-22 16:11:13 -04:00
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 && original_title === undefined ) {
2014-09-23 13:54:36 -04:00
new_title = t;
original_title = win.getTitle();
2014-09-23 13:54:36 -04:00
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, and if sufficient time has passed
if( enabled && (now() - last) > settings.notifications.maxOnePerMs ) {
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
last = now();
2014-09-23 13:54:36 -04:00
} else if ( fallback ) {
blinkTitleUntilFocus("Attention",1000);
2014-09-23 13:54:36 -04:00
}
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
2014-09-24 11:20:20 -04:00
mediator.on('notification:send',function(data) { notify(data.title,data.body,data.icon,true); });
mediator.on('notification:on',function() { on(); });
mediator.on('notification:off',function() { off(); });
2014-09-23 13:54:36 -04:00
// Always enable native notifications
2014-09-23 13:54:36 -04:00
enableNative();
// Start with notifications disabled
2014-09-23 13:54:36 -04:00
off();
2014-09-23 14:16:02 -04:00
// If this is undefined, notifications will fail to show
last = now();
2014-09-22 16:11:13 -04:00
// Make sure we are at square one
resetState();
});