| 1 |
(function( $ ) { |
| 2 |
'use strict'; |
| 3 |
$(document).ready( function(){ |
| 4 |
var $cf7Form = $('form.wpcf7-form'); |
| 5 |
$cf7Form.each(function(){ |
| 6 |
if($(this).is('div.cf7_2_post form.wpcf7-form')){ |
| 7 |
var nonce = $(this).closest('div.cf7_2_post').attr('id'); |
| 8 |
$(this).on(nonce, function(){ //post my cf7 form plugin is ready. |
| 9 |
$('input.cf7sg-benchmark', $(this)).cf7sgBenchmark(false); |
| 10 |
}); |
| 11 |
} |
| 12 |
}); |
| 13 |
//event delgation. |
| 14 |
$cf7Form.on('change', 'input.cf7sg-benchmark', function(event){ |
| 15 |
var $input = $(event.target); |
| 16 |
$input.cf7sgBenchmark(true); |
| 17 |
}); |
| 18 |
$cf7Form.on('click', 'span.dashicons-no-alt', function(event){ |
| 19 |
var $target = $(event.target); |
| 20 |
if($target.is('span.cf7sg-benchmark-warning .dashicons-no-alt')){ |
| 21 |
$target.closest('span.cf7sg-benchmark-warning').hide(); |
| 22 |
$target.remove(); |
| 23 |
} |
| 24 |
}); |
| 25 |
}); |
| 26 |
$.fn.cf7sgBenchmark = function(warn){ |
| 27 |
if(typeof warn === 'undefined') warn =false; |
| 28 |
if(!$(this).is('input.cf7sg-benchmark')){ |
| 29 |
return $(this); |
| 30 |
} |
| 31 |
$(this).removeClass('cf7sg-benchmark-warning'); |
| 32 |
$(this).next('span.cf7sg-benchmark-warning').remove(); |
| 33 |
if('' == $(this).val()){ |
| 34 |
return $(this); |
| 35 |
} |
| 36 |
var type = $(this).data('cf7sg-benchmark'); |
| 37 |
var hidden = ''; |
| 38 |
if($(this).is('input[type="hidden"]')){ |
| 39 |
hidden = ' cf7sg-benchmark-hidden'; |
| 40 |
} |
| 41 |
var msg = '<span class="cf7sg-benchmark-warning'+hidden+'"><span>warning<span class="dashicons dashicons-no-alt"></span></span><span class="cf7sg-benchmark-msg">'+$(this).data('cf7sg-benchmark-msg')+'</span></span>'; |
| 42 |
switch(type){ |
| 43 |
case 'above': |
| 44 |
var limit = $(this).data('cf7sg-benchmark-limit'); |
| 45 |
if($(this).val() > limit ){ |
| 46 |
$(this).after(msg); |
| 47 |
if(!warn) $(this).next('span.cf7sg-benchmark-warning').hide().find('.dashicons-no-alt').remove(); |
| 48 |
$(this).addClass('cf7sg-benchmark-warning'); |
| 49 |
if(hidden.length>0){ |
| 50 |
$(this).trigger('cf7sg-benchmark-'+$(this).attr('name')); |
| 51 |
} |
| 52 |
} |
| 53 |
break; |
| 54 |
case 'below': |
| 55 |
var limit = $(this).data('cf7sg-benchmark-limit'); |
| 56 |
if($(this).val() < limit ){ |
| 57 |
$(this).after(msg); |
| 58 |
if(!warn) $(this).next('span.cf7sg-benchmark-warning').hide().find('.dashicons-no-alt').remove(); |
| 59 |
$(this).addClass('cf7sg-benchmark-warning'); |
| 60 |
if(hidden.length>0){ |
| 61 |
$(this).trigger('cf7sg-benchmark-'+$(this).attr('name')); |
| 62 |
} |
| 63 |
} |
| 64 |
break; |
| 65 |
case 'range': |
| 66 |
var min = $(this).data('cf7sg-benchmark-min'); |
| 67 |
var max = $(this).data('cf7sg-benchmark-max'); |
| 68 |
if($(this).val() < min || $(this).val() > max ){ |
| 69 |
$(this).after(msg); |
| 70 |
if(!warn) $(this).next('span.cf7sg-benchmark-warning').hide().find('.dashicons-no-alt').remove(); |
| 71 |
$(this).addClass('cf7sg-benchmark-warning'); |
| 72 |
if(hidden.length>0){ |
| 73 |
$(this).trigger('cf7sg-benchmark-'+$(this).attr('name')); |
| 74 |
} |
| 75 |
} |
| 76 |
break; |
| 77 |
} |
| 78 |
|
| 79 |
} |
| 80 |
})( jQuery ); |
| 81 |
|