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