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