1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-12-05 04:18:03 +01:00
vichan/js/ajax.js

147 lines
5.1 KiB
JavaScript
Raw Normal View History

2013-09-15 06:03:27 +02: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>
* Copyright (c) 2013-2014 Marcin Łabanowski <marcin@6irc.net>
2013-09-15 06:03:27 +02:00
*
* Usage:
* $config['additional_javascript'][] = 'js/jquery.min.js';
* $config['additional_javascript'][] = 'js/ajax.js';
*
*/
+function() {
var settings = new script_settings('ajax');
2013-09-15 06:54:18 +02:00
var do_not_ajax = false;
2013-09-15 06:03:27 +02:00
var setup_form = function($form) {
$form.submit(function() {
if (do_not_ajax)
2013-09-15 06:54:18 +02:00
return true;
2015-03-11 13:43:51 +01:00
var form = $(this).find('form')[0];
2013-09-15 06:03:27 +02:00
var submit_txt = $(this).find('input[type="submit"]').val();
2013-09-15 06:54:18 +02:00
if (window.FormData === undefined)
2013-09-15 06:03:27 +02:00
return true;
2015-03-11 13:43:51 +01:00
var formData = new FormData(form);
2013-09-15 06:03:27 +02:00
formData.append('json_response', '1');
2013-09-15 07:06:29 +02:00
formData.append('post', submit_txt);
2013-09-15 06:03:27 +02:00
$(document).trigger("ajax_before_post", formData);
2013-09-15 06:03:27 +02:00
var updateProgress = function(e) {
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 06:03:27 +02:00
};
$.ajax({
url: configRoot+'post.php',
2013-09-15 06:03:27 +02:00
type: 'POST',
xhr: function() {
var xhr = $.ajaxSettings.xhr();
if(xhr.upload) {
xhr.upload.addEventListener('progress', updateProgress, false);
}
return xhr;
},
2014-11-21 13:05:20 +01:00
success: function(post_response, textStatus, xhr) {
2013-09-15 06:03:27 +02:00
if (post_response.error) {
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');
if (post_response.error == 'Sorry. Tor users can\'t upload files.') {
$(form).find('input[name="file_url"],input[type="file"]').val('').change();
}
}
2013-09-15 06:03:27 +02:00
} else if (post_response.redirect && post_response.id) {
if (!$(form).find('input[name="thread"]').length
|| (!settings.get('always_noko_replies', true) && !post_response.noko)) {
2013-09-15 06:11:09 +02:00
document.location = post_response.redirect;
} else {
$.ajax({
2013-09-15 07:14:02 +02:00
url: document.location,
2013-09-15 06:11:09 +02: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);
// watch.js & auto-reload.js retrigger
2014-01-26 18:58:42 +01:00
setTimeout(function() { $(window).trigger("scroll"); }, 100);
2013-09-15 06:11:09 +02:00
}
});
2013-09-15 07:06:29 +02:00
2013-09-15 06:11:09 +02:00
highlightReply(post_response.id);
2013-09-15 07:06:29 +02:00
window.location.hash = post_response.id;
$(window).scrollTop($('div.post#reply_' + post_response.id).offset().top);
2013-09-15 06:11:09 +02:00
$(form).find('input[type="submit"]').val(submit_txt);
$(form).find('input[type="submit"]').removeAttr('disabled');
2013-09-15 06:19:25 +02:00
$(form).find('input[name="subject"],input[name="file_url"],\
2015-03-11 00:18:28 +01:00
textarea[name="body"],input[type="file"],input[name="embed"]').val('').change();
2013-09-15 06:11:09 +02:00
},
cache: false,
contentType: false,
processData: false
}, 'html');
}
2013-09-15 12:15:17 +02:00
$(form).find('input[type="submit"]').val(_('Posted...'));
2014-10-18 15:31:48 +02:00
$(document).trigger("ajax_after_post", post_response);
2013-09-15 06:03:27 +02:00
} else {
2014-11-21 13:05:20 +01:00
console.log(xhr);
2013-09-15 06:03:27 +02:00
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) {
2014-11-21 13:05:20 +01:00
console.log(xhr);
2015-03-11 10:58:03 +01:00
alert(_('The server took too long to submit your post. Your post was probably still submitted. If it wasn\'t, 8chan 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 06:03:27 +02:00
},
data: formData,
cache: false,
contentType: false,
processData: false
2013-09-15 06:11:09 +02:00
}, 'json');
2013-09-15 06:03:27 +02:00
$(form).find('input[type="submit"]').val(_('Posting...'));
$(form).find('input[type="submit"]').attr('disabled', true);
return false;
});
};
2013-09-15 06:54:18 +02:00
$(window).on('quick-reply', function() {
$('div#quick-reply form').off('submit');
2015-03-11 13:43:51 +01:00
setup_form($('div#quick-reply'));
2013-09-15 06:03:27 +02:00
});
onready(function(){
// Enable submit button if disabled (cache problem)
$('input[type="submit"]').removeAttr('disabled');
setup_form($('div#post-form-outer'));
});
}();