summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
Diffstat (limited to 'includes')
-rw-r--r--includes/common.inc129
-rw-r--r--includes/form.inc2
2 files changed, 121 insertions, 10 deletions
diff --git a/includes/common.inc b/includes/common.inc
index 0167620c9..c9d68fd82 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -2779,15 +2779,6 @@ function drupal_add_js($data = NULL, $options = NULL) {
'scope' => 'header',
'weight' => JS_LIBRARY,
),
- 'misc/jquery.js' => array(
- 'data' => 'misc/jquery.js',
- 'type' => 'file',
- 'scope' => 'header',
- 'weight' => JS_LIBRARY - 2,
- 'cache' => TRUE,
- 'defer' => FALSE,
- 'preprocess' => TRUE,
- ),
'misc/drupal.js' => array(
'data' => 'misc/drupal.js',
'type' => 'file',
@@ -2798,6 +2789,8 @@ function drupal_add_js($data = NULL, $options = NULL) {
'preprocess' => TRUE,
),
);
+ // jQuery itself is registered as a library.
+ drupal_add_library('system', 'jquery');
}
switch ($options['type']) {
@@ -2954,6 +2947,124 @@ function drupal_get_js($scope = 'header', $javascript = NULL) {
}
/**
+ * Adds multiple JavaScript or CSS files at the same time.
+ *
+ * A library defines a set of JavaScript and/or CSS files, optionally using
+ * settings, and optionally requiring another library. For example, a library
+ * can be a jQuery plugin, a JavaScript framework, or a CSS framework. This
+ * function allows modules to load a library defined/shipped by itself or a
+ * depending module; without having to add all files of the library separately.
+ * Each library is only loaded once.
+ *
+ * @param $module
+ * The name of the module that registered the library.
+ * @param $name
+ * The name of the library to add.
+ * @return
+ * TRUE when the library was successfully added or FALSE if the library or one
+ * of its dependencies could not be added.
+ *
+ * @see drupal_get_library()
+ * @see hook_library()
+ * @see hook_library_alter()
+ */
+function drupal_add_library($module, $name) {
+ $added = &drupal_static(__FUNCTION__, array());
+
+ // Only process the library if it exists and it was not added already.
+ if (!isset($added[$module][$name]) && $library = drupal_get_library($module, $name)) {
+ // Prevent repeated/recursive processing.
+ $added[$module][$name] = TRUE;
+
+ // Ensure dependencies first.
+ foreach ($library['dependencies'] as $dependency) {
+ if (drupal_add_library($dependency[0], $dependency[1]) === FALSE) {
+ // If any dependent library could not be added, this library will break;
+ // stop here.
+ $added[$module][$name] = FALSE;
+ return FALSE;
+ }
+ }
+
+ // Add defined JavaScript.
+ foreach ($library['js'] as $data => $options) {
+ // For JS settings we need to transform $options['data'] into $data.
+ if (isset($options['type'], $options['data']) && $options['type'] == 'setting') {
+ $data = $options['data'];
+ unset($options['data']);
+ }
+ // If not specified, assign a default weight of JS_LIBRARY.
+ elseif (!isset($options['weight'])) {
+ $options['weight'] = JS_LIBRARY;
+ }
+ drupal_add_js($data, $options);
+ }
+
+ // Add defined stylesheets.
+ foreach ($library['css'] as $data => $options) {
+ drupal_add_css($data, $options);
+ }
+ }
+ // Requested library does not exist.
+ else {
+ $added[$module][$name] = FALSE;
+ }
+
+ return $added[$module][$name];
+}
+
+/**
+ * Retrieves information for a JavaScript/CSS library.
+ *
+ * Library information is statically cached. Libraries are keyed by module for
+ * several reasons:
+ * - Libraries are not unique. Multiple modules might ship with the same library
+ * in a different version or variant. This registry cannot (and does not
+ * attempt to) prevent library conflicts.
+ * - Modules implementing and thereby depending on a library that is registered
+ * by another module can only rely on that module's library.
+ * - Two (or more) modules can still register the same library and use it
+ * without conflicts in case the libraries are loaded on certain pages only.
+ *
+ * @param $module
+ * The name of a module that registered a library.
+ * @param $library
+ * The name of a registered library.
+ * @return
+ * The definition of the requested library, if existent, or FALSE.
+ *
+ * @see drupal_add_library()
+ * @see hook_library()
+ * @see hook_library_alter()
+ *
+ * @todo The purpose of drupal_get_*() is completely different to other page
+ * requisite API functions; find and use a different name.
+ */
+function drupal_get_library($module, $name) {
+ $libraries = &drupal_static(__FUNCTION__, array());
+
+ if (!array_key_exists($module, $libraries)) {
+ // Retrieve all libraries associated with the module.
+ $module_libraries = module_invoke($module, 'library');
+
+ // Allow modules to alter the module's registered libraries.
+ if (!empty($module_libraries)) {
+ drupal_alter('library', $module_libraries, $module);
+ }
+ $libraries[$module] = $module_libraries;
+ }
+ if (!empty($libraries[$module][$name]) && is_array($libraries[$module][$name])) {
+ // Add default elements to allow for easier processing.
+ $libraries[$module][$name] += array('dependencies' => array(), 'js' => array(), 'css' => array());
+ }
+ else {
+ $libraries[$module][$name] = FALSE;
+ }
+
+ return $libraries[$module][$name];
+}
+
+/**
* Assist in adding the tableDrag JavaScript behavior to a themed table.
*
* Draggable tables should be used wherever an outline or list of sortable items
diff --git a/includes/form.inc b/includes/form.inc
index 8df13ab5a..18837c6cc 100644
--- a/includes/form.inc
+++ b/includes/form.inc
@@ -2002,7 +2002,7 @@ function form_process_ahah($element) {
// 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_library('system', 'form');
drupal_add_js('misc/ahah.js');
$ahah_binding = array(