diff --git a/jquery_update.install b/jquery_update.install index a4e9168..2acf67b 100644 --- a/jquery_update.install +++ b/jquery_update.install @@ -30,7 +30,10 @@ function jquery_update_requirements($phase) { */ function jquery_update_uninstall() { variable_del('jquery_update_compression_type'); + variable_del('jquery_update_jquery_admin_version'); variable_del('jquery_update_jquery_version'); + variable_del('jquery_update_pages'); + variable_del('jquery_update_visibility'); } /** diff --git a/jquery_update.module b/jquery_update.module index 520e076..d4a0fcd 100644 --- a/jquery_update.module +++ b/jquery_update.module @@ -5,6 +5,11 @@ * Updates Drupal to use the latest version of jQuery. */ +// Mirror the block visibility rule options, with an extra option. +define('JQUERY_UPDATE_VISIBILITY_ADMIN', 0); +define('JQUERY_UPDATE_VISIBILITY_NOTLISTED', 1); +define('JQUERY_UPDATE_VISIBILITY_LISTED', 2); + /** * Implements hook_help(). */ @@ -64,20 +69,73 @@ function jquery_update_library() { * Implementation of hook_library_alter(). */ function jquery_update_library_alter(&$javascript, $module) { - // We are updating just the system module. For all other cases we return. if ($module != 'system') { return; } + // Replace jQuery with the latest version. + $version = variable_get('jquery_update_jquery_version', '1.5'); + + // The alternative jQuery library version. + $admin_version = variable_get('jquery_update_jquery_admin_version', ''); + + // Work out whether this page should use the alternative jQuery version. + if (!empty($admin_version) && $admin_version != $version) { + + // If a different version is specified for administration + // pages, use it instead. + $visibility = variable_get('jquery_update_visibility', JQUERY_UPDATE_VISIBILITY_ADMIN); + $page_match = FALSE; + $current_path = current_path(); + + // use the referrer of the request for system/ajax and similar mixed use paths + $mixed_paths = array( + 'system/ajax', + 'panels/ajax', + ); + if(in_array($_GET['q'], $mixed_paths)) { + $referrer = parse_url($_SERVER['HTTP_REFERER']); + $current_path = trim(substr($referrer['path'], 1)); + $current_path = url($current_path); + } + + // Check admin paths. + if ($visibility == JQUERY_UPDATE_VISIBILITY_ADMIN) { + $page_match = path_is_admin($current_path); + } + else { + + // By default ignore the admin pages and convert the string to lowercase. + $pages = drupal_strtolower(variable_get('jquery_update_pages', "admin\nadmin/*")); + + // Get the alias of the current page and also convert it to lowercase. + $path = drupal_strtolower(drupal_get_path_alias($current_path)); + + // Compare the desired pages and the current path alias. + $page_match = drupal_match_path($path, $pages); + + // If there isn't already a match, check the raw internal path. + if (!$page_match && $path != $current_path) { + $page_match = drupal_match_path($current_path, $pages); + } + + // XOR the visibility setting vs whether the path matched. + $page_match = !($visibility xor $page_match); + } + + // If there's a positive path match, load the alternative jQuery. + if ($page_match) { + $version = $admin_version; + } + } + $path = drupal_get_path('module', 'jquery_update'); // Make sure we inject either the minified or uncompressed version as desired. $min = variable_get('jquery_update_compression_type', 'min') == 'none' ? '' : '.min'; $cdn = variable_get('jquery_update_jquery_cdn', 'none'); - // Replace jQuery with the latest version. - $version = variable_get('jquery_update_jquery_version', '1.5'); jquery_update_jquery_replace($javascript, $cdn, $path, $min, $version); // Replace jQuery UI with CDN or local files. If from a CDN include all of jQuery UI. @@ -117,17 +175,79 @@ function jquery_update_menu() { * Implementation of hook_form_FORM_ID(). */ function jquery_update_settings_form() { - $form['jquery_update_jquery_version'] = array( + $form['version_options'] = array( + '#type' => 'fieldset', + '#title' => t('Version options'), + ); + $form['version_options']['jquery_update_jquery_version'] = array( '#type' => 'select', - '#title' => t('jQuery Version'), + '#title' => t('Default jQuery version'), '#options' => array( + '1.4' => '1.4', '1.5' => '1.5', '1.7' => '1.7', '1.8' => '1.8', ), '#default_value' => variable_get('jquery_update_jquery_version', '1.5'), - '#description' => t('Select which jQuery version branch to use.'), + '#description' => t('Select which jQuery version to use by default.'), + ); + $form['version_options']['jquery_update_jquery_admin_version'] = array( + '#type' => 'select', + '#title' => t('Alternate jQuery version on specific pages'), + '#options' => array( + '' => t('Use the default'), + '1.4' => '1.4', + '1.5' => '1.5', + '1.7' => '1.7', + '1.8' => '1.8', + ), + '#default_value' => variable_get('jquery_update_jquery_admin_version', ''), + '#description' => t('Optionally select a different version of jQuery to use on specific pages.'), + ); + $form['version_options']['jquery_update_visibility'] = array( + '#type' => 'radios', + '#title' => t('Switch jQuery on specific pages'), + '#options' => array( + JQUERY_UPDATE_VISIBILITY_ADMIN => t('Administration pages only'), + JQUERY_UPDATE_VISIBILITY_NOTLISTED => t('All pages except those listed'), + JQUERY_UPDATE_VISIBILITY_LISTED => t('Only the listed pages'), + ), + '#default_value' => variable_get('jquery_update_visibility', JQUERY_UPDATE_VISIBILITY_ADMIN), + + // Use the states API to hide this optional field if the 'admin_version' + // selector is set to "Use the default". + '#states' => array( + 'invisible' => array( + ':input[name="jquery_update_jquery_admin_version"]' => array('value' => ''), + ), + ), + ); + + // Wrap the 'pages' field in a container that will be hidden if the + // 'admin_version' selector is set to "Use the default". + $form['version_options']['pages_wrapper'] = array( + '#type' => 'container', + '#states' => array( + 'invisible' => array( + ':input[name="jquery_update_jquery_admin_version"]' => array('value' => ''), + ), + ), ); + $form['version_options']['pages_wrapper']['jquery_update_pages'] = array( + '#type' => 'textarea', + '#title' => t('Pages'), + '#default_value' => variable_get('jquery_update_pages', "admin\nadmin/*"), + '#description' => t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '')), + + // Use the states API to hide this optional field if the 'switch' option is + // set to 'admin'. + '#states' => array( + 'invisible' => array( + ':input[name="jquery_update_visibility"]' => array('value' => JQUERY_UPDATE_VISIBILITY_ADMIN), + ), + ), + ); + $form['jquery_update_compression_type'] = array( '#type' => 'radios', '#title' => t('jQuery compression level'), @@ -167,9 +287,17 @@ function jquery_update_settings_form() { * The version of jQuery to use. */ function jquery_update_jquery_replace(&$javascript, $cdn, $path, $min, $version) { + // When possible, use jQuery 1.4.4 provided by Drupal core. + if ($version == '1.4' && $cdn == 'none') { + return; + } + // Make sure to use the latest version in given branch. $trueversion = NULL; switch ($version) { + case '1.4': + $trueversion = '1.4.4'; + break; case '1.5': $trueversion = '1.5.2'; break; @@ -219,8 +347,16 @@ function jquery_update_jquery_replace(&$javascript, $cdn, $path, $min, $version) * The verison of jQuery to use. */ function jquery_update_jquery_backup(&$javascript, $path, $min, $version) { + if ($version == '1.4') { + // Use jQuery 1.4.4 provided by Drupal core. + $script_path = base_path() . 'misc/jquery.js'; + } + else { + $script_path = base_path() . $path . '/replace/jquery/'. $version . '/jquery' . $min . '.js'; + } + $javascript['jquery']['js'][] = array( - 'data' => 'window.jQuery || document.write("