JavaScript Function.phpbb.ajaxify

This code is not yet merged into the core, do not try to use it.

phpbb.ajaxify –– automatically makes a link or form use AJAX instead of loading an entire page.

Description
The function automatically makes a link or form use AJAX instead of loading an entire page. It supports both normal links and links that would link to a page which calls confirm_box, detecting which type of link it is without the developer having to tell it. It also supports forms. This function is generally called automatically for links and forms with the data-ajax attribute set.
 * 1) function phpbb.ajaxify ( options )

Callbacks
The callback option specifies the name of a function that has been added to the list of callbacks using phpbb.add_ajax_callback, which will be called on successful completion of the event.

The callback will not be called if the user presses "No" on the confirm_box.

It is sent two parameters, and the element can be accessed using "this":


 * 1) function callback ( data, action )

Filters
Filters are given to the function using the options array (see above), and are called when an ajaxified link or form is clicked or submitted. It can cancel the functions and make the link or form perform normally (load a new page). It receives the form data as an argument, and the element can be accessed using "this":


 * 1) function filter ( data )

If it returns false, then the ajax request will be cancelled and the page will load as if phpbb.ajaxify was never called. If it returns true, then phpbb.ajaxify will continue normally.

data-ajax
Unless the style author has removed the code that calls this (which can be found below), any element with the data-ajax attribute set will automatically be ajaxified. If data-ajax is set to true, then no callback will be sent, but any other value will be sent as the name of the callback to be called. If data-ajax is set to false, then it will be ignored entirely.

data-refresh
On elements with data-ajax set, data-refresh will be used as the refresh value. It can only be true or false - it cannot be a function.

The Code
$('[data-ajax]').each(function {	var $this = $(this),		ajax = $this.attr('data-ajax');

if (ajax !== 'false') { var fn = (ajax !== 'true') ? ajax : null; phpbb.ajaxify({			selector: this,			refresh: $this.attr('data-refresh') !== undefined,			callback: fn		}); } });

Special JSON parameters
If the server responds with a JSON response there are a number of parameters that trigger special behaviour:


 * S_CONFIRM_ACTION: If this key is set on the JSON response, a confirm form will be displayed in an overlay. It will use MESSAGE_TEXT for the text, S_HIDDEN_FIELDS for the hidden fields (this is raw HTML), S_CONFIRM_ACTION as the target URL and YES_VALUE as the label of the confirm button.


 * MESSAGE_TITLE: If S_CONFIRM_ACTION is not set, but this is, a alert box with MESSAGE_TITLE as the title and MESSAGE_TEXT as the text are displayed.


 * REFRESH_DATA: If this is supplied, the user will be redirected (client-side) to REFRESH_DATA.url after REFRESH_DATA.time seconds. Optionally a refresh option can be supplied to the initial phpbb.ajaxify call, which can be a function converting the URL to a boolean, or a boolean directly. Depending on the value of refresh, the refresh will actually happen, or not.