PluginProbe ʕ •ᴥ•ʔ
Conditional Fields for Contact Form 7 / 2.6.2
Conditional Fields for Contact Form 7 v2.6.2
2.7.8 2.7.7 2.7.6 2.7.5 2.7.4 2.7.3 2.7.2 0.2.4 0.2.5 0.2.6 0.2.7 0.2.8 0.2.9 1.0 1.1 1.2 1.2.1 1.2.2 1.2.3 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.4 1.4.1 1.4.2 1.4.3 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.6.1 1.6.2 1.6.3 1.6.5 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.8 1.7.9 1.8 1.8.1 1.8.2 1.8.3 1.8.5 1.8.6 1.8.7 1.9 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.14 1.9.15 1.9.16 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2 2.2.1 2.2.10 2.2.11 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3 2.3.1 2.3.10 2.3.11 2.3.12 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.15 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.14 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.7 2.7.1 trunk 0.1 0.1.1 0.1.2 0.1.3 0.1.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
polyfill.js 3 years ago scripts.js 11 months ago scripts.js.map 3 years ago scripts_admin copy.js 4 years ago scripts_admin.js 1 year ago scripts_admin_all_pages.js 2 years ago scripts_es6.js 3 years ago temp.js 4 years ago
scripts.js
954 lines
1 "use strict";
2
3 let cf7signature_resized = 0; // for compatibility with contact-form-7-signature-addon
4
5 let wpcf7cf_timeout;
6 let wpcf7cf_change_time_ms = 100; // the timeout after a change in the form is detected
7
8 // needed for multistep validation
9 if (window.wpcf7 && !wpcf7.setStatus) {
10 wpcf7.setStatus = ( form, status ) => {
11 form = form.length ? form[0] : form; // if form is a jQuery object, only grab te html-element
12 const defaultStatuses = new Map( [
13 // 0: Status in API response, 1: Status in HTML class
14 [ 'init', 'init' ],
15 [ 'validation_failed', 'invalid' ],
16 [ 'acceptance_missing', 'unaccepted' ],
17 [ 'spam', 'spam' ],
18 [ 'aborted', 'aborted' ],
19 [ 'mail_sent', 'sent' ],
20 [ 'mail_failed', 'failed' ],
21 [ 'submitting', 'submitting' ],
22 [ 'resetting', 'resetting' ],
23 ] );
24
25 if ( defaultStatuses.has( status ) ) {
26 status = defaultStatuses.get( status );
27 }
28
29 if ( ! Array.from( defaultStatuses.values() ).includes( status ) ) {
30 status = status.replace( /[^0-9a-z]+/i, ' ' ).trim();
31 status = status.replace( /\s+/, '-' );
32 status = `custom-${ status }`;
33 }
34
35 const prevStatus = form.getAttribute( 'data-status' );
36
37 form.wpcf7.status = status;
38 form.setAttribute( 'data-status', status );
39 form.classList.add( status );
40
41 if ( prevStatus && prevStatus !== status ) {
42 form.classList.remove( prevStatus );
43 }
44
45 return status;
46 };
47 }
48
49 if (window.wpcf7cf_running_tests) {
50 jQuery('input[name="_wpcf7cf_options"]').each(function(e) {
51 const $input = jQuery(this);
52 const opt = JSON.parse($input.val());
53 opt.settings.animation_intime = 0;
54 opt.settings.animation_outtime = 0;
55 $input.val(JSON.stringify(opt));
56 });
57 wpcf7cf_change_time_ms = 0;
58 }
59
60 const wpcf7cf_show_animation = { "height": "show", "marginTop": "show", "marginBottom": "show", "paddingTop": "show", "paddingBottom": "show" };
61 const wpcf7cf_hide_animation = { "height": "hide", "marginTop": "hide", "marginBottom": "hide", "paddingTop": "hide", "paddingBottom": "hide" };
62
63 const wpcf7cf_show_step_animation = { "opacity": "show" };
64 const wpcf7cf_hide_step_animation = { "opacity": "hide" };
65
66 const wpcf7cf_change_events = 'input.wpcf7cf paste.wpcf7cf change.wpcf7cf click.wpcf7cf propertychange.wpcf7cf changedisabledprop.wpcf7cf';
67
68 const wpcf7cf_forms = [];
69
70 const Wpcf7cfForm = function($form) {
71
72 const options_element = $form.find('input[name="_wpcf7cf_options"]').eq(0);
73 if (!options_element.length || !options_element.val()) {
74 // doesn't look like a CF7 form created with conditional fields plugin enabled.
75 return false;
76 }
77
78 const form = this;
79
80 // always wait until groups are updated before submitting the form
81 $form[0].addEventListener('submit', function(e) {
82 if (window.wpcf7cf_updatingGroups) {
83 e.preventDefault();
84 e.stopImmediatePropagation();
85
86 // Wait until updatingGroups is false, then submit again
87 const retry = () => {
88 if (!window.wpcf7cf_updatingGroups) {
89 $form.off('.wpcf7cf-autosubmit'); // prevent duplicates
90 $form[0].requestSubmit(); // safe submit
91 } else {
92 requestAnimationFrame(retry);
93 }
94 };
95
96 // Attach listener to prevent loop if user resubmits manually
97 $form.on('submit.wpcf7cf-autosubmit', (e) => e.preventDefault());
98
99 retry(); // start retry loop
100 return false;
101 }
102 }, true); // use capture to run before WP's own handler
103
104 // Disable submit buttons while the form is submitting
105 const submitButtons = $form[0].querySelectorAll('button[type=submit], input[type=submit]');
106 const observer = new MutationObserver(() => {
107 const isSubmitting = $form[0].classList.contains('submitting');
108
109 submitButtons.forEach(button => {
110 button.disabled = isSubmitting;
111 button.classList.toggle('is-disabled', isSubmitting);
112 });
113 });
114 observer.observe($form[0], { attributes: true, attributeFilter: ['class'] });
115
116 const form_options = JSON.parse(options_element.val());
117
118 form.$form = $form;
119 form.$input_hidden_group_fields = $form.find('[name="_wpcf7cf_hidden_group_fields"]');
120 form.$input_hidden_groups = $form.find('[name="_wpcf7cf_hidden_groups"]');
121 form.$input_visible_groups = $form.find('[name="_wpcf7cf_visible_groups"]');
122 form.$input_repeaters = $form.find('[name="_wpcf7cf_repeaters"]');
123 form.$input_steps = $form.find('[name="_wpcf7cf_steps"]');
124
125 form.unit_tag = $form.closest('.wpcf7').attr('id');
126 form.conditions = form_options['conditions'];
127
128 form.simpleDom = null;
129
130 form.reloadSimpleDom = function() {
131 form.simpleDom = wpcf7cf.get_simplified_dom_model(form.$form[0]);
132 }
133
134 // quicker than reloading the simpleDom completely with reloadSimpleDom
135 form.updateSimpleDom = function() {
136 if (!form.simpleDom) {
137 form.reloadSimpleDom();
138 }
139 const inputs = Object.values(form.simpleDom).filter(item => item.type === 'input');
140 const formdata = new FormData(form.$form[0]);
141
142 let formdataEntries = [... formdata.entries()].map(entry => [ entry[0], entry[1].name ?? entry[1] ]);
143 const buttonEntries = Array.from(form.$form[0].querySelectorAll('button'), b => [b.name, b.value]);
144 formdataEntries = formdataEntries.concat(buttonEntries);
145
146 inputs.forEach(simpleDomItem => {
147 const newValue = form.getNewDomValueIfChanged(simpleDomItem, formdataEntries);
148 if (newValue !== null) {
149 form.simpleDom[simpleDomItem.name].val = newValue;
150 }
151 });
152
153 }
154
155 form.isDomMatch = function(simpleDomItem, formDataEntries) {
156 const simpleDomItemName = simpleDomItem.name;
157 const simpleDomItemValues = simpleDomItem.val;
158 const currentValues = formDataEntries.filter(entry => entry[0] === simpleDomItemName).map(entry => entry[1]);
159 return currentValues.join('|') === simpleDomItemValues.join('|');
160 }
161
162 /**
163 *
164 * @param {*} simpleDomItem
165 * @param {*} formDataEntries
166 * @returns the new value, or NULL if no change
167 */
168 form.getNewDomValueIfChanged = function(simpleDomItem, formDataEntries) {
169 const simpleDomItemName = simpleDomItem.name;
170 const simpleDomItemValues = simpleDomItem.val;
171 const currentValues = formDataEntries.filter(entry => entry[0] === simpleDomItemName).map(entry => entry[1]);
172 return currentValues.join('|') === simpleDomItemValues.join('|') ? null : currentValues;
173 }
174
175 // Wrapper around jQuery(selector, form.$form)
176 form.get = function (selector) {
177 // TODO: implement some caching here.
178 return jQuery(selector, form.$form);
179 }
180
181 form.getFieldByName = function(name) {
182 return form.simpleDom[name] || form.simpleDom[name+'[]'];
183 }
184
185 // compatibility with conditional forms created with older versions of the plugin ( < 1.4 )
186 for (let i=0; i < form.conditions.length; i++) {
187 const condition = form.conditions[i];
188 if (!('and_rules' in condition)) {
189 condition.and_rules = [{'if_field':condition.if_field,'if_value':condition.if_value,'operator':condition.operator}];
190 }
191 }
192
193 form.initial_conditions = form.conditions;
194 form.settings = form_options['settings'];
195
196 form.$groups = jQuery(); // empty jQuery set
197 form.repeaters = [];
198 form.multistep = null;
199 form.fields = [];
200
201 form.settings.animation_intime = parseInt(form.settings.animation_intime);
202 form.settings.animation_outtime = parseInt(form.settings.animation_outtime);
203
204 if (form.settings.animation === 'no') {
205 form.settings.animation_intime = 0;
206 form.settings.animation_outtime = 0;
207 }
208
209 form.updateGroups();
210 form.updateEventListeners();
211 form.displayFields();
212
213 // bring form in initial state if the reset event is fired on it.
214 // (CF7 triggers the 'reset' event by default on each successfully submitted form)
215 form.$form.on('reset.wpcf7cf', form, function(e) {
216 const form = e.data;
217 setTimeout(function(){
218 form.reloadSimpleDom();
219 form.displayFields();
220 form.resetRepeaters();
221 if (form.multistep != null) {
222 form.multistep.moveToStep(1, false);
223 }
224 setTimeout(function(){
225 if (form.$form.hasClass('sent')) {
226 jQuery('.wpcf7-response-output', form.$form)[0].scrollIntoView({behavior: "smooth", block:"nearest", inline:"nearest"});
227 }
228 }, 400);
229 },200);
230 });
231
232
233
234 }
235
236 /**
237 * reset initial number of subs for each repeater.
238 * (does not clear values)
239 */
240 Wpcf7cfForm.prototype.resetRepeaters = function() {
241 const form = this;
242 form.repeaters.forEach(repeater => {
243 repeater.updateSubs( repeater.params.$repeater.initial_subs );
244 });
245 }
246
247 Wpcf7cfForm.prototype.displayFields = function() {
248
249 const form = this;
250
251 const wpcf7cf_conditions = this.conditions;
252 const wpcf7cf_settings = this.settings;
253
254 //for compatibility with contact-form-7-signature-addon
255 if (cf7signature_resized === 0 && typeof signatures !== 'undefined' && signatures.constructor === Array && signatures.length > 0 ) {
256 for (let i = 0; i < signatures.length; i++) {
257 if (signatures[i].canvas.width === 0) {
258
259 const $sig_canvas = jQuery(".wpcf7-form-control-signature-body>canvas");
260 const $sig_wrap = jQuery(".wpcf7-form-control-signature-wrap");
261 $sig_canvas.eq(i).attr('width', $sig_wrap.width());
262 $sig_canvas.eq(i).attr('height', $sig_wrap.height());
263
264 cf7signature_resized = 1;
265 }
266 }
267 }
268
269 form.$groups.addClass('wpcf7cf-hidden');
270
271 for (let i=0; i < wpcf7cf_conditions.length; i++) {
272
273 const condition = wpcf7cf_conditions[i];
274
275 const show_group = window.wpcf7cf.should_group_be_shown(condition, form);
276
277 if (show_group) {
278 form.get('[data-id="'+condition.then_field+'"]').removeClass('wpcf7cf-hidden');
279 }
280 }
281
282
283 const animation_intime = wpcf7cf_settings.animation_intime;
284 const animation_outtime = wpcf7cf_settings.animation_outtime;
285
286 form.$groups.each(function (index) {
287 const $group = jQuery(this);
288 if ($group.is(':animated')) {
289 $group.finish(); // stop any current animations on the group
290 }
291 if ($group.css('display') === 'none' && !$group.hasClass('wpcf7cf-hidden')) {
292 if ($group.prop('tagName') === 'SPAN') {
293 $group.show().trigger('wpcf7cf_show_group'); // show instantly
294 } else {
295 $group.animate(wpcf7cf_show_animation, animation_intime).trigger('wpcf7cf_show_group'); // show with animation
296 }
297
298 if($group.attr('data-disable_on_hide') !== undefined) {
299 $group.find(':input').prop('disabled', false).trigger('changedisabledprop.wpcf7cf');
300 $group.find('.wpcf7-form-control-wrap').removeClass('wpcf7cf-disabled');
301 }
302
303 } else if ($group.css('display') !== 'none' && $group.hasClass('wpcf7cf-hidden')) {
304
305 if ($group.attr('data-clear_on_hide') !== undefined) {
306 const $inputs = jQuery(':input', $group).not(':button, :submit, :reset, :hidden');
307
308 $inputs.each(function(){
309 const $this = jQuery(this);
310 $this.val(this.defaultValue);
311 $this.prop('checked', this.defaultChecked);
312 });
313
314 jQuery('option', $group).each(function() {
315 this.selected = this.defaultSelected;
316 });
317
318 jQuery('select', $group).each(function() {
319 const $select = jQuery(this);
320 if ($select.val() === null) {
321 $select.val(jQuery("option:first",$select).val());
322 }
323 });
324
325 $inputs.each(function(){this.dispatchEvent(new Event("change",{"bubbles":true}))});
326 }
327
328 if ($group.prop('tagName') === 'SPAN') {
329 $group.hide().trigger('wpcf7cf_hide_group');
330 } else {
331 $group.animate(wpcf7cf_hide_animation, animation_outtime).trigger('wpcf7cf_hide_group'); // hide
332 }
333 }
334 });
335
336 form.updateHiddenFields();
337 form.updateSummaryFields();
338 };
339
340 Wpcf7cfForm.prototype.updateSummaryFields = function() {
341 const form = this;
342 const $summary = form.get('.wpcf7cf-summary');
343
344 if ($summary.length == 0 || !$summary.is(':visible')) {
345 return;
346 }
347
348 const fd = new FormData();
349
350 const formdata = form.$form.serializeArray();
351 jQuery.each(formdata,function(key, input){
352 fd.append(input.name, input.value);
353 });
354
355 // Make sure to add file fields to FormData
356 jQuery.each(form.$form.find('input[type="file"]'), function(index, el) {
357 if (! el.files.length) return true; // continue
358 const fieldName = el.name;
359 fd.append(fieldName, new Blob() , Array.from(el.files).map(file => file.name).join(', '));
360 });
361
362 // add file fields to form-data
363
364 jQuery.ajax({
365 url: wpcf7cf_global_settings.ajaxurl + '?action=wpcf7cf_get_summary',
366 type: 'POST',
367 data: fd,
368 processData: false,
369 contentType: false,
370 dataType: 'json',
371 success: function(json) {
372 $summary.html(json.summaryHtml);
373 }
374 });
375 };
376
377 Wpcf7cfForm.prototype.updateHiddenFields = function() {
378
379 const form = this;
380
381 const hidden_fields = [];
382 const hidden_groups = [];
383 const visible_groups = [];
384
385 form.$groups.each(function () {
386 const $group = jQuery(this);
387 if ($group.hasClass('wpcf7cf-hidden')) {
388 hidden_groups.push($group.attr('data-id'));
389 if($group.attr('data-disable_on_hide') !== undefined) {
390 // fields inside hidden disable_on_hide group
391 $group.find('input,select,textarea').each(function(){
392 const $this = jQuery(this);
393 if (!$this.prop('disabled')) {
394 $this.prop('disabled', true).trigger('changedisabledprop.wpcf7cf');
395 }
396
397 // if there's no other field with the same name visible in the form
398 // then push this field to hidden_fields
399 if (form.$form.find(`[data-class="wpcf7cf_group"]:not(.wpcf7cf-hidden) [name='${$this.attr('name')}']`).length === 0) {
400 hidden_fields.push($this.attr('name'));
401 }
402 })
403 $group.find('.wpcf7-form-control-wrap').addClass('wpcf7cf-disabled');
404 } else {
405 // fields inside regular hidden group are all pushed to hidden_fields
406 $group.find('input,select,textarea').each(function () {
407 hidden_fields.push(jQuery(this).attr('name'));
408 });
409 }
410 } else {
411 visible_groups.push($group.attr('data-id'));
412 }
413 });
414
415 form.hidden_fields = hidden_fields;
416 form.hidden_groups = hidden_groups;
417 form.visible_groups = visible_groups;
418
419 form.$input_hidden_group_fields.val(JSON.stringify(hidden_fields));
420 form.$input_hidden_groups.val(JSON.stringify(hidden_groups));
421 form.$input_visible_groups.val(JSON.stringify(visible_groups));
422
423 return true;
424 };
425 Wpcf7cfForm.prototype.updateGroups = function() {
426 const form = this;
427 form.$groups = form.$form.find('[data-class="wpcf7cf_group"]');
428 form.$groups.height('auto');
429 form.conditions = window.wpcf7cf.get_nested_conditions(form);
430
431 };
432 Wpcf7cfForm.prototype.updateEventListeners = function() {
433
434 const form = this;
435
436 // monitor input changes, and call displayFields() if something has changed
437 form.get('input, select, textarea, button').not('.wpcf7cf_add, .wpcf7cf_remove').off(wpcf7cf_change_events).on(wpcf7cf_change_events,form, function(e) {
438 window.wpcf7cf_updatingGroups = true; // while this is true, don't allow the form to be submitted.
439 const form = e.data;
440 clearTimeout(wpcf7cf_timeout);
441 wpcf7cf_timeout = setTimeout(function() {
442 window.wpcf7cf.updateMultistepState(form.multistep);
443 form.updateSimpleDom();
444 form.displayFields();
445 window.wpcf7cf_updatingGroups = false;
446 }, wpcf7cf_change_time_ms);
447 });
448
449
450 };
451
452
453
454 /**
455 * @global
456 * @namespace wpcf7cf
457 */
458 window.wpcf7cf = {
459
460 hideGroup : function($group, animate) {
461
462 },
463
464 showGroup : function($group, animate) {
465
466 },
467
468 updateRepeaterSubHTML : function(html, oldSuffix, newSuffix, parentRepeaters) {
469 const oldIndexes = oldSuffix.split('__');
470 oldIndexes.shift(); // remove first empty element
471 const newIndexes = newSuffix.split('__');
472 newIndexes.shift(); // remove first empty element
473
474 let returnHtml = html;
475
476 if (
477 oldIndexes && newIndexes &&
478 oldIndexes.length === parentRepeaters.length &&
479 newIndexes.length === parentRepeaters.length
480 ) {
481
482 const parentRepeatersInfo = parentRepeaters.map((repeaterId, i) => {
483 return {[repeaterId.split('__')[0]]: [oldIndexes[i], newIndexes[i]]};
484 });
485
486 const length = parentRepeatersInfo.length;
487
488 let replacements = oldIndexes.map( (oldIndex, i) => {
489 return [
490 '__'+oldIndexes.slice(0,length-i).join('__'),
491 '__'+newIndexes.slice(0,length-i).join('__'),
492 ];
493 });
494
495
496 for (let i=0; i<length ; i++) {
497 const id = Object.keys(parentRepeatersInfo[i])[0];
498 const find = parentRepeatersInfo[i][id][0];
499 const repl = parentRepeatersInfo[i][id][1];
500 replacements.push([
501 `<span class="wpcf7cf-index wpcf7cf__${id}">${find}<\\/span>`,
502 `<span class="wpcf7cf-index wpcf7cf__${id}">${repl}</span>`
503 ]);
504 }
505
506 replacements.forEach( ([oldSuffix, newSuffix]) => {
507 returnHtml = returnHtml.replace(new RegExp(oldSuffix,'g'), newSuffix);
508 });
509
510 }
511
512 return returnHtml ;
513 },
514
515 // keep this for backwards compatibility
516 initForm : function($forms) {
517 $forms.each(function(){
518 const $form = jQuery(this);
519 // only add form is its class is "wpcf7-form" and if the form was not previously added
520 if (
521 $form.hasClass('wpcf7-form') &&
522 !wpcf7cf_forms.some((form)=>{ return form.$form.get(0) === $form.get(0); })
523 ) {
524 wpcf7cf_forms.push(new Wpcf7cfForm($form));
525 }
526 });
527 },
528
529 getWpcf7cfForm : function ($form) {
530 const matched_forms = wpcf7cf_forms.filter((form)=>{
531 return form.$form.get(0) === $form.get(0);
532 });
533 if (matched_forms.length) {
534 return matched_forms[0];
535 }
536 return false;
537 },
538
539 get_nested_conditions : function(form) {
540 const conditions = form.initial_conditions;
541 //loop trough conditions. Then loop trough the dom, and each repeater we pass we should update all sub_values we encounter with __index
542 form.reloadSimpleDom();
543 const groups = Object.values(form.simpleDom).filter(function(item, i) {
544 return item.type==='group';
545 });
546
547 let sub_conditions = [];
548
549 for(let i = 0; i < groups.length; i++) {
550 const g = groups[i];
551 let relevant_conditions = conditions.filter(function(condition, i) {
552 return condition.then_field === g.original_name;
553 });
554
555 relevant_conditions = relevant_conditions.map(function(item,i) {
556 return {
557 then_field : g.name,
558 and_rules : item.and_rules.map(function(and_rule, i) {
559 return {
560 if_field : and_rule.if_field+g.suffix,
561 if_value : and_rule.if_value,
562 operator : and_rule.operator
563 };
564 })
565 }
566 });
567
568 sub_conditions = sub_conditions.concat(relevant_conditions);
569 }
570 return sub_conditions;
571 },
572
573 get_simplified_dom_model : function(currentNode, simplified_dom = {}, parentGroups = [], parentRepeaters = []) {
574
575
576 const type = currentNode.classList && currentNode.classList.contains('wpcf7cf_repeater') ? 'repeater' :
577 currentNode.dataset.class == 'wpcf7cf_group' ? 'group' :
578 currentNode.className == 'wpcf7cf_step' ? 'step' :
579 currentNode.hasAttribute('name') ? 'input' : false;
580
581 let newParentRepeaters = [...parentRepeaters];
582 let newParentGroups = [...parentGroups];
583
584 if (type) {
585
586 const name = type === 'input' ? currentNode.getAttribute('name') : currentNode.dataset.id;
587
588 if (type === 'repeater') {
589 newParentRepeaters.push(name);
590 }
591 if (type === 'group') {
592 newParentGroups.push(name);
593 }
594
595 // skip _wpcf7 hidden fields
596 if (name.substring(0,6) === '_wpcf7') return {};
597
598 const original_name = type === 'repeater' || type === 'group' ? currentNode.dataset.orig_data_id
599 : type === 'input' ? (currentNode.getAttribute('data-orig_name') || name)
600 : name;
601
602 const nameWithoutBrackets = name.replace('[]','');
603 const originalNameWithoutBrackets = original_name.replace('[]','');
604
605 const val = type === 'step' ? [currentNode.dataset.id.substring(5)] : [];
606
607 const suffix = nameWithoutBrackets.replace(originalNameWithoutBrackets, '');
608
609 if (!simplified_dom[name]) {
610 // init entry
611 simplified_dom[name] = {name, type, original_name, suffix, val, parentGroups, parentRepeaters}
612 }
613
614 if (type === 'input') {
615
616 // skip unchecked checkboxes and radiobuttons
617 if ( (currentNode.type === 'checkbox' || currentNode.type === 'radio') && !currentNode.checked ) return {};
618
619 // if multiselect, make sure to add all the values
620 if ( currentNode.multiple && currentNode.options ) {
621 simplified_dom[name].val = Object.values(currentNode.options).filter(o => o.selected).map(o => o.value)
622 } else {
623 simplified_dom[name].val.push(currentNode.value);
624 }
625 }
626 }
627
628 // can't use currentNode.children (because then field name cannot be "children")
629 const getter = Object.getOwnPropertyDescriptor(Element.prototype, "children").get;
630 const children = getter.call(currentNode);
631
632 Array.from(children).forEach(childNode => {
633 const dom = wpcf7cf.get_simplified_dom_model(childNode, simplified_dom, newParentGroups, newParentRepeaters);
634 simplified_dom = {...dom, ...simplified_dom} ;
635 });
636
637 return simplified_dom;
638 },
639
640 updateMultistepState: function (multistep) {
641 if (multistep == null) return;
642 if (multistep.form.$form.hasClass('submitting')) return;
643
644 // update hidden input field
645
646 const stepsData = {
647 currentStep : multistep.currentStep,
648 numSteps : multistep.numSteps,
649 fieldsInCurrentStep : multistep.getFieldsInStep(multistep.currentStep)
650 };
651 multistep.form.$input_steps.val(JSON.stringify(stepsData));
652
653 // update Buttons
654 multistep.$btn_prev.removeClass('disabled').attr('disabled', false);
655 multistep.$btn_next.removeClass('disabled').attr('disabled', false);
656 if (multistep.currentStep == multistep.numSteps) {
657 multistep.$btn_next.addClass('disabled').attr('disabled', true);
658 }
659 if (multistep.currentStep == 1) {
660 multistep.$btn_prev.addClass('disabled').attr('disabled', true);
661 }
662
663 // replace next button with submit button on last step.
664 // TODO: make this depend on a setting
665 const $submit_button = multistep.form.$form.find('input[type="submit"]:last').eq(0);
666 const $ajax_loader = multistep.form.$form.find('.wpcf7-spinner').eq(0);
667
668 $submit_button.detach().prependTo(multistep.$btn_next.parent());
669 $ajax_loader.detach().prependTo(multistep.$btn_next.parent());
670
671 if (multistep.currentStep == multistep.numSteps) {
672 multistep.$btn_next.hide();
673 $submit_button.show();
674 } else {
675 $submit_button.hide();
676 multistep.$btn_next.show();
677 }
678
679 // update dots
680 const $dots = multistep.$dots.find('.dot');
681 $dots.removeClass('active').removeClass('completed');
682 for(let step = 1; step <= multistep.numSteps; step++) {
683 if (step < multistep.currentStep) {
684 $dots.eq(step-1).addClass('completed');
685 } else if (step == multistep.currentStep) {
686 $dots.eq(step-1).addClass('active');
687 }
688 }
689
690 },
691
692 should_group_be_shown : function(condition, form) {
693
694 let show_group = true;
695 let atLeastOneFieldFound = false;
696
697 for (let and_rule_i = 0; and_rule_i < condition.and_rules.length; and_rule_i++) {
698
699 let condition_ok = false;
700
701 const condition_and_rule = condition.and_rules[and_rule_i];
702
703 const inputField = form.getFieldByName(condition_and_rule.if_field);
704
705 if (!inputField) continue; // field not found
706
707 atLeastOneFieldFound = true;
708
709 const if_val = condition_and_rule.if_value;
710 let operator = condition_and_rule.operator;
711
712 //backwards compat
713 operator = operator === '' ? 'less than or equals' : operator;
714 operator = operator === '' ? 'greater than or equals' : operator;
715 operator = operator === '>' ? 'greater than' : operator;
716 operator = operator === '<' ? 'less than' : operator;
717
718 const $field = operator === 'function' && jQuery(`[name="${inputField.name}"]`).eq(0);
719
720 condition_ok = this.isConditionTrue(inputField.val,operator,if_val, $field);
721
722 show_group = show_group && condition_ok;
723 }
724
725 return show_group && atLeastOneFieldFound;
726
727 },
728
729 isConditionTrue(values, operator, testValue='', $field=jQuery()) {
730
731 if (!Array.isArray(values)) {
732 values = [values];
733 }
734
735 let condition_ok = false; // start by assuming that the condition is not met
736
737 // Considered EMPTY: [] [''] [null] ['',null] [,,'']
738 // Considered NOT EMPTY: [0] ['ab','c'] ['',0,null]
739 const valuesAreEmpty = values.length === 0 || values.every((v) => !v&&v!==0); // 0 is not considered empty
740
741 // special cases: [] equals '' => TRUE; [] not equals '' => FALSE
742 if (operator === 'equals' && testValue === '' && valuesAreEmpty) {
743 return true;
744 }
745 if (operator === 'not equals' && testValue === '' && valuesAreEmpty) {
746 return false;
747 }
748
749 if (valuesAreEmpty) {
750 if (operator === 'is empty') {
751 condition_ok = true;
752 }
753 } else {
754 if (operator === 'not empty') {
755 condition_ok = true;
756 }
757 }
758
759 const testValueNumber = isFinite(parseFloat(testValue)) ? parseFloat(testValue) : NaN;
760
761
762 if (operator === 'not equals' || operator === 'not equals (regex)') {
763 // start by assuming that the condition is met
764 condition_ok = true;
765 }
766
767 if (
768 operator === 'function'
769 && typeof window[testValue] == 'function'
770 && window[testValue]($field) // here we call the actual user defined function
771 ) {
772 condition_ok = true;
773 }
774
775 let regex_patt = /.*/i; // fallback regex pattern
776 let isValidRegex = true;
777 if (operator === 'equals (regex)' || operator === 'not equals (regex)') {
778 try {
779 regex_patt = new RegExp(testValue, 'i');
780 } catch(e) {
781 isValidRegex = false;
782 }
783 }
784
785
786 for(let i = 0; i < values.length; i++) {
787
788 const value = values[i];
789
790 const valueNumber = isFinite(parseFloat(value)) ? parseFloat(value) : NaN;
791 const valsAreNumbers = !isNaN(valueNumber) && !isNaN(testValueNumber);
792
793 if (
794
795 operator === 'equals' && value === testValue ||
796 operator === 'equals (regex)' && regex_patt.test(value) ||
797 operator === 'greater than' && valsAreNumbers && valueNumber > testValueNumber ||
798 operator === 'less than' && valsAreNumbers && valueNumber < testValueNumber ||
799 operator === 'greater than or equals' && valsAreNumbers && valueNumber >= testValueNumber ||
800 operator === 'less than or equals' && valsAreNumbers && valueNumber <= testValueNumber
801
802 ) {
803
804 condition_ok = true;
805 break;
806
807 } else if (
808
809 operator === 'not equals' && value === testValue ||
810 operator === 'not equals (regex)' && regex_patt.test(value)
811
812 ) {
813
814 condition_ok = false;
815 break;
816
817 }
818 }
819
820 return condition_ok;
821
822 },
823
824 getFormObj($form) {
825 if (typeof $form === 'string') {
826 $form = jQuery($form).eq(0);
827 }
828 return wpcf7cf.getWpcf7cfForm($form);
829 },
830
831 getRepeaterObj($form, repeaterDataId) {
832 const form = wpcf7cf.getFormObj($form);
833 const repeater = form.repeaters.find( repeater => repeater.params.$repeater.attr('data-id') === repeaterDataId );
834
835 return repeater;
836
837 },
838
839 getMultiStepObj($form){
840 const form = wpcf7cf.getFormObj($form);
841 return form.multistep;
842 },
843
844 /**
845 * Append a new sub-entry to the repeater with the name `repeaterDataId` inside the form `$form`
846 * @memberof wpcf7cf
847 * @function wpcf7cf.repeaterAddSub
848 * @link
849 * @param {String|JQuery} $form - JQuery object or css-selector representing the form
850 * @param {String} repeaterDataId - *data-id* attribute of the repeater. Normally this is simply the name of the repeater. However, in case of a nested repeater you need to append the name with the correct suffix. For example `my-nested-repeater__1__3`. Hint (check the `data-id` attribute in the HTML code to find the correct suffix)
851 */
852 repeaterAddSub($form,repeaterDataId) {
853 const repeater = wpcf7cf.getRepeaterObj($form, repeaterDataId);
854 repeater.updateSubs(repeater.params.$repeater.num_subs+1);
855 },
856
857 /**
858 * Insert a new sub-entry at the given `index` of the repeater with the name `repeaterDataId` inside the form `$form`
859 * @memberof wpcf7cf
860 * @param {String|JQuery} $form - JQuery object or css-selector representing the form
861 * @param {String} repeaterDataId - *data-id* attribute of the repeater.
862 * @param {Number} index - position where to insert the new sub-entry within the repeater
863 */
864 repeaterAddSubAtIndex($form,repeaterDataId,index) {
865 const repeater = wpcf7cf.getRepeaterObj($form, repeaterDataId);
866 repeater.addSubs(1, index);
867 },
868
869 /**
870 * Remove the sub-entry at the given `index` of the repeater with the *data-id* attribute of `repeaterDataId` inside the form `$form`
871 * @memberof wpcf7cf
872 * @param {String|JQuery} $form - JQuery object or css-selector representing the form
873 * @param {String} repeaterDataId - *data-id* attribute of the repeater.
874 * @param {Number} index - position where to insert the new sub-entry within the repeater
875 */
876 repeaterRemoveSubAtIndex($form,repeaterDataId,index) {
877 const repeater = wpcf7cf.getRepeaterObj($form, repeaterDataId);
878 repeater.removeSubs(1, index);
879 },
880
881 /**
882 * Remove the last sub-entry from the repeater with the *data-id* attribute of `repeaterDataId` inside the form `$form`
883 * @memberof wpcf7cf
884 * @param {String|JQuery} $form - JQuery object or css-selector representing the form
885 * @param {String} repeaterDataId - *data-id* attribute of the repeater.
886 * @param {Number} index - position where to insert the new sub-entry within the repeater
887 */
888 repeaterRemoveSub($form,repeaterDataId) {
889 const repeater = wpcf7cf.getRepeaterObj($form, repeaterDataId);
890 repeater.updateSubs(repeater.params.$repeater.num_subs-1);
891 },
892
893 /**
894 * Set the number of subs for the repeater with the *data-id* attribute of `repeaterDataId` inside the form `$form`.
895 * Subs are either appended to or removed from the end of the repeater.
896 * @memberof wpcf7cf
897 * @param {String|JQuery} $form - JQuery object or css-selector representing the form
898 * @param {String} repeaterDataId - *data-id* attribute of the repeater.
899 * @param {Number} numberOfSubs - position where to insert the new sub-entry within the repeater
900 */
901 repeaterSetNumberOfSubs($form, repeaterDataId, numberOfSubs) {
902 const repeater = wpcf7cf.getRepeaterObj($form, repeaterDataId);
903 repeater.updateSubs(numberOfSubs);
904 },
905
906 /**
907 * Move to step number `step`, ignoring any validation.
908 * @memberof wpcf7cf
909 * @param {String|JQuery} $form - JQuery object or css-selector representing the form
910 * @param {*} step
911 */
912 multistepMoveToStep($form, step) {
913 const multistep = wpcf7cf.getMultiStepObj($form);
914 multistep.moveToStep(step);
915 },
916
917 /**
918 * Validate the current step, and move to step number `step` if validation passes.
919 * @memberof wpcf7cf
920 * @param {String|JQuery} $form - JQuery object or css-selector representing the form
921 * @param {Number} step
922 */
923 async multistepMoveToStepWithValidation($form, step) {
924 const multistep = wpcf7cf.getMultiStepObj($form);
925
926 const result = await multistep.validateStep(multistep.currentStep);
927 if (result === 'success') {
928 multistep.moveToStep(step);
929 }
930 },
931
932
933 };
934
935 jQuery('.wpcf7-form').each(function(){
936 wpcf7cf_forms.push(new Wpcf7cfForm(jQuery(this)));
937 });
938
939 // Call displayFields again on all forms
940 // Necessary in case some theme or plugin changed a form value by the time the entire page is fully loaded.
941 jQuery('document').ready( function() {
942 wpcf7cf_forms.forEach(function(f){
943 f.displayFields();
944 });
945 });
946
947 // fix for exclusive checkboxes in IE (this will call the change-event again after all other checkboxes are unchecked, triggering the display_fields() function)
948 const old_wpcf7ExclusiveCheckbox = jQuery.fn.wpcf7ExclusiveCheckbox;
949 jQuery.fn.wpcf7ExclusiveCheckbox = function() {
950 return this.find('input:checkbox').on('click', function() {
951 const name = jQuery(this).attr('name');
952 jQuery(this).closest('form').find('input:checkbox[name="' + name + '"]').not(this).prop('checked', false).eq(0).change();
953 });
954 };