[feature/ajax] Cleaned up AJAX-related JavaScript.

Mostly just added comments, but has cleaned up some actual code too.

PHPBB3-10270
This commit is contained in:
Callum Macrae 2011-07-14 17:49:17 +01:00 committed by Igor Wiedler
parent 2556f5fcc2
commit c4aaf3ae5a

View file

@ -1,7 +1,7 @@
var phpbb = {}; var phpbb = {};
/** /**
* Display a simple alert. * Display a simple alert similar to JSs native alert().
* *
* @param string title Title of the message, eg "Information" * @param string title Title of the message, eg "Information"
* @param string msg Message to display. Can be HTML. * @param string msg Message to display. Can be HTML.
@ -54,7 +54,7 @@ phpbb.confirm = function(msg, callback) {
} }
/** /**
* Clearing up some duplicate code - don't use this. * Works out what to do with the refresh. Don't use this.
*/ */
function handle_refresh(data, refresh, div) function handle_refresh(data, refresh, div)
{ {
@ -97,7 +97,6 @@ function handle_refresh(data, refresh, div)
*/ */
phpbb.confirm_box = function(condition, refresh, callback) phpbb.confirm_box = function(condition, refresh, callback)
{ {
__self = this;
$(condition).click(function() { $(condition).click(function() {
var __self = this; var __self = this;
$.get(this.href, function(res) { $.get(this.href, function(res) {
@ -106,16 +105,13 @@ phpbb.confirm_box = function(condition, refresh, callback)
if (del) if (del)
{ {
var p = res.S_CONFIRM_ACTION.split('?'); var p = res.S_CONFIRM_ACTION.split('?');
p[1] += '&confirm=Yes'; $.post(p[0], p[1] + '&confirm=Yes', function(res) {
$.post(p[0], p[1], 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(__self); callback(__self);
} }
handle_refresh(res.REFRESH_DATA, refresh, alert); handle_refresh(res.REFRESH_DATA, refresh, alert);
}); });
} }
@ -127,6 +123,11 @@ phpbb.confirm_box = function(condition, refresh, callback)
/** /**
* Makes a link use AJAX instead of loading an entire page. * Makes a link use AJAX instead of loading an entire page.
*
* @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 = function(selector, refresh, callback) { phpbb.ajaxify = function(selector, refresh, callback) {
$(selector).click(function() { $(selector).click(function() {
@ -138,23 +139,25 @@ phpbb.ajaxify = function(selector, refresh, callback) {
{ {
callback(__self, res); callback(__self, res);
} }
handle_refresh(res.REFRESH_DATA, refresh, alert); handle_refresh(res.REFRESH_DATA, refresh, alert);
}); });
return false; return false;
}); });
} }
//bind the confirm_boxes
var refresh = function(url) { var refresh = function(url) {
return (url.indexOf('t=') === -1); return (url.indexOf('t=') === -1);
} }
var callback = function(el) { phpbb.confirm_box('.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('.delete-icon a', refresh, callback);
phpbb.confirm_box('a[href$="ucp.php?mode=delete_cookies"]', true); phpbb.confirm_box('a[href$="ucp.php?mode=delete_cookies"]', true);
//AJAXify some links
phpbb.ajaxify('a[href*="&bookmark=1"]', false, function(el, res) { phpbb.ajaxify('a[href*="&bookmark=1"]', false, function(el, 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';