diff --git a/phpBB/styles/script.js b/phpBB/styles/script.js index 5d9b89e386..e893b84bf8 100644 --- a/phpBB/styles/script.js +++ b/phpBB/styles/script.js @@ -75,72 +75,7 @@ phpbb.confirm = function(msg, callback) { /** * Works out what to do with the refresh. Don't use this. */ -function handle_refresh(data, refresh, div) -{ - if (data) - { - if (typeof refresh === 'function') - { - refresh = refresh(data.url) - } - else if (typeof refresh !== 'boolean') - { - refresh = false; - } - if (refresh) - { - setTimeout(function() { - window.location = data.url; - }, data.time * 1000); - } - else - { - setTimeout(function() { - div.animate({opacity: 0}, 300, function() { - div.remove(); - }); - }, data.time * 1000); - } - } -} - - -/** - * This function interacts via AJAX with phpBBs confirm_box function. - * - * @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.confirm_box = function(condition, refresh, callback) -{ - $(condition).click(function() { - 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=' + 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); - } - handle_refresh(res.REFRESH_DATA, refresh, alert); - }); - } - }); - }); - return false; - }); -} /** * Makes a link use AJAX instead of loading an entire page. @@ -151,62 +86,92 @@ phpbb.confirm_box = function(condition, refresh, callback) * @param function callback Callback. */ phpbb.ajaxify = function(selector, refresh, callback) { - $(selector).click(function() { - 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') - { - callback(that, res); - } - handle_refresh(res.REFRESH_DATA, refresh, alert); - }); - return false; - }); -} -/** - * 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) + //private function to handle refreshes + function handle_refresh(data, refresh, div) + { + if (!data) { - data += '&action=' + act[1]; + return; } - $.post(path, data, function(res) { + refresh = ((typeof refresh === 'function') ? refresh(data.url) : + (typeof refresh === 'boolean') && refresh); + + setTimeout(function() { + if (refresh) + { + window.location = data.url; + } + else + { + div.remove(); + } + }, data.time * 1000); + } + + var is_form = $(selector).is('form'); + $(selector + ((is_form) ? ' input:submit' : '')).click(function() { + var act, data, path, that = this; + function return_handler(res) + { res = JSON.parse(res); - phpbb.confirm(res.MESSAGE_TEXT, function(del) { - if (del) + + if (typeof res.S_CONFIRM_ACTION === 'undefined') + { + /** + * It is a standard link, no confirm_box required. + */ + var alert = phpbb.alert(res.MESSAGE_TITLE, res.MESSAGE_TEXT); + if (typeof callback !== 'undefined') { - 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); - }); + callback(that, res); } - }); - }); + handle_refresh(res.REFRESH_DATA, refresh, alert); + } + else + { + /** + * confirm_box - confirm with the user and send back + */ + phpbb.confirm(res.MESSAGE_TEXT, function(del) { + if (del) + { + data = $('
' + res.S_HIDDEN_FIELDS + '
').serialize(); + path = res.S_CONFIRM_ACTION; + $.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, (is_form) ? act : null); + } + handle_refresh(res.REFRESH_DATA, refresh, alert); + }); + } + }); + } + } + + if (is_form) + { + act = /action\[([a-z]+)\]/.exec(this.name); + data = decodeURI($(this).closest('form').serialize()); + path = $(this).closest('form').attr('action').replace('&', '&'); + + if (act) + { + act = act[1] + data += '&action=' + act; + } + + $.post(path, data, return_handler); + } + else + { + $.get(this.href, return_handler); + } + return false; }); } @@ -216,20 +181,21 @@ phpbb.ajaxify_form = function(selector, refresh, callback) var refresh = function(url) { return (url.indexOf('t=') === -1); } -phpbb.confirm_box('.delete-icon a', refresh, function(el) { +phpbb.ajaxify('.delete-icon a', refresh, function(el) { var pid = el.href.split('&p=')[1]; $(el).parents('div #p' + pid).remove(); }); -phpbb.confirm_box('a[href$="ucp.php?mode=delete_cookies"]', true); +phpbb.ajaxify('a[href$="ucp.php?mode=delete_cookies"]', true); //AJAXify some links phpbb.ajaxify('a[href*="&bookmark=1"]', false, function(el, res) { + console.log(res); var text = (res.MESSAGE_TEXT.indexOf('Removed') === -1); text = (text) ? 'Remove from bookmarks' : 'Bookmark topic'; $(el).text(el.title = text); }); -phpbb.ajaxify('a[href*="&watch=topic"]', false, function(el, res) { +phpbb.ajaxify('a[href*="watch=topic"]', false, function(el, res) { var text = (res.MESSAGE_TEXT.indexOf('no longer subscribed') === -1); text = (text) ? 'Unsubscribe topic' : 'Subscribe topic'; $(el).text(el.title = text); @@ -242,6 +208,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 -phpbb.ajaxify_form('.mcp_approve', false, function(el, act) { +phpbb.ajaxify('.mcp_approve', false, function(el, act) { $(el).parents((act === 'approve') ? '.rules' : '.post').remove(); });