2013-09-15 00:03:27 -04:00
/ *
* ajax . js
* https : //github.com/savetheinternet/Tinyboard/blob/master/js/ajax.js
*
* Released under the MIT license
* Copyright ( c ) 2013 Michael Save < savetheinternet @ tinyboard . org >
2014-01-19 08:27:24 -05:00
* Copyright ( c ) 2013 - 2014 Marcin Łabanowski < marcin @ 6 irc . net >
2013-09-15 00:03:27 -04:00
*
* Usage :
* $config [ 'additional_javascript' ] [ ] = 'js/jquery.min.js' ;
* $config [ 'additional_javascript' ] [ ] = 'js/ajax.js' ;
*
* /
$ ( window ) . ready ( function ( ) {
2013-09-18 00:40:39 -04:00
var settings = new script _settings ( 'ajax' ) ;
2013-09-15 00:54:18 -04:00
var do _not _ajax = false ;
2013-12-22 16:51:28 -05:00
// Enable submit button if disabled (cache problem)
$ ( 'input[type="submit"]' ) . removeAttr ( 'disabled' ) ;
2013-09-15 00:54:18 -04:00
2013-09-15 00:03:27 -04:00
var setup _form = function ( $form ) {
$form . submit ( function ( ) {
2014-01-29 17:32:22 -05:00
if ( do _not _ajax )
2013-09-15 00:54:18 -04:00
return true ;
2013-09-15 00:03:27 -04:00
var form = this ;
var submit _txt = $ ( this ) . find ( 'input[type="submit"]' ) . val ( ) ;
2013-09-15 00:54:18 -04:00
if ( window . FormData === undefined )
2013-09-15 00:03:27 -04:00
return true ;
var formData = new FormData ( this ) ;
formData . append ( 'json_response' , '1' ) ;
2013-09-15 01:06:29 -04:00
formData . append ( 'post' , submit _txt ) ;
2013-09-15 00:03:27 -04:00
2014-01-29 17:31:39 -05:00
$ ( document ) . trigger ( "ajax_before_post" , formData ) ;
2013-09-15 00:03:27 -04:00
var updateProgress = function ( e ) {
2013-09-17 18:54:06 -04:00
var percentage ;
if ( e . position === undefined ) { // Firefox
percentage = Math . round ( e . loaded * 100 / e . total ) ;
}
else { // Chrome?
percentage = Math . round ( e . position * 100 / e . total ) ;
}
$ ( form ) . find ( 'input[type="submit"]' ) . val ( _ ( 'Posting... (#%)' ) . replace ( '#' , percentage ) ) ;
2013-09-15 00:03:27 -04:00
} ;
$ . ajax ( {
url : this . action ,
type : 'POST' ,
xhr : function ( ) {
var xhr = $ . ajaxSettings . xhr ( ) ;
if ( xhr . upload ) {
xhr . upload . addEventListener ( 'progress' , updateProgress , false ) ;
}
return xhr ;
} ,
success : function ( post _response ) {
if ( post _response . error ) {
2013-09-16 19:15:24 -04:00
if ( post _response . banned ) {
// You are banned. Must post the form normally so the user can see the ban message.
do _not _ajax = true ;
$ ( form ) . find ( 'input[type="submit"]' ) . each ( function ( ) {
var $replacement = $ ( '<input type="hidden">' ) ;
$replacement . attr ( 'name' , $ ( this ) . attr ( 'name' ) ) ;
$replacement . val ( submit _txt ) ;
$ ( this )
. after ( $replacement )
. replaceWith ( $ ( '<input type="button">' ) . val ( submit _txt ) ) ;
} ) ;
$ ( form ) . submit ( ) ;
} else {
alert ( post _response . error ) ;
$ ( form ) . find ( 'input[type="submit"]' ) . val ( submit _txt ) ;
$ ( form ) . find ( 'input[type="submit"]' ) . removeAttr ( 'disabled' ) ;
}
2013-09-15 00:03:27 -04:00
} else if ( post _response . redirect && post _response . id ) {
2013-09-18 00:40:39 -04:00
if ( ! $ ( form ) . find ( 'input[name="thread"]' ) . length
|| ( ! settings . get ( 'always_noko_replies' , true ) && ! post _response . noko ) ) {
2013-09-15 00:11:09 -04:00
document . location = post _response . redirect ;
} else {
$ . ajax ( {
2013-09-15 01:14:02 -04:00
url : document . location ,
2013-09-15 00:11:09 -04:00
success : function ( data ) {
$ ( data ) . find ( 'div.post.reply' ) . each ( function ( ) {
var id = $ ( this ) . attr ( 'id' ) ;
if ( $ ( '#' + id ) . length == 0 ) {
$ ( this ) . insertAfter ( $ ( 'div.post:last' ) . next ( ) ) . after ( '<br class="clear">' ) ;
$ ( document ) . trigger ( 'new_post' , this ) ;
2014-01-26 12:07:34 -05:00
// watch.js & auto-reload.js retrigger
2014-01-26 12:58:42 -05:00
setTimeout ( function ( ) { $ ( window ) . trigger ( "scroll" ) ; } , 100 ) ;
2013-09-15 00:11:09 -04:00
}
} ) ;
2013-09-15 01:06:29 -04:00
2013-09-15 00:11:09 -04:00
highlightReply ( post _response . id ) ;
2013-09-15 01:06:29 -04:00
window . location . hash = post _response . id ;
2015-09-07 18:31:14 -04:00
$ ( window ) . scrollTop ( $ ( document ) . height ( ) ) ;
2013-09-15 00:11:09 -04:00
$ ( form ) . find ( 'input[type="submit"]' ) . val ( submit _txt ) ;
$ ( form ) . find ( 'input[type="submit"]' ) . removeAttr ( 'disabled' ) ;
2013-09-15 00:19:25 -04:00
$ ( form ) . find ( ' input [ name = "subject" ] , input [ name = "file_url" ] , \
textarea [ name = "body" ] , input [ type = "file" ] ').val(' ' ) . change ( ) ;
2013-09-15 00:11:09 -04:00
} ,
cache : false ,
contentType : false ,
processData : false
} , 'html' ) ;
}
2013-09-15 06:15:17 -04:00
$ ( form ) . find ( 'input[type="submit"]' ) . val ( _ ( 'Posted...' ) ) ;
2014-10-18 09:31:48 -04:00
$ ( document ) . trigger ( "ajax_after_post" , post _response ) ;
2013-09-15 00:03:27 -04:00
} else {
alert ( _ ( 'An unknown error occured when posting!' ) ) ;
$ ( form ) . find ( 'input[type="submit"]' ) . val ( submit _txt ) ;
$ ( form ) . find ( 'input[type="submit"]' ) . removeAttr ( 'disabled' ) ;
}
} ,
error : function ( xhr , status , er ) {
2015-03-11 05:58:03 -04:00
console . log ( xhr ) ;
alert ( _ ( 'The server took too long to submit your post. Your post was probably still submitted. If it wasn\'t, we might be experiencing issues right now -- please try your post again later. Error information: ' ) + "<div><textarea>" + JSON . stringify ( xhr ) + "</textarea></div>" ) ;
$ ( form ) . find ( 'input[type="submit"]' ) . val ( submit _txt ) ;
$ ( form ) . find ( 'input[type="submit"]' ) . removeAttr ( 'disabled' ) ;
2013-09-15 00:03:27 -04:00
} ,
data : formData ,
cache : false ,
contentType : false ,
processData : false
2013-09-15 00:11:09 -04:00
} , 'json' ) ;
2013-09-15 00:03:27 -04:00
$ ( form ) . find ( 'input[type="submit"]' ) . val ( _ ( 'Posting...' ) ) ;
$ ( form ) . find ( 'input[type="submit"]' ) . attr ( 'disabled' , true ) ;
return false ;
} ) ;
} ;
setup _form ( $ ( 'form[name="post"]' ) ) ;
2013-09-15 00:54:18 -04:00
$ ( window ) . on ( 'quick-reply' , function ( ) {
2014-11-08 19:56:41 -05:00
$ ( 'form#quick-reply' ) . off ( 'submit' ) ;
2013-09-15 00:03:27 -04:00
setup _form ( $ ( 'form#quick-reply' ) ) ;
} ) ;
} ) ;