Index: includes/form.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/form.inc,v retrieving revision 1.327 diff -u -r1.327 form.inc --- includes/form.inc 11 Apr 2009 22:19:44 -0000 1.327 +++ includes/form.inc 21 Apr 2009 04:30:19 -0000 @@ -2020,7 +2020,7 @@ // Adding the same javascript settings twice will cause a recursion error, // we avoid the problem by checking if the javascript has already been added. if ((isset($element['#ahah']['callback']) || isset($element['#ahah']['path'])) && isset($element['#ahah']['event']) && !isset($js_added[$element['#id']])) { - drupal_add_js('misc/jquery.form.js', array('weight' => JS_LIBRARY)); + drupal_add_plugin('form'); drupal_add_js('misc/ahah.js'); $ahah_binding = array( Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.876 diff -u -r1.876 common.inc --- includes/common.inc 15 Apr 2009 23:49:06 -0000 1.876 +++ includes/common.inc 21 Apr 2009 04:30:19 -0000 @@ -2525,6 +2525,88 @@ } /** + * Adds multiple JavaScript files and CSS files for a JavaScript plugin. + * + * @param $plugin + * The name of the plugin to add. + * @param ... + * Additional arguments to pass to the call to hook_js_plugin_add(). + * @return + * The results from invoking hook_js_plugin_add(). + * @see hook_js_plugin() + * @see hook_js_plugin_add() + * @see hook_js_plugin_alter() + */ +function drupal_add_plugin($plugin) { + $plugin = drupal_get_plugin($plugin); + if (!isset($plugin)) { + return NULL; + } + + // Add all the JavaScript and stylesheets associated with the plugin. + foreach (array('js', 'css') as $type) { + if (isset($plugin[$type])) { + foreach ($plugin[$type] as $data => $options) { + // Allow the possibility of adding more options, otherwise, just + // interpret the item as a file. + if (!is_array($options)) { + $data = $options; + $options = array('weight' => JS_LIBRARY); + } + // Call drupal_add_js or drupal_add_css based on the $type. + call_user_func('drupal_add_' . $type, $data, $options); + } + } + } + + // Invoke hook_js_plugin_add() with extra parameters to allow any additional + // action. + $args = func_get_args(); + array_unshift($args, 'js_plugin_add'); + return call_user_func_array('module_invoke_all', $args); +} + +/** + * Retrieves information from the JavaScript plugin registry. + * + * @param $plugin + * If given, will provide the JavaScript plugin information for just the given + * plugin name. + * @return + * Returns all available JavaScript plugins, or the requested one in $plugin. + * Otherwise, returns NULL. + * @see drupal_add_plugin() + */ +function drupal_get_plugin($plugin = NULL) { + $plugins = &drupal_static(__FUNCTION__); + + if (!isset($plugins)) { + if ($cache = cache_get('js_plugin')) { + $plugins = $cache->data; + } + else { + // Invoke hook_js_plugin() to create the plugin registry. + $plugins = module_invoke_all('js_plugin'); + + // Invoke hook_js_plugin_alter() to allow modification of the + // registry. This is helpful to allow modules to update any plugins + // to later versions. + drupal_alter('js_plugin', $plugins); + + // Save the registry to the cache. + cache_set('js_plugin', $plugins); + } + } + + if (isset($plugin)) { + return isset($plugins[$plugin]) ? $plugins[$plugin] : NULL; + } + else { + return $plugins; + } +} + +/** * Constructs an array of the defaults that are used for JavaScript items. * * @param $data Index: modules/color/color.module =================================================================== RCS file: /cvs/drupal/drupal/modules/color/color.module,v retrieving revision 1.57 diff -u -r1.57 color.module --- modules/color/color.module 20 Apr 2009 20:02:31 -0000 1.57 +++ modules/color/color.module 21 Apr 2009 04:30:19 -0000 @@ -165,19 +165,10 @@ * Form callback. Returns the configuration form. */ function color_scheme_form(&$form_state, $theme) { - $base = drupal_get_path('module', 'color'); $info = color_get_info($theme); // Add Farbtastic color picker. - drupal_add_css('misc/farbtastic/farbtastic.css', array('preprocess' => FALSE)); - drupal_add_js('misc/farbtastic/farbtastic.js', array('weight' => JS_LIBRARY)); - - // Add custom CSS and JS. - drupal_add_css($base . '/color.css', array('preprocess' => FALSE)); - drupal_add_js($base . '/color.js'); - drupal_add_js(array('color' => array( - 'reference' => color_get_palette($theme, TRUE) - )), 'setting'); + drupal_add_plugin('farbtastic', $theme); // See if we're using a predefined scheme. $current = implode(',', variable_get('color_' . $theme . '_palette', array())); Index: modules/color/color.info =================================================================== RCS file: /cvs/drupal/drupal/modules/color/color.info,v retrieving revision 1.9 diff -u -r1.9 color.info --- modules/color/color.info 11 Oct 2008 02:32:40 -0000 1.9 +++ modules/color/color.info 21 Apr 2009 04:30:19 -0000 @@ -7,3 +7,4 @@ core = 7.x files[] = color.module files[] = color.install +files[] = color.inc Index: modules/simpletest/tests/common.test =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/tests/common.test,v retrieving revision 1.32 diff -u -r1.32 common.test --- modules/simpletest/tests/common.test 31 Mar 2009 01:49:53 -0000 1.32 +++ modules/simpletest/tests/common.test 21 Apr 2009 04:30:19 -0000 @@ -524,6 +524,25 @@ } /** + * Checks to make sure that JavaScript plugins registry is built correctly. + */ + function testPluginRegistry() { + $plugins = drupal_get_plugin(); + $this->assertTrue(isset($plugins['farbtastic']) && isset($plugins['form']), t('The JavaScript plugin registry is built correctly.')); + } + + /** + * Adds a JavaScript plugin to the page and tests for both it and its CSS. + */ + function testRenderPlugin() { + drupal_add_plugin('farbtastic'); + $javascript = drupal_get_js(); + $stylesheets = drupal_get_css(); + $this->assertTrue(strpos($javascript, 'misc/farbtastic/farbtastic.js') > 0, t('JavaScript plugins are rendered to the page.')); + $this->assertTrue(strpos($stylesheets, 'misc/farbtastic/farbtastic.css') > 0, t('JavaScript plugins render their CSS to the page.')); + } + + /** * Test adding a JavaScript file with a different weight. */ function testDifferentWeight() { Index: modules/system/system.info =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.info,v retrieving revision 1.11 diff -u -r1.11 system.info --- modules/system/system.info 12 Oct 2008 01:23:06 -0000 1.11 +++ modules/system/system.info 21 Apr 2009 04:30:19 -0000 @@ -8,4 +8,5 @@ files[] = system.admin.inc files[] = image.gd.inc files[] = system.install +files[] = system.inc required = TRUE Index: modules/system/system.api.php =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.api.php,v retrieving revision 1.26 diff -u -r1.26 system.api.php --- modules/system/system.api.php 20 Apr 2009 07:42:22 -0000 1.26 +++ modules/system/system.api.php 21 Apr 2009 04:30:19 -0000 @@ -237,6 +237,81 @@ } /** + * Registers any JavaScript plugins associated with the module. + * + * @return + * An array defining any JavaScript plugins associated with the module. + * @see system_js_plugin() + */ +function hook_js_plugin() { + // jCarousel. + $plugins['jcarousel'] = array( + 'title' => 'jCarousel', + 'project_page' => 'http://sorgalla.com/jcarousel/', + 'version' => 0.23, + 'download' => 'http://sorgalla.com/projects/download.php?jcarousel', + 'js' => array( + // Items that are just strings are interpreted as stright files. + drupal_get_path('module', 'jcarousel') . '/jquery.jcarousel.js', + ), + 'css' => array( + // By providing an array, it becomes the $options parameter during the + // drupal_add_js/drupal_add_css call. + drupal_get_path('module', 'jcarousel') . '/jquery.jcarousel.css' => array( + 'type' => 'file', + 'preprocess' => FALSE, + 'media' => 'screen', + ), + ), + ); + return $plugins; +} + +/** + * Alters the JavaScript plugin registry. + * + * @param $plugins + * The JavaScript plugin registry. + * @return + * An array with the updated JavaScript plugin registry. + * @see hook_js_plugin() + */ +function hook_js_plugin_alter(&$plugins) { + // Update the jCarousel plugin to version 1.0. + if (isset($plugins['jcarousel'])) { + if ($plugins['jcarousel']['version'] < 1.0) { + $plugins['jcarousel']['version'] = 1.0; + $plugins['jcarousel']['js'] = drupal_get_path('module', 'jcarousel_update') . '/jquery.jcarousel.js'; + $plugins['jcarousel']['css'] = drupal_get_path('module', 'jcarousel_update') . '/jquery.jcarousel.css'; + } + } + return $plugins; +} + +/** + * A JavaScript plugin is being added to the page. + * + * This is helpful when special actions are to be taken when the plugin is + * added. Some plugins, for example, require special CSS files, or settings + * to be added to the page. + * + * @param $plugin + * The JavaScript plugin being added. + * @param ... + * Any additional arguments passed in when calling drupal_add_plugin(). + * @see drupal_add_plugin() + */ +function hook_js_plugin_add($plugin) { + if ($plugin == 'jcarousel') { + // Add our JavaScript which registers the Drupal behaviors. + drupal_add_js(drupal_get_path('module', 'jcarousel') . '/jcarousel.js'); + + // Add the settings so that the Drupal behaviors act on them. + drupal_add_js($options['args'], 'setting'); + } +} + +/** * Perform alterations before a form is rendered. * * One popular use of this hook is to add form elements to the node form. When Index: modules/system/system.install =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.install,v retrieving revision 1.316 diff -u -r1.316 system.install --- modules/system/system.install 20 Apr 2009 02:23:16 -0000 1.316 +++ modules/system/system.install 21 Apr 2009 04:30:19 -0000 @@ -283,6 +283,40 @@ } } + // Make sure that all files from the JavaScript plugins are available. + if ($phase == 'runtime') { + $plugins = drupal_get_plugin(); + foreach ($plugins as $name => $plugin) { + foreach (array('js', 'css') as $type) { + if (isset($plugin[$type])) { + foreach ($plugin[$type] as $data => $options) { + // Construct the data and options properties properly. + if (!is_array($options)) { + $data = $options; + $options = array(); + } + $options += array('type' => 'file', 'data' => $data); + if ($options['type'] == 'file' && !file_exists($options['data'])) { + $requirements['plugin ' . $name] = array( + 'title' => $plugin['title'], + 'value' => $t('Files missing'), + 'severity' => REQUIREMENT_ERROR, + 'description' => $t('Files from !title were not found. Download version %version and install %file.', array( + '@project_page' => $plugin['project_page'], + '!title' => $plugin['title'], + '@download' => $plugin['download'], + '%version' => $plugin['version'], + '%file' => $options['data'], + )), + ); + break; + } + } + } + } + } + } + return $requirements; } Index: modules/system/system.inc =================================================================== RCS file: modules/system/system.inc diff -N modules/system/system.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/system/system.inc 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,34 @@ + 'jQuery Form Plugin', + 'project_page' => 'http://malsup.com/jquery/form/', + 'version' => 2.16, + 'download' => 'http://github.com/malsup/form/tarball/e52ed46bff664431a51d9562803acbcfbbd2e3b4', + 'js' => array( + 'misc/jquery.form.js', + ), + ); + + // Farbtastic. + $plugins['farbtastic'] = array( + 'title' => 'Farbtastic', + 'project_page' => 'http://acko.net/dev/farbtastic', + 'version' => 1.2, + 'download' => 'http://acko.net/files/farbtastic_/farbtastic12.zip', + 'js' => array( + 'misc/farbtastic/farbtastic.js', + ), + 'css' => array( + 'misc/farbtastic/farbtastic.css', + ), + ); + + return $plugins; +} Index: modules/color/color.inc =================================================================== RCS file: modules/color/color.inc diff -N modules/color/color.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/color/color.inc 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,31 @@ + array( + 'reference' => color_get_palette($theme, TRUE), + ), + ); + drupal_add_js($settings, 'setting'); + } + } +}