Browse Source

retab ajax.js

pull/40/head
towards-a-new-leftypol 3 years ago
parent
commit
11ee04f973
  1. 240
      js/ajax.js

240
js/ajax.js

@ -13,128 +13,128 @@
*/ */
$(window).ready(function() { $(window).ready(function() {
var settings = new script_settings('ajax'); var settings = new script_settings('ajax');
var do_not_ajax = false; var do_not_ajax = false;
// Enable submit button if disabled (cache problem) // Enable submit button if disabled (cache problem)
$('input[type="submit"]').removeAttr('disabled'); $('input[type="submit"]').removeAttr('disabled');
var setup_form = function($form) { var setup_form = function($form) {
$form.submit(function() { $form.submit(function() {
if (do_not_ajax) if (do_not_ajax)
return true; return true;
var form = this; var form = this;
var submit_txt = $(this).find('input[type="submit"]').val(); var submit_txt = $(this).find('input[type="submit"]').val();
if (window.FormData === undefined) if (window.FormData === undefined)
return true; return true;
var formData = new FormData(this); var formData = new FormData(this);
formData.append('json_response', '1'); formData.append('json_response', '1');
formData.append('post', submit_txt); formData.append('post', submit_txt);
$(document).trigger("ajax_before_post", formData); $(document).trigger("ajax_before_post", formData);
var updateProgress = function(e) { var updateProgress = function(e) {
var percentage; var percentage;
if (e.position === undefined) { // Firefox if (e.position === undefined) { // Firefox
percentage = Math.round(e.loaded * 100 / e.total); percentage = Math.round(e.loaded * 100 / e.total);
} }
else { // Chrome? else { // Chrome?
percentage = Math.round(e.position * 100 / e.total); percentage = Math.round(e.position * 100 / e.total);
} }
$(form).find('input[type="submit"]').val(_('Posting... (#%)').replace('#', percentage)); $(form).find('input[type="submit"]').val(_('Posting... (#%)').replace('#', percentage));
}; };
$.ajax({ $.ajax({
url: this.action, url: this.action,
type: 'POST', type: 'POST',
xhr: function() { xhr: function() {
var xhr = $.ajaxSettings.xhr(); var xhr = $.ajaxSettings.xhr();
if(xhr.upload) { if(xhr.upload) {
xhr.upload.addEventListener('progress', updateProgress, false); xhr.upload.addEventListener('progress', updateProgress, false);
} }
return xhr; return xhr;
}, },
success: function(post_response) { success: function(post_response) {
if (post_response.error) { if (post_response.error) {
if (post_response.banned) { if (post_response.banned) {
// You are banned. Must post the form normally so the user can see the ban message. // You are banned. Must post the form normally so the user can see the ban message.
do_not_ajax = true; do_not_ajax = true;
$(form).find('input[type="submit"]').each(function() { $(form).find('input[type="submit"]').each(function() {
var $replacement = $('<input type="hidden">'); var $replacement = $('<input type="hidden">');
$replacement.attr('name', $(this).attr('name')); $replacement.attr('name', $(this).attr('name'));
$replacement.val(submit_txt); $replacement.val(submit_txt);
$(this) $(this)
.after($replacement) .after($replacement)
.replaceWith($('<input type="button">').val(submit_txt)); .replaceWith($('<input type="button">').val(submit_txt));
}); });
$(form).submit(); $(form).submit();
} else { } else {
alert(post_response.error); alert(post_response.error);
$(form).find('input[type="submit"]').val(submit_txt); $(form).find('input[type="submit"]').val(submit_txt);
$(form).find('input[type="submit"]').removeAttr('disabled'); $(form).find('input[type="submit"]').removeAttr('disabled');
} }
} else if (post_response.redirect && post_response.id) { } else if (post_response.redirect && post_response.id) {
if (!$(form).find('input[name="thread"]').length if (!$(form).find('input[name="thread"]').length
|| (!settings.get('always_noko_replies', true) && !post_response.noko)) { || (!settings.get('always_noko_replies', true) && !post_response.noko)) {
document.location = post_response.redirect; document.location = post_response.redirect;
} else { } else {
$.ajax({ $.ajax({
url: document.location, url: document.location,
success: function(data) { success: function(data) {
$(data).find('div.post.reply').each(function() { $(data).find('div.post.reply').each(function() {
var id = $(this).attr('id'); var id = $(this).attr('id');
if($('#' + id).length == 0) { if($('#' + id).length == 0) {
$(this).insertAfter($('div.post:last').next()).after('<br class="clear">'); $(this).insertAfter($('div.post:last').next()).after('<br class="clear">');
$(document).trigger('new_post', this); $(document).trigger('new_post', this);
// watch.js & auto-reload.js retrigger // watch.js & auto-reload.js retrigger
setTimeout(function() { $(window).trigger("scroll"); }, 100); setTimeout(function() { $(window).trigger("scroll"); }, 100);
} }
}); });
highlightReply(post_response.id); highlightReply(post_response.id);
window.location.hash = post_response.id; window.location.hash = post_response.id;
$(window).scrollTop($(document).height()); $(window).scrollTop($(document).height());
$(form).find('input[type="submit"]').val(submit_txt); $(form).find('input[type="submit"]').val(submit_txt);
$(form).find('input[type="submit"]').removeAttr('disabled'); $(form).find('input[type="submit"]').removeAttr('disabled');
$(form).find('input[name="subject"],input[name="file_url"],\ $(form).find('input[name="subject"],input[name="file_url"],\
textarea[name="body"],input[type="file"]').val('').change(); textarea[name="body"],input[type="file"]').val('').change();
}, },
cache: false, cache: false,
contentType: false, contentType: false,
processData: false processData: false
}, 'html'); }, 'html');
} }
$(form).find('input[type="submit"]').val(_('Posted...')); $(form).find('input[type="submit"]').val(_('Posted...'));
$(document).trigger("ajax_after_post", post_response); $(document).trigger("ajax_after_post", post_response);
} else { } else {
alert(_('An unknown error occured when posting!')); alert(_('An unknown error occured when posting!'));
$(form).find('input[type="submit"]').val(submit_txt); $(form).find('input[type="submit"]').val(submit_txt);
$(form).find('input[type="submit"]').removeAttr('disabled'); $(form).find('input[type="submit"]').removeAttr('disabled');
} }
}, },
error: function(xhr, status, er) { error: function(xhr, status, er) {
console.log(xhr); 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>"); 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"]').val(submit_txt);
$(form).find('input[type="submit"]').removeAttr('disabled'); $(form).find('input[type="submit"]').removeAttr('disabled');
}, },
data: formData, data: formData,
cache: false, cache: false,
contentType: false, contentType: false,
processData: false processData: false
}, 'json'); }, 'json');
$(form).find('input[type="submit"]').val(_('Posting...')); $(form).find('input[type="submit"]').val(_('Posting...'));
$(form).find('input[type="submit"]').attr('disabled', true); $(form).find('input[type="submit"]').attr('disabled', true);
return false; return false;
}); });
}; };
setup_form($('form[name="post"]')); setup_form($('form[name="post"]'));
$(window).on('quick-reply', function() { $(window).on('quick-reply', function() {
$('form#quick-reply').off('submit'); $('form#quick-reply').off('submit');
setup_form($('form#quick-reply')); setup_form($('form#quick-reply'));
}); });
}); });

Loading…
Cancel
Save