From f42f6f19028fab5b047baae0df5004a43e946a30 Mon Sep 17 00:00:00 2001 From: Callum Macrae Date: Sun, 17 Jul 2011 14:57:13 +0100 Subject: [PATCH] [ticket/10273] Added phpbb.ajaxify_form and converted accept / deny to it. Also made a few minor improvements to other JavaScript. PHPBB3-10273 --- .../prosilver/template/viewtopic_body.html | 4 +- phpBB/styles/script.js | 125 +++++++++++------- 2 files changed, 80 insertions(+), 49 deletions(-) diff --git a/phpBB/styles/prosilver/template/viewtopic_body.html b/phpBB/styles/prosilver/template/viewtopic_body.html index 5571ce07c4..856ee6962c 100644 --- a/phpBB/styles/prosilver/template/viewtopic_body.html +++ b/phpBB/styles/prosilver/template/viewtopic_body.html @@ -139,8 +139,8 @@

{UNAPPROVED_IMG} {L_POST_UNAPPROVED}   -   - +   + {S_FORM_TOKEN}
diff --git a/phpBB/styles/script.js b/phpBB/styles/script.js index 17934ed6ae..5d9b89e386 100644 --- a/phpBB/styles/script.js +++ b/phpBB/styles/script.js @@ -1,3 +1,26 @@ +/** + * Make some changes to the jQuery core. + */ +$.fn.hide = function() { + this.animate({opacity: 0}, 300, function() { + $(this).css('display', 'none') + .css('opacity', 1); + }); +} +$.fn.show = function() { + this.css('opacity', 0) + .css('display', 'block') + .animate({opacity: 1}, 300); +} + +$.fn.remove_old = $.fn.remove; +$.fn.remove = function() { + this.animate({opacity: 0}, 300, function() { + $(this).remove_old(); + }); +} + + var phpbb = {}; /** @@ -16,14 +39,12 @@ phpbb.alert = function(title, msg) { { return true; } - div.animate({opacity: 0}, 300, function() { - div.remove(); - }); + div.remove(); return false; }); $('body').append(div); - div.css('opacity', 0).show().animate({opacity: 1}, 300); + div.show(); return div; } @@ -37,19 +58,17 @@ phpbb.alert = function(title, msg) { */ phpbb.confirm = function(msg, callback) { var div = $('

' + msg + '

\ -  \ -
'); +  \ + '); $('body').append(div); $('.jalertbut').bind('click', function(event) { - div.animate({opacity: 0}, 300, function() { - div.remove(); - }); + div.remove(); callback(this.value === 'Yes'); return false; }); - div.css('opacity', 0).show().animate({opacity: 1}, 300); + div.show(); return div; } @@ -101,12 +120,13 @@ phpbb.confirm_box = function(condition, refresh, callback) var that = this; $.get(this.href, function(res) { res = JSON.parse(res); + console.log(res); phpbb.confirm(res.MESSAGE_TEXT, function(del) { if (del) { var path = res.S_CONFIRM_ACTION; var data = $('
' + res.S_HIDDEN_FIELDS + '
').serialize(); - $.post(path, data + '&confirm=Yes', function(res) { + $.post(path, data + '&confirm=' + res.YES_VALUE, function(res) { res = JSON.parse(res); var alert = phpbb.alert(res.MESSAGE_TITLE, res.MESSAGE_TEXT); if (typeof callback !== 'undefined') @@ -135,6 +155,7 @@ phpbb.ajaxify = function(selector, refresh, callback) { var that = this; $.get(this.href, function(res) { res = JSON.parse(res); + console.log(res); var alert = phpbb.alert(res.MESSAGE_TITLE, res.MESSAGE_TEXT); if (typeof callback !== 'undefined') { @@ -146,6 +167,50 @@ phpbb.ajaxify = function(selector, refresh, callback) { }); } +/** + * AJAXifies a form. This will automatically get the action from the submit. + * + * @param string condition The element to capture. + * @param bool/function refresh If we are sent back a refresh, should it be + * acted upon? This can either be true / false / a function. + * @param function callback Callback. + */ +phpbb.ajaxify_form = function(selector, refresh, callback) +{ + $(selector + ' input:submit').click(function(e) { + var act = /action\[([a-z]+)\]/.exec(this.name), + data = decodeURI($(this).closest('form').serialize()), + path = $(this).closest('form').attr('action').replace('&', '&'), + that = this; + + if (act) + { + data += '&action=' + act[1]; + } + + $.post(path, data, function(res) { + res = JSON.parse(res); + phpbb.confirm(res.MESSAGE_TEXT, function(del) { + if (del) + { + path = res.S_CONFIRM_ACTION; + data = $('
' + res.S_HIDDEN_FIELDS + '
').serialize(); + $.post(path, data + '&confirm=' + res.YES_VALUE, function(res) { + res = JSON.parse(res); + var alert = phpbb.alert(res.MESSAGE_TITLE, res.MESSAGE_TEXT); + if (typeof callback !== 'undefined') + { + callback(that, act[1]); + } + handle_refresh(res.REFRESH_DATA, refresh, alert); + }); + } + }); + }); + return false; + }); +} + //bind the confirm_boxes var refresh = function(url) { @@ -177,40 +242,6 @@ phpbb.ajaxify('a[href*="watch=forum"]', false, function(el, res) { phpbb.ajaxify('a[href*="mode=bump"]'); phpbb.ajaxify('a[href*="mark="]'); //captures topics and forums - - -/** - * Forms have to be captured manually, as they're all different. - */ -$('input[name^="action"]').click(function(e) { - var that = this; - var path = $(this).parents('form')[0].action.replace('&', '&'); - var action = (this.name === 'action[approve]') ? 'approve' : 'disapprove'; - var data = { - action: action, - post_id_list: [$(this).siblings('input[name="post_id_list[]"]')[0].value] - }; - $.post(path, data, function(res) { - res = JSON.parse(res); - phpbb.confirm(res.MESSAGE_TEXT, function(del) { - if (del) - { - path = res.S_CONFIRM_ACTION; - data = $('
' + res.S_HIDDEN_FIELDS + '
').serialize(); - $.post(path, data + '&confirm=Yes', function(res) { - res = JSON.parse(res); - var alert = phpbb.alert(res.MESSAGE_TITLE, res.MESSAGE_TEXT); - - $(that).parents((action === 'approve') ? '.rules' : '.post').remove(); - - setTimeout(function() { - alert.hide(300, function() { - alert.remove(); - }); - }, 5000); - }); - } - }); - }); - return false; +phpbb.ajaxify_form('.mcp_approve', false, function(el, act) { + $(el).parents((act === 'approve') ? '.rules' : '.post').remove(); });