[feature/ajax] Reduced duplicate code by merging all AJAX function into one.

This commit merges phpbb.confirm_box, phpbb.ajaxify and phpbb.ajaxify_form into
one function which automatically detects what is happening and calls the
correct code accordingly. This has removed a lot of duplicate code and
generally made the code cleaner.

PHPBB3-10270
This commit is contained in:
Callum Macrae 2011-07-17 15:27:58 +01:00 committed by Igor Wiedler
parent f42f6f1902
commit fbad17f912

View file

@ -75,72 +75,7 @@ phpbb.confirm = function(msg, callback) {
/** /**
* Works out what to do with the refresh. Don't use this. * 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 = $('<form>' + res.S_HIDDEN_FIELDS + '</form>').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. * 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. * @param function callback Callback.
*/ */
phpbb.ajaxify = function(selector, refresh, callback) { phpbb.ajaxify = function(selector, refresh, callback) {
$(selector).click(function() {
var that = this; //private function to handle refreshes
$.get(this.href, function(res) { function handle_refresh(data, refresh, div)
{
if (!data)
{
return;
}
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); res = JSON.parse(res);
console.log(res);
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); var alert = phpbb.alert(res.MESSAGE_TITLE, res.MESSAGE_TEXT);
if (typeof callback !== 'undefined') if (typeof callback !== 'undefined')
{ {
callback(that, res); callback(that, res);
} }
handle_refresh(res.REFRESH_DATA, refresh, alert); handle_refresh(res.REFRESH_DATA, refresh, alert);
});
return false;
});
} }
else
{
/** /**
* AJAXifies a form. This will automatically get the action from the submit. * confirm_box - confirm with the user and send back
*
* @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('&amp;', '&'),
that = this;
if (act)
{
data += '&action=' + act[1];
}
$.post(path, data, function(res) {
res = JSON.parse(res);
phpbb.confirm(res.MESSAGE_TEXT, function(del) { phpbb.confirm(res.MESSAGE_TEXT, function(del) {
if (del) if (del)
{ {
path = res.S_CONFIRM_ACTION;
data = $('<form>' + res.S_HIDDEN_FIELDS + '</form>').serialize(); data = $('<form>' + res.S_HIDDEN_FIELDS + '</form>').serialize();
path = res.S_CONFIRM_ACTION;
$.post(path, data + '&confirm=' + res.YES_VALUE, function(res) { $.post(path, data + '&confirm=' + res.YES_VALUE, function(res) {
res = JSON.parse(res); res = JSON.parse(res);
var alert = phpbb.alert(res.MESSAGE_TITLE, res.MESSAGE_TEXT); var alert = phpbb.alert(res.MESSAGE_TITLE, res.MESSAGE_TEXT);
if (typeof callback !== 'undefined') if (typeof callback !== 'undefined')
{ {
callback(that, act[1]); callback(that, (is_form) ? act : null);
} }
handle_refresh(res.REFRESH_DATA, refresh, alert); 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('&amp;', '&');
if (act)
{
act = act[1]
data += '&action=' + act;
}
$.post(path, data, return_handler);
}
else
{
$.get(this.href, return_handler);
}
return false; return false;
}); });
} }
@ -216,20 +181,21 @@ phpbb.ajaxify_form = function(selector, refresh, callback)
var refresh = function(url) { var refresh = function(url) {
return (url.indexOf('t=') === -1); 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]; var pid = el.href.split('&p=')[1];
$(el).parents('div #p' + pid).remove(); $(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 //AJAXify some links
phpbb.ajaxify('a[href*="&bookmark=1"]', false, function(el, res) { phpbb.ajaxify('a[href*="&bookmark=1"]', false, function(el, res) {
console.log(res);
var text = (res.MESSAGE_TEXT.indexOf('Removed') === -1); var text = (res.MESSAGE_TEXT.indexOf('Removed') === -1);
text = (text) ? 'Remove from bookmarks' : 'Bookmark topic'; text = (text) ? 'Remove from bookmarks' : 'Bookmark topic';
$(el).text(el.title = text); $(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); var text = (res.MESSAGE_TEXT.indexOf('no longer subscribed') === -1);
text = (text) ? 'Unsubscribe topic' : 'Subscribe topic'; text = (text) ? 'Unsubscribe topic' : 'Subscribe topic';
$(el).text(el.title = text); $(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*="mode=bump"]');
phpbb.ajaxify('a[href*="mark="]'); //captures topics and forums 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(); $(el).parents((act === 'approve') ? '.rules' : '.post').remove();
}); });