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 85ad3d82 Assos Assos
<?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 503b3f7b Assos Assos
    // Help for another path in the block module.
14 85ad3d82 Assos Assos
    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 503b3f7b Assos Assos
    '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 85ad3d82 Assos Assos
  );
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 503b3f7b Assos Assos
 * Implements hook_library_alter().
76 85ad3d82 Assos Assos
 */
77
function jquery_update_library_alter(&$javascript, $module) {
78 503b3f7b Assos Assos
  $path = drupal_get_path('module', 'jquery_update');
79
  $version = variable_get('jquery_update_jquery_version', '1.10');
80 85ad3d82 Assos Assos
81
82 503b3f7b Assos Assos
  // 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 85ad3d82 Assos Assos
89 503b3f7b Assos Assos
    // Replace jQuery with the alternative version.
90
    $admin_version = variable_get('jquery_update_jquery_admin_version', '');
91 85ad3d82 Assos Assos
92 503b3f7b Assos Assos
    if (!empty($admin_version) && path_is_admin(current_path())) {
93
      if (version_compare($version, $admin_version, '!=')) {
94
        $version = $admin_version;
95
      }
96
    }
97 85ad3d82 Assos Assos
98 503b3f7b Assos Assos
    // 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 85ad3d82 Assos Assos
108 503b3f7b Assos Assos
    // 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 85ad3d82 Assos Assos
114 503b3f7b Assos Assos
    // 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 85ad3d82 Assos Assos
119 503b3f7b Assos Assos
    // 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 85ad3d82 Assos Assos
  }
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 503b3f7b Assos Assos
 * Admin settings menu callback.
153
 *
154
 * @see jquery_update_menu()
155 85ad3d82 Assos Assos
 */
156
function jquery_update_settings_form() {
157 503b3f7b Assos Assos
  $form['version_options'] = array(
158
    '#type' => 'fieldset',
159
    '#title' => t('Version options'),
160
  );
161
162
  $form['version_options']['jquery_update_jquery_version'] = array(
163 85ad3d82 Assos Assos
    '#type' => 'select',
164 503b3f7b Assos Assos
    '#title' => t('Default jQuery Version'),
165 85ad3d82 Assos Assos
    '#options' => array(
166
      '1.5' => '1.5',
167
      '1.7' => '1.7',
168
      '1.8' => '1.8',
169 503b3f7b Assos Assos
      '1.9' => '1.9',
170
      '1.10' => '1.10',
171 85ad3d82 Assos Assos
    ),
172 503b3f7b Assos Assos
    '#default_value' => variable_get('jquery_update_jquery_version', '1.10'),
173
    '#description' => t('Select which jQuery version to use by default.'),
174 85ad3d82 Assos Assos
  );
175 503b3f7b Assos Assos
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 85ad3d82 Assos Assos
  $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 503b3f7b Assos Assos
 * @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 85ad3d82 Assos Assos
 * @param string $version
231
 *   The version of jQuery to use.
232
 */
233
function jquery_update_jquery_replace(&$javascript, $cdn, $path, $min, $version) {
234 503b3f7b Assos Assos
  // 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 85ad3d82 Assos Assos
  // 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 503b3f7b Assos Assos
249 85ad3d82 Assos Assos
    case '1.7':
250
      $trueversion = '1.7.1';
251
      break;
252 503b3f7b Assos Assos
253 85ad3d82 Assos Assos
    case '1.8':
254
      $trueversion = '1.8.2';
255
      break;
256 503b3f7b Assos Assos
257
    case '1.9':
258
      $trueversion = '1.9.1';
259
      break;
260
261
    case '1.10':
262
      $trueversion = '1.10.2';
263
      break;
264 85ad3d82 Assos Assos
  }
265
  $javascript['jquery']['version'] = $trueversion;
266
267
  // Check for CDN support.
268 503b3f7b Assos Assos
  switch ($cdn) {
269 85ad3d82 Assos Assos
    case 'google':
270 503b3f7b Assos Assos
      $javascript['jquery']['js']['misc/jquery.js']['data'] = 'https://ajax.googleapis.com/ajax/libs/jquery/' . $trueversion . '/jquery' . $min . '.js';
271 85ad3d82 Assos Assos
      $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external';
272
      jquery_update_jquery_backup($javascript, $path, $min, $version);
273
      break;
274 503b3f7b Assos Assos
275 85ad3d82 Assos Assos
    case 'microsoft':
276 503b3f7b Assos Assos
      $javascript['jquery']['js']['misc/jquery.js']['data'] = 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-' . $trueversion . $min . '.js';
277 85ad3d82 Assos Assos
      $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external';
278
      jquery_update_jquery_backup($javascript, $path, $min, $version);
279
      break;
280 503b3f7b Assos Assos
281 85ad3d82 Assos Assos
    case 'jquery':
282 503b3f7b Assos Assos
      $javascript['jquery']['js']['misc/jquery.js']['data'] = 'http://code.jquery.com/jquery-' . $trueversion . $min . '.js';
283 85ad3d82 Assos Assos
      $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external';
284
      jquery_update_jquery_backup($javascript, $path, $min, $version);
285
      break;
286 503b3f7b Assos Assos
287 85ad3d82 Assos Assos
    case 'none':
288
    default:
289 503b3f7b Assos Assos
      $javascript['jquery']['js']['misc/jquery.js']['data'] = $path . '/replace/jquery/' . $version . '/jquery' . $min . '.js';
290 85ad3d82 Assos Assos
      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 503b3f7b Assos Assos
 *   The '.min' to include in the file name if we are requesting a minified
303
 *   version.
304 85ad3d82 Assos Assos
 * @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 503b3f7b Assos Assos
    'data' => 'window.jQuery || document.write("<script src=\'' . base_path() . $path . '/replace/jquery/' . $version . '/jquery' . $min . '.js\'>\x3C/script>")',
310 85ad3d82 Assos Assos
    '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 503b3f7b Assos Assos
 * @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 85ad3d82 Assos Assos
 */
332
function jquery_update_jqueryui_replace(&$javascript, $cdn, $path, $min) {
333 503b3f7b Assos Assos
  // 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 85ad3d82 Assos Assos
  // Replace all CSS files.
362
  $names = drupal_map_assoc(array(
363 503b3f7b Assos Assos
    '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 85ad3d82 Assos Assos
  ));
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 503b3f7b Assos Assos
    '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 85ad3d82 Assos Assos
  ));
384
  $names['ui'] = 'ui.core';
385 503b3f7b Assos Assos
  $names['effects'] = 'ui.effect';
386 85ad3d82 Assos Assos
387 503b3f7b Assos Assos
  switch ($cdn) {
388 85ad3d82 Assos Assos
    case 'google':
389 503b3f7b Assos Assos
      $cdn = 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui' . $min . '.js';
390 85ad3d82 Assos Assos
      jquery_update_jqueryui_cdn($cdn, $javascript, $path, $min, $names);
391
      jquery_update_jqueryui_backup($javascript, $path, $min);
392
      break;
393 503b3f7b Assos Assos
394 85ad3d82 Assos Assos
    case 'microsoft':
395 503b3f7b Assos Assos
      $cdn = 'http://ajax.aspnetcdn.com/ajax/jquery.ui/1.10.2/jquery-ui' . $min . '.js';
396 85ad3d82 Assos Assos
      jquery_update_jqueryui_cdn($cdn, $javascript, $path, $min, $names);
397
      jquery_update_jqueryui_backup($javascript, $path, $min);
398
      break;
399 503b3f7b Assos Assos
400 85ad3d82 Assos Assos
    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 503b3f7b Assos Assos
 *   The '.min' to include in the file name if we are requesting a minified
415
 *   version.
416 85ad3d82 Assos Assos
 */
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 503b3f7b Assos Assos
 * @param string $cdn
431
 *   The name of the CDN option to use. Possible options are:
432
 *   - none
433
 *   - google
434
 *   - microsoft
435 85ad3d82 Assos Assos
 * @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 503b3f7b Assos Assos
 *   The '.min' to include in the file name if we are requesting a minified
441
 *   version.
442
 * * @param array $names
443 85ad3d82 Assos Assos
 *   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 503b3f7b Assos Assos
    $javascript[$name]['version'] = '1.10.2';
454 85ad3d82 Assos Assos
  }
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 503b3f7b Assos Assos
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 85ad3d82 Assos Assos
}
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 503b3f7b Assos Assos
 *   The '.min' to include in the file name if we are requesting a minified
481
 *   version.
482 85ad3d82 Assos Assos
 * @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 503b3f7b Assos Assos
    $javascript[$name]['version'] = '1.10.2';
493 85ad3d82 Assos Assos
  }
494
}