[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/jquery_update/ -> jquery_update.module (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Updates Drupal to use the latest version of jQuery.
   6   */
   7  
   8  /**
   9   * Implements hook_jquery_update_alter().
  10   *
  11   * @see hook_jquery_update_alter()
  12   */
  13  function jquery_update_jquery_update_alter(&$javascript) {
  14    $path = drupal_get_path('module', 'jquery_update') . '/replace/';
  15    $scripts = jquery_update_get_replacements();
  16  
  17    // Replace jquery.js first.
  18    $jquerypath = jquery_update_jquery_path();
  19    $new_jquery = array($jquerypath => $javascript['core']['misc/jquery.js']);
  20    $javascript['core'] = array_merge($new_jquery, $javascript['core']);
  21    $javascript['core'][$jquerypath]['version'] = variable_get('jquery_update_jquery_version', '1.3');
  22    unset($javascript['core']['misc/jquery.js']);
  23  
  24    // Loop through each of the required replacements.
  25    foreach ($scripts as $type => $replacements) {
  26      foreach ($replacements as $find => $replace) {
  27        // If the file to replace is loaded on this page...
  28        if (isset($javascript[$type][$find])) {
  29          // Create a new entry for the replacement file, and unset the original one.
  30          $javascript[$type][$path . $replace] = $javascript[$type][$find];
  31          unset($javascript[$type][$find]);
  32        }
  33      }
  34    }
  35  }
  36  
  37  /**
  38   * Array of jQuery files to replace if jQuery is loaded.
  39   */
  40  function jquery_update_get_replacements() {
  41    return array(
  42      'module' => array(
  43        'misc/farbtastic/farbtastic.js' => 'farbtastic.js',
  44        'misc/teaser.js' => 'teaser.js',
  45        'misc/jquery.form.js' => 'jquery.form.js',
  46        'misc/ahah.js' => 'ahah.js',
  47        // Certain versions of Views re-add tabledrag.js as $type 'module'.
  48        'misc/tabledrag.js' => 'tabledrag.js',
  49      ),
  50      'core' => array(
  51        'misc/tabledrag.js' => 'tabledrag.js',
  52      ),
  53    );
  54  }
  55  
  56  /**
  57   * Implementation of hook_theme_registry_alter().
  58   *
  59   * Make jQuery Update's page preprocess function run *after* everything else's,
  60   * so that a theme can't call drupal_get_js() and mess everything up.
  61   */
  62  function jquery_update_theme_registry_alter(&$theme_registry) {
  63    if (isset($theme_registry['page'])) {
  64      if (count($theme_registry['page']['preprocess functions']) > 0) {
  65        // If jquery_update's preprocess function is there already, remove it.
  66        if ($key = array_search('jquery_update_preprocess_page', $theme_registry['page']['preprocess functions'])) {
  67          unset($theme_registry['page']['preprocess functions'][$key]);
  68        }
  69      }
  70      // Now tack it on at the end so it runs after everything else.
  71      $theme_registry['page']['preprocess functions'][] = 'jquery_update_preprocess_page';
  72    }
  73  }
  74  
  75  /**
  76   * Implementation of moduleName_preprocess_hook().
  77   *
  78   * Replace Drupal core's jquery.js with the new one from jQuery Update module.
  79   */
  80  function jquery_update_preprocess_page(&$variables) {
  81    // Only do this for pages that have JavaScript on them.
  82    if (!empty($variables['scripts'])) {
  83  
  84      // Perform the logic if either jQuery Update's jquery.js is newer than core's.
  85      if (variable_get('jquery_update_replace', TRUE)) {
  86        // Get an array of all the JavaScript files loaded by Drupal on this page.
  87        $javascript = drupal_add_js();
  88  
  89        // Invoke hook_js_alter().
  90        drupal_alter('jquery_update', $javascript);
  91  
  92        // Replace with all the new JavaScript.
  93        $variables['scripts'] = drupal_get_js('header', $javascript);
  94      }
  95    }
  96  }
  97  
  98  /**
  99   * Return the version of jQuery that is installed.
 100   *
 101   * This can be used by other modules' hook_requirements() to ensure that the
 102   * proper version of jQuery is installed.
 103   *
 104   * @see version_compare
 105   */
 106  function jquery_update_get_version($jquery_path = NULL) {
 107    $version = 0;
 108    $pattern = '# * jQuery JavaScript Library v([0-9\.a-z]+)#';
 109  
 110    // No file is passed in so default to the file included with this module.
 111    if (is_null($jquery_path)) {
 112      $jquery_path = jquery_update_jquery_path();
 113    }
 114  
 115    // Return the version provided by jQuery Update.
 116    $jquery = file_get_contents($jquery_path);
 117    if (preg_match($pattern, $jquery, $matches)) {
 118      $version = $matches[1];
 119    }
 120  
 121    return $version;
 122  }
 123  
 124  /**
 125   * Implementation of hook_flush_caches().
 126   */
 127  function jquery_update_flush_caches() {
 128    // Find the versions of jQuery provided by core and this module.
 129    $jquery_update_version = jquery_update_get_version();
 130    $jquery_core_version = jquery_update_get_version('misc/jquery.js');
 131  
 132    // Set a variable according to whether core's version needs to be replaced.
 133    $replace = version_compare($jquery_core_version, $jquery_update_version, '<');
 134    variable_set('jquery_update_replace', $replace);
 135  }
 136  
 137  /**
 138   * Implementation of hook_menu().
 139   */
 140  function jquery_update_menu() {
 141    $items['admin/settings/jquery_update'] = array(
 142      'title' => 'jQuery Update',
 143      'description' => 'Configure settings for jQuery Update module.',
 144      'page callback' => 'drupal_get_form',
 145      'page arguments' => array('jquery_update_settings'),
 146      'access arguments' => array('administer site configuration'),
 147    );
 148    return $items;
 149  }
 150  
 151  /**
 152   * Admin settings form.
 153   */
 154  function jquery_update_settings() {
 155    // Clear the javascript cache when the setting is updated and check version of jquery file.
 156    $form['#submit'][] = 'drupal_clear_js_cache';
 157    $form['#submit'][] = 'jquery_update_flush_caches';
 158  
 159    $form['jquery_update_compression_type'] = array(
 160      '#type' => 'radios',
 161      '#title' => t('Choose jQuery compression level'),
 162      '#options' => array(
 163        'min' => t('Production (Minified)'),
 164        'none' => t('Development (Uncompressed Code)'),
 165      ),
 166      '#default_value' => variable_get('jquery_update_compression_type', 'min'),
 167    );
 168    $form['jquery_update_jquery_version'] = array(
 169      '#type' => 'select',
 170      '#title' => t('jQuery Version'),
 171      '#options' => array(
 172        '1.3' => '1.3',
 173        '1.7' => '1.7',
 174      ),
 175      '#default_value' => variable_get('jquery_update_jquery_version', '1.3'),
 176      '#description' => t('Select which jQuery version branch to use.'),
 177    );
 178  
 179    return system_settings_form($form);
 180  }
 181  
 182  /**
 183   * Return the path to the jQuery file.
 184   */
 185  function jquery_update_jquery_path() {
 186    // Check whether we are to retrieve the minified version.
 187    $jquery_file = array(
 188      'none' => 'jquery.js',
 189      'min' => 'jquery.min.js',
 190    );
 191    $type = variable_get('jquery_update_compression_type', 'min');
 192  
 193    // Find the jQuery version to use.
 194    $version = variable_get('jquery_update_jquery_version', '1.3');
 195    $path = drupal_get_path('module', 'jquery_update') .'/replace/jquery/'. $version .'/';
 196    return $path . $jquery_file[$type];
 197  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7