jquery_update.module~ 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. '1.8' => '1.11',
  113. ),
  114. '#default_value' => variable_get('jquery_update_jquery_version', '1.5'),
  115. '#description' => t('Select which jQuery version branch to use.'),
  116. );
  117. $form['jquery_update_compression_type'] = array(
  118. '#type' => 'radios',
  119. '#title' => t('jQuery compression level'),
  120. '#options' => array(
  121. 'min' => t('Production (minified)'),
  122. 'none' => t('Development (uncompressed)'),
  123. ),
  124. '#default_value' => variable_get('jquery_update_compression_type', 'min'),
  125. );
  126. $form['jquery_update_jquery_cdn'] = array(
  127. '#type' => 'select',
  128. '#title' => t('jQuery and jQuery UI CDN'),
  129. '#options' => array(
  130. 'none' => t('None'),
  131. 'google' => t('Google'),
  132. 'microsoft' => t('Microsoft'),
  133. 'jquery' => t('jQuery'),
  134. ),
  135. '#default_value' => variable_get('jquery_update_jquery_cdn', 'none'),
  136. '#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.'),
  137. );
  138. return system_settings_form($form);
  139. }
  140. /**
  141. * Update jQuery to the CDN or local path.
  142. *
  143. * @param array $javascript
  144. * The library definition array as seen in hook_library_alter().
  145. * @param string $cdn
  146. * The name of the CDN option to use. Possible options are:
  147. * - none
  148. * - google
  149. * - microsoft
  150. * @param string $version
  151. * The version of jQuery to use.
  152. */
  153. function jquery_update_jquery_replace(&$javascript, $cdn, $path, $min, $version) {
  154. // Make sure to use the latest version in given branch.
  155. $trueversion = NULL;
  156. switch ($version) {
  157. case '1.5':
  158. $trueversion = '1.5.2';
  159. break;
  160. case '1.7':
  161. $trueversion = '1.7.1';
  162. break;
  163. case '1.8':
  164. $trueversion = '1.8.2';
  165. break;
  166. case '1.8':
  167. $trueversion = '1.11.1';
  168. break;
  169. }
  170. $javascript['jquery']['version'] = $trueversion;
  171. // Check for CDN support.
  172. switch($cdn) {
  173. case 'google':
  174. $javascript['jquery']['js']['misc/jquery.js']['data'] = 'https://ajax.googleapis.com/ajax/libs/jquery/'. $trueversion . '/jquery' . $min . '.js';
  175. $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external';
  176. jquery_update_jquery_backup($javascript, $path, $min, $version);
  177. break;
  178. case 'microsoft':
  179. $javascript['jquery']['js']['misc/jquery.js']['data'] = 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-'. $trueversion . $min . '.js';
  180. $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external';
  181. jquery_update_jquery_backup($javascript, $path, $min, $version);
  182. break;
  183. case 'jquery':
  184. $javascript['jquery']['js']['misc/jquery.js']['data'] = 'http://code.jquery.com/jquery-'. $trueversion . $min . '.js';
  185. $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external';
  186. jquery_update_jquery_backup($javascript, $path, $min, $version);
  187. break;
  188. case 'none':
  189. default:
  190. $javascript['jquery']['js']['misc/jquery.js']['data'] = $path . '/replace/jquery/'. $version . '/jquery' . $min . '.js';
  191. break;
  192. }
  193. }
  194. /**
  195. * Add the local fallback in case jQuery from the CDN is unavailable.
  196. *
  197. * @param array $javascript
  198. * The $libraries array as seen in hook_library_alter()
  199. * @param string $path
  200. * The path to the module where replacements can be found.
  201. * @param string $min
  202. * The '.min' to include in the file name if we are requesting a minified version.
  203. * @param string $version
  204. * The verison of jQuery to use.
  205. */
  206. function jquery_update_jquery_backup(&$javascript, $path, $min, $version) {
  207. $javascript['jquery']['js'][] = array(
  208. 'data' => 'window.jQuery || document.write("<script src=\'' . base_path() . $path . '/replace/jquery/'. $version . '/jquery' . $min . '.js\'>\x3C/script>")',
  209. 'type' => 'inline',
  210. 'group' => JS_LIBRARY,
  211. 'weight' => -19.999999999,
  212. );
  213. }
  214. /**
  215. * Update jQuery UI to the CDN or local path.
  216. *
  217. * @param array $javascript
  218. * The library definition array as seen in hook_library_alter().
  219. * @param string $cdn
  220. * The name of the CDN option to use. Possible options are:
  221. * - none
  222. * - google
  223. * - microsoft
  224. */
  225. function jquery_update_jqueryui_replace(&$javascript, $cdn, $path, $min) {
  226. // Replace all CSS files.
  227. $names = drupal_map_assoc(array(
  228. 'ui.accordion', 'ui.autocomplete', 'ui.button', 'ui.datepicker',
  229. 'ui.dialog', 'ui.progressbar', 'ui.resizable', 'ui.selectable',
  230. 'ui.slider', 'ui.tabs',
  231. ));
  232. $names['ui'] = 'ui.core';
  233. $csspath = $path . '/replace/ui/themes/base/' . (($min == '.min') ? 'minified/' : '');
  234. foreach ($names as $name => $file) {
  235. $javascript[$name]['css']["misc/ui/jquery.$file.css"]['data'] = $csspath . 'jquery.' . $file . $min . '.css';
  236. }
  237. // Make sure ui.theme is replaced as well.
  238. $javascript['ui']['css']['misc/ui/jquery.ui.theme.css']['data'] = $csspath . 'jquery.ui.theme' . $min . '.css';
  239. // Replace jQuery UI's JavaScript, beginning by defining the mapping.
  240. $names = drupal_map_assoc(array(
  241. 'ui.accordion', 'ui.autocomplete', 'ui.button', 'ui.datepicker',
  242. 'ui.dialog', 'ui.draggable', 'ui.droppable', 'ui.mouse', 'ui.position',
  243. 'ui.progressbar', 'ui.resizable', 'ui.selectable', 'ui.slider',
  244. 'ui.sortable', 'ui.tabs', 'ui.widget', 'effects.blind', 'effects.bounce',
  245. 'effects.clip', 'effects.drop', 'effects.explode', 'effects.fade',
  246. 'effects.fold', 'effects.highlight', 'effects.pulsate', 'effects.scale',
  247. 'effects.shake', 'effects.slide', 'effects.transfer',
  248. ));
  249. $names['ui'] = 'ui.core';
  250. $names['effects'] = 'effects.core';
  251. switch($cdn) {
  252. case 'google':
  253. $cdn = 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui' . $min . '.js';
  254. jquery_update_jqueryui_cdn($cdn, $javascript, $path, $min, $names);
  255. jquery_update_jqueryui_backup($javascript, $path, $min);
  256. break;
  257. case 'microsoft':
  258. $cdn = 'http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.11/jquery-ui' . $min . '.js';
  259. jquery_update_jqueryui_cdn($cdn, $javascript, $path, $min, $names);
  260. jquery_update_jqueryui_backup($javascript, $path, $min);
  261. break;
  262. case 'none':
  263. jquery_update_jqueryui_local($javascript, $path, $min, $names);
  264. break;
  265. }
  266. }
  267. /**
  268. * Add the local fallback in case jQuery UI from the CDN is unavailable.
  269. *
  270. * @param array $javascript
  271. * The $libraries array as seen in hook_library_alter()
  272. * @param string $path
  273. * The path to the module where replacements can be found.
  274. * @param string $min
  275. * The '.min' to include in the file name if we are requesting a minified version.
  276. */
  277. function jquery_update_jqueryui_backup(&$javascript, $path, $min) {
  278. $js_path = ($min == '.min') ? '/replace/ui/ui/minified/jquery-ui.min.js' : '/replace/ui/ui/jquery-ui.js';
  279. $javascript['ui']['js'][] = array(
  280. 'data' => 'window.jQuery.ui || document.write("<script src=\'' . base_path() . $path . $js_path . '\'>\x3C/script>")',
  281. 'type' => 'inline',
  282. 'group' => JS_LIBRARY,
  283. 'weight' => -10.999999999,
  284. );
  285. }
  286. /**
  287. * Handle when jQuery UI is updated to the cdn version.
  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. * @param array $names
  296. * An array mapping jquery ui parts to their file names.
  297. */
  298. function jquery_update_jqueryui_cdn($cdn, &$javascript, $path, $min, $names) {
  299. // Construct the jQuery UI path and replace the JavaScript.
  300. $jspath = $path . '/replace/ui/ui/' . ($min == '.min' ? 'minified/' : '');
  301. foreach ($names as $name => $file) {
  302. $corefile = 'misc/ui/jquery.' . $file . '.min.js';
  303. // Remove the core files.
  304. unset($javascript[$name]['js'][$corefile]);
  305. $javascript[$name]['version'] = '1.8.11';
  306. }
  307. // UI is used by all of UI. Add the js cdn here.
  308. $javascript['ui']['js'][$cdn] = array(
  309. 'data' => $cdn,
  310. 'type' => 'external',
  311. 'group' => JS_LIBRARY,
  312. 'weight' => -11,
  313. );
  314. }
  315. /**
  316. * Handle when jQuery UI is updated to the local version.
  317. *
  318. * @param array $javascript
  319. * The $libraries array as seen in hook_library_alter()
  320. * @param string $path
  321. * The path to the module where replacements can be found.
  322. * @param string $min
  323. * The '.min' to include in the file name if we are requesting a minified version.
  324. * @param array $names
  325. * An array mapping jquery ui parts to their file names.
  326. */
  327. function jquery_update_jqueryui_local(&$javascript, $path, $min, $names) {
  328. // Construct the jQuery UI path and replace the JavaScript.
  329. $jspath = $path . '/replace/ui/ui/' . ($min == '.min' ? 'minified/' : '');
  330. foreach ($names as $name => $file) {
  331. $corefile = 'misc/ui/jquery.' . $file . '.min.js';
  332. $javascript[$name]['js'][$corefile]['data'] = $jspath . 'jquery.' . $file . $min . '.js';
  333. $javascript[$name]['version'] = '1.8.11';
  334. }
  335. }