jquery_update.module 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /**
  3. * @file
  4. * Updates Drupal to use the latest version of jQuery.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function jquery_update_help($path, $arg) {
  10. switch ($path) {
  11. // Help for another path in the block module
  12. case 'admin/config/development/jquery_update':
  13. 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(
  14. '@jquery' => 'http://jquery.com',
  15. )) . '</p>';
  16. }
  17. }
  18. /**
  19. * Implements hook_library().
  20. */
  21. function jquery_update_library() {
  22. // Register libraries available in the external directory.
  23. $path = drupal_get_path('module', 'jquery_update') . '/ui/external';
  24. $libraries['qunit'] = array(
  25. 'title' => 'QUnit',
  26. 'js' => array(
  27. $path . '/qunit.js' => array(
  28. 'group' => JS_LIBRARY,
  29. 'weight' => 2,
  30. ),
  31. ),
  32. 'css' => array(
  33. $path . '/qunit.css' => array(),
  34. ),
  35. );
  36. $libraries['jquery.metadata'] = array(
  37. 'title' => 'QUnit',
  38. 'js' => array(
  39. $path . '/jquery.metadata.js' => array(
  40. 'group' => JS_LIBRARY,
  41. 'weight' => 2,
  42. ),
  43. ),
  44. 'version' => '4187',
  45. );
  46. $libraries['jquery.bgiframe'] = array(
  47. 'title' => 'bgiframe',
  48. 'website' => 'http://docs.jquery.com/Plugins/bgiframe',
  49. 'js' => array(
  50. $path . '/jquery.bgiframe.js' => array(
  51. 'group' => JS_LIBRARY,
  52. 'weight' => 2,
  53. ),
  54. ),
  55. 'version' => '2.1.2',
  56. );
  57. return $libraries;
  58. }
  59. /**
  60. * Implementation of hook_library_alter().
  61. */
  62. function jquery_update_library_alter(&$javascript, $module) {
  63. // We are updating just the system module. For all other cases we return.
  64. if ($module != 'system') {
  65. return;
  66. }
  67. $path = drupal_get_path('module', 'jquery_update');
  68. // Make sure we inject either the minified or uncompressed version as desired.
  69. $min = variable_get('jquery_update_compression_type', 'min') == 'none' ? '' : '.min';
  70. $cdn = variable_get('jquery_update_jquery_cdn', 'none');
  71. // Replace jQuery with the latest version.
  72. $version = variable_get('jquery_update_jquery_version', '1.5');
  73. jquery_update_jquery_replace($javascript, $cdn, $path, $min, $version);
  74. // Replace jQuery UI with CDN or local files. If from a CDN include all of jQuery UI.
  75. jquery_update_jqueryui_replace($javascript, $cdn, $path, $min);
  76. // Replace the jQuery Cookie plugin.
  77. $javascript['cookie']['js']['misc/jquery.cookie.js']['data'] = $path . '/replace/ui/external/jquery.cookie.js';
  78. // Noting the version based on git commit as no version number is available.
  79. $javascript['cookie']['version'] = '67fb34f6a866c40d0570';
  80. // Replace jQuery Form plugin.
  81. $javascript['jquery.form']['js']['misc/jquery.form.js']['data'] = $path . '/replace/misc/jquery.form' . $min . '.js';
  82. $javascript['jquery.form']['version'] = '2.69';
  83. // Replace files for jQuery 1.7 and up
  84. if (version_compare($version, '1.7', '>=')) {
  85. $javascript['drupal.states']['js']['misc/states.js']['data'] = $path . '/replace/misc/1.7/states.js';
  86. }
  87. }
  88. /**
  89. * Implements hook_menu().
  90. */
  91. function jquery_update_menu() {
  92. $items['admin/config/development/jquery_update'] = array(
  93. 'title' => 'jQuery update',
  94. 'description' => 'Configure settings related to the jQuery upgrade, the library path and compression.',
  95. 'page callback' => 'drupal_get_form',
  96. 'page arguments' => array('jquery_update_settings_form'),
  97. 'access arguments' => array('administer site configuration'),
  98. );
  99. return $items;
  100. }
  101. /**
  102. * Implementation of hook_form_FORM_ID().
  103. */
  104. function jquery_update_settings_form() {
  105. $form['jquery_update_jquery_version'] = array(
  106. '#type' => 'select',
  107. '#title' => t('jQuery Version'),
  108. '#options' => array(
  109. '1.5' => '1.5',
  110. '1.7' => '1.7',
  111. '1.8' => '1.8',
  112. ),
  113. '#default_value' => variable_get('jquery_update_jquery_version', '1.5'),
  114. '#description' => t('Select which jQuery version branch to use.'),
  115. );
  116. $form['jquery_update_compression_type'] = array(
  117. '#type' => 'radios',
  118. '#title' => t('jQuery compression level'),
  119. '#options' => array(
  120. 'min' => t('Production (minified)'),
  121. 'none' => t('Development (uncompressed)'),
  122. ),
  123. '#default_value' => variable_get('jquery_update_compression_type', 'min'),
  124. );
  125. $form['jquery_update_jquery_cdn'] = array(
  126. '#type' => 'select',
  127. '#title' => t('jQuery and jQuery UI CDN'),
  128. '#options' => array(
  129. 'none' => t('None'),
  130. 'google' => t('Google'),
  131. 'microsoft' => t('Microsoft'),
  132. 'jquery' => t('jQuery'),
  133. ),
  134. '#default_value' => variable_get('jquery_update_jquery_cdn', 'none'),
  135. '#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.'),
  136. );
  137. return system_settings_form($form);
  138. }
  139. /**
  140. * Update jQuery to the CDN or local path.
  141. *
  142. * @param array $javascript
  143. * The library definition array as seen in hook_library_alter().
  144. * @param string $cdn
  145. * The name of the CDN option to use. Possible options are:
  146. * - none
  147. * - google
  148. * - microsoft
  149. * @param string $version
  150. * The version of jQuery to use.
  151. */
  152. function jquery_update_jquery_replace(&$javascript, $cdn, $path, $min, $version) {
  153. // Make sure to use the latest version in given branch.
  154. $trueversion = NULL;
  155. switch ($version) {
  156. case '1.5':
  157. $trueversion = '1.5.2';
  158. break;
  159. case '1.7':
  160. $trueversion = '1.7.1';
  161. break;
  162. case '1.8':
  163. $trueversion = '1.8.2';
  164. break;
  165. }
  166. $javascript['jquery']['version'] = $trueversion;
  167. // Check for CDN support.
  168. switch($cdn) {
  169. case 'google':
  170. $javascript['jquery']['js']['misc/jquery.js']['data'] = 'https://ajax.googleapis.com/ajax/libs/jquery/'. $trueversion . '/jquery' . $min . '.js';
  171. $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external';
  172. jquery_update_jquery_backup($javascript, $path, $min, $version);
  173. break;
  174. case 'microsoft':
  175. $javascript['jquery']['js']['misc/jquery.js']['data'] = 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-'. $trueversion . $min . '.js';
  176. $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external';
  177. jquery_update_jquery_backup($javascript, $path, $min, $version);
  178. break;
  179. case 'jquery':
  180. $javascript['jquery']['js']['misc/jquery.js']['data'] = 'http://code.jquery.com/jquery-'. $trueversion . $min . '.js';
  181. $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external';
  182. jquery_update_jquery_backup($javascript, $path, $min, $version);
  183. break;
  184. case 'none':
  185. default:
  186. $javascript['jquery']['js']['misc/jquery.js']['data'] = $path . '/replace/jquery/'. $version . '/jquery' . $min . '.js';
  187. break;
  188. }
  189. }
  190. /**
  191. * Add the local fallback in case jQuery from the CDN is unavailable.
  192. *
  193. * @param array $javascript
  194. * The $libraries array as seen in hook_library_alter()
  195. * @param string $path
  196. * The path to the module where replacements can be found.
  197. * @param string $min
  198. * The '.min' to include in the file name if we are requesting a minified version.
  199. * @param string $version
  200. * The verison of jQuery to use.
  201. */
  202. function jquery_update_jquery_backup(&$javascript, $path, $min, $version) {
  203. $javascript['jquery']['js'][] = array(
  204. 'data' => 'window.jQuery || document.write("<script src=\'' . base_path() . $path . '/replace/jquery/'. $version . '/jquery' . $min . '.js\'>\x3C/script>")',
  205. 'type' => 'inline',
  206. 'group' => JS_LIBRARY,
  207. 'weight' => -19.999999999,
  208. );
  209. }
  210. /**
  211. * Update jQuery UI to the CDN or local path.
  212. *
  213. * @param array $javascript
  214. * The library definition array as seen in hook_library_alter().
  215. * @param string $cdn
  216. * The name of the CDN option to use. Possible options are:
  217. * - none
  218. * - google
  219. * - microsoft
  220. */
  221. function jquery_update_jqueryui_replace(&$javascript, $cdn, $path, $min) {
  222. // Replace all CSS files.
  223. $names = drupal_map_assoc(array(
  224. 'ui.accordion', 'ui.autocomplete', 'ui.button', 'ui.datepicker',
  225. 'ui.dialog', 'ui.progressbar', 'ui.resizable', 'ui.selectable',
  226. 'ui.slider', 'ui.tabs',
  227. ));
  228. $names['ui'] = 'ui.core';
  229. $csspath = $path . '/replace/ui/themes/base/' . (($min == '.min') ? 'minified/' : '');
  230. foreach ($names as $name => $file) {
  231. $javascript[$name]['css']["misc/ui/jquery.$file.css"]['data'] = $csspath . 'jquery.' . $file . $min . '.css';
  232. }
  233. // Make sure ui.theme is replaced as well.
  234. $javascript['ui']['css']['misc/ui/jquery.ui.theme.css']['data'] = $csspath . 'jquery.ui.theme' . $min . '.css';
  235. // Replace jQuery UI's JavaScript, beginning by defining the mapping.
  236. $names = drupal_map_assoc(array(
  237. 'ui.accordion', 'ui.autocomplete', 'ui.button', 'ui.datepicker',
  238. 'ui.dialog', 'ui.draggable', 'ui.droppable', 'ui.mouse', 'ui.position',
  239. 'ui.progressbar', 'ui.resizable', 'ui.selectable', 'ui.slider',
  240. 'ui.sortable', 'ui.tabs', 'ui.widget', 'effects.blind', 'effects.bounce',
  241. 'effects.clip', 'effects.drop', 'effects.explode', 'effects.fade',
  242. 'effects.fold', 'effects.highlight', 'effects.pulsate', 'effects.scale',
  243. 'effects.shake', 'effects.slide', 'effects.transfer',
  244. ));
  245. $names['ui'] = 'ui.core';
  246. $names['effects'] = 'effects.core';
  247. switch($cdn) {
  248. case 'google':
  249. $cdn = 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui' . $min . '.js';
  250. jquery_update_jqueryui_cdn($cdn, $javascript, $path, $min, $names);
  251. jquery_update_jqueryui_backup($javascript, $path, $min);
  252. break;
  253. case 'microsoft':
  254. $cdn = 'http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.11/jquery-ui' . $min . '.js';
  255. jquery_update_jqueryui_cdn($cdn, $javascript, $path, $min, $names);
  256. jquery_update_jqueryui_backup($javascript, $path, $min);
  257. break;
  258. case 'none':
  259. jquery_update_jqueryui_local($javascript, $path, $min, $names);
  260. break;
  261. }
  262. }
  263. /**
  264. * Add the local fallback in case jQuery UI from the CDN is unavailable.
  265. *
  266. * @param array $javascript
  267. * The $libraries array as seen in hook_library_alter()
  268. * @param string $path
  269. * The path to the module where replacements can be found.
  270. * @param string $min
  271. * The '.min' to include in the file name if we are requesting a minified version.
  272. */
  273. function jquery_update_jqueryui_backup(&$javascript, $path, $min) {
  274. $js_path = ($min == '.min') ? '/replace/ui/ui/minified/jquery-ui.min.js' : '/replace/ui/ui/jquery-ui.js';
  275. $javascript['ui']['js'][] = array(
  276. 'data' => 'window.jQuery.ui || document.write("<script src=\'' . base_path() . $path . $js_path . '\'>\x3C/script>")',
  277. 'type' => 'inline',
  278. 'group' => JS_LIBRARY,
  279. 'weight' => -10.999999999,
  280. );
  281. }
  282. /**
  283. * Handle when jQuery UI is updated to the cdn version.
  284. *
  285. * @param array $javascript
  286. * The $libraries array as seen in hook_library_alter()
  287. * @param string $path
  288. * The path to the module where replacements can be found.
  289. * @param string $min
  290. * The '.min' to include in the file name if we are requesting a minified version.
  291. * @param array $names
  292. * An array mapping jquery ui parts to their file names.
  293. */
  294. function jquery_update_jqueryui_cdn($cdn, &$javascript, $path, $min, $names) {
  295. // Construct the jQuery UI path and replace the JavaScript.
  296. $jspath = $path . '/replace/ui/ui/' . ($min == '.min' ? 'minified/' : '');
  297. foreach ($names as $name => $file) {
  298. $corefile = 'misc/ui/jquery.' . $file . '.min.js';
  299. // Remove the core files.
  300. unset($javascript[$name]['js'][$corefile]);
  301. $javascript[$name]['version'] = '1.8.11';
  302. }
  303. // UI is used by all of UI. Add the js cdn here.
  304. $javascript['ui']['js'][$cdn] = array(
  305. 'data' => $cdn,
  306. 'type' => 'external',
  307. 'group' => JS_LIBRARY,
  308. 'weight' => -11,
  309. );
  310. }
  311. /**
  312. * Handle when jQuery UI is updated to the local version.
  313. *
  314. * @param array $javascript
  315. * The $libraries array as seen in hook_library_alter()
  316. * @param string $path
  317. * The path to the module where replacements can be found.
  318. * @param string $min
  319. * The '.min' to include in the file name if we are requesting a minified version.
  320. * @param array $names
  321. * An array mapping jquery ui parts to their file names.
  322. */
  323. function jquery_update_jqueryui_local(&$javascript, $path, $min, $names) {
  324. // Construct the jQuery UI path and replace the JavaScript.
  325. $jspath = $path . '/replace/ui/ui/' . ($min == '.min' ? 'minified/' : '');
  326. foreach ($names as $name => $file) {
  327. $corefile = 'misc/ui/jquery.' . $file . '.min.js';
  328. $javascript[$name]['js'][$corefile]['data'] = $jspath . 'jquery.' . $file . $min . '.js';
  329. $javascript[$name]['version'] = '1.8.11';
  330. }
  331. }