From 4b45ccc4ee3780f1393cfa9b1944d8f2b809afa6 Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Mon, 16 Sep 2013 04:42:13 +1000 Subject: ajax-post-controls.js --- js/ajax-post-controls.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++ js/quick-post-controls.js | 2 ++ 2 files changed, 79 insertions(+) create mode 100644 js/ajax-post-controls.js (limited to 'js') 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'); -- cgit v1.3