diff --git a/recaptcha.js b/recaptcha.js index a0adb80..9c7a4fe 100644 --- a/recaptcha.js +++ b/recaptcha.js @@ -1,7 +1,29 @@ (function ($) { Drupal.behaviors.recaptcha = { attach: function (context) { - Recaptcha.create(Drupal.settings.recaptcha.public_key, Drupal.settings.recaptcha.container, {theme: Drupal.settings.recaptcha.theme}); + $(document).ready(Recaptcha.create(Drupal.settings.recaptcha.public_key, Drupal.settings.recaptcha.containers[0], {theme: Drupal.settings.recaptcha.theme})); + Drupal.settings.recaptcha.current_container = 0; + Drupal.settings.recaptcha.form_map = new Array(); + for (var i = 0, j = Drupal.settings.recaptcha.containers.length; i < j; i++) { + var form = $('#' + Drupal.settings.recaptcha.containers[i]).parents('form'); + Drupal.settings.recaptcha.form_map[$(form[0]).attr('id')] = i; + var children = $(form).find(':input'); + if (i > 0) { + $(form).find('.captcha').hide(); + } + $(children).focus(function() { + var form = $(this).parents('form'); + var form_id = $(form[0]).attr('id'); + var container_id = Drupal.settings.recaptcha.form_map[form_id]; + if (container_id != Drupal.settings.recaptcha.current_container) { + Recaptcha.destroy(); + $('.captcha').hide(); + $(form).find('.captcha').show(); + Recaptcha.create(Drupal.settings.recaptcha.public_key, Drupal.settings.recaptcha.containers[container_id], {theme: Drupal.settings.recaptcha.theme}); + Drupal.settings.recaptcha.current_container = container_id; + } + }); + } }, detach: function (context) {} }; diff --git a/recaptcha.module b/recaptcha.module index 9ce68b0..824fc56 100644 --- a/recaptcha.module +++ b/recaptcha.module @@ -65,9 +65,24 @@ function recaptcha_permission() { } /** + * Implements hook_js_alter(). + */ +function recaptcha_js_alter(&$javascript) { + $containers = &drupal_static('recaptcha_captcha_containers', array()); + foreach ($javascript['settings']['data'] as $js_key => $values) { + if (isset($values['recaptcha'])) { + $javascript['settings']['data'][$js_key]['recaptcha']['containers'] = $containers; + return; + } + } +} + +/** * Implements hook_captcha(). */ function recaptcha_captcha($op, $captcha_type = '') { + $containers = &drupal_static('recaptcha_captcha_containers', array()); + $current_counter = count($containers); switch ($op) { case 'list': return array('reCAPTCHA'); @@ -156,11 +171,12 @@ function recaptcha_captcha($op, $captcha_type = '') { // Create the destination container, inserting any custom theme HTML // necessary ($recaptcha_custom_html will be empty if we are not // using custom theme). - '#markup' => '
' . $recaptcha_custom_html . '
', + '#markup' => '
' . $recaptcha_custom_html . '
', ); drupal_add_js('https://www.google.com/recaptcha/api/js/recaptcha_ajax.js', array('type' => 'external')); $recaptcha_options['public_key'] = $recaptcha_public_key; - $recaptcha_options['container'] = 'recaptcha_ajax_api_container'; + $recaptcha_options['container'] = "recaptcha_ajax_api_container_{$current_counter}"; + $containers[] = "recaptcha_ajax_api_container_{$current_counter}"; drupal_add_js(array('recaptcha' => $recaptcha_options), 'setting'); drupal_add_js(drupal_get_path('module', 'recaptcha') . '/recaptcha.js'); }