diff --git a/js/ajax-post-controls.js b/js/ajax-post-controls.js new file mode 100644 index 00000000..4671e82e --- /dev/null +++ b/js/ajax-post-controls.js @@ -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 + * + * 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)); + }); +}); diff --git a/js/quick-post-controls.js b/js/quick-post-controls.js index 1cbc0711..efce3371 100644 --- a/js/quick-post-controls.js +++ b/js/quick-post-controls.js @@ -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'); diff --git a/post.php b/post.php index 026e2b2d..8bc7b51d 100644 --- a/post.php +++ b/post.php @@ -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']);