mollom.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. (function ($) {
  2. Drupal.mollom = Drupal.mollom || {};
  3. /**
  4. * Open links to Mollom.com in a new window.
  5. *
  6. * Required for valid XHTML Strict markup.
  7. */
  8. Drupal.behaviors.mollomTarget = {
  9. attach: function (context) {
  10. $(context).find('.mollom-target').click(function () {
  11. this.target = '_blank';
  12. });
  13. }
  14. };
  15. /**
  16. * Retrieve and attach the form behavior analysis tracking image if it has not
  17. * yet been added for the form.
  18. */
  19. Drupal.behaviors.mollomFBA = {
  20. attach: function (context, settings) {
  21. $(':input[name="mollom[fba]"][value=""]', context).once().each(function() {
  22. $input = $(this);
  23. $.ajax({
  24. url: Drupal.settings.basePath + Drupal.settings.pathPrefix + 'mollom/fba',
  25. type: 'POST',
  26. dataType: 'json',
  27. success: function(data) {
  28. if (!data.tracking_id || !data.tracking_url) {
  29. return;
  30. }
  31. // Save the tracking id in the hidden field.
  32. $input.val(data.tracking_id);
  33. // Attach the tracking image.
  34. $('<img src="' + data.tracking_url + '" width="1" height="1" alt="" />').appendTo('body');
  35. }
  36. })
  37. });
  38. }
  39. };
  40. /**
  41. * Attach click event handlers for CAPTCHA links.
  42. */
  43. Drupal.behaviors.mollomCaptcha = {
  44. attach: function (context, settings) {
  45. $('a.mollom-switch-captcha', context).click(function (e) {
  46. var $mollomForm = $(this).parents('form');
  47. var newCaptchaType = $(this).hasClass('mollom-audio-captcha') ? 'audio' : 'image';
  48. Drupal.mollom.getMollomCaptcha(newCaptchaType, $mollomForm);
  49. });
  50. $('a.mollom-refresh-captcha', context).click(function (e) {
  51. var $mollomForm = $(this).parents('form');
  52. var currentCaptchaType = $(this).hasClass('mollom-refresh-audio') ? 'audio' : 'image';
  53. Drupal.mollom.getMollomCaptcha(currentCaptchaType, $mollomForm);
  54. });
  55. }
  56. };
  57. /**
  58. * Fetch a Mollom CAPTCHA and output the image or audio into the form.
  59. *
  60. * @param captchaType
  61. * The type of CAPTCHA to retrieve; one of "audio" or "image".
  62. * @param context
  63. * The form context for this retrieval.
  64. */
  65. Drupal.mollom.getMollomCaptcha = function (captchaType, context) {
  66. var formBuildId = $('input[name="form_build_id"]', context).val();
  67. var mollomContentId = $('input.mollom-content-id', context).val();
  68. var path = 'mollom/captcha/' + captchaType + '/' + formBuildId;
  69. if (mollomContentId) {
  70. path += '/' + mollomContentId;
  71. }
  72. path += '?cb=' + new Date().getTime();
  73. // Retrieve a new CAPTCHA.
  74. $.ajax({
  75. url: Drupal.settings.basePath + Drupal.settings.pathPrefix + path,
  76. type: 'POST',
  77. dataType: 'json',
  78. success: function (data) {
  79. if (!(data && data.content)) {
  80. return;
  81. }
  82. // Inject new CAPTCHA.
  83. $('.mollom-captcha-content', context).parent().replaceWith(data.content);
  84. // Update CAPTCHA ID.
  85. $('input.mollom-captcha-id', context).val(data.captchaId);
  86. // Add an onclick-event handler for the new link.
  87. Drupal.attachBehaviors(context);
  88. // Focus on the CAPTCHA input.
  89. if (captchaType == 'image') {
  90. $('input[name="mollom[captcha]"]', context).focus();
  91. } else {
  92. // Focus on audio player.
  93. // Fallback player code is responsible for setting focus upon embed.
  94. if ($('#mollom_captcha_audio').is(":visible")) {
  95. $('#mollom_captcha_audio').focus();
  96. }
  97. }
  98. }
  99. });
  100. return false;
  101. }
  102. })(jQuery);