From 5a23b3fdb970bb0c3e79f47ba9296455d237d9f0 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Mon, 4 Oct 2010 17:46:01 +0000 Subject: - 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. --- includes/ajax.inc | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++--- includes/common.inc | 46 +++++++++++++++++++++---- includes/theme.inc | 9 +++++ 3 files changed, 141 insertions(+), 10 deletions(-) (limited to 'includes') diff --git a/includes/ajax.inc b/includes/ajax.inc index 2c987f035..bd590aa66 100644 --- a/includes/ajax.inc +++ b/includes/ajax.inc @@ -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. * diff --git a/includes/common.inc b/includes/common.inc index b99ceac97..0cddf5de0 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -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', diff --git a/includes/theme.inc b/includes/theme.inc index 9024b7d47..4ccbcbf2c 100644 --- a/includes/theme.inc +++ b/includes/theme.inc @@ -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'); } /** -- cgit v1.2.3