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.4 0.1.5 0.1.6 0.1.7 0.2 0.2.1 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
385 lines
1 var cf7signature_resized = 0; // for compatibility with contact-form-7-signature-addon
2
3 var wpcf7cf_timeout;
4
5 var wpcf7cf_show_animation = { "height": "show", "marginTop": "show", "marginBottom": "show", "paddingTop": "show", "paddingBottom": "show" };
6 var wpcf7cf_hide_animation = { "height": "hide", "marginTop": "hide", "marginBottom": "hide", "paddingTop": "hide", "paddingBottom": "hide" };
7
8 var wpcf7cf_show_step_animation = { "width": "show", "marginLeft": "show", "marginRight": "show", "paddingRight": "show", "paddingLeft": "show" };
9 var wpcf7cf_hide_step_animation = { "width": "hide", "marginLeft": "hide", "marginRight": "hide", "paddingRight": "hide", "paddingLeft": "hide" };
10
11 var wpcf7cf_change_events = 'input.wpcf7cf paste.wpcf7cf change.wpcf7cf click.wpcf7cf propertychange.wpcf7cf';
12
13 var wpcf7cf_forms = [];
14
15 function Wpcf7cfForm($form) {
16
17 var options_element = $form.find('input[name="_wpcf7cf_options"]').eq(0);
18 if (!options_element.length || !options_element.val()) {
19 // doesn't look like a CF7 form created with conditional fields plugin enabled.
20 return false;
21 }
22
23 var form = this;
24
25 var form_options = JSON.parse(options_element.val());
26
27 form.$form = $form;
28 form.$hidden_group_fields = $form.find('[name="_wpcf7cf_hidden_group_fields"]');
29 form.$hidden_groups = $form.find('[name="_wpcf7cf_hidden_groups"]');
30 form.$visible_groups = $form.find('[name="_wpcf7cf_visible_groups"]');
31 form.$repeaters = $form.find('[name="_wpcf7cf_repeaters"]');
32
33 form.unit_tag = $form.closest('.wpcf7').attr('id');
34 form.conditions = form_options['conditions'];
35 form.settings = form_options['settings'];
36
37 form.$groups = [];
38 form.repeaters = [];
39 form.multistep = null;
40 form.fields = [];
41
42 form.settings.animation_intime = parseInt(form.settings.animation_intime);
43 form.settings.animation_outtime = parseInt(form.settings.animation_outtime);
44
45 if (form.settings.animation === 'no') {
46 form.settings.animation_intime = 0;
47 form.settings.animation_outtime = 0;
48 }
49
50 form.updateGroups();
51 form.updateEventListeners();
52 form.displayFields();
53
54 // bring form in initial state if the reset event is fired on it.
55 form.$form.on('reset', form, function(e) {
56 var form = e.data;
57 setTimeout(function(){
58 form.displayFields();
59 },200);
60 });
61
62 //removed pro functions
63
64 }
65 Wpcf7cfForm.prototype.displayFields = function() {
66
67 var form = this;
68
69 var unit_tag = this.unit_tag;
70 var wpcf7cf_conditions = this.conditions;
71 var wpcf7cf_settings = this.settings;
72
73 //for compatibility with contact-form-7-signature-addon
74 if (cf7signature_resized === 0 && typeof signatures !== 'undefined' && signatures.constructor === Array && signatures.length > 0 ) {
75 for (var i = 0; i < signatures.length; i++) {
76 if (signatures[i].canvas.width === 0) {
77
78 var $sig_canvas = jQuery(".wpcf7-form-control-signature-body>canvas");
79 var $sig_wrap = jQuery(".wpcf7-form-control-signature-wrap");
80 $sig_canvas.eq(i).attr('width', $sig_wrap.width());
81 $sig_canvas.eq(i).attr('height', $sig_wrap.height());
82
83 cf7signature_resized = 1;
84 }
85 }
86 }
87
88 form.$groups.addClass('wpcf7cf-hidden');
89
90 for (var i=0; i < wpcf7cf_conditions.length; i++) {
91
92 var condition = wpcf7cf_conditions[i];
93
94 // compatibility with conditional forms created with older versions of the plugin ( < 1.4 )
95 if (!('and_rules' in condition)) {
96 condition.and_rules = [{'if_field':condition.if_field,'if_value':condition.if_value,'operator':condition.operator}];
97 }
98
99 var show_group = wpcf7cf.should_group_be_shown(condition, form.$form);
100
101 if (show_group) {
102 jQuery('[data-id='+condition.then_field+']',form.$form).eq(0).removeClass('wpcf7cf-hidden');
103 }
104 }
105
106 var animation_intime = wpcf7cf_settings.animation_intime;
107 var animation_outtime = wpcf7cf_settings.animation_outtime;
108
109 form.$groups.each(function (index) {
110 $group = jQuery(this);
111 if ($group.is(':animated')) $group.finish(); // stop any current animations on the group
112 if ($group.css('display') === 'none' && !$group.hasClass('wpcf7cf-hidden')) {
113 if ($group.prop('tagName') === 'SPAN') {
114 $group.show().trigger('wpcf7cf_show_group');
115 } else {
116 $group.animate(wpcf7cf_show_animation, animation_intime).trigger('wpcf7cf_show_group'); // show
117 }
118 } else if ($group.css('display') !== 'none' && $group.hasClass('wpcf7cf-hidden')) {
119
120 if ($group.attr('data-clear_on_hide') !== undefined) {
121 $inputs = jQuery(':input', $group).not(':button, :submit, :reset, :hidden');
122 $inputs.prop('checked', false).prop('selected', false).prop('selectedIndex', 0);
123 $inputs.not('[type=checkbox],[type=radio],select').val('');
124 $inputs.change();
125 //display_fields();
126 }
127
128 if ($group.prop('tagName') === 'SPAN') {
129 $group.hide().trigger('wpcf7cf_hide_group');
130 } else {
131 $group.animate(wpcf7cf_hide_animation, animation_outtime).trigger('wpcf7cf_hide_group'); // hide
132 }
133
134 }
135 });
136
137 form.updateHiddenFields();
138 };
139 Wpcf7cfForm.prototype.updateHiddenFields = function() {
140
141 var form = this;
142
143 var hidden_fields = [];
144 var hidden_groups = [];
145 var visible_groups = [];
146
147 form.$groups.each(function () {
148 var $this = jQuery(this);
149 if ($this.hasClass('wpcf7cf-hidden')) {
150 hidden_groups.push($this.data('id'));
151 $this.find('input,select,textarea').each(function () {
152 hidden_fields.push(jQuery(this).attr('name'));
153 });
154 } else {
155 visible_groups.push($this.data('id'));
156 }
157 });
158
159 form.hidden_fields = hidden_fields;
160 form.hidden_groups = hidden_groups;
161 form.visible_groups = visible_groups;
162
163 form.$hidden_group_fields.val(JSON.stringify(hidden_fields));
164 form.$hidden_groups.val(JSON.stringify(hidden_groups));
165 form.$visible_groups.val(JSON.stringify(visible_groups));
166
167 return true;
168 };
169 Wpcf7cfForm.prototype.updateGroups = function() {
170 var form = this;
171 form.$groups = form.$form.find('[data-class="wpcf7cf_group"]');
172
173 form.$subgroups = form.$form.find('.wpcf7cf_repeater_sub [data-class="wpcf7cf_group"]');
174
175 // var sub_conditions = [];
176
177 form.$subgroups.each(function() {
178 $group = jQuery(this);
179 var index = $group.data('repeater_index');
180 var group_name_orig = $group.data('orig_id');
181
182 form.conditions.forEach(function (condition) {
183 if (condition.then_field !== group_name_orig) return;
184 var and_rules = [];
185 condition.and_rules.forEach(function(entry) {
186 and_rules.push({if_field:entry.if_field+'__'+index, operator: entry.operator, if_value:entry.if_value});
187 });
188 form.conditions.push({
189 then_field:condition.then_field+'__'+index,
190 and_rules: and_rules
191 });
192 });
193 });
194
195 };
196 Wpcf7cfForm.prototype.updateEventListeners = function() {
197
198 var form = this;
199
200 // monitor input changes, and call display_fields() if something has changed
201 jQuery('input, select, textarea, button',form.$form).not('.wpcf7cf_add, .wpcf7cf_remove').off(wpcf7cf_change_events).on(wpcf7cf_change_events,form, function(e) {
202 var form = e.data;
203 clearTimeout(wpcf7cf_timeout);
204 wpcf7cf_timeout = setTimeout(function() {
205 form.displayFields();
206 }, 100);
207 });
208
209 //removed pro functions
210 };
211
212 //removed pro functions
213
214 var wpcf7cf = {
215
216 // keep this for backwards compatibility
217 initForm : function($form) {
218 wpcf7cf_forms.push(new Wpcf7cfForm($form));
219 },
220
221 should_group_be_shown : function(condition, $current_form) {
222
223 var $ = jQuery;
224
225 var show_group = true;
226
227 for (var and_rule_i = 0; and_rule_i < condition.and_rules.length; and_rule_i++) {
228
229 var condition_ok = false;
230
231 var condition_and_rule = condition.and_rules[and_rule_i];
232
233 var $field = jQuery('[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);
234
235 var if_val = condition_and_rule.if_value;
236 var if_val_as_number = isFinite(parsedval=parseFloat(if_val)) ? parsedval:0;
237 var operator = condition_and_rule.operator;
238 var regex_patt = new RegExp(if_val, 'i');
239
240
241 if ($field.length === 1) {
242
243 // single field (tested with text field, single checkbox, select with single value (dropdown), select with multiple values)
244
245 if ($field.is('select')) {
246
247 if (operator === 'not equals') {
248 condition_ok = true;
249 }
250
251 $field.find('option:selected').each(function () {
252 var $option = jQuery(this);
253 option_val = $option.val()
254 if (
255 operator === 'equals' && option_val === if_val ||
256 operator === 'equals (regex)' && regex_patt.test($option.val())
257 ) {
258 condition_ok = true;
259 } else if (
260 operator === 'not equals' && option_val === if_val ||
261 operator === 'not equals (regex)' && !regex_patt.test($option.val())
262 ) {
263 condition_ok = false;
264 return false; // break out of the loop
265 }
266 });
267
268 show_group = show_group && condition_ok;
269 }
270
271 var field_val = $field.val();
272 var field_val_as_number = isFinite(parsedval=parseFloat(field_val)) ? parsedval:0;
273
274 if ($field.attr('type') === 'checkbox') {
275 var field_is_checked = $field.is(':checked');
276 if (
277 operator === 'equals' && field_is_checked && field_val === if_val ||
278 operator === 'not equals' && !field_is_checked ||
279 operator === 'is empty' && !field_is_checked ||
280 operator === 'not empty' && field_is_checked ||
281 operator === '>' && field_is_checked && field_val_as_number > if_val_as_number ||
282 operator === '<' && field_is_checked && field_val_as_number < if_val_as_number ||
283 operator === '' && field_is_checked && field_val_as_number >= if_val_as_number ||
284 operator === '' && field_is_checked && field_val_as_number <= if_val_as_number ||
285 operator === 'equals (regex)' && field_is_checked && regex_patt.test(field_val) ||
286 operator === 'not equals (regex)' && !field_is_checked
287
288 ) {
289 condition_ok = true;
290 }
291 } else if (
292 operator === 'equals' && field_val === if_val ||
293 operator === 'not equals' && field_val !== if_val ||
294 operator === 'equals (regex)' && regex_patt.test(field_val) ||
295 operator === 'not equals (regex)' && !regex_patt.test(field_val) ||
296 operator === '>' && field_val_as_number > if_val_as_number ||
297 operator === '<' && field_val_as_number < if_val_as_number ||
298 operator === '' && field_val_as_number >= if_val_as_number ||
299 operator === '' && field_val_as_number <= if_val_as_number ||
300 operator === 'is empty' && field_val === '' ||
301 operator === 'not empty' && field_val !== '' ||
302 (
303 operator === 'function'
304 && typeof window[if_val] == 'function'
305 && window[if_val]($field)
306 )
307 ) {
308 condition_ok = true;
309 }
310
311
312 } else if ($field.length > 1) {
313
314 // multiple fields (tested with checkboxes, exclusive checkboxes, dropdown with multiple values)
315
316 var all_values = [];
317 var checked_values = [];
318 $field.each(function () {
319 all_values.push(jQuery(this).val());
320 if (jQuery(this).is(':checked')) {
321 checked_values.push(jQuery(this).val());
322 }
323 });
324
325 var checked_value_index = jQuery.inArray(if_val, checked_values);
326 var value_index = jQuery.inArray(if_val, all_values);
327
328 if (
329 ( operator === 'is empty' && checked_values.length === 0 ) ||
330 ( operator === 'not empty' && checked_values.length > 0 )
331 ) {
332 condition_ok = true;
333 }
334
335
336 for (var ind = 0; ind < checked_values.length; ind++) {
337 var checked_val = checked_values[ind];
338 var checked_val_as_number = isFinite(parsedval=parseFloat(checked_val)) ? parsedval:0;
339 if (
340 ( operator === 'equals' && checked_val === if_val ) ||
341 ( operator === 'not equals' && checked_val !== if_val ) ||
342 ( operator === 'equals (regex)' && regex_patt.test(checked_val) ) ||
343 ( operator === 'not equals (regex)' && !regex_patt.test(checked_val) ) ||
344 ( operator === '>' && checked_val_as_number > if_val_as_number ) ||
345 ( operator === '<' && checked_val_as_number < if_val_as_number ) ||
346 ( operator === '' && checked_val_as_number >= if_val_as_number ) ||
347 ( operator === '' && checked_val_as_number <= if_val_as_number )
348 ) {
349 condition_ok = true;
350 }
351 }
352 }
353
354 show_group = show_group && condition_ok;
355 }
356
357 return show_group;
358
359 }
360
361 };
362
363
364 jQuery('.wpcf7-form').each(function(){
365 wpcf7cf_forms.push(new Wpcf7cfForm(jQuery(this)));
366 });
367
368 // Call displayFields again on all forms
369 // Necessary in case some theme or plugin changed a form value by the time the entire page is fully loaded.
370 jQuery('document').ready(function() {
371 wpcf7cf_forms.forEach(function(f){
372 f.displayFields();
373 });
374 });
375
376 // fix for exclusive checkboxes in IE (this will call the change-event again after all other checkboxes are unchecked, triggering the display_fields() function)
377 var old_wpcf7ExclusiveCheckbox = jQuery.fn.wpcf7ExclusiveCheckbox;
378 jQuery.fn.wpcf7ExclusiveCheckbox = function() {
379 return this.find('input:checkbox').click(function() {
380 var name = jQuery(this).attr('name');
381 jQuery(this).closest('form').find('input:checkbox[name="' + name + '"]').not(this).prop('checked', false).eq(0).change();
382 });
383 };
384
385