ajax-responder.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @file
  3. *
  4. * CTools flexible AJAX responder object.
  5. */
  6. (function ($) {
  7. Drupal.CTools = Drupal.CTools || {};
  8. Drupal.CTools.AJAX = Drupal.CTools.AJAX || {};
  9. /**
  10. * Grab the response from the server and store it.
  11. *
  12. * @todo restore the warm cache functionality
  13. */
  14. Drupal.CTools.AJAX.warmCache = function () {
  15. // Store this expression for a minor speed improvement.
  16. $this = $(this);
  17. var old_url = $this.attr('href');
  18. // If we are currently fetching, or if we have fetched this already which is
  19. // ideal for things like pagers, where the previous page might already have
  20. // been seen in the cache.
  21. if ($this.hasClass('ctools-fetching') || Drupal.CTools.AJAX.commandCache[old_url]) {
  22. return false;
  23. }
  24. // Grab all the links that match this url and add the fetching class.
  25. // This allows the caching system to grab each url once and only once
  26. // instead of grabbing the url once per <a>.
  27. var $objects = $('a[href="' + old_url + '"]')
  28. $objects.addClass('ctools-fetching');
  29. try {
  30. url = old_url.replace(/\/nojs(\/|$)/g, '/ajax$1');
  31. $.ajax({
  32. type: "POST",
  33. url: url,
  34. data: { 'js': 1, 'ctools_ajax': 1},
  35. global: true,
  36. success: function (data) {
  37. Drupal.CTools.AJAX.commandCache[old_url] = data;
  38. $objects.addClass('ctools-cache-warmed').trigger('ctools-cache-warm', [data]);
  39. },
  40. complete: function() {
  41. $objects.removeClass('ctools-fetching');
  42. },
  43. dataType: 'json'
  44. });
  45. }
  46. catch (err) {
  47. $objects.removeClass('ctools-fetching');
  48. return false;
  49. }
  50. return false;
  51. };
  52. /**
  53. * Cachable click handler to fetch the commands out of the cache or from url.
  54. */
  55. Drupal.CTools.AJAX.clickAJAXCacheLink = function () {
  56. $this = $(this);
  57. if ($this.hasClass('ctools-fetching')) {
  58. $this.bind('ctools-cache-warm', function (event, data) {
  59. Drupal.CTools.AJAX.respond(data);
  60. });
  61. return false;
  62. }
  63. else {
  64. if ($this.hasClass('ctools-cache-warmed') && Drupal.CTools.AJAX.commandCache[$this.attr('href')]) {
  65. Drupal.CTools.AJAX.respond(Drupal.CTools.AJAX.commandCache[$this.attr('href')]);
  66. return false;
  67. }
  68. else {
  69. return Drupal.CTools.AJAX.clickAJAXLink.apply(this);
  70. }
  71. }
  72. };
  73. /**
  74. * Find a URL for an AJAX button.
  75. *
  76. * The URL for this gadget will be composed of the values of items by
  77. * taking the ID of this item and adding -url and looking for that
  78. * class. They need to be in the form in order since we will
  79. * concat them all together using '/'.
  80. */
  81. Drupal.CTools.AJAX.findURL = function(item) {
  82. var url = '';
  83. var url_class = '.' + $(item).attr('id') + '-url';
  84. $(url_class).each(
  85. function() {
  86. var $this = $(this);
  87. if (url && $this.val()) {
  88. url += '/';
  89. }
  90. url += $this.val();
  91. });
  92. return url;
  93. };
  94. // Hide these in a ready to ensure that Drupal.ajax is set up first.
  95. $(function() {
  96. Drupal.ajax.prototype.commands.attr = function(ajax, data, status) {
  97. $(data.selector).attr(data.name, data.value);
  98. };
  99. Drupal.ajax.prototype.commands.redirect = function(ajax, data, status) {
  100. if (data.delay > 0) {
  101. setTimeout(function () {
  102. location.href = data.url;
  103. }, data.delay);
  104. }
  105. else {
  106. location.href = data.url;
  107. }
  108. };
  109. Drupal.ajax.prototype.commands.reload = function(ajax, data, status) {
  110. location.reload();
  111. };
  112. Drupal.ajax.prototype.commands.submit = function(ajax, data, status) {
  113. $(data.selector).submit();
  114. }
  115. });
  116. })(jQuery);