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 / js / scripts.js
cf7-conditional-fields / js Last commit date
polyfill.js 3 years ago scripts.js 8 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
947 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 form.updateSummaryFields();
340 };
341
342 Wpcf7cfForm.prototype.updateSummaryFields = function() {
343 const form = this;
344 const $summary = form.get('.wpcf7cf-summary');
345
346 if ($summary.length == 0 || !$summary.is(':visible')) {
347 return;
348 }
349
350 const fd = new FormData();
351
352 const formdata = form.$form.serializeArray();
353 jQuery.each(formdata,function(key, input){
354 fd.append(input.name, input.value);
355 });
356
357 // Make sure to add file fields to FormData
358 jQuery.each(form.$form.find('input[type="file"]'), function(index, el) {
359 if (! el.files.length) return true; // continue
360 const fieldName = el.name;
361 fd.append(fieldName, new Blob() , Array.from(el.files).map(file => file.name).join(', '));
362 });
363
364 // add file fields to form-data
365
366 jQuery.ajax({
367 url: wpcf7cf_global_settings.ajaxurl + '?action=wpcf7cf_get_summary',
368 type: 'POST',
369 data: fd,
370 processData: false,
371 contentType: false,
372 dataType: 'json',
373 success: function(json) {
374 $summary.html(json.summaryHtml);
375 }
376 });
377 };
378
379 Wpcf7cfForm.prototype.updateHiddenFields = function() {
380
381 const form = this;
382
383 const hidden_fields = [];
384 const hidden_groups = [];
385 const visible_groups = [];
386
387 form.$groups.each(function () {
388 const $group = jQuery(this);
389 if ($group.hasClass('wpcf7cf-hidden')) {
390 hidden_groups.push($group.attr('data-id'));
391 if($group.attr('data-disable_on_hide') !== undefined) {
392 // fields inside hidden disable_on_hide group
393 $group.find('input,select,textarea').each(function(){
394 const $this = jQuery(this);
395 if (!$this.prop('disabled')) {
396 $this.prop('disabled', true).trigger('changedisabledprop.wpcf7cf');
397 }
398
399 // if there's no other field with the same name visible in the form
400 // then push this field to hidden_fields
401 if (form.$form.find(`[data-class="wpcf7cf_group"]:not(.wpcf7cf-hidden) [name='${$this.attr('name')}']`).length === 0) {
402 hidden_fields.push($this.attr('name'));
403 }
404 })
405 $group.find('.wpcf7-form-control-wrap').addClass('wpcf7cf-disabled');
406 } else {
407 // fields inside regular hidden group are all pushed to hidden_fields
408 $group.find('input,select,textarea').each(function () {
409 hidden_fields.push(jQuery(this).attr('name'));
410 });
411 }
412 } else {
413 visible_groups.push($group.attr('data-id'));
414 }
415 });
416
417 form.hidden_fields = hidden_fields;
418 form.hidden_groups = hidden_groups;
419 form.visible_groups = visible_groups;
420
421 form.$input_hidden_group_fields.val(JSON.stringify(hidden_fields));
422 form.$input_hidden_groups.val(JSON.stringify(hidden_groups));
423 form.$input_visible_groups.val(JSON.stringify(visible_groups));
424
425 return true;
426 };
427 Wpcf7cfForm.prototype.updateGroups = function() {
428 const form = this;
429 form.$groups = form.$form.find('[data-class="wpcf7cf_group"]');
430 form.$groups.height('auto');
431 form.conditions = window.wpcf7cf.get_nested_conditions(form);
432
433 };
434 Wpcf7cfForm.prototype.updateEventListeners = function() {
435
436 const form = this;
437
438 // monitor input changes, and call displayFields() if something has changed
439 form.get('input, select, textarea, button').not('.wpcf7cf_add, .wpcf7cf_remove').off(wpcf7cf_change_events).on(wpcf7cf_change_events,form, function(e) {
440 window.wpcf7cf_updatingGroups = true; // while this is true, don't allow the form to be submitted.
441 const form = e.data;
442 clearTimeout(wpcf7cf_timeout);
443 wpcf7cf_timeout = setTimeout(function() {
444 window.wpcf7cf.updateMultistepState(form.multistep);
445 form.updateSimpleDom();
446 form.displayFields();
447 window.wpcf7cf_updatingGroups = false;
448 }, wpcf7cf_change_time_ms);
449 });
450
451
452 };
453
454
455
456 /**
457 * @global
458 * @namespace wpcf7cf
459 */
460 window.wpcf7cf = {
461
462 hideGroup : function($group, animate) {
463
464 },
465
466 showGroup : function($group, animate) {
467
468 },
469
470 updateRepeaterSubHTML : function(html, oldSuffix, newSuffix, parentRepeaters) {
471 const oldIndexes = oldSuffix.split('__');
472 oldIndexes.shift(); // remove first empty element
473 const newIndexes = newSuffix.split('__');
474 newIndexes.shift(); // remove first empty element
475
476 let returnHtml = html;
477
478 if (
479 oldIndexes && newIndexes &&
480 oldIndexes.length === parentRepeaters.length &&
481 newIndexes.length === parentRepeaters.length
482 ) {
483
484 const parentRepeatersInfo = parentRepeaters.map((repeaterId, i) => {
485 return {[repeaterId.split('__')[0]]: [oldIndexes[i], newIndexes[i]]};
486 });
487
488 const length = parentRepeatersInfo.length;
489
490 let replacements = oldIndexes.map( (oldIndex, i) => {
491 return [
492 '__'+oldIndexes.slice(0,length-i).join('__'),
493 '__'+newIndexes.slice(0,length-i).join('__'),
494 ];
495 });
496
497
498 for (let i=0; i<length ; i++) {
499 const id = Object.keys(parentRepeatersInfo[i])[0];
500 const find = parentRepeatersInfo[i][id][0];
501 const repl = parentRepeatersInfo[i][id][1];
502 replacements.push([
503 `<span class="wpcf7cf-index wpcf7cf__${id}">${find}<\\/span>`,
504 `<span class="wpcf7cf-index wpcf7cf__${id}">${repl}</span>`
505 ]);
506 }
507
508 replacements.forEach( ([oldSuffix, newSuffix]) => {
509 returnHtml = returnHtml.replace(new RegExp(oldSuffix,'g'), newSuffix);
510 });
511
512 }
513
514 return returnHtml ;
515 },
516
517 // keep this for backwards compatibility
518 initForm : function($forms) {
519 $forms.each(function(){
520 const formElement = this;
521 // only add form is its class is "wpcf7-form" and if the form was not previously added
522 if (
523 formElement.classList.contains('wpcf7-form') &&
524 !wpcf7cf_forms.some((form)=>{ return form.$form.get(0) === formElement; })
525 ) {
526 wpcf7cf_forms.push(new Wpcf7cfForm(formElement));
527 }
528 });
529 },
530
531 getWpcf7cfForm : function ($form) {
532 const matched_forms = wpcf7cf_forms.filter((form)=>{
533 return form.$form.get(0) === $form.get(0);
534 });
535 if (matched_forms.length) {
536 return matched_forms[0];
537 }
538 return false;
539 },
540
541 get_nested_conditions : function(form) {
542 const conditions = form.initial_conditions;
543 //loop trough conditions. Then loop trough the dom, and each repeater we pass we should update all sub_values we encounter with __index
544 form.reloadSimpleDom();
545 const groups = Object.values(form.simpleDom).filter(function(item, i) {
546 return item.type==='group';
547 });
548
549 let sub_conditions = [];
550
551 for(let i = 0; i < groups.length; i++) {
552 const g = groups[i];
553 let relevant_conditions = conditions.filter(function(condition, i) {
554 return condition.then_field === g.original_name;
555 });
556
557 relevant_conditions = relevant_conditions.map(function(item,i) {
558 return {
559 then_field : g.name,
560 and_rules : item.and_rules.map(function(and_rule, i) {
561 return {
562 if_field : and_rule.if_field+g.suffix,
563 if_value : and_rule.if_value,
564 operator : and_rule.operator
565 };
566 })
567 }
568 });
569
570 sub_conditions = sub_conditions.concat(relevant_conditions);
571 }
572 return sub_conditions;
573 },
574
575 get_simplified_dom_model : function(currentNode, simplified_dom = {}, parentGroups = [], parentRepeaters = []) {
576
577
578 const type = currentNode.classList && currentNode.classList.contains('wpcf7cf_repeater') ? 'repeater' :
579 currentNode.dataset.class == 'wpcf7cf_group' ? 'group' :
580 currentNode.className == 'wpcf7cf_step' ? 'step' :
581 currentNode.hasAttribute('name') ? 'input' : false;
582
583 let newParentRepeaters = [...parentRepeaters];
584 let newParentGroups = [...parentGroups];
585
586 if (type) {
587
588 const name = type === 'input' ? currentNode.getAttribute('name') : currentNode.dataset.id;
589
590 if (type === 'repeater') {
591 newParentRepeaters.push(name);
592 }
593 if (type === 'group') {
594 newParentGroups.push(name);
595 }
596
597 // skip _wpcf7 hidden fields
598 if (name.substring(0,6) === '_wpcf7') return {};
599
600 const original_name = type === 'repeater' || type === 'group' ? currentNode.dataset.orig_data_id
601 : type === 'input' ? (currentNode.getAttribute('data-orig_name') || name)
602 : name;
603
604 const nameWithoutBrackets = name.replace('[]','');
605 const originalNameWithoutBrackets = original_name.replace('[]','');
606
607 const val = type === 'step' ? [currentNode.dataset.id.substring(5)] : [];
608
609 const suffix = nameWithoutBrackets.replace(originalNameWithoutBrackets, '');
610
611 if (!simplified_dom[name]) {
612 // init entry
613 simplified_dom[name] = {name, type, original_name, suffix, val, parentGroups, parentRepeaters}
614 }
615
616 if (type === 'input') {
617
618 // skip unchecked checkboxes and radiobuttons
619 if ( (currentNode.type === 'checkbox' || currentNode.type === 'radio') && !currentNode.checked ) return {};
620
621 // if multiselect, make sure to add all the values
622 if ( currentNode.multiple && currentNode.options ) {
623 simplified_dom[name].val = Object.values(currentNode.options).filter(o => o.selected).map(o => o.value)
624 } else {
625 simplified_dom[name].val.push(currentNode.value);
626 }
627 }
628 }
629
630 // can't use currentNode.children (because then field name cannot be "children")
631 const getter = Object.getOwnPropertyDescriptor(Element.prototype, "children").get;
632 const children = getter.call(currentNode);
633
634 Array.from(children).forEach(childNode => {
635 const dom = wpcf7cf.get_simplified_dom_model(childNode, simplified_dom, newParentGroups, newParentRepeaters);
636 simplified_dom = {...dom, ...simplified_dom} ;
637 });
638
639 return simplified_dom;
640 },
641
642 updateMultistepState: function (multistep) {
643 if (multistep == null) return;
644 if (multistep.form.$form.hasClass('submitting')) return;
645
646 // update hidden input field
647
648 const stepsData = {
649 currentStep : multistep.currentStep,
650 numSteps : multistep.numSteps,
651 fieldsInCurrentStep : multistep.getFieldsInStep(multistep.currentStep)
652 };
653 multistep.form.$input_steps.val(JSON.stringify(stepsData));
654
655 // update Buttons
656 multistep.$btn_prev.removeClass('disabled').attr('disabled', false);
657 multistep.$btn_next.removeClass('disabled').attr('disabled', false);
658 if (multistep.currentStep == multistep.numSteps) {
659 multistep.$btn_next.addClass('disabled').attr('disabled', true);
660 }
661 if (multistep.currentStep == 1) {
662 multistep.$btn_prev.addClass('disabled').attr('disabled', true);
663 }
664
665 // replace next button with submit button on last step.
666 // TODO: make this depend on a setting
667 const $submit_button = multistep.form.$form.find('input[type="submit"]:last').eq(0);
668 const $ajax_loader = multistep.form.$form.find('.wpcf7-spinner').eq(0);
669
670 $submit_button.detach().prependTo(multistep.$btn_next.parent());
671 $ajax_loader.detach().prependTo(multistep.$btn_next.parent());
672
673 if (multistep.currentStep == multistep.numSteps) {
674 multistep.$btn_next.hide();
675 $submit_button.show();
676 } else {
677 $submit_button.hide();
678 multistep.$btn_next.show();
679 }
680
681 // update dots
682 const $dots = multistep.$dots.find('.dot');
683 $dots.removeClass('active').removeClass('completed');
684 for(let step = 1; step <= multistep.numSteps; step++) {
685 if (step < multistep.currentStep) {
686 $dots.eq(step-1).addClass('completed');
687 } else if (step == multistep.currentStep) {
688 $dots.eq(step-1).addClass('active');
689 }
690 }
691
692 },
693
694 should_group_be_shown : function(condition, form) {
695
696 let show_group = true;
697 let atLeastOneFieldFound = false;
698
699 for (let and_rule_i = 0; and_rule_i < condition.and_rules.length; and_rule_i++) {
700
701 let condition_ok = false;
702
703 const condition_and_rule = condition.and_rules[and_rule_i];
704
705 const inputField = form.getFieldByName(condition_and_rule.if_field);
706
707 if (!inputField) continue; // field not found
708
709 atLeastOneFieldFound = true;
710
711 const if_val = condition_and_rule.if_value;
712 let operator = condition_and_rule.operator;
713
714 //backwards compat
715 operator = operator === '' ? 'less than or equals' : operator;
716 operator = operator === '' ? 'greater than or equals' : operator;
717 operator = operator === '>' ? 'greater than' : operator;
718 operator = operator === '<' ? 'less than' : operator;
719
720 const $field = operator === 'function' && jQuery(`[name="${inputField.name}"]`).eq(0);
721
722 condition_ok = this.isConditionTrue(inputField.val,operator,if_val, $field);
723
724 show_group = show_group && condition_ok;
725 }
726
727 return show_group && atLeastOneFieldFound;
728
729 },
730
731 isConditionTrue(values, operator, testValue='', $field=jQuery()) {
732
733 if (!Array.isArray(values)) {
734 values = [values];
735 }
736
737 let condition_ok = false; // start by assuming that the condition is not met
738
739 // Considered EMPTY: [] [''] [null] ['',null] [,,'']
740 // Considered NOT EMPTY: [0] ['ab','c'] ['',0,null]
741 const valuesAreEmpty = values.length === 0 || values.every((v) => !v&&v!==0); // 0 is not considered empty
742
743 // special cases: [] equals '' => TRUE; [] not equals '' => FALSE
744 if (operator === 'equals' && testValue === '' && valuesAreEmpty) {
745 return true;
746 }
747 if (operator === 'not equals' && testValue === '' && valuesAreEmpty) {
748 return false;
749 }
750
751 if (valuesAreEmpty) {
752 if (operator === 'is empty') {
753 condition_ok = true;
754 }
755 } else {
756 if (operator === 'not empty') {
757 condition_ok = true;
758 }
759 }
760
761 const testValueNumber = isFinite(parseFloat(testValue)) ? parseFloat(testValue) : NaN;
762
763
764 if (operator === 'not equals' || operator === 'not equals (regex)') {
765 // start by assuming that the condition is met
766 condition_ok = true;
767 }
768
769 if (
770 operator === 'function'
771 && typeof window[testValue] == 'function'
772 && window[testValue]($field) // here we call the actual user defined function
773 ) {
774 condition_ok = true;
775 }
776
777 let regex_patt = /.*/i; // fallback regex pattern
778 let isValidRegex = true;
779 if (operator === 'equals (regex)' || operator === 'not equals (regex)') {
780 try {
781 regex_patt = new RegExp(testValue, 'i');
782 } catch(e) {
783 isValidRegex = false;
784 }
785 }
786
787
788 for(let i = 0; i < values.length; i++) {
789
790 const value = values[i];
791
792 const valueNumber = isFinite(parseFloat(value)) ? parseFloat(value) : NaN;
793 const valsAreNumbers = !isNaN(valueNumber) && !isNaN(testValueNumber);
794
795 if (
796
797 operator === 'equals' && value === testValue ||
798 operator === 'equals (regex)' && regex_patt.test(value) ||
799 operator === 'greater than' && valsAreNumbers && valueNumber > testValueNumber ||
800 operator === 'less than' && valsAreNumbers && valueNumber < testValueNumber ||
801 operator === 'greater than or equals' && valsAreNumbers && valueNumber >= testValueNumber ||
802 operator === 'less than or equals' && valsAreNumbers && valueNumber <= testValueNumber
803
804 ) {
805
806 condition_ok = true;
807 break;
808
809 } else if (
810
811 operator === 'not equals' && value === testValue ||
812 operator === 'not equals (regex)' && regex_patt.test(value)
813
814 ) {
815
816 condition_ok = false;
817 break;
818
819 }
820 }
821
822 return condition_ok;
823
824 },
825
826 getFormObj($form) {
827 if (typeof $form === 'string') {
828 $form = jQuery($form).eq(0);
829 }
830 return wpcf7cf.getWpcf7cfForm($form);
831 },
832
833 getRepeaterObj($form, repeaterDataId) {
834 const form = wpcf7cf.getFormObj($form);
835 const repeater = form.repeaters.find( repeater => repeater.params.$repeater.attr('data-id') === repeaterDataId );
836
837 return repeater;
838
839 },
840
841 getMultiStepObj($form){
842 const form = wpcf7cf.getFormObj($form);
843 return form.multistep;
844 },
845
846 /**
847 * Append a new sub-entry to the repeater with the name `repeaterDataId` inside the form `$form`
848 * @memberof wpcf7cf
849 * @function wpcf7cf.repeaterAddSub
850 * @link
851 * @param {String|JQuery} $form - JQuery object or css-selector representing the form
852 * @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)
853 */
854 repeaterAddSub($form,repeaterDataId) {
855 const repeater = wpcf7cf.getRepeaterObj($form, repeaterDataId);
856 repeater.updateSubs(repeater.params.$repeater.num_subs+1);
857 },
858
859 /**
860 * Insert a new sub-entry at the given `index` of the repeater with the name `repeaterDataId` inside the form `$form`
861 * @memberof wpcf7cf
862 * @param {String|JQuery} $form - JQuery object or css-selector representing the form
863 * @param {String} repeaterDataId - *data-id* attribute of the repeater.
864 * @param {Number} index - position where to insert the new sub-entry within the repeater
865 */
866 repeaterAddSubAtIndex($form,repeaterDataId,index) {
867 const repeater = wpcf7cf.getRepeaterObj($form, repeaterDataId);
868 repeater.addSubs(1, index);
869 },
870
871 /**
872 * Remove the sub-entry at the given `index` of the repeater with the *data-id* attribute of `repeaterDataId` inside the form `$form`
873 * @memberof wpcf7cf
874 * @param {String|JQuery} $form - JQuery object or css-selector representing the form
875 * @param {String} repeaterDataId - *data-id* attribute of the repeater.
876 * @param {Number} index - position where to insert the new sub-entry within the repeater
877 */
878 repeaterRemoveSubAtIndex($form,repeaterDataId,index) {
879 const repeater = wpcf7cf.getRepeaterObj($form, repeaterDataId);
880 repeater.removeSubs(1, index);
881 },
882
883 /**
884 * Remove the last sub-entry from the repeater with the *data-id* attribute of `repeaterDataId` inside the form `$form`
885 * @memberof wpcf7cf
886 * @param {String|JQuery} $form - JQuery object or css-selector representing the form
887 * @param {String} repeaterDataId - *data-id* attribute of the repeater.
888 * @param {Number} index - position where to insert the new sub-entry within the repeater
889 */
890 repeaterRemoveSub($form,repeaterDataId) {
891 const repeater = wpcf7cf.getRepeaterObj($form, repeaterDataId);
892 repeater.updateSubs(repeater.params.$repeater.num_subs-1);
893 },
894
895 /**
896 * Set the number of subs for the repeater with the *data-id* attribute of `repeaterDataId` inside the form `$form`.
897 * Subs are either appended to or removed from the end of the repeater.
898 * @memberof wpcf7cf
899 * @param {String|JQuery} $form - JQuery object or css-selector representing the form
900 * @param {String} repeaterDataId - *data-id* attribute of the repeater.
901 * @param {Number} numberOfSubs - position where to insert the new sub-entry within the repeater
902 */
903 repeaterSetNumberOfSubs($form, repeaterDataId, numberOfSubs) {
904 const repeater = wpcf7cf.getRepeaterObj($form, repeaterDataId);
905 repeater.updateSubs(numberOfSubs);
906 },
907
908 /**
909 * Move to step number `step`, ignoring any validation.
910 * @memberof wpcf7cf
911 * @param {String|JQuery} $form - JQuery object or css-selector representing the form
912 * @param {*} step
913 */
914 multistepMoveToStep($form, step) {
915 const multistep = wpcf7cf.getMultiStepObj($form);
916 multistep.moveToStep(step);
917 },
918
919 /**
920 * Validate the current step, and move to step number `step` if validation passes.
921 * @memberof wpcf7cf
922 * @param {String|JQuery} $form - JQuery object or css-selector representing the form
923 * @param {Number} step
924 */
925 async multistepMoveToStepWithValidation($form, step) {
926 const multistep = wpcf7cf.getMultiStepObj($form);
927
928 const result = await multistep.validateStep(multistep.currentStep);
929 if (result === 'success') {
930 multistep.moveToStep(step);
931 }
932 },
933
934
935 };
936
937 document.querySelectorAll('.wpcf7-form').forEach(function(formElement){
938 wpcf7cf_forms.push(new Wpcf7cfForm(formElement));
939 });
940
941 // Call displayFields again on all forms
942 // Necessary in case some theme or plugin changed a form value by the time the entire page is fully loaded.
943 document.addEventListener('DOMContentLoaded', function () {
944 wpcf7cf_forms.forEach(function(f){
945 f.displayFields();
946 });
947 });