Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.552 diff -u -d -F^function -r1.552 common.inc --- includes/common.inc 7 Aug 2006 15:04:13 -0000 1.552 +++ includes/common.inc 7 Aug 2006 19:18:15 -0000 @@ -1251,44 +1251,126 @@ function drupal_get_css($css = NULL) { } /** - * Add a JavaScript file to the output. + * Add a JavaScript file, function call, setting or code to the page. * - * The first time this function is invoked per page request, - * it adds "misc/drupal.js" to the output. Other scripts - * depends on the 'killswitch' inside it. + * The behavior of this function depends on the parameters it is called with. + * Generally, it handles the addition of JavaScript to the page, either as + * reference to an existing file or as inline code. The following actions can be + * performed using this function: + * + * - Add a file ('core', 'module' and 'theme'): + * Adds a reference to a JavaScript file to the page header. JavaScript files + * are placed in a certain order from 'core' first, to 'module' and finally + * 'theme' so that files that are later added, can override previously added + * files with ease. + * + * - Add inline JavaScript code ('page'): + * Executes a piece of JavaScript code on the current page by placing the code + * directly in the head of the page. This can, for example, be useful to tell + * the user that a new message arrived using a pop up, alert box or similar + * method. + * + * - Call a JavaScript function ('call'): + * Generates a Javascript call, while importing the arguments as is. PHP + * arrays are turned into JS objects to preserve keys. This means the array + * keys must conform to JS's member naming rules. + * + * - Add settings ('setting'): + * Adds a setting to Drupal's global storage of JavaScript settings. Per-page + * settings are required by some modules to function properly. The settings + * will be accessible at Drupal.settings[$flag]. + * + * @param $data + * (optional) If given, the value depends on the $type parameter: + * - 'core', 'module' or 'theme': Path to the file relative to base_path(). + * - 'page': The JS code that should be placed directly in the page head. + * - 'setting': An array with configuration options as associative array. + * - 'call': The name of the function that should be called on page load. + * @param $type + * (optional) The type of JavaScript that should be added to the page. Allowed + * values are 'core', 'module', 'theme', 'page', 'setting' and 'call'. + * @param $flag + * (optional) The value of this parameter depends on the $type parameter: + * - 'core', 'module', 'theme' and any undefined value: Sets whether the file + * should be cached (TRUE, default) or loaded anew on every page (FALSE). + * - 'setting': The name of the module that adds settings to the page. + * - 'page' and 'call': The value should be omitted. + * @param ... + * (optional) If the parameter $type is set to 'call', every further parameter + * after $type is passed as an argument to the function called in the page + * header. + * @return + * The JavaScript array that has been built so far. */ -function drupal_add_js($file, $nocache = FALSE) { - static $sent = array(); +function drupal_add_js($data = NULL, $type = 'module', $flag = TRUE) { + static $javascript = array( + 'core' => array(), 'module' => array(), 'theme' => array(), + 'page' => array(), 'setting' => array(), 'call' => array(), + ); - $postfix = $nocache ? '?'. time() : ''; - if (!isset($sent['misc/drupal.js'])) { - drupal_set_html_head(''); - $sent['misc/drupal.js'] = TRUE; - } - if (!isset($sent[$file])) { - drupal_set_html_head(''); - $sent[$file] = TRUE; + if (!is_null($data)) { + switch ($type) { + case 'page': + $javascript[$type][] = $data; + break; + case 'setting': + $javascript[$type][$flag] = array_merge_recursive((array)$javascript[$type][$flag], (array)$data); + break; + case 'call': + $javascript[$type][] = array('name' => $data, 'arguments' => array_slice(func_get_args(), 2)); + break; + default: + $javascript[$type][$data] = $flag; + } + + if (!isset($javascript['core']['misc/drupal.js'])) { + $javascript['core']['misc/drupal.js'] = TRUE; + } } + + return $javascript; } /** - * Generates a Javascript call, while importing the arguments as is. - * PHP arrays are turned into JS objects to preserve keys. This means the array - * keys must conform to JS's member naming rules. + * Returns a themed presentation of all JavaScript code for the current page. + * References to JavaScript files are placed in a certain order: first, all + * 'core' files, then all 'module' and finally all 'theme' JavaScript files + * are added to the page. Then, all settings are output, followed by in-'page' + * JavaScript code. All 'calls' to JavaScript functions are appended at the end. * - * @param $function - * The name of the function to call. - * @param $arguments - * An array of arguments. + * @parameter $javascript + * (optional) An array with all JavaScript code. Defaults to the default + * JavaScript array. + * @return + * All JavaScript code segments and includes for the page as HTML tags. */ -function drupal_call_js($function) { - $arguments = func_get_args(); - array_shift($arguments); - $args = array(); - foreach ($arguments as $arg) { - $args[] = drupal_to_js($arg); +function drupal_get_js($javascript = NULL) { + $output = ''; + if (is_null($javascript)) { + $javascript = drupal_add_js(); } - $output = ''; + + foreach ($javascript as $type => $data) { + if ($data) { + switch ($type) { + case 'call': + foreach ($data as $key => $function) { + $data[$key] = $function['name'] .'('. implode(', ', array_map('drupal_to_js', $function['arguments'])) .');'; + } // fall through + case 'page': + $output .= '\n"; + break; + case 'setting': + $output .= '\n"; + break; + default: + foreach ($data as $path => $cache) { + $output .= '\n"; + } + } + } + } + return $output; } Index: includes/theme.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/theme.inc,v retrieving revision 1.306 diff -u -d -F^function -r1.306 theme.inc --- includes/theme.inc 5 Aug 2006 22:17:31 -0000 1.306 +++ includes/theme.inc 7 Aug 2006 19:18:17 -0000 @@ -366,6 +366,7 @@ function theme_page($content) { $output .= ' '. (drupal_get_title() ? strip_tags(drupal_get_title()) : variable_get('site_name', 'drupal')) .''; $output .= drupal_get_html_head(); $output .= drupal_get_css(); + $output .= drupal_get_js(); $output .= ' '; $output .= ' '; @@ -407,6 +408,7 @@ function theme_maintenance_page($content $output .= ' '. strip_tags(drupal_get_title()) .''; $output .= drupal_get_html_head(); $output .= drupal_get_css(); + $output .= drupal_get_js(); $output .= ''; $output .= ''; $output .= '

' . drupal_get_title() . '

'; @@ -436,6 +438,7 @@ function theme_install_page($content) { $output .= ' '. strip_tags(drupal_get_title()) .''; $output .= drupal_get_html_head(); $output .= drupal_get_css(); + $output .= drupal_get_js(); $output .= ''; $output .= ''; $output .= '

' . drupal_get_title() . '

'; Index: themes/bluemarine/page.tpl.php =================================================================== RCS file: /cvs/drupal/drupal/themes/bluemarine/page.tpl.php,v retrieving revision 1.17 diff -u -d -F^function -r1.17 page.tpl.php --- themes/bluemarine/page.tpl.php 1 Feb 2006 16:04:02 -0000 1.17 +++ themes/bluemarine/page.tpl.php 7 Aug 2006 19:18:18 -0000 @@ -5,6 +5,7 @@ <?php print $head_title ?> + Index: themes/chameleon/chameleon.theme =================================================================== RCS file: /cvs/drupal/drupal/themes/chameleon/chameleon.theme,v retrieving revision 1.46 diff -u -d -F^function -r1.46 chameleon.theme --- themes/chameleon/chameleon.theme 3 Aug 2006 07:06:36 -0000 1.46 +++ themes/chameleon/chameleon.theme 7 Aug 2006 19:18:18 -0000 @@ -38,6 +38,7 @@ function chameleon_page($content) { $output .= " ". ($title ? strip_tags($title) ." | ". variable_get("site_name", "drupal") : variable_get("site_name", "drupal") ." | ". variable_get("site_slogan", "")) ."\n"; $output .= drupal_get_html_head(); $output .= drupal_get_css(); + $output .= drupal_get_js(); $output .= ""; $output .= "\n"; $output .= "
"; Index: themes/engines/phptemplate/phptemplate.engine =================================================================== RCS file: /cvs/drupal/drupal/themes/engines/phptemplate/phptemplate.engine,v retrieving revision 1.39 diff -u -d -F^function -r1.39 phptemplate.engine --- themes/engines/phptemplate/phptemplate.engine 3 Aug 2006 07:06:36 -0000 1.39 +++ themes/engines/phptemplate/phptemplate.engine 7 Aug 2006 19:18:19 -0000 @@ -212,6 +212,7 @@ function phptemplate_page($content) { 'site_slogan' => (theme_get_setting('toggle_slogan') ? variable_get('site_slogan', '') : ''), 'css' => drupal_add_css(), 'styles' => drupal_get_css(), + 'scripts' => drupal_get_js(), 'tabs' => theme('menu_local_tasks'), 'title' => drupal_get_title() ); Index: themes/pushbutton/page.tpl.php =================================================================== RCS file: /cvs/drupal/drupal/themes/pushbutton/page.tpl.php,v retrieving revision 1.13 diff -u -d -F^function -r1.13 page.tpl.php --- themes/pushbutton/page.tpl.php 1 Feb 2006 16:04:03 -0000 1.13 +++ themes/pushbutton/page.tpl.php 7 Aug 2006 19:18:20 -0000 @@ -5,6 +5,7 @@ +