Guest User

wpcf7

a guest
Aug 28th, 2013
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($) {
  2.  
  3.     $(function() {
  4.         try {
  5.             if (typeof _wpcf7 == 'undefined' || _wpcf7 === null)
  6.                 _wpcf7 = {};
  7.  
  8.             _wpcf7 = $.extend({ cached: 0 }, _wpcf7);
  9.  
  10.             $('div.wpcf7 > form').ajaxForm({
  11.                 beforeSubmit: function(formData, jqForm, options) {
  12.                     jqForm.wpcf7ClearResponseOutput();
  13.                     jqForm.find('img.ajax-loader').css({ visibility: 'visible' });
  14.                     return true;
  15.                 },
  16.                 beforeSerialize: function(jqForm, options) {
  17.                     jqForm.find('.wpcf7-use-title-as-watermark.watermark').each(function(i, n) {
  18.                         $(n).val('');
  19.                     });
  20.                     return true;
  21.                 },
  22.                 data: { '_wpcf7_is_ajax_call': 1 },
  23.                 dataType: 'json',
  24.                 success: function(data) {
  25.                     var ro = $(data.into).find('div.wpcf7-response-output');
  26.                     $(data.into).wpcf7ClearResponseOutput();
  27.  
  28.                     $(data.into).find('.wpcf7-form-control').removeClass('wpcf7-not-valid');
  29.                     $(data.into).find('form.wpcf7-form').removeClass('invalid spam sent failed');
  30.  
  31.                     if (data.captcha)
  32.                         $(data.into).wpcf7RefillCaptcha(data.captcha);
  33.  
  34.                     if (data.quiz)
  35.                         $(data.into).wpcf7RefillQuiz(data.quiz);
  36.  
  37.                     if (data.invalids) {
  38.                         $.each(data.invalids, function(i, n) {
  39.                             $(data.into).find(n.into).wpcf7NotValidTip(n.message);
  40.                             $(data.into).find(n.into).find('.wpcf7-form-control').addClass('wpcf7-not-valid');
  41.                         });
  42.  
  43.                         ro.addClass('wpcf7-validation-errors');
  44.                         $(data.into).find('form.wpcf7-form').addClass('invalid');
  45.  
  46.                         $(data.into).trigger('invalid.wpcf7');
  47.  
  48.                     } else if (1 == data.spam) {
  49.                         ro.addClass('wpcf7-spam-blocked');
  50.                         $(data.into).find('form.wpcf7-form').addClass('spam');
  51.  
  52.                         $(data.into).trigger('spam.wpcf7');
  53.  
  54.                     } else if (1 == data.mailSent) {
  55.                         ro.addClass('wpcf7-mail-sent-ok');
  56.                         $(data.into).find('form.wpcf7-form').addClass('sent');
  57.  
  58.                         if (data.onSentOk)
  59.                             $.each(data.onSentOk, function(i, n) { eval(n) });
  60.  
  61.                         $(data.into).trigger('mailsent.wpcf7');
  62.  
  63.                     } else {
  64.                         ro.addClass('wpcf7-mail-sent-ng');
  65.                         $(data.into).find('form.wpcf7-form').addClass('failed');
  66.  
  67.                         $(data.into).trigger('mailfailed.wpcf7');
  68.                     }
  69.  
  70.                     if (data.onSubmit)
  71.                         $.each(data.onSubmit, function(i, n) { eval(n) });
  72.  
  73.                     $(data.into).trigger('submit.wpcf7');
  74.  
  75.                     if (1 == data.mailSent)
  76.                         $(data.into).find('form').resetForm().clearForm();
  77.  
  78.                     $(data.into).find('.wpcf7-use-title-as-watermark.watermark').each(function(i, n) {
  79.                         $(n).val($(n).attr('title'));
  80.                     });
  81.  
  82.                     $(data.into).wpcf7FillResponseOutput(data.message);
  83.                 }
  84.             });
  85.  
  86.             $('div.wpcf7 > form').each(function(i, n) {
  87.                 if (_wpcf7.cached)
  88.                     $(n).wpcf7OnloadRefill();
  89.  
  90.                 $(n).wpcf7ToggleSubmit();
  91.  
  92.                 $(n).find('.wpcf7-submit').wpcf7AjaxLoader();
  93.  
  94.                 $(n).find('.wpcf7-acceptance').click(function() {
  95.                     $(n).wpcf7ToggleSubmit();
  96.                 });
  97.  
  98.                 $(n).find('.wpcf7-exclusive-checkbox').each(function(i, n) {
  99.                     $(n).find('input:checkbox').click(function() {
  100.                         $(n).find('input:checkbox').not(this).removeAttr('checked');
  101.                     });
  102.                 });
  103.  
  104.                 $(n).find('.wpcf7-use-title-as-watermark').each(function(i, n) {
  105.                     var input = $(n);
  106.                     input.val(input.attr('title'));
  107.                     input.addClass('watermark');
  108.  
  109.                     input.focus(function() {
  110.                         if ($(this).hasClass('watermark'))
  111.                             $(this).val('').removeClass('watermark');
  112.                     });
  113.  
  114.                     input.blur(function() {
  115.                         if ('' == $(this).val())
  116.                             $(this).val($(this).attr('title')).addClass('watermark');
  117.                     });
  118.                 });
  119.             });
  120.  
  121.         } catch (e) {
  122.         }
  123.     });
  124.  
  125.     $.fn.wpcf7AjaxLoader = function() {
  126.         return this.each(function() {
  127.             var loader = $('<img class="ajax-loader" />')
  128.                 .attr({ src: _wpcf7.loaderUrl, alt: _wpcf7.sending })
  129.                 .css('visibility', 'hidden');
  130.  
  131.             $(this).after(loader);
  132.         });
  133.     };
  134.  
  135.     $.fn.wpcf7ToggleSubmit = function() {
  136.         return this.each(function() {
  137.             var form = $(this);
  138.             if (this.tagName.toLowerCase() != 'form')
  139.                 form = $(this).find('form').first();
  140.  
  141.             if (form.hasClass('wpcf7-acceptance-as-validation'))
  142.                 return;
  143.  
  144.             var submit = form.find('input:submit');
  145.             if (! submit.length) return;
  146.  
  147.             var acceptances = form.find('input:checkbox.wpcf7-acceptance');
  148.             if (! acceptances.length) return;
  149.  
  150.             submit.removeAttr('disabled');
  151.             acceptances.each(function(i, n) {
  152.                 n = $(n);
  153.                 if (n.hasClass('wpcf7-invert') && n.is(':checked')
  154.                 || ! n.hasClass('wpcf7-invert') && ! n.is(':checked'))
  155.                     submit.attr('disabled', 'disabled');
  156.             });
  157.         });
  158.     };
  159.  
  160.     $.fn.wpcf7NotValidTip = function(message) {
  161.         return this.each(function() {
  162.             var into = $(this);
  163.             into.append('<span class="wpcf7-not-valid-tip">' + message + '</span>');
  164.             $('span.wpcf7-not-valid-tip').mouseover(function() {
  165.                 $(this).fadeOut('fast');
  166.             });
  167.             into.find(':input').mouseover(function() {
  168.                 into.find('.wpcf7-not-valid-tip').not(':hidden').fadeOut('fast');
  169.             });
  170.             into.find(':input').focus(function() {
  171.                 into.find('.wpcf7-not-valid-tip').not(':hidden').fadeOut('fast');
  172.             });
  173.         });
  174.     };
  175.  
  176.     $.fn.wpcf7OnloadRefill = function() {
  177.         return this.each(function() {
  178.             var url = $(this).attr('action');
  179.             if (0 < url.indexOf('#'))
  180.                 url = url.substr(0, url.indexOf('#'));
  181.  
  182.             var id = $(this).find('input[name="_wpcf7"]').val();
  183.             var unitTag = $(this).find('input[name="_wpcf7_unit_tag"]').val();
  184.  
  185.             $.getJSON(url,
  186.                 { _wpcf7_is_ajax_call: 1, _wpcf7: id },
  187.                 function(data) {
  188.                     if (data && data.captcha)
  189.                         $('#' + unitTag).wpcf7RefillCaptcha(data.captcha);
  190.  
  191.                     if (data && data.quiz)
  192.                         $('#' + unitTag).wpcf7RefillQuiz(data.quiz);
  193.                 }
  194.             );
  195.         });
  196.     };
  197.  
  198.     $.fn.wpcf7RefillCaptcha = function(captcha) {
  199.         return this.each(function() {
  200.             var form = $(this);
  201.  
  202.             $.each(captcha, function(i, n) {
  203.                 form.find(':input[name="' + i + '"]').clearFields();
  204.                 form.find('img.wpcf7-captcha-' + i).attr('src', n);
  205.                 var match = /([0-9]+)\.(png|gif|jpeg)$/.exec(n);
  206.                 form.find('input:hidden[name="_wpcf7_captcha_challenge_' + i + '"]').attr('value', match[1]);
  207.             });
  208.         });
  209.     };
  210.  
  211.     $.fn.wpcf7RefillQuiz = function(quiz) {
  212.         return this.each(function() {
  213.             var form = $(this);
  214.  
  215.             $.each(quiz, function(i, n) {
  216.                 form.find(':input[name="' + i + '"]').clearFields();
  217.                 form.find(':input[name="' + i + '"]').siblings('span.wpcf7-quiz-label').text(n[0]);
  218.                 form.find('input:hidden[name="_wpcf7_quiz_answer_' + i + '"]').attr('value', n[1]);
  219.             });
  220.         });
  221.     };
  222.  
  223.     $.fn.wpcf7ClearResponseOutput = function() {
  224.         return this.each(function() {
  225.             $(this).find('div.wpcf7-response-output').hide().empty().removeClass('wpcf7-mail-sent-ok wpcf7-mail-sent-ng wpcf7-validation-errors wpcf7-spam-blocked');
  226.             $(this).find('span.wpcf7-not-valid-tip').remove();
  227.             $(this).find('img.ajax-loader').css({ visibility: 'hidden' });
  228.         });
  229.     };
  230.  
  231.     $.fn.wpcf7FillResponseOutput = function(message) {
  232.         return this.each(function() {
  233.             $(this).find('div.wpcf7-response-output').append(message).slideDown('fast');
  234.         });
  235.     };
  236.  
  237. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment