1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-11-23 23:20:57 +01:00

ajax-post-controls.js

This commit is contained in:
Michael Foster 2013-09-16 04:42:13 +10:00
parent 5d108319eb
commit 4b45ccc4ee
3 changed files with 92 additions and 4 deletions

77
js/ajax-post-controls.js Normal file
View File

@ -0,0 +1,77 @@
/*
* ajax-post-controls.js
* https://github.com/savetheinternet/Tinyboard/blob/master/js/ajax-post-controls.js
*
* Released under the MIT license
* Copyright (c) 2013 Michael Save <savetheinternet@tinyboard.org>
*
* Usage:
* $config['additional_javascript'][] = 'js/jquery.min.js';
* $config['additional_javascript'][] = 'js/ajax-post-controls.js';
*
*/
$(window).ready(function() {
var do_not_ajax = false;
var setup_form = function($form) {
$form.find('input[type="submit"]').click(function() {
$form.data('submit-btn', this);
});;
$form.submit(function(e) {
if (!$(this).data('submit-btn'))
return true;
if (do_not_ajax)
return true;
if (window.FormData === undefined)
return true;
var form = this;
var formData = new FormData(this);
formData.append('json_response', '1');
formData.append($($(form).data('submit-btn')).attr('name'), $($(form).data('submit-btn')).val());
$.ajax({
url: this.action,
type: 'POST',
success: function(post_response) {
if (post_response.error) {
alert(post_response.error);
} else if (post_response.success) {
if ($($(form).data('submit-btn')).attr('name') == 'report') {
alert(_('Reported post(s).'));
if ($(form).hasClass('post-actions')) {
$(form).parent().find('input[type="checkbox"].delete').click();
} else {
$(form).find('input[name="reason"]').val('');
}
} else {
window.location.reload();
}
} else {
alert(_('An unknown error occured!'));
}
$($(form).data('submit-btn')).val($($(form).data('submit-btn')).data('orig-val')).removeAttr('disabled');
},
error: function(xhr, status, er) {
// An error occured
// TODO
alert(_('Something went wrong... An unknown error occured!'));
},
data: formData,
cache: false,
contentType: false,
processData: false
}, 'json');
$($(form).data('submit-btn')).attr('disabled', true).data('orig-val', $($(form).data('submit-btn')).val()).val(_('Working...'));
return false;
});
};
setup_form($('form[name="postcontrols"]'));
$(window).on('quick-post-controls', function(e, form) {
setup_form($(form));
});
});

View File

@ -63,6 +63,8 @@ $(document).ready(function(){
post_form.appendTo($(this).parent().parent());
//post_form.insertBefore($(this));
}
$(window).trigger('quick-post-controls', post_form);
} else {
var elm = $(this).parent().parent().find('form');

View File

@ -58,7 +58,7 @@ if (isset($_POST['delete'])) {
if ($password != '' && $post['password'] != $password)
error($config['error']['invalidpassword']);
if ($post['time'] >= time() - $config['delete_time']) {
if ($post['time'] > time() - $config['delete_time']) {
error(sprintf($config['error']['delete_too_soon'], until($post['time'] + $config['delete_time'])));
}
@ -84,8 +84,12 @@ if (isset($_POST['delete'])) {
$is_mod = isset($_POST['mod']) && $_POST['mod'];
$root = $is_mod ? $config['root'] . $config['file_mod'] . '?/' : $config['root'];
header('Location: ' . $root . $board['dir'] . $config['file_index'], true, $config['redirect_http']);
if (!isset($_POST['json_response'])) {
header('Location: ' . $root . $board['dir'] . $config['file_index'], true, $config['redirect_http']);
} else {
header('Content-Type: text/json');
echo json_encode(array('success' => true));
}
} elseif (isset($_POST['report'])) {
if (!isset($_POST['board'], $_POST['password'], $_POST['reason']))
error($config['error']['bot']);
@ -139,7 +143,12 @@ if (isset($_POST['delete'])) {
$is_mod = isset($_POST['mod']) && $_POST['mod'];
$root = $is_mod ? $config['root'] . $config['file_mod'] . '?/' : $config['root'];
header('Location: ' . $root . $board['dir'] . $config['file_index'], true, $config['redirect_http']);
if (!isset($_POST['json_response'])) {
header('Location: ' . $root . $board['dir'] . $config['file_index'], true, $config['redirect_http']);
} else {
header('Content-Type: text/json');
echo json_encode(array('success' => true));
}
} elseif (isset($_POST['post'])) {
if (!isset($_POST['body'], $_POST['board']))
error($config['error']['bot']);