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