rules_link.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * @file
  3. * Converts a normal link into javascript rules link.
  4. */
  5. (function($) {
  6. Drupal.behaviors.rules_link = {
  7. attach: function(context) {
  8. $('a.rules-link-js').once( function() {
  9. var message = $('<span class="rules-link-message">Link executed</span>').hide();
  10. var wrapper = $(this).parents('.rules-link-wrapper').append(message);
  11. });
  12. $('a.rules-link-js').click(rules_link);
  13. }
  14. };
  15. function rules_link(context) {
  16. var element = this;
  17. var wrapper = $(element).parents('.rules-link-wrapper');
  18. var message = wrapper.children('.rules-link-message');
  19. if (wrapper.is('.rules-link-waiting')) {
  20. return false;
  21. }
  22. wrapper.addClass('rules-link-waiting');
  23. $.ajax({
  24. type: 'GET',
  25. url: element.href,
  26. dataType: 'json',
  27. success: function (data) {
  28. wrapper.removeClass('rules-link-waiting');
  29. if (data.message.status !== undefined) {
  30. $(element).replaceWith("<span>" + data.message.status +"</span>");
  31. }
  32. else {
  33. $(element).remove();
  34. }
  35. },
  36. error: function (xmlhttp) {
  37. alert(Drupal.t('An HTTP error '+ xmlhttp.status +' occurred.\n'+ element.href));
  38. wrapper.removeClass('rules-link-waiting');
  39. message.text("Error while executing the rules link.");
  40. message.show();
  41. setTimeout(function(){
  42. message.fadeOut();
  43. }, 3000);
  44. }
  45. });
  46. return false;
  47. };
  48. })(jQuery);