ajax.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. (function ($) {
  2. /**
  3. * Provides Ajax page updating via jQuery $.ajax (Asynchronous JavaScript and XML).
  4. *
  5. * Ajax is a method of making a request via JavaScript while viewing an HTML
  6. * page. The request returns an array of commands encoded in JSON, which is
  7. * then executed to make any changes that are necessary to the page.
  8. *
  9. * Drupal uses this file to enhance form elements with #ajax['path'] and
  10. * #ajax['wrapper'] properties. If set, this file will automatically be included
  11. * to provide Ajax capabilities.
  12. */
  13. Drupal.ajax = Drupal.ajax || {};
  14. /**
  15. * Attaches the Ajax behavior to each Ajax form element.
  16. */
  17. Drupal.behaviors.AJAX = {
  18. attach: function (context, settings) {
  19. // Load all Ajax behaviors specified in the settings.
  20. for (var base in settings.ajax) {
  21. if (!$('#' + base + '.ajax-processed').length) {
  22. var element_settings = settings.ajax[base];
  23. if (typeof element_settings.selector == 'undefined') {
  24. element_settings.selector = '#' + base;
  25. }
  26. $(element_settings.selector).each(function () {
  27. element_settings.element = this;
  28. Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
  29. });
  30. $('#' + base).addClass('ajax-processed');
  31. }
  32. }
  33. // Bind Ajax behaviors to all items showing the class.
  34. $('.use-ajax:not(.ajax-processed)').addClass('ajax-processed').each(function () {
  35. var element_settings = {};
  36. // Clicked links look better with the throbber than the progress bar.
  37. element_settings.progress = { 'type': 'throbber' };
  38. // For anchor tags, these will go to the target of the anchor rather
  39. // than the usual location.
  40. if ($(this).attr('href')) {
  41. element_settings.url = $(this).attr('href');
  42. element_settings.event = 'click';
  43. }
  44. var base = $(this).attr('id');
  45. Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
  46. });
  47. // This class means to submit the form to the action using Ajax.
  48. $('.use-ajax-submit:not(.ajax-processed)').addClass('ajax-processed').each(function () {
  49. var element_settings = {};
  50. // Ajax submits specified in this manner automatically submit to the
  51. // normal form action.
  52. element_settings.url = $(this.form).attr('action');
  53. // Form submit button clicks need to tell the form what was clicked so
  54. // it gets passed in the POST request.
  55. element_settings.setClick = true;
  56. // Form buttons use the 'click' event rather than mousedown.
  57. element_settings.event = 'click';
  58. // Clicked form buttons look better with the throbber than the progress bar.
  59. element_settings.progress = { 'type': 'throbber' };
  60. var base = $(this).attr('id');
  61. Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
  62. });
  63. }
  64. };
  65. /**
  66. * Ajax object.
  67. *
  68. * All Ajax objects on a page are accessible through the global Drupal.ajax
  69. * object and are keyed by the submit button's ID. You can access them from
  70. * your module's JavaScript file to override properties or functions.
  71. *
  72. * For example, if your Ajax enabled button has the ID 'edit-submit', you can
  73. * redefine the function that is called to insert the new content like this
  74. * (inside a Drupal.behaviors attach block):
  75. * @code
  76. * Drupal.behaviors.myCustomAJAXStuff = {
  77. * attach: function (context, settings) {
  78. * Drupal.ajax['edit-submit'].commands.insert = function (ajax, response, status) {
  79. * new_content = $(response.data);
  80. * $('#my-wrapper').append(new_content);
  81. * alert('New content was appended to #my-wrapper');
  82. * }
  83. * }
  84. * };
  85. * @endcode
  86. */
  87. Drupal.ajax = function (base, element, element_settings) {
  88. var defaults = {
  89. url: 'system/ajax',
  90. event: 'mousedown',
  91. keypress: true,
  92. selector: '#' + base,
  93. effect: 'none',
  94. speed: 'none',
  95. method: 'replaceWith',
  96. progress: {
  97. type: 'throbber',
  98. message: Drupal.t('Please wait...')
  99. },
  100. submit: {
  101. 'js': true
  102. }
  103. };
  104. $.extend(this, defaults, element_settings);
  105. this.element = element;
  106. this.element_settings = element_settings;
  107. // Replacing 'nojs' with 'ajax' in the URL allows for an easy method to let
  108. // the server detect when it needs to degrade gracefully.
  109. // There are five scenarios to check for:
  110. // 1. /nojs/
  111. // 2. /nojs$ - The end of a URL string.
  112. // 3. /nojs? - Followed by a query (with clean URLs enabled).
  113. // E.g.: path/nojs?destination=foobar
  114. // 4. /nojs& - Followed by a query (without clean URLs enabled).
  115. // E.g.: ?q=path/nojs&destination=foobar
  116. // 5. /nojs# - Followed by a fragment.
  117. // E.g.: path/nojs#myfragment
  118. this.url = element_settings.url.replace(/\/nojs(\/|$|\?|&|#)/g, '/ajax$1');
  119. this.wrapper = '#' + element_settings.wrapper;
  120. // If there isn't a form, jQuery.ajax() will be used instead, allowing us to
  121. // bind Ajax to links as well.
  122. if (this.element.form) {
  123. this.form = $(this.element.form);
  124. }
  125. // Set the options for the ajaxSubmit function.
  126. // The 'this' variable will not persist inside of the options object.
  127. var ajax = this;
  128. ajax.options = {
  129. url: ajax.url,
  130. data: ajax.submit,
  131. beforeSerialize: function (element_settings, options) {
  132. return ajax.beforeSerialize(element_settings, options);
  133. },
  134. beforeSubmit: function (form_values, element_settings, options) {
  135. ajax.ajaxing = true;
  136. return ajax.beforeSubmit(form_values, element_settings, options);
  137. },
  138. beforeSend: function (xmlhttprequest, options) {
  139. ajax.ajaxing = true;
  140. return ajax.beforeSend(xmlhttprequest, options);
  141. },
  142. success: function (response, status) {
  143. // Sanity check for browser support (object expected).
  144. // When using iFrame uploads, responses must be returned as a string.
  145. if (typeof response == 'string') {
  146. response = $.parseJSON(response);
  147. }
  148. return ajax.success(response, status);
  149. },
  150. complete: function (response, status) {
  151. ajax.ajaxing = false;
  152. if (status == 'error' || status == 'parsererror') {
  153. return ajax.error(response, ajax.url);
  154. }
  155. },
  156. dataType: 'json',
  157. type: 'POST'
  158. };
  159. // Bind the ajaxSubmit function to the element event.
  160. $(ajax.element).bind(element_settings.event, function (event) {
  161. return ajax.eventResponse(this, event);
  162. });
  163. // If necessary, enable keyboard submission so that Ajax behaviors
  164. // can be triggered through keyboard input as well as e.g. a mousedown
  165. // action.
  166. if (element_settings.keypress) {
  167. $(ajax.element).keypress(function (event) {
  168. return ajax.keypressResponse(this, event);
  169. });
  170. }
  171. // If necessary, prevent the browser default action of an additional event.
  172. // For example, prevent the browser default action of a click, even if the
  173. // AJAX behavior binds to mousedown.
  174. if (element_settings.prevent) {
  175. $(ajax.element).bind(element_settings.prevent, false);
  176. }
  177. };
  178. /**
  179. * Handle a key press.
  180. *
  181. * The Ajax object will, if instructed, bind to a key press response. This
  182. * will test to see if the key press is valid to trigger this event and
  183. * if it is, trigger it for us and prevent other keypresses from triggering.
  184. * In this case we're handling RETURN and SPACEBAR keypresses (event codes 13
  185. * and 32. RETURN is often used to submit a form when in a textfield, and
  186. * SPACE is often used to activate an element without submitting.
  187. */
  188. Drupal.ajax.prototype.keypressResponse = function (element, event) {
  189. // Create a synonym for this to reduce code confusion.
  190. var ajax = this;
  191. // Detect enter key and space bar and allow the standard response for them,
  192. // except for form elements of type 'text' and 'textarea', where the
  193. // spacebar activation causes inappropriate activation if #ajax['keypress'] is
  194. // TRUE. On a text-type widget a space should always be a space.
  195. if (event.which == 13 || (event.which == 32 && element.type != 'text' && element.type != 'textarea')) {
  196. $(ajax.element_settings.element).trigger(ajax.element_settings.event);
  197. return false;
  198. }
  199. };
  200. /**
  201. * Handle an event that triggers an Ajax response.
  202. *
  203. * When an event that triggers an Ajax response happens, this method will
  204. * perform the actual Ajax call. It is bound to the event using
  205. * bind() in the constructor, and it uses the options specified on the
  206. * ajax object.
  207. */
  208. Drupal.ajax.prototype.eventResponse = function (element, event) {
  209. // Create a synonym for this to reduce code confusion.
  210. var ajax = this;
  211. // Do not perform another ajax command if one is already in progress.
  212. if (ajax.ajaxing) {
  213. return false;
  214. }
  215. try {
  216. if (ajax.form) {
  217. // If setClick is set, we must set this to ensure that the button's
  218. // value is passed.
  219. if (ajax.setClick) {
  220. // Mark the clicked button. 'form.clk' is a special variable for
  221. // ajaxSubmit that tells the system which element got clicked to
  222. // trigger the submit. Without it there would be no 'op' or
  223. // equivalent.
  224. element.form.clk = element;
  225. }
  226. ajax.form.ajaxSubmit(ajax.options);
  227. }
  228. else {
  229. ajax.beforeSerialize(ajax.element, ajax.options);
  230. $.ajax(ajax.options);
  231. }
  232. }
  233. catch (e) {
  234. // Unset the ajax.ajaxing flag here because it won't be unset during
  235. // the complete response.
  236. ajax.ajaxing = false;
  237. alert("An error occurred while attempting to process " + ajax.options.url + ": " + e.message);
  238. }
  239. // For radio/checkbox, allow the default event. On IE, this means letting
  240. // it actually check the box.
  241. if (typeof element.type != 'undefined' && (element.type == 'checkbox' || element.type == 'radio')) {
  242. return true;
  243. }
  244. else {
  245. return false;
  246. }
  247. };
  248. /**
  249. * Handler for the form serialization.
  250. *
  251. * Runs before the beforeSend() handler (see below), and unlike that one, runs
  252. * before field data is collected.
  253. */
  254. Drupal.ajax.prototype.beforeSerialize = function (element, options) {
  255. // Allow detaching behaviors to update field values before collecting them.
  256. // This is only needed when field values are added to the POST data, so only
  257. // when there is a form such that this.form.ajaxSubmit() is used instead of
  258. // $.ajax(). When there is no form and $.ajax() is used, beforeSerialize()
  259. // isn't called, but don't rely on that: explicitly check this.form.
  260. if (this.form) {
  261. var settings = this.settings || Drupal.settings;
  262. Drupal.detachBehaviors(this.form, settings, 'serialize');
  263. }
  264. // Prevent duplicate HTML ids in the returned markup.
  265. // @see drupal_html_id()
  266. options.data['ajax_html_ids[]'] = [];
  267. $('[id]').each(function () {
  268. options.data['ajax_html_ids[]'].push(this.id);
  269. });
  270. // Allow Drupal to return new JavaScript and CSS files to load without
  271. // returning the ones already loaded.
  272. // @see ajax_base_page_theme()
  273. // @see drupal_get_css()
  274. // @see drupal_get_js()
  275. options.data['ajax_page_state[theme]'] = Drupal.settings.ajaxPageState.theme;
  276. options.data['ajax_page_state[theme_token]'] = Drupal.settings.ajaxPageState.theme_token;
  277. for (var key in Drupal.settings.ajaxPageState.css) {
  278. options.data['ajax_page_state[css][' + key + ']'] = 1;
  279. }
  280. for (var key in Drupal.settings.ajaxPageState.js) {
  281. options.data['ajax_page_state[js][' + key + ']'] = 1;
  282. }
  283. };
  284. /**
  285. * Modify form values prior to form submission.
  286. */
  287. Drupal.ajax.prototype.beforeSubmit = function (form_values, element, options) {
  288. // This function is left empty to make it simple to override for modules
  289. // that wish to add functionality here.
  290. };
  291. /**
  292. * Prepare the Ajax request before it is sent.
  293. */
  294. Drupal.ajax.prototype.beforeSend = function (xmlhttprequest, options) {
  295. // For forms without file inputs, the jQuery Form plugin serializes the form
  296. // values, and then calls jQuery's $.ajax() function, which invokes this
  297. // handler. In this circumstance, options.extraData is never used. For forms
  298. // with file inputs, the jQuery Form plugin uses the browser's normal form
  299. // submission mechanism, but captures the response in a hidden IFRAME. In this
  300. // circumstance, it calls this handler first, and then appends hidden fields
  301. // to the form to submit the values in options.extraData. There is no simple
  302. // way to know which submission mechanism will be used, so we add to extraData
  303. // regardless, and allow it to be ignored in the former case.
  304. if (this.form) {
  305. options.extraData = options.extraData || {};
  306. // Let the server know when the IFRAME submission mechanism is used. The
  307. // server can use this information to wrap the JSON response in a TEXTAREA,
  308. // as per http://jquery.malsup.com/form/#file-upload.
  309. options.extraData.ajax_iframe_upload = '1';
  310. // The triggering element is about to be disabled (see below), but if it
  311. // contains a value (e.g., a checkbox, textfield, select, etc.), ensure that
  312. // value is included in the submission. As per above, submissions that use
  313. // $.ajax() are already serialized prior to the element being disabled, so
  314. // this is only needed for IFRAME submissions.
  315. var v = $.fieldValue(this.element);
  316. if (v !== null) {
  317. options.extraData[this.element.name] = Drupal.checkPlain(v);
  318. }
  319. }
  320. // Disable the element that received the change to prevent user interface
  321. // interaction while the Ajax request is in progress. ajax.ajaxing prevents
  322. // the element from triggering a new request, but does not prevent the user
  323. // from changing its value.
  324. $(this.element).addClass('progress-disabled').attr('disabled', true);
  325. // Insert progressbar or throbber.
  326. if (this.progress.type == 'bar') {
  327. var progressBar = new Drupal.progressBar('ajax-progress-' + this.element.id, eval(this.progress.update_callback), this.progress.method, eval(this.progress.error_callback));
  328. if (this.progress.message) {
  329. progressBar.setProgress(-1, this.progress.message);
  330. }
  331. if (this.progress.url) {
  332. progressBar.startMonitoring(this.progress.url, this.progress.interval || 1500);
  333. }
  334. this.progress.element = $(progressBar.element).addClass('ajax-progress ajax-progress-bar');
  335. this.progress.object = progressBar;
  336. $(this.element).after(this.progress.element);
  337. }
  338. else if (this.progress.type == 'throbber') {
  339. this.progress.element = $('<div class="ajax-progress ajax-progress-throbber"><div class="throbber">&nbsp;</div></div>');
  340. if (this.progress.message) {
  341. $('.throbber', this.progress.element).after('<div class="message">' + this.progress.message + '</div>');
  342. }
  343. $(this.element).after(this.progress.element);
  344. }
  345. };
  346. /**
  347. * Handler for the form redirection completion.
  348. */
  349. Drupal.ajax.prototype.success = function (response, status) {
  350. // Remove the progress element.
  351. if (this.progress.element) {
  352. $(this.progress.element).remove();
  353. }
  354. if (this.progress.object) {
  355. this.progress.object.stopMonitoring();
  356. }
  357. $(this.element).removeClass('progress-disabled').removeAttr('disabled');
  358. Drupal.freezeHeight();
  359. for (var i in response) {
  360. if (response.hasOwnProperty(i) && response[i]['command'] && this.commands[response[i]['command']]) {
  361. this.commands[response[i]['command']](this, response[i], status);
  362. }
  363. }
  364. // Reattach behaviors, if they were detached in beforeSerialize(). The
  365. // attachBehaviors() called on the new content from processing the response
  366. // commands is not sufficient, because behaviors from the entire form need
  367. // to be reattached.
  368. if (this.form) {
  369. var settings = this.settings || Drupal.settings;
  370. Drupal.attachBehaviors(this.form, settings);
  371. }
  372. Drupal.unfreezeHeight();
  373. // Remove any response-specific settings so they don't get used on the next
  374. // call by mistake.
  375. this.settings = null;
  376. };
  377. /**
  378. * Build an effect object which tells us how to apply the effect when adding new HTML.
  379. */
  380. Drupal.ajax.prototype.getEffect = function (response) {
  381. var type = response.effect || this.effect;
  382. var speed = response.speed || this.speed;
  383. var effect = {};
  384. if (type == 'none') {
  385. effect.showEffect = 'show';
  386. effect.hideEffect = 'hide';
  387. effect.showSpeed = '';
  388. }
  389. else if (type == 'fade') {
  390. effect.showEffect = 'fadeIn';
  391. effect.hideEffect = 'fadeOut';
  392. effect.showSpeed = speed;
  393. }
  394. else {
  395. effect.showEffect = type + 'Toggle';
  396. effect.hideEffect = type + 'Toggle';
  397. effect.showSpeed = speed;
  398. }
  399. return effect;
  400. };
  401. /**
  402. * Handler for the form redirection error.
  403. */
  404. Drupal.ajax.prototype.error = function (response, uri) {
  405. alert(Drupal.ajaxError(response, uri));
  406. // Remove the progress element.
  407. if (this.progress.element) {
  408. $(this.progress.element).remove();
  409. }
  410. if (this.progress.object) {
  411. this.progress.object.stopMonitoring();
  412. }
  413. // Undo hide.
  414. $(this.wrapper).show();
  415. // Re-enable the element.
  416. $(this.element).removeClass('progress-disabled').removeAttr('disabled');
  417. // Reattach behaviors, if they were detached in beforeSerialize().
  418. if (this.form) {
  419. var settings = response.settings || this.settings || Drupal.settings;
  420. Drupal.attachBehaviors(this.form, settings);
  421. }
  422. };
  423. /**
  424. * Provide a series of commands that the server can request the client perform.
  425. */
  426. Drupal.ajax.prototype.commands = {
  427. /**
  428. * Command to insert new content into the DOM.
  429. */
  430. insert: function (ajax, response, status) {
  431. // Get information from the response. If it is not there, default to
  432. // our presets.
  433. var wrapper = response.selector ? $(response.selector) : $(ajax.wrapper);
  434. var method = response.method || ajax.method;
  435. var effect = ajax.getEffect(response);
  436. // We don't know what response.data contains: it might be a string of text
  437. // without HTML, so don't rely on jQuery correctly iterpreting
  438. // $(response.data) as new HTML rather than a CSS selector. Also, if
  439. // response.data contains top-level text nodes, they get lost with either
  440. // $(response.data) or $('<div></div>').replaceWith(response.data).
  441. var new_content_wrapped = $('<div></div>').html(response.data);
  442. var new_content = new_content_wrapped.contents();
  443. // For legacy reasons, the effects processing code assumes that new_content
  444. // consists of a single top-level element. Also, it has not been
  445. // sufficiently tested whether attachBehaviors() can be successfully called
  446. // with a context object that includes top-level text nodes. However, to
  447. // give developers full control of the HTML appearing in the page, and to
  448. // enable Ajax content to be inserted in places where DIV elements are not
  449. // allowed (e.g., within TABLE, TR, and SPAN parents), we check if the new
  450. // content satisfies the requirement of a single top-level element, and
  451. // only use the container DIV created above when it doesn't. For more
  452. // information, please see http://drupal.org/node/736066.
  453. if (new_content.length != 1 || new_content.get(0).nodeType != 1) {
  454. new_content = new_content_wrapped;
  455. }
  456. // If removing content from the wrapper, detach behaviors first.
  457. switch (method) {
  458. case 'html':
  459. case 'replaceWith':
  460. case 'replaceAll':
  461. case 'empty':
  462. case 'remove':
  463. var settings = response.settings || ajax.settings || Drupal.settings;
  464. Drupal.detachBehaviors(wrapper, settings);
  465. }
  466. // Add the new content to the page.
  467. wrapper[method](new_content);
  468. // Immediately hide the new content if we're using any effects.
  469. if (effect.showEffect != 'show') {
  470. new_content.hide();
  471. }
  472. // Determine which effect to use and what content will receive the
  473. // effect, then show the new content.
  474. if ($('.ajax-new-content', new_content).length > 0) {
  475. $('.ajax-new-content', new_content).hide();
  476. new_content.show();
  477. $('.ajax-new-content', new_content)[effect.showEffect](effect.showSpeed);
  478. }
  479. else if (effect.showEffect != 'show') {
  480. new_content[effect.showEffect](effect.showSpeed);
  481. }
  482. // Attach all JavaScript behaviors to the new content, if it was successfully
  483. // added to the page, this if statement allows #ajax['wrapper'] to be
  484. // optional.
  485. if (new_content.parents('html').length > 0) {
  486. // Apply any settings from the returned JSON if available.
  487. var settings = response.settings || ajax.settings || Drupal.settings;
  488. Drupal.attachBehaviors(new_content, settings);
  489. }
  490. },
  491. /**
  492. * Command to remove a chunk from the page.
  493. */
  494. remove: function (ajax, response, status) {
  495. var settings = response.settings || ajax.settings || Drupal.settings;
  496. Drupal.detachBehaviors($(response.selector), settings);
  497. $(response.selector).remove();
  498. },
  499. /**
  500. * Command to mark a chunk changed.
  501. */
  502. changed: function (ajax, response, status) {
  503. if (!$(response.selector).hasClass('ajax-changed')) {
  504. $(response.selector).addClass('ajax-changed');
  505. if (response.asterisk) {
  506. $(response.selector).find(response.asterisk).append(' <span class="ajax-changed">*</span> ');
  507. }
  508. }
  509. },
  510. /**
  511. * Command to provide an alert.
  512. */
  513. alert: function (ajax, response, status) {
  514. alert(response.text, response.title);
  515. },
  516. /**
  517. * Command to provide the jQuery css() function.
  518. */
  519. css: function (ajax, response, status) {
  520. $(response.selector).css(response.argument);
  521. },
  522. /**
  523. * Command to set the settings that will be used for other commands in this response.
  524. */
  525. settings: function (ajax, response, status) {
  526. if (response.merge) {
  527. $.extend(true, Drupal.settings, response.settings);
  528. }
  529. else {
  530. ajax.settings = response.settings;
  531. }
  532. },
  533. /**
  534. * Command to attach data using jQuery's data API.
  535. */
  536. data: function (ajax, response, status) {
  537. $(response.selector).data(response.name, response.value);
  538. },
  539. /**
  540. * Command to apply a jQuery method.
  541. */
  542. invoke: function (ajax, response, status) {
  543. var $element = $(response.selector);
  544. $element[response.method].apply($element, response.arguments);
  545. },
  546. /**
  547. * Command to restripe a table.
  548. */
  549. restripe: function (ajax, response, status) {
  550. // :even and :odd are reversed because jQuery counts from 0 and
  551. // we count from 1, so we're out of sync.
  552. // Match immediate children of the parent element to allow nesting.
  553. $('> tbody > tr:visible, > tr:visible', $(response.selector))
  554. .removeClass('odd even')
  555. .filter(':even').addClass('odd').end()
  556. .filter(':odd').addClass('even');
  557. },
  558. /**
  559. * Command to add css.
  560. *
  561. * Uses the proprietary addImport method if available as browsers which
  562. * support that method ignore @import statements in dynamically added
  563. * stylesheets.
  564. */
  565. add_css: function (ajax, response, status) {
  566. // Add the styles in the normal way.
  567. $('head').prepend(response.data);
  568. // Add imports in the styles using the addImport method if available.
  569. var match, importMatch = /^@import url\("(.*)"\);$/igm;
  570. if (document.styleSheets[0].addImport && importMatch.test(response.data)) {
  571. importMatch.lastIndex = 0;
  572. while (match = importMatch.exec(response.data)) {
  573. document.styleSheets[0].addImport(match[1]);
  574. }
  575. }
  576. },
  577. /**
  578. * Command to update a form's build ID.
  579. */
  580. updateBuildId: function(ajax, response, status) {
  581. $('input[name="form_build_id"][value="' + response['old'] + '"]').val(response['new']);
  582. }
  583. };
  584. })(jQuery);