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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 7 years ago scripts_admin.js 7 years ago
scripts.js
323 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
8 var show_animation = { "height": "show", "marginTop": "show", "marginBottom": "show", "paddingTop": "show", "paddingBottom": "show" };
9 var hide_animation = { "height": "hide", "marginTop": "hide", "marginBottom": "hide", "paddingTop": "hide", "paddingBottom": "hide" };
10
11 $('.wpcf7').each(function(){
12
13 // Bug fix thanks to 972 creative (@toddedelman) https://wordpress.org/support/topic/conditional-fields-not-opening-using-radio-buttons/#post-10442923
14
15 var $this = $(this);
16 var options_element = $this.find('input[name="_wpcf7cf_options"]').eq(0);
17 if (options_element.length) {
18 var value = options_element.val();
19 if (value) {
20 form_options = JSON.parse(value);
21 form_options.unit_tag = $this.attr('id');
22 options.push(form_options);
23 }
24 }
25 });
26
27 $(document).ready(function() {
28
29 //wpcf7pro-togglebutton
30
31 $('.wpcf7cf-togglebutton').click(function() {
32 $this = $(this);
33 if ($this.text() == $this.data('val-1')) {
34 $this.text($this.data('val-2'));
35 $this.val($this.data('val-2'));
36 } else {
37 $this.text($this.data('val-1'));
38 $this.val($this.data('val-1'));
39 }
40 });
41
42
43 function display_fields(unit_tag, wpcf7cf_conditions, wpcf7cf_settings) {
44
45 $current_form = $('#'+unit_tag);
46 $groups = $("[data-class='wpcf7cf_group']",$current_form);
47
48 //for compatibility with contact-form-7-signature-addon
49 if (cf7signature_resized == 0 && typeof signatures !== 'undefined' && signatures.constructor === Array && signatures.length > 0 ) {
50 for (var i = 0; i < signatures.length; i++) {
51 if (signatures[i].canvas.width == 0) {
52
53 jQuery(".wpcf7-form-control-signature-body>canvas").eq(i).attr('width', jQuery(".wpcf7-form-control-signature-wrap").width());
54 jQuery(".wpcf7-form-control-signature-body>canvas").eq(i).attr('height', jQuery(".wpcf7-form-control-signature-wrap").height());
55
56 cf7signature_resized = 1;
57 }
58 }
59 }
60
61 $groups.addClass('wpcf7cf-hidden');
62
63 for (var i=0; i < wpcf7cf_conditions.length; i++) {
64
65 var condition = wpcf7cf_conditions[i];
66
67 // compatibility with conditional forms created with older versions of the plugin ( < 1.4 )
68 if (!('and_rules' in condition)) {
69 condition.and_rules = [{'if_field':condition.if_field,'if_value':condition.if_value,'operator':condition.operator}];
70 }
71
72 var show_group = should_group_be_shown(condition, $current_form);
73
74 if (show_group) {
75 $('[data-id='+condition.then_field+']',$current_form).eq(0).removeClass('wpcf7cf-hidden');
76 }
77 }
78
79 var animation_intime = parseInt(wpcf7cf_settings.animation_intime);
80 var animation_outtime = parseInt(wpcf7cf_settings.animation_outtime);
81
82 if (wpcf7cf_settings.animation === 'no') {
83 animation_intime = 0;
84 animation_outtime = 0;
85 }
86
87 $groups.each(function (index) {
88 $group = $(this);
89 if ($group.is(':animated')) $group.finish(); // stop any current animations on the group
90 if ($group.css('display') === 'none' && !$group.hasClass('wpcf7cf-hidden')) {
91 if ($group.prop('tagName') === 'SPAN') {
92 $group.show().trigger('wpcf7cf_show_group');
93 } else {
94 $group.animate(show_animation, animation_intime).trigger('wpcf7cf_show_group'); // show
95 }
96 } else if ($group.css('display') !== 'none' && $group.hasClass('wpcf7cf-hidden')) {
97 if ($group.prop('tagName') === 'SPAN') {
98 $group.hide().trigger('wpcf7cf_hide_group');
99 } else {
100 $group.animate(hide_animation, animation_outtime).trigger('wpcf7cf_hide_group'); // hide
101 }
102
103 if ($group.attr('data-clear_on_hide') !== undefined) {
104 $inputs = $(':input', $group).not(':button, :submit, :reset, :hidden');
105 $inputs.prop('checked', false).prop('selected', false).prop('selectedIndex', 0);
106 $inputs.not('[type=checkbox],[type=radio],select').val('');
107 $inputs.change();
108 //display_fields();
109 }
110 }
111 });
112
113 wpcf7cf_update_hidden_fields($current_form);
114 }
115
116 var timeout;
117
118 for (var i = 0; i<options.length; i++) {
119
120 var unit_tag = options[i]['unit_tag'];
121 var conditions = options[i]['conditions'];
122 var settings = options[i]['settings'];
123
124 display_fields(unit_tag, conditions, settings);
125
126 $('#'+unit_tag+' input, #'+unit_tag+' select, #'+unit_tag+' textarea, #'+unit_tag+' button').on('input paste change click',{unit_tag:unit_tag, conditions:conditions, settings:settings}, function(e) {
127 clearTimeout(timeout);
128 timeout = setTimeout(display_fields, 100, e.data.unit_tag, e.data.conditions, e.data.settings);
129 });
130
131 // bring form in initial state if
132 $('#'+unit_tag+' form').on('reset', {unit_tag:unit_tag, conditions:conditions, settings:settings}, function(e) {
133 setTimeout(display_fields, 200, e.data.unit_tag, e.data.conditions, e.data.settings);
134 });
135 }
136
137 // Also add hidden fields in case a form gets submitted without any input:
138 $('form.wpcf7-form').each(function(){
139 wpcf7cf_update_hidden_fields($(this));
140 });
141 });
142
143 function wpcf7cf_update_hidden_fields($form) {
144
145 $hidden_group_fields = $form.find('[name="_wpcf7cf_hidden_group_fields"]');
146 $hidden_groups = $form.find('[name="_wpcf7cf_hidden_groups"]');
147 $visible_groups = $form.find('[name="_wpcf7cf_visible_groups"]');
148 $repeaters = $form.find('[name="_wpcf7cf_repeaters"]');
149
150 var hidden_fields = [];
151 var hidden_groups = [];
152 var visible_groups = [];
153
154 $form.find('[data-class="wpcf7cf_group"]').each(function () {
155 var $this = $(this);
156 if ($this.hasClass('wpcf7cf-hidden')) {
157 hidden_groups.push($this.data('id'));
158 $this.find('input,select,textarea').each(function () {
159 hidden_fields.push($(this).attr('name'));
160 });
161 } else {
162 visible_groups.push($this.data('id'));
163 }
164 });
165
166 $hidden_group_fields.val(JSON.stringify(hidden_fields));
167 $hidden_groups.val(JSON.stringify(hidden_groups));
168 $visible_groups.val(JSON.stringify(visible_groups));
169
170 return true;
171 }
172
173 //reset the form completely
174 $( document ).ajaxComplete(function(e,xhr) {
175 if( typeof xhr.responseJSON !== 'undefined' &&
176 typeof xhr.responseJSON.mailSent !== 'undefined' &&
177 typeof xhr.responseJSON.into !== 'undefined' &&
178 xhr.responseJSON.mailSent === true)
179 {
180 $( xhr.responseJSON.into + ' input, '+xhr.responseJSON.into+' select, ' + xhr.responseJSON.into + ' textarea, ' + xhr.responseJSON.into + ' button' ).change();
181 }
182 });
183
184 // fix for exclusive checkboxes in IE (this will call the change-event again after all other checkboxes are unchecked, triggering the display_fields() function)
185 var old_wpcf7ExclusiveCheckbox = $.fn.wpcf7ExclusiveCheckbox;
186 $.fn.wpcf7ExclusiveCheckbox = function() {
187 return this.find('input:checkbox').click(function() {
188 var name = $(this).attr('name');
189 $(this).closest('form').find('input:checkbox[name="' + name + '"]').not(this).prop('checked', false).eq(0).change();
190 });
191 };
192
193
194
195
196 function should_group_be_shown(condition, $current_form) {
197
198 var show_group = true;
199
200 for (var and_rule_i = 0; and_rule_i < condition.and_rules.length; and_rule_i++) {
201
202 var condition_ok = false;
203
204 var condition_and_rule = condition.and_rules[and_rule_i];
205
206 var regex_patt = new RegExp(condition_and_rule.if_value, 'i');
207
208 $field = $('[name="' + condition_and_rule.if_field + '"], [name="' + condition_and_rule.if_field + '[]"], [data-original-name="' + condition_and_rule.if_field + '"], [data-original-name="' + condition_and_rule.if_field + '[]"]',$current_form); //, [data-original-name="' + condition_and_rule.if_field + '"]
209
210 //TODO: ignore everything outside the sub_repeater if field is inside sub_repeater
211
212 if ($field.length == 1) {
213
214 // single field (tested with text field, single checkbox, select with single value (dropdown), select with multiple values)
215
216 if ($field.is('select')) {
217
218 if (condition_and_rule.operator == 'not equals') {
219 condition_ok = true;
220 }
221
222 $field.find('option:selected').each(function () {
223 var $option = $(this);
224 if (
225 condition_and_rule.operator == 'equals' && $option.val() == condition_and_rule.if_value ||
226 condition_and_rule.operator == 'equals (regex)' && regex_patt.test($option.val())
227 ) {
228 condition_ok = true;
229 } else if (
230 condition_and_rule.operator == 'not equals' && $option.val() == condition_and_rule.if_value ||
231 condition_and_rule.operator == 'not equals (regex)' && !regex_patt.test($option.val())
232 ) {
233 condition_ok = false;
234 }
235 });
236
237 show_group = show_group && condition_ok;
238
239 return show_group;
240 }
241
242 if ($field.attr('type') == 'checkbox') {
243 if (
244 condition_and_rule.operator == 'equals' && $field.is(':checked') && $field.val() == condition_and_rule.if_value ||
245 condition_and_rule.operator == 'not equals' && !$field.is(':checked') ||
246 condition_and_rule.operator == 'is empty' && !$field.is(':checked') ||
247 condition_and_rule.operator == 'not empty' && $field.is(':checked') ||
248 condition_and_rule.operator == '>' && $field.is(':checked') && $field.val() > condition_and_rule.if_value ||
249 condition_and_rule.operator == '<' && $field.is(':checked') && $field.val() < condition_and_rule.if_value ||
250 condition_and_rule.operator == '>=' && $field.is(':checked') && $field.val() >= condition_and_rule.if_value ||
251 condition_and_rule.operator == '<=' && $field.is(':checked') && $field.val() <= condition_and_rule.if_value ||
252 condition_and_rule.operator == 'equals (regex)' && $field.is(':checked') && regex_patt.test($field.val()) ||
253 condition_and_rule.operator == 'not equals (regex)' && !$field.is(':checked')
254 ) {
255 condition_ok = true;
256 }
257 } else if (
258 ( condition_and_rule.operator == 'equals' && $field.val() == condition_and_rule.if_value ) ||
259 ( condition_and_rule.operator == 'not equals' && $field.val() != condition_and_rule.if_value ) ||
260 ( condition_and_rule.operator == 'equals (regex)' && regex_patt.test($field.val()) ) ||
261 ( condition_and_rule.operator == 'not equals (regex)' && !regex_patt.test($field.val()) ) ||
262 ( condition_and_rule.operator == '>' && $field.val() > condition_and_rule.if_value ) ||
263 ( condition_and_rule.operator == '<' && $field.val() < condition_and_rule.if_value ) ||
264 ( condition_and_rule.operator == '>=' && $field.val() >= condition_and_rule.if_value ) ||
265 ( condition_and_rule.operator == '<=' && $field.val() <= condition_and_rule.if_value ) ||
266 ( condition_and_rule.operator == 'is empty' && $field.val() == '' ) ||
267 ( condition_and_rule.operator == 'not empty' && $field.val() != '' )
268 ) {
269 condition_ok = true;
270 }
271
272
273 } else if ($field.length > 1) {
274
275 // multiple fields (tested with checkboxes, exclusive checkboxes, dropdown with multiple values)
276
277 var all_values = [];
278 var checked_values = [];
279 $field.each(function () {
280 all_values.push($(this).val());
281 if ($(this).is(':checked')) {
282 checked_values.push($(this).val());
283 }
284 });
285
286 var checked_value_index = $.inArray(condition_and_rule.if_value, checked_values);
287 var value_index = $.inArray(condition_and_rule.if_value, all_values);
288
289 if (
290 ( condition_and_rule.operator == 'is empty' && checked_values.length == 0 ) ||
291 ( condition_and_rule.operator == 'not empty' && checked_values.length > 0 )
292 ) {
293 condition_ok = true;
294 }
295
296
297 for (var ind = 0; ind < checked_values.length; ind++) {
298 if (
299 ( condition_and_rule.operator == 'equals' && checked_values[ind] == condition_and_rule.if_value ) ||
300 ( condition_and_rule.operator == 'not equals' && checked_values[ind] != condition_and_rule.if_value ) ||
301 ( condition_and_rule.operator == 'equals (regex)' && regex_patt.test(checked_values[ind]) ) ||
302 ( condition_and_rule.operator == 'not equals (regex)' && !regex_patt.test(checked_values[ind]) ) ||
303 ( condition_and_rule.operator == '>' && checked_values[ind] > condition_and_rule.if_value ) ||
304 ( condition_and_rule.operator == '<' && checked_values[ind] < condition_and_rule.if_value ) ||
305 ( condition_and_rule.operator == '>=' && checked_values[ind] >= condition_and_rule.if_value ) ||
306 ( condition_and_rule.operator == '<=' && checked_values[ind] <= condition_and_rule.if_value )
307 ) {
308 condition_ok = true;
309 }
310 }
311 }
312
313 show_group = show_group && condition_ok;
314 }
315
316 return show_group;
317
318 }
319
320
321 //removed pro functions
322
323 })( jQuery );