Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.799 diff -u -p -r1.799 common.inc --- includes/common.inc 20 Sep 2008 20:22:23 -0000 1.799 +++ includes/common.inc 30 Sep 2008 22:57:25 -0000 @@ -648,7 +648,7 @@ function _drupal_get_last_caller($backtr // The first trace is the call itself. // It gives us the line and the file of the last call. $call = $backtrace[0]; - + // The second call give us the function where the call originated. if (isset($backtrace[1])) { if (isset($backtrace[1]['class'])) { @@ -2340,6 +2340,41 @@ function drupal_to_js($var) { } /** + * Returns all module defined plugins that are registered using hook_jq. + * This is cached, so a module is responsible for calling this function on installation. + * @TODO: When hook_modules_installed is patched, then call this function there. + */ +function jq_plugins($plugin = NULL, $cached = TRUE, $display_errors = FALSE, $log_errors = TRUE) { + static $plugins; + if (!isset($plugins) || !$cached) { + if ($cached && $cache = cache_get('jq_plugins')) { + $plugins = $cache->data; + } + else { + include_once(DRUPAL_ROOT .'/includes/jq.cache.inc'); + $plugins = _jq_plugins($display_errors); + cache_set('jq_plugins', $plugins); + } + } + if (isset($plugin)) { + return $plugins[$plugin]; + } + return $plugins; +} + +/** + * Adds any registered jQuery Plugins to the page. + * This will add a specific jquery plugin to a page, if it hasn't already been. + * Returns whether the plugin was successfully loaded or not. It will pass through any extra arguments. + */ +function drupal_add_jq($plugin) { + include_once(DRUPAL_ROOT .'/includes/jq.inc'); + $extra = func_get_args(); + array_shift($extra); + return _drupal_add_jq($plugin, $extra); +} + +/** * Return data in JSON format. * * This function should be used for JavaScript callback functions returning Index: includes/jq.cache.inc =================================================================== RCS file: includes/jq.cache.inc diff -N includes/jq.cache.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ includes/jq.cache.inc 30 Sep 2008 22:57:25 -0000 @@ -0,0 +1,48 @@ + // ... Name of the plugin. + * 'description' => // ... Description of the plugin. + * 'files' => array( + * 'js' => array( + * // ... An array of js files to be loaded on the page. + * ), + * 'css' => array( + * // ... An array of css files to be loaded on the page. + * ), + * ), + */ +function _jq_plugins() { + $plugins = array(); + // Any module may put its hook_jquery_plugins functions in module_name.jquery_plugins.inc, + // so that it may be included automatically. + module_load_all_includes('jquery_plugins.inc'); + foreach (module_implements('jquery_plugins') as $module) { + $mod_jq = module_invoke($module, 'jquery_plugins', 'info'); + if (is_array($mod_jq)) { + foreach ($mod_jq as $key => $plugin) { + if (isset($plugins[$key])) { + $error = 'There is a conflict with the %plugin jQuery plugin. It is defined by both the %module1 and %module2 modules.'; + $error_args = array('%plugin' => $key, '%module1' => $plugins[$key]['module'], '%module2' => $module); + watchdog('jq', $error, $error_args, WATCHDOG_WARNING); + } + else { + $plugins[$key] = $plugin; + $plugins[$key]['plugin'] = $key; + $plugins[$key]['module'] = $module; + } + } + } + } + ksort($plugins); + return $plugins; +} + Index: includes/jq.inc =================================================================== RCS file: includes/jq.inc diff -N includes/jq.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ includes/jq.inc 30 Sep 2008 22:57:25 -0000 @@ -0,0 +1,47 @@ + $plugin)); + watchdog('jq', $error, WATCHDOG_NOTICE); + $invoked_plugins[$plugin] = FALSE; + } + else { + if (is_array($jq['files']['js'])) { + foreach ($jq['files']['js'] as $file) { + drupal_add_js($file); + } + } + if (is_array($jq['files']['css'])) { + foreach ($jq['files']['css'] as $file) { + drupal_add_css($file); + } + } + $invoked_plugins[$plugin] = TRUE; + } + } + else { + // Log an error, but only if we haven't already. don't want to overwhelm with a lot of identical errors per page + $error = t('The %plugin jQuery plugin is not defined.', array('%plugin' => $plugin)); + watchdog('jq', $error, WATCHDOG_ERROR); + $invoked_plugins[$plugin] = FALSE; + } + } + if ($invoked_plugins[$plugin]) { + module_load_include('jq.inc', $jq['module']); + $args = array('add', $plugin); + $args = array_merge($args, (array)$extra); + $function = $jq['module'] .'_jq'; + call_user_func_array($function, $args); + } + return $invoked_plugins[$plugin]; +} +