scripts.js
175 lines
| 1 | var cf7signature_resized = 0; // for compatibility with contact-form-7-signature-addon |
| 2 | |
| 3 | (function($) { |
| 4 | |
| 5 | var i=0; |
| 6 | var options = []; |
| 7 | while (true) { |
| 8 | i++; |
| 9 | if ('wpcf7cf_options_'+i in window) { |
| 10 | options.push(window['wpcf7cf_options_'+i]); |
| 11 | continue; |
| 12 | } |
| 13 | break; |
| 14 | } |
| 15 | |
| 16 | $(document).ready(function() { |
| 17 | function display_fields(unit_tag, wpcf7cf_conditions) { |
| 18 | |
| 19 | //for compatibility with contact-form-7-signature-addon |
| 20 | if (cf7signature_resized == 0 && typeof signatures !== 'undefined' && signatures.constructor === Array && signatures.length > 0 ) { |
| 21 | if (signatures[0].canvas.width == 0) { |
| 22 | for (var i = 0; i < signatures.length; i++) { |
| 23 | |
| 24 | jQuery(".wpcf7-form-control-signature-body>canvas").eq(0).attr('width', jQuery(".wpcf7-form-control-signature-wrap").width()); |
| 25 | jQuery(".wpcf7-form-control-signature-body>canvas").eq(0).attr('height', jQuery(".wpcf7-form-control-signature-wrap").height()); |
| 26 | |
| 27 | cf7signature_resized = 1; |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | $("#"+unit_tag+" [data-class='wpcf7cf_group']").hide(); |
| 33 | for (var i=0; i < wpcf7cf_conditions.length; i++) { |
| 34 | |
| 35 | var condition = wpcf7cf_conditions[i]; |
| 36 | |
| 37 | $field = $('#'+unit_tag+' [name="'+condition.if_field+'"]').length ? $('#'+unit_tag+' [name="'+condition.if_field+'"]') : $('#'+unit_tag+' [name="'+condition.if_field+'[]"]'); |
| 38 | |
| 39 | if ($field.length == 1) { |
| 40 | |
| 41 | |
| 42 | |
| 43 | // single field (tested with text field, single checkbox, select with single value (dropdown), select with multiple values) |
| 44 | |
| 45 | if ($field.is('select')) { |
| 46 | |
| 47 | var show = false; |
| 48 | |
| 49 | if(condition.operator == 'not equals') { |
| 50 | show = true; |
| 51 | } |
| 52 | |
| 53 | $field.find('option:selected').each(function () { |
| 54 | var $option = $(this); |
| 55 | if (condition.operator == 'equals' && $option.val() == condition.if_value) { |
| 56 | show = true; |
| 57 | } else if (condition.operator == 'not equals' && $option.val() == condition.if_value) { |
| 58 | show = false; |
| 59 | } |
| 60 | }); |
| 61 | |
| 62 | if(show == true) { |
| 63 | $('#' + unit_tag + ' #' + condition.then_field).show(); |
| 64 | } |
| 65 | |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | if ($field.attr('type') == 'checkbox') { |
| 70 | if ( |
| 71 | $field.is(':checked') && condition.operator == 'equals' && $field.val() == condition.if_value |
| 72 | || !$field.is(':checked') && condition.operator == 'not equals' && $field.val() == condition.if_value |
| 73 | || condition.operator == 'not equals' && $field.val() != condition.if_value |
| 74 | ) { |
| 75 | $('#'+unit_tag+' #'+condition.then_field).show(); |
| 76 | } |
| 77 | } else if (condition.operator == 'equals' && $field.val() == condition.if_value || condition.operator == 'not equals' && $field.val() != condition.if_value) { |
| 78 | $('#'+unit_tag+' #'+condition.then_field).show(); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | } else if ($field.length > 1) { |
| 83 | |
| 84 | // multiple fields (tested with checkboxes, exclusive checkboxes, dropdown with multiple values) |
| 85 | |
| 86 | var all_values = []; |
| 87 | var checked_values = []; |
| 88 | $field.each(function() { |
| 89 | all_values.push($(this).val()); |
| 90 | if($(this).is(':checked')) { |
| 91 | checked_values.push($(this).val()); |
| 92 | } |
| 93 | }); |
| 94 | |
| 95 | |
| 96 | |
| 97 | if (condition.operator == 'equals' && $.inArray(condition.if_value, checked_values) != -1) { |
| 98 | $('#'+unit_tag+' #'+condition.then_field).show(); |
| 99 | } else if (condition.operator == 'not equals' && $.inArray(condition.if_value, all_values) != -1 && $.inArray(condition.if_value, checked_values) == -1) { |
| 100 | $('#'+unit_tag+' #'+condition.then_field).show(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | for (var i = 0; i<options.length; i++) { |
| 108 | var unit_tag = options[i]['unit_tag']; |
| 109 | var conditions = options[i]['conditions']; |
| 110 | display_fields(unit_tag, conditions); |
| 111 | $('#'+unit_tag+' input, #'+unit_tag+' select, #'+unit_tag+' textarea').change({unit_tag:unit_tag, conditions:conditions}, function(e) { |
| 112 | console.log('change'); |
| 113 | display_fields(e.data.unit_tag, e.data.conditions); |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | // before the form values are serialized to submit via ajax, we quickly add all invisible fields in the hidden |
| 118 | // _wpcf7cf_hidden_group_fields field, so the PHP code knows which fields were inside hidden groups. |
| 119 | // TODO: maybe modify this code so it only takes fields which are strictly inside hidden group tags. |
| 120 | // TODO: For now the hidden field is filled with all hidden form elements. |
| 121 | |
| 122 | $('form.wpcf7-form').on('form-pre-serialize', function(form,options,veto) { |
| 123 | $form = $(form.target); |
| 124 | |
| 125 | $hidden_group_fields = $form.find('[name="_wpcf7cf_hidden_group_fields"]'); |
| 126 | $hidden_groups = $form.find('[name="_wpcf7cf_hidden_groups"]'); |
| 127 | $visible_groups = $form.find('[name="_wpcf7cf_visible_groups"]'); |
| 128 | |
| 129 | var hidden_fields = []; |
| 130 | var hidden_groups = []; |
| 131 | var visible_groups = []; |
| 132 | |
| 133 | $form.find('input:hidden,select:hidden,textarea:hidden').each(function () { |
| 134 | hidden_fields.push($(this).attr('name')); |
| 135 | }); |
| 136 | |
| 137 | $form.find('[data-class="wpcf7cf_group"]:hidden').each(function () { |
| 138 | hidden_groups.push($(this).attr('id')); |
| 139 | }); |
| 140 | |
| 141 | $form.find('[data-class="wpcf7cf_group"]:visible').each(function () { |
| 142 | visible_groups.push($(this).attr('id')); |
| 143 | }); |
| 144 | |
| 145 | $($hidden_group_fields).val(JSON.stringify(hidden_fields)); |
| 146 | $($hidden_groups).val(JSON.stringify(hidden_groups)); |
| 147 | $($visible_groups).val(JSON.stringify(visible_groups)); |
| 148 | |
| 149 | return true; |
| 150 | }); |
| 151 | }); |
| 152 | |
| 153 | //reset the form completely |
| 154 | $( document ).ajaxComplete(function(e,xhr) { |
| 155 | if( typeof xhr.responseJSON !== 'undefined' && |
| 156 | typeof xhr.responseJSON.mailSent !== 'undefined' && |
| 157 | typeof xhr.responseJSON.into !== 'undefined' && |
| 158 | xhr.responseJSON.mailSent === true) |
| 159 | { |
| 160 | $( xhr.responseJSON.into + ' input, '+xhr.responseJSON.into+' select, ' + xhr.responseJSON.into + ' textarea' ).change(); |
| 161 | } |
| 162 | }); |
| 163 | |
| 164 | // fix for exclusive checkboxes in IE (this will call the change-event again after all other checkboxes are unchecked, triggering the display_fields() function) |
| 165 | var old_wpcf7ExclusiveCheckbox = $.fn.wpcf7ExclusiveCheckbox; |
| 166 | $.fn.wpcf7ExclusiveCheckbox = function() { |
| 167 | return this.find('input:checkbox').click(function() { |
| 168 | var name = $(this).attr('name'); |
| 169 | console.log('new func'); |
| 170 | $(this).closest('form').find('input:checkbox[name="' + name + '"]').not(this).prop('checked', false).eq(0).change(); |
| 171 | }); |
| 172 | }; |
| 173 | |
| 174 | })( jQuery ); |
| 175 |