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 0.2.2 0.2.3
cf7-conditional-fields / js / scripts.js
cf7-conditional-fields / js Last commit date
scripts.js 9 years ago scripts_admin.js 9 years ago
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