Projet

Général

Profil

Paste
Télécharger (12,4 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / jquery_update / jquery_update.module @ dc45a079

1
<?php
2

    
3
/**
4
 * @file
5
 * Updates Drupal to use the latest version of jQuery.
6
 */
7

    
8
/**
9
 * Implements hook_help().
10
 */
11
function jquery_update_help($path, $arg) {
12
  switch ($path) {
13
      // Help for another path in the block module
14
    case 'admin/config/development/jquery_update':
15
      return '<p>' . t('Configure how <a href="@jquery">jQuery</a> behaves on the site. Select which jQuery version, the compression level and whether or not to use a CDN.', array(
16
        '@jquery' => 'http://jquery.com',
17
      )) . '</p>';
18
  }
19
}
20

    
21
/**
22
 * Implements hook_library().
23
 */
24
function jquery_update_library() {
25
  // Register libraries available in the external directory.
26
  $path = drupal_get_path('module', 'jquery_update') . '/ui/external';
27
  $libraries['qunit'] = array(
28
    'title' => 'QUnit',
29
    'js' => array(
30
      $path . '/qunit.js' => array(
31
        'group' => JS_LIBRARY,
32
        'weight' => 2,
33
      ),
34
    ),
35
    'css' => array(
36
      $path . '/qunit.css' => array(),
37
    ),
38
  );
39
  $libraries['jquery.metadata'] = array(
40
    'title' => 'QUnit',
41
    'js' => array(
42
      $path . '/jquery.metadata.js' => array(
43
        'group' => JS_LIBRARY,
44
        'weight' => 2,
45
      ),
46
    ),
47
    'version' => '4187',
48
  );
49
  $libraries['jquery.bgiframe'] = array(
50
    'title' => 'bgiframe',
51
    'website' => 'http://docs.jquery.com/Plugins/bgiframe',
52
    'js' => array(
53
      $path . '/jquery.bgiframe.js' => array(
54
        'group' => JS_LIBRARY,
55
        'weight' => 2,
56
      ),
57
    ),
58
    'version' => '2.1.2',
59
  );
60
  return $libraries;
61
}
62

    
63
/**
64
 * Implementation of hook_library_alter().
65
 */
66
function jquery_update_library_alter(&$javascript, $module) {
67

    
68
  // We are updating just the system module. For all other cases we return.
69
  if ($module != 'system') {
70
    return;
71
  }
72

    
73
  $path = drupal_get_path('module', 'jquery_update');
74

    
75
  // Make sure we inject either the minified or uncompressed version as desired.
76
  $min = variable_get('jquery_update_compression_type', 'min') == 'none' ? '' : '.min';
77
  $cdn = variable_get('jquery_update_jquery_cdn', 'none');
78

    
79
  // Replace jQuery with the latest version.
80
  $version = variable_get('jquery_update_jquery_version', '1.5');
81
  jquery_update_jquery_replace($javascript, $cdn, $path, $min, $version);
82

    
83
  // Replace jQuery UI with CDN or local files. If from a CDN include all of jQuery UI.
84
  jquery_update_jqueryui_replace($javascript, $cdn, $path, $min);
85

    
86
  // Replace the jQuery Cookie plugin.
87
  $javascript['cookie']['js']['misc/jquery.cookie.js']['data'] = $path . '/replace/ui/external/jquery.cookie.js';
88
  // Noting the version based on git commit as no version number is available.
89
  $javascript['cookie']['version'] = '67fb34f6a866c40d0570';
90

    
91
  // Replace jQuery Form plugin.
92
  $javascript['jquery.form']['js']['misc/jquery.form.js']['data'] = $path . '/replace/misc/jquery.form' . $min . '.js';
93
  $javascript['jquery.form']['version'] = '2.69';
94

    
95
  // Replace files for jQuery 1.7 and up
96
  if (version_compare($version, '1.7', '>=')) {
97
    $javascript['drupal.states']['js']['misc/states.js']['data'] = $path . '/replace/misc/1.7/states.js';
98
  }
99
}
100

    
101
/**
102
 * Implements hook_menu().
103
 */
104
function jquery_update_menu() {
105
  $items['admin/config/development/jquery_update'] = array(
106
    'title' => 'jQuery update',
107
    'description' => 'Configure settings related to the jQuery upgrade, the library path and compression.',
108
    'page callback' => 'drupal_get_form',
109
    'page arguments' => array('jquery_update_settings_form'),
110
    'access arguments' => array('administer site configuration'),
111
  );
112

    
113
  return $items;
114
}
115

    
116
/**
117
 * Implementation of hook_form_FORM_ID().
118
 */
119
function jquery_update_settings_form() {
120
  $form['jquery_update_jquery_version'] = array(
121
    '#type' => 'select',
122
    '#title' => t('jQuery Version'),
123
    '#options' => array(
124
      '1.5' => '1.5',
125
      '1.7' => '1.7',
126
      '1.8' => '1.8',
127
    ),
128
    '#default_value' => variable_get('jquery_update_jquery_version', '1.5'),
129
    '#description' => t('Select which jQuery version branch to use.'),
130
  );
131
  $form['jquery_update_compression_type'] = array(
132
    '#type' => 'radios',
133
    '#title' => t('jQuery compression level'),
134
    '#options' => array(
135
      'min' => t('Production (minified)'),
136
      'none' => t('Development (uncompressed)'),
137
    ),
138
    '#default_value' => variable_get('jquery_update_compression_type', 'min'),
139
  );
140
  $form['jquery_update_jquery_cdn'] = array(
141
    '#type' => 'select',
142
    '#title' => t('jQuery and jQuery UI CDN'),
143
    '#options' => array(
144
      'none' => t('None'),
145
      'google' => t('Google'),
146
      'microsoft' => t('Microsoft'),
147
      'jquery' => t('jQuery'),
148
    ),
149
    '#default_value' => variable_get('jquery_update_jquery_cdn', 'none'),
150
    '#description' => t('Use jQuery and jQuery UI from a CDN. If the CDN is not available the local version of jQuery and jQuery UI will be used.'),
151
  );
152

    
153
  return system_settings_form($form);
154
}
155

    
156
/**
157
 * Update jQuery to the CDN or local path.
158
 *
159
 * @param array $javascript
160
 *   The library definition array as seen in hook_library_alter().
161
 * @param string $cdn
162
 *   The name of the CDN option to use. Possible options are:
163
 *   - none
164
 *   - google
165
 *   - microsoft
166
 * @param string $version
167
 *   The version of jQuery to use.
168
 */
169
function jquery_update_jquery_replace(&$javascript, $cdn, $path, $min, $version) {
170
  // Make sure to use the latest version in given branch.
171
  $trueversion = NULL;
172
  switch ($version) {
173
    case '1.5':
174
      $trueversion = '1.5.2';
175
      break;
176
    case '1.7':
177
      $trueversion = '1.7.1';
178
      break;
179
    case '1.8':
180
      $trueversion = '1.8.2';
181
      break;
182
  }
183
  $javascript['jquery']['version'] = $trueversion;
184

    
185
  // Check for CDN support.
186
  switch($cdn) {
187
    case 'google':
188
      $javascript['jquery']['js']['misc/jquery.js']['data'] = 'https://ajax.googleapis.com/ajax/libs/jquery/'. $trueversion . '/jquery' . $min . '.js';
189
      $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external';
190
      jquery_update_jquery_backup($javascript, $path, $min, $version);
191
      break;
192
    case 'microsoft':
193
      $javascript['jquery']['js']['misc/jquery.js']['data'] = 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-'. $trueversion . $min . '.js';
194
      $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external';
195
      jquery_update_jquery_backup($javascript, $path, $min, $version);
196
      break;
197
    case 'jquery':
198
      $javascript['jquery']['js']['misc/jquery.js']['data'] = 'http://code.jquery.com/jquery-'. $trueversion . $min . '.js';
199
      $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external';
200
      jquery_update_jquery_backup($javascript, $path, $min, $version);
201
      break;
202
    case 'none':
203
    default:
204
      $javascript['jquery']['js']['misc/jquery.js']['data'] = $path . '/replace/jquery/'. $version . '/jquery' . $min . '.js';
205
      break;
206
  }
207
}
208

    
209
/**
210
 * Add the local fallback in case jQuery from the CDN is unavailable.
211
 *
212
 * @param array $javascript
213
 *   The $libraries array as seen in hook_library_alter()
214
 * @param string $path
215
 *   The path to the module where replacements can be found.
216
 * @param string $min
217
 *   The '.min' to include in the file name if we are requesting a minified version.
218
 * @param string $version
219
 *   The verison of jQuery to use.
220
 */
221
function jquery_update_jquery_backup(&$javascript, $path, $min, $version) {
222
  $javascript['jquery']['js'][] = array(
223
    'data' => 'window.jQuery || document.write("<script src=\'' . base_path() . $path . '/replace/jquery/'. $version . '/jquery' . $min . '.js\'>\x3C/script>")',
224
    'type' => 'inline',
225
    'group' => JS_LIBRARY,
226
    'weight' => -19.999999999,
227
  );
228
}
229

    
230
/**
231
 * Update jQuery UI to the CDN or local path.
232
 *
233
 * @param array $javascript
234
 *   The library definition array as seen in hook_library_alter().
235
 * @param string $cdn
236
 *   The name of the CDN option to use. Possible options are:
237
 *   - none
238
 *   - google
239
 *   - microsoft
240
 */
241
function jquery_update_jqueryui_replace(&$javascript, $cdn, $path, $min) {
242
  // Replace all CSS files.
243
  $names = drupal_map_assoc(array(
244
    'ui.accordion', 'ui.autocomplete', 'ui.button', 'ui.datepicker',
245
    'ui.dialog', 'ui.progressbar', 'ui.resizable', 'ui.selectable',
246
    'ui.slider', 'ui.tabs',
247
  ));
248
  $names['ui'] = 'ui.core';
249
  $csspath = $path . '/replace/ui/themes/base/' . (($min == '.min') ? 'minified/' : '');
250
  foreach ($names as $name => $file) {
251
    $javascript[$name]['css']["misc/ui/jquery.$file.css"]['data'] = $csspath . 'jquery.' . $file . $min . '.css';
252
  }
253
  // Make sure ui.theme is replaced as well.
254
  $javascript['ui']['css']['misc/ui/jquery.ui.theme.css']['data'] = $csspath . 'jquery.ui.theme' . $min . '.css';
255

    
256
  // Replace jQuery UI's JavaScript, beginning by defining the mapping.
257
  $names = drupal_map_assoc(array(
258
    'ui.accordion', 'ui.autocomplete', 'ui.button', 'ui.datepicker',
259
    'ui.dialog', 'ui.draggable', 'ui.droppable', 'ui.mouse', 'ui.position',
260
    'ui.progressbar', 'ui.resizable', 'ui.selectable', 'ui.slider',
261
    'ui.sortable', 'ui.tabs', 'ui.widget', 'effects.blind', 'effects.bounce',
262
    'effects.clip', 'effects.drop', 'effects.explode', 'effects.fade',
263
    'effects.fold', 'effects.highlight', 'effects.pulsate', 'effects.scale',
264
    'effects.shake', 'effects.slide', 'effects.transfer',
265
  ));
266
  $names['ui'] = 'ui.core';
267
  $names['effects'] = 'effects.core';
268

    
269
  switch($cdn) {
270
    case 'google':
271
      $cdn = 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui' . $min . '.js';
272
      jquery_update_jqueryui_cdn($cdn, $javascript, $path, $min, $names);
273
      jquery_update_jqueryui_backup($javascript, $path, $min);
274
      break;
275
    case 'microsoft':
276
      $cdn = 'http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.11/jquery-ui' . $min . '.js';
277
      jquery_update_jqueryui_cdn($cdn, $javascript, $path, $min, $names);
278
      jquery_update_jqueryui_backup($javascript, $path, $min);
279
      break;
280
    case 'none':
281
      jquery_update_jqueryui_local($javascript, $path, $min, $names);
282
      break;
283
  }
284
}
285

    
286
/**
287
 * Add the local fallback in case jQuery UI from the CDN is unavailable.
288
 *
289
 * @param array $javascript
290
 *   The $libraries array as seen in hook_library_alter()
291
 * @param string $path
292
 *   The path to the module where replacements can be found.
293
 * @param string $min
294
 *   The '.min' to include in the file name if we are requesting a minified version.
295
 */
296
function jquery_update_jqueryui_backup(&$javascript, $path, $min) {
297
  $js_path = ($min == '.min') ? '/replace/ui/ui/minified/jquery-ui.min.js' : '/replace/ui/ui/jquery-ui.js';
298
  $javascript['ui']['js'][] = array(
299
    'data' => 'window.jQuery.ui || document.write("<script src=\'' . base_path() . $path . $js_path . '\'>\x3C/script>")',
300
    'type' => 'inline',
301
    'group' => JS_LIBRARY,
302
    'weight' => -10.999999999,
303
  );
304
}
305

    
306
/**
307
 * Handle when jQuery UI is updated to the cdn version.
308
 *
309
 * @param array $javascript
310
 *   The $libraries array as seen in hook_library_alter()
311
 * @param string $path
312
 *   The path to the module where replacements can be found.
313
 * @param string $min
314
 *   The '.min' to include in the file name if we are requesting a minified version.
315
 * @param array $names
316
 *   An array mapping jquery ui parts to their file names.
317
 */
318
function jquery_update_jqueryui_cdn($cdn, &$javascript, $path, $min, $names) {
319

    
320
  // Construct the jQuery UI path and replace the JavaScript.
321
  $jspath = $path . '/replace/ui/ui/' . ($min == '.min' ? 'minified/' : '');
322
  foreach ($names as $name => $file) {
323
    $corefile = 'misc/ui/jquery.' . $file . '.min.js';
324
    // Remove the core files.
325
    unset($javascript[$name]['js'][$corefile]);
326
    $javascript[$name]['version'] = '1.8.11';
327
  }
328

    
329
  // UI is used by all of UI. Add the js cdn here.
330
  $javascript['ui']['js'][$cdn] = array(
331
    'data' => $cdn,
332
    'type' => 'external',
333
    'group' => JS_LIBRARY,
334
    'weight' => -11,
335
  );
336
}
337

    
338
/**
339
 * Handle when jQuery UI is updated to the local version.
340
 *
341
 * @param array $javascript
342
 *   The $libraries array as seen in hook_library_alter()
343
 * @param string $path
344
 *   The path to the module where replacements can be found.
345
 * @param string $min
346
 *   The '.min' to include in the file name if we are requesting a minified version.
347
 * @param array $names
348
 *   An array mapping jquery ui parts to their file names.
349
 */
350
function jquery_update_jqueryui_local(&$javascript, $path, $min, $names) {
351

    
352
  // Construct the jQuery UI path and replace the JavaScript.
353
  $jspath = $path . '/replace/ui/ui/' . ($min == '.min' ? 'minified/' : '');
354
  foreach ($names as $name => $file) {
355
    $corefile = 'misc/ui/jquery.' . $file . '.min.js';
356
    $javascript[$name]['js'][$corefile]['data'] = $jspath . 'jquery.' . $file . $min . '.js';
357
    $javascript[$name]['version'] = '1.8.11';
358
  }
359
}