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 1 Oct 2008 02:19:32 -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 drupal_get_jq($plugin = NULL, $cached = TRUE, $display_errors = FALSE, $log_errors = TRUE) { + static $plugins; + if (!isset($plugins) || !$cached) { + if ($cached && $cache = cache_get('drupal_get_jq')) { + $plugins = $cache->data; + } + else { + include_once(DRUPAL_ROOT .'/includes/jq.cache.inc'); + $plugins = _drupal_get_jq($display_errors); + cache_set('drupal_get_jq', $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/jquery.cache.inc =================================================================== RCS file: includes/jquery.cache.inc diff -N includes/jquery.cache.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ includes/jquery.cache.inc 1 Oct 2008 02:19:32 -0000 @@ -0,0 +1,63 @@ + // ... 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 _drupal_get_jq() { + $plugins = array(); + + // Scan the /plugins and /sites/example.com/plugins directories for .js and .css files. + // If there are multiple plugins with the same filename in the directory, the LAST file read will be used. + $files = drupal_system_listing('(\.js$|\.css$)', 'plugins', 'basename', 0); + foreach ($files as $file) { + $plugins[$file->name]['name'] = $file->name; + $plugins[$file->name]['plugin'] = $file->name; + if (substr($file->filename, strripos($file->filename, '.') + 1) == 'js') { + $plugins[$file->name]['files']['js'][] = $file->filename; + } + else { + $plugins[$file->name]['files']['css'][] = $file->filename; + } + } + + // 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) { + // On conflicting plugin names, use the latest version. + if (isset($plugins[$name]) && isset($plugins[$name]['version'])) { + if (!isset($details['version']) || !is_numeric($details['version']) || $details['version'] < $plugins[$name]['version']) { + // Attempting to add an older plugin version. + continue; + } + } + $plugins[$key] = $plugin; + $plugins[$key]['plugin'] = $key; + $plugins[$key]['module'] = $module; + } + } + } + ksort($plugins); + return $plugins; +} + Index: includes/jquery.inc =================================================================== RCS file: includes/jquery.inc diff -N includes/jquery.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ includes/jquery.inc 1 Oct 2008 02:19:32 -0000 @@ -0,0 +1,56 @@ + $plugin)); + watchdog('jq', $error, WATCHDOG_NOTICE); + $invoked_plugins[$plugin] = FALSE; + } + else { + if (isset($jq['dependencies']) && is_array($jq['dependencies'])) { + foreach ($jq['dependencies'] as $dependency) { + _drupal_add_jq($dependency); + } + } + if (isset($jq['files']['js']) && is_array($jq['files']['js'])) { + foreach ($jq['files']['js'] as $file) { + drupal_add_js($file); + } + } + if (isset($jq['files']['css']) && 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_exists($jq['module'])) { + module_load_include('jq.inc', $jq['module']); + $args = array('add', $plugin); + $args = array_merge($args, (array)$extra); + $function = $jq['module'] .'_jq'; + if (function_exists($function)) { + call_user_func_array($function, $args); + } + } + return isset($invoked_plugins[$plugin]) ? $invoked_plugins[$plugin] : NULL; +} +