2016-03-23 19:28:22 -04:00
var FFZ = window . FrankerFaceZ ,
2016-07-13 02:31:26 -04:00
utils = require ( "../utils" ) ,
2016-04-19 15:16:37 -04:00
constants = require ( "../constants" ) ;
2015-01-20 01:53:18 -05:00
2015-02-08 02:01:09 -05:00
// ---------------------
// Initialization
// ---------------------
FFZ . prototype . setup _notifications = function ( ) {
this . log ( "Adding event handler for window focus." ) ;
window . addEventListener ( "focus" , this . clear _notifications . bind ( this ) ) ;
}
// ---------------------
// Settings
// ---------------------
2015-08-21 19:00:48 -04:00
FFZ . settings _info . server _messages = {
type : "boolean" ,
value : true ,
category : "Appearance" ,
name : "Server Notifications" ,
help : "Display global FrankerFaceZ notifications."
} ;
2015-02-08 02:01:09 -05:00
FFZ . settings _info . highlight _notifications = {
type : "boolean" ,
value : false ,
2015-07-13 21:52:44 -04:00
category : "Chat Filtering" ,
2015-05-17 19:02:57 -04:00
no _bttv : true ,
2015-07-18 21:10:27 -04:00
no _mobile : true ,
2015-05-17 19:02:57 -04:00
//visible: function() { return ! this.has_bttv },
2015-02-08 02:01:09 -05:00
name : "Highlight Notifications" ,
2015-07-04 17:06:36 -04:00
help : "Display notifications when a highlighted word appears in chat in an unfocused tab. This is automatically disabled on the dashboard." ,
2015-02-08 02:01:09 -05:00
on _update : function ( val , direct ) {
// Check to see if we have notification permission. If this is
// enabled, at least.
if ( ! val || ! direct )
return ;
if ( Notification . permission === "denied" ) {
this . log ( "Notifications have been denied by the user." ) ;
this . settings . set ( "highlight_notifications" , false ) ;
return ;
} else if ( Notification . permission === "granted" )
return ;
var f = this ;
Notification . requestPermission ( function ( e ) {
if ( e === "denied" ) {
f . log ( "Notifications have been denied by the user." ) ;
f . settings . set ( "highlight_notifications" , false ) ;
}
} ) ;
}
} ;
2015-07-18 21:10:27 -04:00
FFZ . settings _info . notification _timeout = {
type : "button" ,
value : 60 ,
category : "Chat Filtering" ,
no _bttv : true ,
no _mobile : true ,
name : "Notification Timeout" ,
help : "Specify how long notifications should be displayed before automatically closing." ,
method : function ( ) {
2017-04-05 19:12:11 -04:00
var f = this ,
old _val = this . settings . notification _timeout ;
2016-07-13 02:31:26 -04:00
utils . prompt (
"Notification Timeout" ,
2017-04-05 19:12:11 -04:00
"Please enter the time you'd like notifications to be displayed before automatically closing, in seconds. A value of zero may prevent notifications from disappearing automatically. This value is subject to the limitations of your web browser.</p><p><b>Default:</b> 60" ,
old _val === false ? 0 : old _val ,
2016-07-13 02:31:26 -04:00
function ( new _val ) {
if ( new _val === null || new _val === undefined )
return ;
new _val = parseInt ( new _val ) ;
2017-04-05 19:12:11 -04:00
if ( new _val <= 0 )
new _val = false ;
else if ( Number . isNaN ( new _val ) || ! Number . isFinite ( new _val ) )
2016-07-13 02:31:26 -04:00
new _val = 60 ;
f . settings . set ( "notification_timeout" , new _val ) ;
} ) ;
2015-07-18 21:10:27 -04:00
}
} ;
2015-02-08 02:01:09 -05:00
// ---------------------
// Socket Commands
// ---------------------
FFZ . ws _commands . message = function ( message ) {
2015-08-21 19:00:48 -04:00
if ( ! this . settings . server _messages )
return ;
2015-02-10 01:34:23 -05:00
this . show _message ( message ) ;
2015-02-08 02:01:09 -05:00
}
// ---------------------
// Notifications
// ---------------------
FFZ . _notifications = { } ;
FFZ . _last _notification = 0 ;
FFZ . prototype . clear _notifications = function ( ) {
for ( var k in FFZ . _notifications ) {
var n = FFZ . _notifications [ k ] ;
if ( n )
try {
n . close ( ) ;
} catch ( err ) { }
}
FFZ . _notifications = { } ;
FFZ . _last _notification = 0 ;
}
FFZ . prototype . show _notification = function ( message , title , tag , timeout , on _click , on _close ) {
var perm = Notification . permission ;
2016-10-14 20:43:34 -04:00
if ( perm === "denied" )
2015-02-08 02:01:09 -05:00
return false ;
if ( perm === "granted" ) {
title = title || "FrankerFaceZ" ;
2017-04-05 19:12:11 -04:00
timeout = timeout || ( this . settings . notification _timeout === false ? false : this . settings . notification _timeout * 1000 ) ;
2015-02-08 02:01:09 -05:00
var options = {
lang : "en-US" ,
dir : "ltr" ,
body : message ,
tag : tag || "FrankerFaceZ" ,
2016-03-23 19:28:22 -04:00
icon : "//cdn.frankerfacez.com/icon32.png"
2015-02-08 02:01:09 -05:00
} ;
var f = this ,
n = new Notification ( title , options ) ,
nid = FFZ . _last _notification ++ ;
FFZ . _notifications [ nid ] = n ;
n . addEventListener ( "click" , function ( ) {
delete FFZ . _notifications [ nid ] ;
if ( on _click )
on _click . bind ( f ) ( ) ;
} ) ;
n . addEventListener ( "close" , function ( ) {
delete FFZ . _notifications [ nid ] ;
if ( on _close )
on _close . bind ( f ) ( ) ;
} ) ;
if ( typeof timeout == "number" )
n . addEventListener ( "show" , function ( ) {
setTimeout ( function ( ) {
delete FFZ . _notifications [ nid ] ;
n . close ( ) ;
} , timeout ) ;
} ) ;
return ;
}
var f = this ;
Notification . requestPermission ( function ( e ) {
f . show _notification ( message , title , tag ) ;
} ) ;
}
// ---------------------
// Noty Notification
// ---------------------
FFZ . prototype . show _message = function ( message ) {
2015-11-07 22:56:15 -05:00
if ( ! window . jQuery || ! window . jQuery . noty || ! jQuery . noty . themes . ffzTheme ) {
setTimeout ( this . show _message . bind ( this , message ) , 50 ) ;
return ;
}
2015-01-20 01:53:18 -05:00
window . noty ( {
text : message ,
2016-04-19 15:16:37 -04:00
template : '<div class="noty_message"><span class="noty_text"></span><div class="noty_close">' + constants . CLOSE + '</div></div>' ,
2015-01-20 01:53:18 -05:00
theme : "ffzTheme" ,
layout : "bottomCenter" ,
closeWith : [ "button" ]
} ) . show ( ) ;
2015-01-12 17:58:07 -05:00
}