Commit 5a23b3fd authored by Dries's avatar Dries

- Patch #561858 by effulgentsia, sun, rfay, Nick_vh, merlinofchaos, katbailey,...

- Patch #561858 by effulgentsia, sun, rfay, Nick_vh, merlinofchaos, katbailey, dereine, tstoeckler: drupal_add_js() and drupal_add_css() to work for AJAX requests too by adding lazy-load to AJAX framework.
parent a50d47f9
......@@ -213,11 +213,66 @@
* functions.
*/
function ajax_render($commands = array()) {
// Automatically extract any 'settings' added via drupal_add_js() and make
// them the first command.
$scripts = drupal_add_js(NULL, NULL);
// AJAX responses aren't rendered with html.tpl.php, so we have to call
// drupal_get_css() and drupal_get_js() here, in order to have new files added
// during this request to be loaded by the page. We only want to send back
// files that the page hasn't already loaded, so we implement simple diffing
// logic using array_diff_key().
foreach (array('css', 'js') as $type) {
// It is highly suspicious if $_POST['ajax_page_state'][$type] is empty,
// since the base page ought to have at least one JS file and one CSS file
// loaded. It probably indicates an error, and rather than making the page
// reload all of the files, instead we return no new files.
if (empty($_POST['ajax_page_state'][$type])) {
$items[$type] = array();
}
else {
$function = 'drupal_add_' . $type;
$items[$type] = $function();
drupal_alter($type, $items[$type]);
// @todo Inline CSS and JS items are indexed numerically. These can't be
// reliably diffed with array_diff_key(), since the number can change
// due to factors unrelated to the inline content, so for now, we strip
// the inline items from AJAX responses, and can add support for them
// when drupal_add_css() and drupal_add_js() are changed to using md5()
// or some other hash of the inline content.
foreach ($items[$type] as $key => $item) {
if (is_numeric($key)) {
unset($items[$type][$key]);
}
}
// Ensure that the page doesn't reload what it already has.
$items[$type] = array_diff_key($items[$type], $_POST['ajax_page_state'][$type]);
}
}
// Render the HTML to load these files, and add AJAX commands to insert this
// HTML in the page. We pass TRUE as the $skip_alter argument to prevent the
// data from being altered again, as we already altered it above.
$styles = drupal_get_css($items['css'], TRUE);
$scripts_footer = drupal_get_js('footer', $items['js'], TRUE);
$scripts_header = drupal_get_js('header', $items['js'], TRUE);
$extra_commands = array();
if (!empty($styles)) {
$extra_commands[] = ajax_command_prepend('head', $styles);
}
if (!empty($scripts_header)) {
$extra_commands[] = ajax_command_prepend('head', $scripts_header);
}
if (!empty($scripts_footer)) {
$extra_commands[] = ajax_command_append('body', $scripts_footer);
}
if (!empty($extra_commands)) {
$commands = array_merge($extra_commands, $commands);
}
$scripts = drupal_add_js();
if (!empty($scripts['settings'])) {
array_unshift($commands, ajax_command_settings(call_user_func_array('array_merge_recursive', $scripts['settings']['data'])));
$settings = $scripts['settings'];
// Automatically extract any settings added via drupal_add_js() and make
// them the first command.
array_unshift($commands, ajax_command_settings(call_user_func_array('array_merge_recursive', $settings['data']), TRUE));
}
// Allow modules to alter any AJAX response.
......@@ -306,6 +361,39 @@ function ajax_form_callback() {
}
}
/**
* Theme callback for AJAX requests.
*
* Many different pages can invoke an AJAX request to system/ajax or another
* generic AJAX path. It is almost always desired for an AJAX response to be
* rendered using the same theme as the base page, because most themes are built
* with the assumption that they control the entire page, so if the CSS for two
* themes are both loaded for a given page, they may conflict with each other.
* For example, Bartik is Drupal's default theme, and Seven is Drupal's default
* administration theme. Depending on whether the "Use the administration theme
* when editing or creating content" checkbox is checked, the node edit form may
* be displayed in either theme, but the AJAX response to the Field module's
* "Add another item" button should be rendered using the same theme as the rest
* of the page. Therefore, system_menu() sets the 'theme callback' for
* 'system/ajax' to this function, and it is recommended that modules
* implementing other generic AJAX paths do the same.
*/
function ajax_base_page_theme() {
if (!empty($_POST['ajax_page_state']['theme']) && !empty($_POST['ajax_page_state']['theme_token'])) {
$theme = $_POST['ajax_page_state']['theme'];
$token = $_POST['ajax_page_state']['theme_token'];
// Prevent a request forgery from giving a person access to a theme they
// shouldn't be otherwise allowed to see. However, since everyone is allowed
// to see the default theme, token validation isn't required for that, and
// bypassing it allows most use-cases to work even when accessed from the
// page cache.
if ($theme === variable_get('theme_default', 'bartik') || drupal_valid_token($token, $theme)) {
return $theme;
}
}
}
/**
* Package and send the result of a page callback to the browser as an AJAX response.
*
......
......@@ -2822,16 +2822,22 @@ function drupal_add_css($data = NULL, $options = NULL) {
* @param $css
* (optional) An array of CSS files. If no array is provided, the default
* stylesheets array is used instead.
* @param $skip_alter
* (optional) If set to TRUE, this function skips calling drupal_alter() on
* $css, useful when the calling function passes a $css array that has already
* been altered.
* @return
* A string of XHTML CSS tags.
*/
function drupal_get_css($css = NULL) {
function drupal_get_css($css = NULL, $skip_alter = FALSE) {
if (!isset($css)) {
$css = drupal_add_css();
}
// Allow modules and themes to alter the CSS items.
drupal_alter('css', $css);
if (!$skip_alter) {
drupal_alter('css', $css);
}
// Sort CSS items according to their weights.
uasort($css, 'drupal_sort_weight');
......@@ -2855,6 +2861,12 @@ function drupal_get_css($css = NULL) {
'#type' => 'styles',
'#items' => $css,
);
// Provide the page with information about the individual CSS files used,
// information not otherwise available when CSS aggregation is enabled.
$setting['ajaxPageState']['css'] = array_fill_keys(array_keys($css), 1);
$styles['#attached']['js'][] = array('type' => 'setting', 'data' => $setting);
return drupal_render($styles);
}
......@@ -3811,13 +3823,17 @@ function drupal_js_defaults($data = NULL) {
* @param $javascript
* (optional) An array with all JavaScript code. Defaults to the default
* JavaScript array for the given scope.
* @param $skip_alter
* (optional) If set to TRUE, this function skips calling drupal_alter() on
* $javascript, useful when the calling function passes a $javascript array
* that has already been altered.
* @return
* All JavaScript code segments and includes for the scope as HTML tags.
* @see drupal_add_js()
* @see locale_js_alter()
* @see drupal_js_defaults()
*/
function drupal_get_js($scope = 'header', $javascript = NULL) {
function drupal_get_js($scope = 'header', $javascript = NULL, $skip_alter = FALSE) {
if (!isset($javascript)) {
$javascript = drupal_add_js();
}
......@@ -3826,13 +3842,15 @@ function drupal_get_js($scope = 'header', $javascript = NULL) {
}
// Allow modules to alter the JavaScript.
drupal_alter('js', $javascript);
if (!$skip_alter) {
drupal_alter('js', $javascript);
}
// Filter out elements of the given scope.
$items = array();
foreach ($javascript as $item) {
foreach ($javascript as $key => $item) {
if ($item['scope'] == $scope) {
$items[] = $item;
$items[$key] = $item;
}
}
......@@ -3865,6 +3883,22 @@ function drupal_get_js($scope = 'header', $javascript = NULL) {
// Sort the JavaScript by weight so that it appears in the correct order.
uasort($items, 'drupal_sort_weight');
// Provide the page with information about the individual JavaScript files
// used, information not otherwise available when aggregation is enabled.
$setting['ajaxPageState']['js'] = array_fill_keys(array_keys($items), 1);
unset($setting['ajaxPageState']['js']['settings']);
drupal_add_js($setting, 'setting');
// If we're outputting the header scope, then this might be the final time
// that drupal_get_js() is running, so add the setting to this output as well
// as to the drupal_add_js() cache. If $items['settings'] doesn't exist, it's
// because drupal_get_js() was intentionally passed a $javascript argument
// stripped off settings, potentially in order to override how settings get
// output, so in this case, do not add the setting to this output.
if ($scope == 'header' && isset($items['settings'])) {
$items['settings']['data'][] = $setting;
}
// Loop through the JavaScript to construct the rendered output.
$element = array(
'#tag' => 'script',
......
......@@ -102,6 +102,15 @@ function drupal_theme_initialize() {
// Themes can have alter functions, so reset the drupal_alter() cache.
drupal_static_reset('drupal_alter');
// Provide the page with information about the theme that's used, so that a
// later AJAX request can be rendered using the same theme.
// @see ajax_base_page_theme()
$setting['ajaxPageState'] = array(
'theme' => $theme_key,
'themeToken' => drupal_get_token($theme_key),
);
drupal_add_js($setting, 'setting');
}
/**
......
......@@ -231,6 +231,17 @@ Drupal.ajax.prototype.beforeSubmit = function (form_values, element, options) {
form_values.push({ name: 'ajax_html_ids[]', value: this.id });
});
// Allow Drupal to return new JavaScript and CSS files to load without
// returning the ones already loaded.
form_values.push({ name: 'ajax_page_state[theme]', value: Drupal.settings.ajaxPageState.theme });
form_values.push({ name: 'ajax_page_state[theme_token]', value: Drupal.settings.ajaxPageState.themeToken });
for (var key in Drupal.settings.ajaxPageState.css) {
form_values.push({ name: 'ajax_page_state[css][' + key + ']', value: 1 });
}
for (var key in Drupal.settings.ajaxPageState.js) {
form_values.push({ name: 'ajax_page_state[js][' + key + ']', value: 1 });
}
// Insert progressbar or throbber.
if (this.progress.type == 'bar') {
var progressBar = new Drupal.progressBar('ajax-progress-' + this.element.id, eval(this.progress.update_callback), this.progress.method, eval(this.progress.error_callback));
......
......@@ -41,12 +41,14 @@ function file_menu() {
'page callback' => 'file_ajax_upload',
'delivery callback' => 'ajax_deliver',
'access arguments' => array('access content'),
'theme callback' => 'ajax_base_page_theme',
'type' => MENU_CALLBACK,
);
$items['file/progress'] = array(
'page callback' => 'file_ajax_progress',
'delivery callback' => 'ajax_deliver',
'access arguments' => array('access content'),
'theme callback' => 'ajax_base_page_theme',
'type' => MENU_CALLBACK,
);
......
This diff is collapsed.
......@@ -217,7 +217,7 @@ function ajax_forms_test_ajax_commands_form($form, &$form_state) {
'#suffix' => '<div id="remove_div"><div id="remove_text">text to be removed</div></div>',
);
// Show off the AJAX 'restripe' command.
// Shows the AJAX 'restripe' command.
$form['restripe_command_example'] = array(
'#type' => 'submit',
'#value' => t("AJAX 'restripe' command"),
......@@ -230,8 +230,17 @@ function ajax_forms_test_ajax_commands_form($form, &$form_state) {
<tr ><td>second row</td></tr>
</table>
</div>',
);
// Demonstrates the AJAX 'settings' command. The 'settings' command has
// nothing visual to "show", but it can be tested via SimpleTest and via
// Firebug.
$form['settings_command_example'] = array(
'#type' => 'submit',
'#value' => t("AJAX 'settings' command"),
'#ajax' => array(
'callback' => 'ajax_forms_test_advanced_commands_settings_callback',
),
);
$form['submit'] = array(
......@@ -367,7 +376,15 @@ function ajax_forms_test_advanced_commands_restripe_callback($form, $form_state)
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* AJAX callback for 'settings'.
*/
function ajax_forms_test_advanced_commands_settings_callback($form, $form_state) {
$commands = array();
$setting['ajax_forms_test']['foo'] = 42;
$commands[] = ajax_command_settings($setting);
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* This form and its related submit and callback functions demonstrate
......
......@@ -28,21 +28,15 @@ function ajax_test_menu() {
}
/**
* Menu callback; Returns $_GET['commands'] suitable for use by ajax_deliver().
* Menu callback; Return an element suitable for use by ajax_deliver().
*
* Additionally ensures that ajax_render() incorporates JavaScript settings
* by invoking drupal_add_js() with a dummy setting.
* generated during the page request by invoking drupal_add_js() with a dummy
* setting.
*/
function ajax_test_render() {
// Prepare AJAX commands.
$commands = array();
if (!empty($_GET['commands'])) {
$commands = $_GET['commands'];
}
// Add a dummy JS setting.
drupal_add_js(array('ajax' => 'test'), 'setting');
return array('#type' => 'ajax', '#commands' => $commands);
return array('#type' => 'ajax', '#commands' => array());
}
/**
......
......@@ -514,6 +514,7 @@ function system_menu() {
'page callback' => 'ajax_form_callback',
'delivery callback' => 'ajax_deliver',
'access callback' => TRUE,
'theme callback' => 'ajax_base_page_theme',
'type' => MENU_CALLBACK,
'file path' => 'includes',
'file' => 'form.inc',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment