[ticket/10271] Added phpbb.confirm_box (JavaScript).

As well as adding the method, this commit also changes the previous commit
so that deleting a post from viewtopic uses this method, too. This commit
has also made some improvements to phpbb.alert and phpbb.confirm.

PHPBB3-10271
This commit is contained in:
Callum Macrae 2011-07-14 14:41:24 +01:00 committed by Igor Wiedler
parent d420ceb9c7
commit e6401c081e

View file

@ -5,11 +5,13 @@ var phpbb = {};
* *
* @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.
*
* @return Returns the div created.
*/ */
phpbb.alert = function(title, msg) { phpbb.alert = function(title, msg) {
var div = $('<div class="jalert"><h3>' + title + '</h3><p>' + msg + '</p></div>'); var div = $('<div class="jalert"><h3>' + title + '</h3><p>' + msg + '</p></div>');
$(document).bind('click', function(e) { $(document).one('click', function(e) {
if ($(e.target).parents('.jalert').length) if ($(e.target).parents('.jalert').length)
{ {
return true; return true;
@ -22,6 +24,7 @@ phpbb.alert = function(title, msg) {
$('body').append(div); $('body').append(div);
div.show(300); div.show(300);
return div;
} }
/** /**
@ -29,6 +32,8 @@ phpbb.alert = function(title, msg) {
* *
* @param string msg Message to display. Can be HTML. * @param string msg Message to display. Can be HTML.
* @param function callback Callback. * @param function callback Callback.
*
* @return Returns the div created.
*/ */
phpbb.confirm = function(msg, callback) { phpbb.confirm = function(msg, callback) {
var div = $('<div class="jalert"><p>' + msg + '</p>\ var div = $('<div class="jalert"><p>' + msg + '</p>\
@ -45,36 +50,74 @@ phpbb.confirm = function(msg, callback) {
return false; return false;
}); });
div.show(300); div.show(300);
return div;
} }
/**
$('.delete-icon a').click(function() * 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)
{ {
var pid = this.href.split('&p=')[1]; __self = this;
var __self = this; $(condition).click(function() {
$.get(this.href, function(res) { var __self = this;
res = JSON.parse(res); $.get(this.href, function(res) {
phpbb.confirm(res.MESSAGE_TEXT, function(del) { res = JSON.parse(res);
if (del) phpbb.confirm(res.MESSAGE_TEXT, function(del) {
{ if (del)
var p = res.S_CONFIRM_ACTION.split('?'); {
p[1] += '&confirm=Yes' var p = res.S_CONFIRM_ACTION.split('?');
$.post(p[0], p[1], function(res) { p[1] += '&confirm=Yes';
res = JSON.parse(res); $.post(p[0], p[1], function(res) {
phpbb.alert(res.MESSAGE_TITLE, res.MESSAGE_TEXT) res = JSON.parse(res);
$(__self).parents('div #p' + pid).remove(); var alert = phpbb.alert(res.MESSAGE_TITLE, res.MESSAGE_TEXT);
callback(__self);
//if there is a refresh, check that it isn't to the same place if (res.REFRESH_DATA)
if (res.REFRESH_DATA && res.REFRESH_DATA.url.indexOf('t=') === -1) {
{ if (typeof refresh === 'function')
setTimeout(function() { {
window.location = res.REFRESH_DATA.url; refresh = refresh(res.REFRESH_DATA.url)
}, res.REFRESH_DATA.time * 1000); }
} else if (typeof refresh !== 'boolean')
}); {
} refresh = false;
}
if (refresh)
{
setTimeout(function() {
window.location = res.REFRESH_DATA.url;
}, res.REFRESH_DATA.time * 1000);
}
else
{
setTimeout(function() {
div.hide(300, function() {
div.remove();
});
}, res.REFRESH_DATA.time * 1000);
}
}
});
}
});
}); });
return false;
}); });
return false; }
});
var refresh = function(url) {
return (url.indexOf('t=') === -1);
}
var callback = function(el) {
var pid = el.href.split('&p=')[1];
$(el).parents('div #p' + pid).remove();
}
phpbb.confirm_box('.delete-icon a', refresh, callback);