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
|
|
|
*/
|
2014-09-26 08:46:35 -04:00
|
|
|
define(['castrato','window','settings'], function (mediator, win, settings) {
|
2014-09-25 17:43:24 -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-25 17:43:24 -04:00
|
|
|
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);
|
2014-09-25 17:43:24 -04:00
|
|
|
if (original_title !== undefined) win.setTitle(original_title);
|
2014-09-22 16:11:13 -04:00
|
|
|
original_title = undefined;
|
|
|
|
new_title = undefined;
|
|
|
|
},
|
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) {
|
2017-02-22 17:09:50 -05:00
|
|
|
if( win.getTitle() === original_title )
|
2014-09-23 13:54:36 -04:00
|
|
|
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) {
|
2017-02-22 17:09:50 -05:00
|
|
|
interval = (i === undefined) ? 1000 : i;
|
2014-09-25 17:43:24 -04:00
|
|
|
if ( enabled && original_title === undefined ) {
|
2014-09-23 13:54:36 -04:00
|
|
|
new_title = t;
|
2014-09-25 17:43:24 -04:00
|
|
|
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) {
|
2014-09-25 17:43:24 -04:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2017-02-22 17:09:50 -05: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);
|
2017-02-22 17:09:50 -05:00
|
|
|
};
|
2014-09-22 16:11:13 -04:00
|
|
|
|
2014-09-25 17:43:24 -04:00
|
|
|
last = now();
|
|
|
|
|
2014-09-23 13:54:36 -04:00
|
|
|
} else if ( fallback ) {
|
2017-02-22 17:09:50 -05:00
|
|
|
blinkTitleUntilFocus('Attention', 1000);
|
2014-09-25 17:43:24 -04:00
|
|
|
|
2014-09-23 13:54:36 -04:00
|
|
|
}
|
2014-09-22 16:11:13 -04:00
|
|
|
}
|
2014-09-23 13:54:36 -04:00
|
|
|
};
|
2017-02-22 17:09:50 -05: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
|
|
|
|
2014-09-25 17:43:24 -04:00
|
|
|
// Always enable native notifications
|
2017-02-22 17:09:50 -05:00
|
|
|
enableNative();
|
2014-09-23 13:54:36 -04:00
|
|
|
|
2017-02-22 17:09:50 -05:00
|
|
|
// Start with notifications disabled
|
|
|
|
off();
|
2014-09-23 14:16:02 -04:00
|
|
|
|
2017-02-22 17:09:50 -05:00
|
|
|
// If this is undefined, notifications will fail to show
|
2014-09-25 17:43:24 -04:00
|
|
|
last = now();
|
|
|
|
|
2014-09-22 16:11:13 -04:00
|
|
|
// Make sure we are at square one
|
|
|
|
resetState();
|
|
|
|
|
|
|
|
});
|