@ -14,6 +14,10 @@ var socket,
config = data ;
socket = io . connect ( 'http://' + config . socket . address + config . socket . port ? ':' + config . socket . port : '' ) ;
var reconnecting = false ;
var reconnectTries = 0 ;
socket . on ( 'event:connect' , function ( data ) {
console . log ( 'connected to nodebb socket: ' , data ) ;
} ) ;
@ -32,15 +36,56 @@ var socket,
contentEl . value = data . post ;
} ) ;
socket . on ( 'disconnect' , function ( data ) {
setTimeout ( function ( ) {
$ ( '#disconnect-modal' ) . modal ( 'show' ) ;
$ ( '#reload-button' ) . on ( 'click' , function ( ) {
$ ( '#disconnect-modal' ) . modal ( 'hide' ) ;
window . location . reload ( ) ;
socket . on ( 'connect' , function ( data ) {
if ( reconnecting ) {
app . alert ( {
alert _id : 'connection_alert' ,
title : 'Connected' ,
message : 'Connection successfull' ,
type : 'success' ,
timeout : 5000
} ) ;
} , 500 ) ;
reconnecting = false ;
}
} ) ;
socket . on ( 'disconnect' , function ( data ) {
app . alert ( {
alert _id : 'connection_alert' ,
title : 'Disconnect' ,
message : 'You have disconnected from NodeBB, we will try to reconnect!' ,
type : 'error' ,
timeout : 5000
} ) ;
} ) ;
socket . on ( 'reconnecting' , function ( data ) {
reconnecting = true ;
reconnectTries ++ ;
if ( reconnectTries > 4 ) {
showDisconnectModal ( ) ;
return ;
}
app . alert ( {
alert _id : 'connection_alert' ,
title : 'Reconnecting' ,
message : 'You have disconnected from NodeBB, we will try to reconnect you. <br/><i class="icon-refresh icon-spin"></i>' ,
type : 'notify' ,
timeout : 5000
} ) ;
} ) ;
function showDisconnectModal ( ) {
$ ( '#disconnect-modal' ) . modal ( {
backdrop : 'static' ,
show : true
} ) ;
$ ( '#reload-button' ) . on ( 'click' , function ( ) {
$ ( '#disconnect-modal' ) . modal ( 'hide' ) ;
window . location . reload ( ) ;
} ) ;
}
} ,
async : false
} ) ;
@ -50,6 +95,17 @@ var socket,
return text . replace ( /(\d)(?=(\d\d\d)+(?!\d))/g , "$1," ) ;
}
// Willingly stolen from: http://phpjs.org/functions/strip_tags/
app . strip _tags = function ( input , allowed ) {
allowed = ( ( ( allowed || "" ) + "" ) . toLowerCase ( ) . match ( /<[a-z][a-z0-9]*>/g ) || [ ] ) . join ( '' ) ; // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi ,
commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi ;
return input . replace ( commentsAndPhpTags , '' ) . replace ( tags , function ( $0 , $1 ) {
return allowed . indexOf ( '<' + $1 . toLowerCase ( ) + '>' ) > - 1 ? $0 : '' ;
} ) ;
}
// use unique alert_id to have multiple alerts visible at a time, use the same alert_id to fade out the current instance
// type : error, success, info, warning/notify
// title = bolded title text
@ -57,51 +113,60 @@ var socket,
// timeout default = permanent
// location : notification_window (default) or content
app . alert = function ( params ) {
var div = document . createElement ( 'div' ) ,
button = document . createElement ( 'button' ) ,
strong = document . createElement ( 'strong' ) ,
p = document . createElement ( 'p' ) ;
var alert _id = 'alert_button_' + ( ( params . alert _id ) ? params . alert _id : new Date ( ) . getTime ( ) ) ;
jQuery ( '#' + alert _id ) . fadeOut ( 500 , function ( ) {
this . remove ( ) ;
} ) ;
p . innerHTML = params . message ;
strong . innerHTML = params . title ;
var alert = $ ( '#' + alert _id ) ;
div . className = "alert toaster-alert " + ( ( params . type == 'warning' ) ? '' : "alert-" + params . type ) ;
div . setAttribute ( 'id' , alert _id ) ;
div . appendChild ( button ) ;
div . appendChild ( strong ) ;
div . appendChild ( p ) ;
button . className = 'close' ;
button . innerHTML = '×' ;
button . onclick = function ( ev ) {
div . parentNode . removeChild ( div ) ;
if ( alert . length > 0 ) {
alert . find ( 'strong' ) . html ( params . title ) ;
alert . find ( 'p' ) . html ( params . message ) ;
alert . attr ( 'class' , "alert toaster-alert " + ( ( params . type == 'warning' ) ? '' : "alert-" + params . type ) ) ;
}
else {
if ( params . location == null ) params . location = 'notification_window' ;
var div = document . createElement ( 'div' ) ,
button = document . createElement ( 'button' ) ,
strong = document . createElement ( 'strong' ) ,
p = document . createElement ( 'p' ) ;
jQuery ( '#' + params . location ) . prepend ( jQuery ( div ) . fadeIn ( '100' ) ) ;
p . innerHTML = params . message ;
strong . innerHTML = params . title ;
if ( params . timeout ) {
setTimeout ( function ( ) {
jQuery ( div ) . fadeOut ( 1000 , function ( ) {
this . remove ( ) ;
} ) ;
} , params . timeout )
}
div . className = "alert toaster-alert " + ( ( params . type == 'warning' ) ? '' : "alert-" + params . type ) ;
div . setAttribute ( 'id' , alert _id ) ;
div . appendChild ( button ) ;
div . appendChild ( strong ) ;
div . appendChild ( p ) ;
button . className = 'close' ;
button . innerHTML = '×' ;
button . onclick = function ( ev ) {
div . parentNode . removeChild ( div ) ;
}
if ( params . clickfn ) {
div . onclick = function ( ) {
params . clickfn ( ) ;
jQuery ( div ) . fadeOut ( 500 , function ( ) {
this . remove ( ) ;
} ) ;
if ( params . location == null )
params . location = 'notification_window' ;
jQuery ( '#' + params . location ) . prepend ( jQuery ( div ) . fadeIn ( '100' ) ) ;
if ( params . timeout ) {
setTimeout ( function ( ) {
jQuery ( div ) . fadeOut ( 1000 , function ( ) {
this . remove ( ) ;
} ) ;
} , params . timeout )
}
if ( params . clickfn ) {
div . onclick = function ( ) {
params . clickfn ( ) ;
jQuery ( div ) . fadeOut ( 500 , function ( ) {
this . remove ( ) ;
} ) ;
}
}
}
}
@ -114,6 +179,7 @@ var socket,
app . open _post _window = function ( post _mode , id , title , pid ) {
submit _post _btn = submit _post _btn || document . getElementById ( 'submit_post_btn' ) ;
post _title = post _title || document . getElementById ( 'post_title' ) ;
reply _title = reply _title || document . getElementById ( 'reply_title' ) ;