Projet

Général

Profil

Paste
Télécharger (17 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / jquery_update / jquery_update.module @ 503b3f7b

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
    'version' => '1.11.0',
39
  );
40
  $libraries['jquery_update.ajax.fix'] = array(
41
    'title' => 'jQuery Update Version Fix',
42
    'js' => array(
43
      drupal_get_path('module', 'jquery_update') . '/js/jquery_update.js' => array(
44
        'group' => JS_LIBRARY,
45
        'weight' => 3,
46
      ),
47
    ),
48
    'version' => '0.0.1',
49
  );
50
  $libraries['jquery.metadata'] = array(
51
    'title' => 'QUnit',
52
    'js' => array(
53
      $path . '/jquery.metadata.js' => array(
54
        'group' => JS_LIBRARY,
55
        'weight' => 2,
56
      ),
57
    ),
58
    'version' => '4187',
59
  );
60
  $libraries['jquery.bgiframe'] = array(
61
    'title' => 'bgiframe',
62
    'website' => 'http://docs.jquery.com/Plugins/bgiframe',
63
    'js' => array(
64
      $path . '/jquery.bgiframe.js' => array(
65
        'group' => JS_LIBRARY,
66
        'weight' => 2,
67
      ),
68
    ),
69
    'version' => '2.1.2',
70
  );
71
  return $libraries;
72
}
73

    
74
/**
75
 * Implements hook_library_alter().
76
 */
77
function jquery_update_library_alter(&$javascript, $module) {
78
  $path = drupal_get_path('module', 'jquery_update');
79
  $version = variable_get('jquery_update_jquery_version', '1.10');
80

    
81

    
82
  // Modified System Library.
83
  if ($module === 'system') {
84

    
85
    // Make sure we inject either the minified or uncompressed version as desired.
86
    $min = variable_get('jquery_update_compression_type', 'min') == 'none' ? '' : '.min';
87
    $cdn = variable_get('jquery_update_jquery_cdn', 'none');
88

    
89
    // Replace jQuery with the alternative version.
90
    $admin_version = variable_get('jquery_update_jquery_admin_version', '');
91

    
92
    if (!empty($admin_version) && path_is_admin(current_path())) {
93
      if (version_compare($version, $admin_version, '!=')) {
94
        $version = $admin_version;
95
      }
96
    }
97

    
98
    // Always add a new jquery_version array to ajaxPageState.
99
    // This is what we used to determine which version to use
100
    // for any ajax callback.
101
    $javascript['drupal.ajax']['js'][] = array(
102
      'data' => array('ajaxPageState' => array('jquery_version' => $version)),
103
      'type' => 'setting',
104
    );
105
    $javascript['drupal.ajax']['dependencies'][] = array('jquery_update', 'jquery_update.ajax.fix');
106
    jquery_update_jquery_replace($javascript, $cdn, $path, $min, $version);
107

    
108
    // Replace jQuery UI with CDN or local files. If from a CDN include all of
109
    // jQuery UI.
110
    if (version_compare($version, '1.6', '>=')) {
111
      jquery_update_jqueryui_replace($javascript, $cdn, $path, $min);
112
    }
113

    
114
    // Replace the jQuery Cookie plugin.
115
    $javascript['cookie']['js']['misc/jquery.cookie.js']['data'] = $path . '/replace/ui/external/jquery.cookie.js';
116
    // Noting the version based on git commit as no version number is available.
117
    $javascript['cookie']['version'] = '67fb34f6a866c40d0570';
118

    
119
    // Replace jQuery Form plugin.
120
    $javascript['jquery.form']['js']['misc/jquery.form.js']['data'] = $path . '/replace/misc/jquery.form' . $min . '.js';
121
    $javascript['jquery.form']['version'] = '2.69';
122

    
123
    // Replace files for Jquery 1.9 and up
124
    if (version_compare($version, '1.9', '>=')) {
125
      $javascript['jquery.bbq']['js']['misc/jquery.ba-bbq.js']['data'] = $path . '/replace/misc/1.9/jquery.ba-bbq' . $min . '.js';
126
    }
127
  }
128

    
129
  if ($module == 'overlay') {
130
    if (version_compare($version, '1.9', '>=')) {
131
      $javascript['parent']['js']['modules/overlay/overlay-parent.js']['data'] = $path . '/replace/misc/1.9/overlay-parent.js';
132
    }
133
  }
134
}
135

    
136
/**
137
 * Implements hook_menu().
138
 */
139
function jquery_update_menu() {
140
  $items['admin/config/development/jquery_update'] = array(
141
    'title' => 'jQuery update',
142
    'description' => 'Configure settings related to the jQuery upgrade, the library path and compression.',
143
    'page callback' => 'drupal_get_form',
144
    'page arguments' => array('jquery_update_settings_form'),
145
    'access arguments' => array('administer site configuration'),
146
  );
147

    
148
  return $items;
149
}
150

    
151
/**
152
 * Admin settings menu callback.
153
 *
154
 * @see jquery_update_menu()
155
 */
156
function jquery_update_settings_form() {
157
  $form['version_options'] = array(
158
    '#type' => 'fieldset',
159
    '#title' => t('Version options'),
160
  );
161

    
162
  $form['version_options']['jquery_update_jquery_version'] = array(
163
    '#type' => 'select',
164
    '#title' => t('Default jQuery Version'),
165
    '#options' => array(
166
      '1.5' => '1.5',
167
      '1.7' => '1.7',
168
      '1.8' => '1.8',
169
      '1.9' => '1.9',
170
      '1.10' => '1.10',
171
    ),
172
    '#default_value' => variable_get('jquery_update_jquery_version', '1.10'),
173
    '#description' => t('Select which jQuery version to use by default.'),
174
  );
175

    
176
  $form['version_options']['jquery_update_jquery_admin_version'] = array(
177
    '#type' => 'select',
178
    '#title' => t('Alternate jQuery version for administrative pages'),
179
    '#options' => array(
180
      '' => t('Use the default'),
181
      '1.5' => '1.5',
182
      '1.7' => '1.7',
183
      '1.8' => '1.8',
184
      '1.10' => '1.10',
185
    ),
186
    '#default_value' => variable_get('jquery_update_jquery_admin_version', ''),
187
    '#description' => t('Optionally select a different version of jQuery to use on administrative pages.'),
188
  );
189

    
190
  $form['jquery_update_compression_type'] = array(
191
    '#type' => 'radios',
192
    '#title' => t('jQuery compression level'),
193
    '#options' => array(
194
      'min' => t('Production (minified)'),
195
      'none' => t('Development (uncompressed)'),
196
    ),
197
    '#default_value' => variable_get('jquery_update_compression_type', 'min'),
198
  );
199
  $form['jquery_update_jquery_cdn'] = array(
200
    '#type' => 'select',
201
    '#title' => t('jQuery and jQuery UI CDN'),
202
    '#options' => array(
203
      'none' => t('None'),
204
      'google' => t('Google'),
205
      'microsoft' => t('Microsoft'),
206
      'jquery' => t('jQuery'),
207
    ),
208
    '#default_value' => variable_get('jquery_update_jquery_cdn', 'none'),
209
    '#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.'),
210
  );
211

    
212
  return system_settings_form($form);
213
}
214

    
215
/**
216
 * Update jQuery to the CDN or local path.
217
 *
218
 * @param array $javascript
219
 *   The library definition array as seen in hook_library_alter().
220
 * @param string $cdn
221
 *   The name of the CDN option to use. Possible options are:
222
 *   - none
223
 *   - google
224
 *   - microsoft
225
 * @param string $path
226
 *   The path to the module where replacements can be found.
227
 * @param string $min
228
 *   The '.min' to include in the file name if we are requesting a minified
229
 *   version.
230
 * @param string $version
231
 *   The version of jQuery to use.
232
 */
233
function jquery_update_jquery_replace(&$javascript, $cdn, $path, $min, $version) {
234
  // If the ajax version is set then that one always win.
235
  if (!empty($_POST['ajax_page_state']['jquery_version'])) {
236
    $ajax_version = $_POST['ajax_page_state']['jquery_version'];
237
    if (in_array($ajax_version, array('1.5', '1.6', '1.7', '1.8', '1.9', '1.10'))) {
238
      $version = $ajax_version;
239
    }
240
  }
241

    
242
  // Make sure to use the latest version in given branch.
243
  $trueversion = NULL;
244
  switch ($version) {
245
    case '1.5':
246
      $trueversion = '1.5.2';
247
      break;
248

    
249
    case '1.7':
250
      $trueversion = '1.7.1';
251
      break;
252

    
253
    case '1.8':
254
      $trueversion = '1.8.2';
255
      break;
256

    
257
    case '1.9':
258
      $trueversion = '1.9.1';
259
      break;
260

    
261
    case '1.10':
262
      $trueversion = '1.10.2';
263
      break;
264
  }
265
  $javascript['jquery']['version'] = $trueversion;
266

    
267
  // Check for CDN support.
268
  switch ($cdn) {
269
    case 'google':
270
      $javascript['jquery']['js']['misc/jquery.js']['data'] = 'https://ajax.googleapis.com/ajax/libs/jquery/' . $trueversion . '/jquery' . $min . '.js';
271
      $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external';
272
      jquery_update_jquery_backup($javascript, $path, $min, $version);
273
      break;
274

    
275
    case 'microsoft':
276
      $javascript['jquery']['js']['misc/jquery.js']['data'] = 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-' . $trueversion . $min . '.js';
277
      $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external';
278
      jquery_update_jquery_backup($javascript, $path, $min, $version);
279
      break;
280

    
281
    case 'jquery':
282
      $javascript['jquery']['js']['misc/jquery.js']['data'] = 'http://code.jquery.com/jquery-' . $trueversion . $min . '.js';
283
      $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external';
284
      jquery_update_jquery_backup($javascript, $path, $min, $version);
285
      break;
286

    
287
    case 'none':
288
    default:
289
      $javascript['jquery']['js']['misc/jquery.js']['data'] = $path . '/replace/jquery/' . $version . '/jquery' . $min . '.js';
290
      break;
291
  }
292
}
293

    
294
/**
295
 * Add the local fallback in case jQuery from the CDN is unavailable.
296
 *
297
 * @param array $javascript
298
 *   The $libraries array as seen in hook_library_alter()
299
 * @param string $path
300
 *   The path to the module where replacements can be found.
301
 * @param string $min
302
 *   The '.min' to include in the file name if we are requesting a minified
303
 *   version.
304
 * @param string $version
305
 *   The verison of jQuery to use.
306
 */
307
function jquery_update_jquery_backup(&$javascript, $path, $min, $version) {
308
  $javascript['jquery']['js'][] = array(
309
    'data' => 'window.jQuery || document.write("<script src=\'' . base_path() . $path . '/replace/jquery/' . $version . '/jquery' . $min . '.js\'>\x3C/script>")',
310
    'type' => 'inline',
311
    'group' => JS_LIBRARY,
312
    'weight' => -19.999999999,
313
  );
314
}
315

    
316
/**
317
 * Update jQuery UI to the CDN or local path.
318
 *
319
 * @param array $javascript
320
 *   The library definition array as seen in hook_library_alter().
321
 * @param string $cdn
322
 *   The name of the CDN option to use. Possible options are:
323
 *   - none
324
 *   - google
325
 *   - microsoft
326
 * @param string $path
327
 *   The path to the module where replacements can be found.
328
 * @param string $min
329
 *   The '.min' to include in the file name if we are requesting a minified
330
 *   version.
331
 */
332
function jquery_update_jqueryui_replace(&$javascript, $cdn, $path, $min) {
333
  // Add new components
334
  $javascript['ui.menu'] = array(
335
    'title' => 'jQuery UI: Menu',
336
    'website' => 'http://jqueryui.com/demos/menu/',
337
    'version' => '1.10.2',
338
    'js' => array('misc/ui/jquery.ui.menu.min.js' => array()),
339
    'css' => array('misc/ui/jquery.ui.menu.css' => array()),
340
    'dependencies' => array(array('system', 'ui.widget'), array('system', 'ui.position')),
341
  );
342
  $javascript['ui.spinner'] = array(
343
    'title' => 'jQuery UI: Spinner',
344
    'website' => 'http://jqueryui.com/demos/spinner/',
345
    'version' => '1.10.2',
346
    'js' => array('misc/ui/jquery.ui.spinner.min.js' => array()),
347
    'css' => array('misc/ui/jquery.ui.spinner.css' => array()),
348
    'dependencies' => array(array('system', 'ui.widget'), array('system', 'ui.button')),
349
  );
350
  $javascript['ui.tooltip'] = array(
351
    'title' => 'jQuery UI: Spinner',
352
    'website' => 'http://jqueryui.com/demos/tooltip/',
353
    'version' => '1.10.2',
354
    'js' => array('misc/ui/jquery.ui.tooltip.min.js' => array()),
355
    'css' => array('misc/ui/jquery.ui.tooltip.css' => array()),
356
    'dependencies' => array(array('system', 'ui.widget'), array('system', 'ui.position')),
357
  );
358

    
359
  // fix dependencies
360
  $javascript['ui.autocomplete']['dependencies'][] = array('system', 'ui.menu');
361
  // Replace all CSS files.
362
  $names = drupal_map_assoc(array(
363
    'ui.accordion', 'ui.autocomplete', 'ui.button', 'ui.datepicker', 'ui.dialog',
364
    'ui.progressbar', 'ui.resizable', 'ui.selectable', 'ui.slider', 'ui.tabs',
365
    'ui.menu', 'ui.spinner', 'ui.tooltip',
366
  ));
367
  $names['ui'] = 'ui.core';
368
  $csspath = $path . '/replace/ui/themes/base/' . (($min == '.min') ? 'minified/' : '');
369
  foreach ($names as $name => $file) {
370
    $javascript[$name]['css']["misc/ui/jquery.$file.css"]['data'] = $csspath . 'jquery.' . $file . $min . '.css';
371
  }
372
  // Make sure ui.theme is replaced as well.
373
  $javascript['ui']['css']['misc/ui/jquery.ui.theme.css']['data'] = $csspath . 'jquery.ui.theme' . $min . '.css';
374

    
375
  // Replace jQuery UI's JavaScript, beginning by defining the mapping.
376
  $names = drupal_map_assoc(array(
377
    'ui.accordion', 'ui.autocomplete', 'ui.button', 'ui.datepicker', 'ui.dialog', 'ui.draggable',
378
    'ui.droppable', 'ui.mouse', 'ui.position', 'ui.progressbar', 'ui.resizable', 'ui.selectable',
379
    'ui.slider', 'ui.sortable', 'ui.tabs', 'ui.widget', 'ui.spinner', 'ui.menu', 'ui.tooltip',
380
    'effects.blind', 'effects.bounce', 'effects.clip', 'effects.drop', 'effects.explode', 'effects.fade',
381
    'effects.fold', 'effects.highlight', 'effects.pulsate', 'effects.scale', 'effects.shake', 'effects.slide',
382
    'effects.transfer',
383
  ));
384
  $names['ui'] = 'ui.core';
385
  $names['effects'] = 'ui.effect';
386

    
387
  switch ($cdn) {
388
    case 'google':
389
      $cdn = 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui' . $min . '.js';
390
      jquery_update_jqueryui_cdn($cdn, $javascript, $path, $min, $names);
391
      jquery_update_jqueryui_backup($javascript, $path, $min);
392
      break;
393

    
394
    case 'microsoft':
395
      $cdn = 'http://ajax.aspnetcdn.com/ajax/jquery.ui/1.10.2/jquery-ui' . $min . '.js';
396
      jquery_update_jqueryui_cdn($cdn, $javascript, $path, $min, $names);
397
      jquery_update_jqueryui_backup($javascript, $path, $min);
398
      break;
399

    
400
    case 'none':
401
      jquery_update_jqueryui_local($javascript, $path, $min, $names);
402
      break;
403
  }
404
}
405

    
406
/**
407
 * Add the local fallback in case jQuery UI from the CDN is unavailable.
408
 *
409
 * @param array $javascript
410
 *   The $libraries array as seen in hook_library_alter()
411
 * @param string $path
412
 *   The path to the module where replacements can be found.
413
 * @param string $min
414
 *   The '.min' to include in the file name if we are requesting a minified
415
 *   version.
416
 */
417
function jquery_update_jqueryui_backup(&$javascript, $path, $min) {
418
  $js_path = ($min == '.min') ? '/replace/ui/ui/minified/jquery-ui.min.js' : '/replace/ui/ui/jquery-ui.js';
419
  $javascript['ui']['js'][] = array(
420
    'data' => 'window.jQuery.ui || document.write("<script src=\'' . base_path() . $path . $js_path . '\'>\x3C/script>")',
421
    'type' => 'inline',
422
    'group' => JS_LIBRARY,
423
    'weight' => -10.999999999,
424
  );
425
}
426

    
427
/**
428
 * Handle when jQuery UI is updated to the cdn version.
429
 *
430
 * @param string $cdn
431
 *   The name of the CDN option to use. Possible options are:
432
 *   - none
433
 *   - google
434
 *   - microsoft
435
 * @param array $javascript
436
 *   The $libraries array as seen in hook_library_alter()
437
 * @param string $path
438
 *   The path to the module where replacements can be found.
439
 * @param string $min
440
 *   The '.min' to include in the file name if we are requesting a minified
441
 *   version.
442
 * * @param array $names
443
 *   An array mapping jquery ui parts to their file names.
444
 */
445
function jquery_update_jqueryui_cdn($cdn, &$javascript, $path, $min, $names) {
446

    
447
  // Construct the jQuery UI path and replace the JavaScript.
448
  $jspath = $path . '/replace/ui/ui/' . ($min == '.min' ? 'minified/' : '');
449
  foreach ($names as $name => $file) {
450
    $corefile = 'misc/ui/jquery.' . $file . '.min.js';
451
    // Remove the core files.
452
    unset($javascript[$name]['js'][$corefile]);
453
    $javascript[$name]['version'] = '1.10.2';
454
  }
455

    
456
  // UI is used by all of UI. Add the js cdn here.
457
  $javascript['ui']['js'][$cdn] = array(
458
    'data' => $cdn,
459
    'type' => 'external',
460
    'group' => JS_LIBRARY,
461
    'weight' => -11,
462
  );
463

    
464
  // The cdn puts jQuery UI core and the jQuery UI Effects library in the same
465
  // file, but the latter can normally be used without the former. So we need
466
  // to add a dependency to guarantee that code which uses the Effects library
467
  // has the file loaded regardless of whether they are also using jQuery UI
468
  // core.
469
  $javascript['effects']['dependencies'][] = array('system', 'ui');
470
}
471

    
472
/**
473
 * Handle when jQuery UI is updated to the local version.
474
 *
475
 * @param array $javascript
476
 *   The $libraries array as seen in hook_library_alter()
477
 * @param string $path
478
 *   The path to the module where replacements can be found.
479
 * @param string $min
480
 *   The '.min' to include in the file name if we are requesting a minified
481
 *   version.
482
 * @param array $names
483
 *   An array mapping jquery ui parts to their file names.
484
 */
485
function jquery_update_jqueryui_local(&$javascript, $path, $min, $names) {
486

    
487
  // Construct the jQuery UI path and replace the JavaScript.
488
  $jspath = $path . '/replace/ui/ui/' . ($min == '.min' ? 'minified/' : '');
489
  foreach ($names as $name => $file) {
490
    $corefile = 'misc/ui/jquery.' . $file . '.min.js';
491
    $javascript[$name]['js'][$corefile]['data'] = $jspath . 'jquery.' . $file . $min . '.js';
492
    $javascript[$name]['version'] = '1.10.2';
493
  }
494
}