PluginProbe ʕ •ᴥ•ʔ
Conditional Fields for Contact Form 7 / 2.7.10
Conditional Fields for Contact Form 7 v2.7.10
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_admin.js
cf7-conditional-fields / js Last commit date
polyfill.js 3 years ago scripts.js 17 hours ago scripts.js.map 3 years ago scripts_admin copy.js 4 years ago scripts_admin.js 17 hours ago scripts_admin_all_pages.js 8 months ago scripts_es6.js 3 years ago temp.js 4 years ago
scripts_admin.js
589 lines
1 /**
2 * These scripts are part of the Conditional Fields for Contact Form 7 plugin.
3 * Should only be loaded when editing a form in the WP backend.
4 */
5
6 let wpcf7cf_formcode = null; // used to detect if the form code has changed
7
8 var regexes = [
9 { label: wpcf7cf_options_0.regex_email_label, desc: wpcf7cf_options_0.regex_email },
10 { label: wpcf7cf_options_0.regex_numeric_label, desc: wpcf7cf_options_0.regex_numeric },
11 { label: wpcf7cf_options_0.regex_alphanumeric_label, desc: wpcf7cf_options_0.regex_alphanumeric },
12 { label: wpcf7cf_options_0.regex_alphabetic_label, desc: wpcf7cf_options_0.regex_alphabetic },
13 { label: wpcf7cf_options_0.regex_date_label, desc: wpcf7cf_options_0.regex_date },
14 { label: wpcf7cf_options_0.regex_custom_1_label, desc: wpcf7cf_options_0.regex_custom_1 },
15 { label: wpcf7cf_options_0.regex_custom_2_label, desc: wpcf7cf_options_0.regex_custom_2 },
16 { label: wpcf7cf_options_0.regex_custom_3_label, desc: wpcf7cf_options_0.regex_custom_3 },
17 { label: wpcf7cf_options_0.regex_custom_4_label, desc: wpcf7cf_options_0.regex_custom_4 },
18 { label: wpcf7cf_options_0.regex_custom_5_label, desc: wpcf7cf_options_0.regex_custom_5 },
19 ];
20
21 var i = regexes.length;
22 while (i--) {
23 if (null == regexes[i].label || null == regexes[i].desc || regexes[i].label == '' || regexes[i].desc == '') {
24 regexes.splice(i,1);
25 }
26 }
27
28
29 if (typeof(_wpcf7) != 'undefined' || typeof(wpcf7) != 'undefined') {
30
31 var wpcf7cf = {};
32
33 wpcf7cf.MAX_CONDITIONS = 50;
34
35 wpcf7cf.operators = [
36 'equals',
37 'not equals',
38 'greater than',
39 'greater than or equals',
40 'less than',
41 'less than or equals',
42 'is empty',
43 'not empty',
44 'equals (regex)',
45 'not equals (regex)',
46
47 ];
48
49 wpcf7cf.$newEntry = jQuery(`<div class="entry">
50 <div class="wpcf7cf-if">
51 <span class="label">Show</span>
52 <select class="then-field-select"></select>
53 </div>
54 <div class="wpcf7cf-and-rules ui-sortable" data-next-index="1">
55 <div class="wpcf7cf-and-rule ui-sortable-handle">
56 <span class="rule-part if-txt label">if</span>
57 <select class="rule-part if-field-select"></select>
58 <select class="rule-part operator">${ wpcf7cf.operators.map(o => `<option value="${o}">${o}</option>`) }</select>
59 <input class="rule-part if-value" type="text" placeholder="value" value="" style="visibility: visible;">
60 <span class="and-button" style="height: 22px; line-height: 22px;">And</span>
61 <span title="delete rule" class="rule-part delete-button">remove</span>
62 </div>
63 </div>
64 </div>`);
65 wpcf7cf.$textView = jQuery('#wpcf7cf-settings-text').eq(0);
66 wpcf7cf.$textOnlyCheckbox = jQuery('#wpcf7cf-text-only-checkbox').eq(0);
67 wpcf7cf.$textOnlyLinks = jQuery('.wpcf7cf-switch-to-txt-link');
68 wpcf7cf.$entriesUi = jQuery('#wpcf7cf-entries-ui').eq(0);
69 wpcf7cf.$addButton = jQuery('#wpcf7cf-add-button').eq(0);
70 wpcf7cf.$maxReachedWarning = jQuery('#wpcf7cf-a-lot-of-conditions').eq(0);
71 wpcf7cf.$formEditorForm = jQuery('#wpcf7-admin-form-element').eq(0);
72 wpcf7cf.$formEditor = jQuery('#wpcf7-form').eq(0);
73
74 // Smart Grid compat https://wordpress.org/support/topic/rule-sets-only-saving-when-in-text-mode/
75 if(jQuery('#cf7sg-editor').length>0) wpcf7cf.$formEditorForm = jQuery('form#post').eq(0);
76
77 wpcf7cf.regexCondition = /(?:show \[([^\]]*?)\]) if \[([^\]]*?)\] (?:(equals \(regex\)|not equals \(regex\)|equals|not equals|greater than or equals|greater than|less than or equals|less than|is empty|not empty|function)(?: \"(.*)\")?)/g;
78 wpcf7cf.regexConditionAnd = /and if \[([^\]]*?)\] (?:(equals \(regex\)|not equals \(regex\)|equals|not equals|greater than or equals|greater than|less than or equals|less than|is empty|not empty|function)(?: \"(.*)\")?)/g;
79
80 wpcf7cf.transformConditionsFromStringToArrayOfObjects = function(str) {
81
82 if (!str) str = '';
83
84 var conditionsAsStrings = str.split(/\r?\n(?=show)/);
85 var conditionsAsObjects = [];
86 for (var i = 0; i<conditionsAsStrings.length; i++) {
87
88 var lines = conditionsAsStrings[i].split(/\r?\n/);
89
90 wpcf7cf.regexCondition.lastIndex = 0;
91 var line1Match = wpcf7cf.regexCondition.exec(lines[0]);
92
93 if (line1Match != null) {
94
95 var conditionObject = {
96 then_field:line1Match[1],
97 and_rules: [
98 {
99 if_field: line1Match[2],
100 operator: line1Match[3],
101 if_value: line1Match[4],
102 },
103 ],
104 };
105
106 for(var and_i = 1; and_i < lines.length; and_i++) {
107 wpcf7cf.regexConditionAnd.lastIndex = 0;
108 lineMatch = wpcf7cf.regexConditionAnd.exec(lines[and_i]);
109 if (lineMatch != null) {
110 conditionObject.and_rules.push({
111 if_field: lineMatch[1],
112 operator: lineMatch[2],
113 if_value: lineMatch[3],
114 });
115 }
116 }
117
118 conditionsAsObjects.push(conditionObject);
119
120 }
121 }
122 return conditionsAsObjects;
123 }
124
125 wpcf7cf.getnumberOfTextEntries = function () {
126 const textConditions = wpcf7cf.transformConditionsFromStringToArrayOfObjects(wpcf7cf.$textView.val());
127 return textConditions.length;
128 }
129
130 wpcf7cf.getnumberOfFieldEntries = function () {
131 return wpcf7cf.$entriesUi.find('.entry').length;
132 }
133
134 wpcf7cf.transformConditionsFromArrayOfObjectsToString = function(conditions) {
135 return conditions.map(function(condition){
136 var indent = ' '.repeat(condition.then_field.length + 4);
137 return `show [${condition.then_field}] `+condition.and_rules.map(function(rule, i){
138 return ( i>0 ? indent+'and ':'' ) + `if [${rule.if_field}] ${rule.operator} "${rule.if_value}"`
139 }).join('\n');
140 }).join('\n');
141 }
142
143 /**
144 * Tranform an array of conditions (Objects) to HTML fields
145 * @param Array conditions
146 * @returns jQuery
147 */
148 wpcf7cf.transformConditionsFromArrayOfObjectsToFieldElements = function(conditions) {
149
150 if ( wpcf7cf.MAX_CONDITIONS < conditions.length ) {
151 jQuery('#wpcf7cf-entries').html('');
152 wpcf7cf.maybeDisableAddButton();
153 return;
154 }
155
156 var entries = [];
157
158 for (var c_i = 0; c_i<conditions.length; c_i++) {
159
160 var condition = conditions[c_i];
161 var id=0;
162
163 // setup then_field
164 var $entry = jQuery(wpcf7cf.template_for_condition_fields_without_and_rules);
165 jQuery('.then-field-select', $entry).val(condition.then_field);
166
167 for (var a_i = 0; a_i < condition.and_rules.length; a_i++) {
168 var and_rule = condition.and_rules[a_i];
169
170 $rule = jQuery(wpcf7cf.template_for_and_rule);
171
172 jQuery('.if-field-select', $rule).val(and_rule.if_field);
173 jQuery('.operator', $rule).val(and_rule.operator);
174 jQuery('.if-value', $rule).val(and_rule.if_value);
175
176 jQuery('.wpcf7cf-and-rules', $entry).eq(0).append($rule);
177
178 }
179
180 entries.push($entry);
181 }
182
183 jQuery('#wpcf7cf-entries').html(entries);
184
185 updateDisplayOfEntries();
186
187 }
188
189 wpcf7cf.isMaxConditionsReached = function() {
190 return wpcf7cf.getnumberOfTextEntries() >= wpcf7cf.MAX_CONDITIONS && wpcf7cf.getnumberOfFieldEntries() == 0 ||
191 wpcf7cf.getnumberOfFieldEntries() >= wpcf7cf.MAX_CONDITIONS;
192
193 }
194
195 wpcf7cf.maybeDisableAddButton = function() {
196 if (wpcf7cf.isMaxConditionsReached()) {
197 wpcf7cf.$addButton.hide();
198 wpcf7cf.$maxReachedWarning.show();
199 } else {
200 wpcf7cf.$addButton.show();
201 wpcf7cf.$maxReachedWarning.hide();
202 }
203 }
204
205 wpcf7cf.transformConditionsFromFieldsToArrayOfObjects = function($entries) {
206
207 if (!$entries) {
208 $entries = jQuery('#wpcf7cf-entries .entry');
209 }
210
211 var conditionsAsObjects = [];
212
213 $entries.each(function() {
214
215 var $entry = jQuery(this);
216 var then_field = $entry.find('.then-field-select').val() ?? '';
217
218 var conditionObject = {
219 then_field: then_field,
220 and_rules: [],
221 };
222
223 $entry.find('.wpcf7cf-and-rule').each(function(i) {
224 const $and_rule = jQuery(this);
225 conditionObject.and_rules.push({
226 operator : $and_rule.find('.operator').val() ?? '',
227 if_field : $and_rule.find('.if-field-select').val() ?? '',
228 if_value : $and_rule.find('.if-value').val() ?? '',
229 });
230 });
231
232 conditionsAsObjects.push(conditionObject);
233
234 });
235
236 return conditionsAsObjects;
237 }
238
239
240 wpcf7cf.copyTextToFields = function() {
241 var str = wpcf7cf.$textView.val();
242 var obj = wpcf7cf.transformConditionsFromStringToArrayOfObjects(str);
243 wpcf7cf.transformConditionsFromArrayOfObjectsToFieldElements(obj);
244 wpcf7cf.setDefaultValues();
245 }
246
247 wpcf7cf.copyFieldsToText = function() {
248 var obj = wpcf7cf.transformConditionsFromFieldsToArrayOfObjects();
249 var str = wpcf7cf.transformConditionsFromArrayOfObjectsToString(obj);
250 wpcf7cf.$textView.val(str);
251 }
252
253 function add_condition_fields() {
254 $c = jQuery(wpcf7cf.template_for_condition_fields_with_one_and_rule)
255 $c.appendTo('#wpcf7cf-entries');
256 updateDisplayOfEntries();
257 }
258
259 /**
260 * Update visibility / autocomplete and some other visual properties based on the selected conditions.
261 */
262 function updateDisplayOfEntries() {
263 wpcf7cf.$if_values = jQuery('.if-value');
264 init_autocomplete();
265 wpcf7cf.$if_values.css({'visibility':'visible'});
266 wpcf7cf.$if_values.autocomplete( "disable" );
267
268 jQuery('#wpcf7cf-entries .wpcf7cf-and-rule').each(function() {
269 var $and_rule = jQuery(this);
270 var $operatorField = $and_rule.find('.operator').eq(0);
271 var operator = $operatorField.val() || 'equals';
272 if ($and_rule.find('.operator').eq(0).val() === 'is empty' || $and_rule.find('.operator').eq(0).val() === 'not empty') {
273 $and_rule.find('.if-value').eq(0).css({'visibility':'hidden'});
274 } else if (operator.endsWith('(regex)')) {
275 $and_rule.find('.if-value').eq(0).autocomplete( "enable" );
276 }
277 // offer a native date-picker when a [date] field is compared with a value-taking operator
278 var if_field = $and_rule.find('.if-field-select').eq(0).val();
279 var dateOperators = ['equals','not equals','greater than','greater than or equals','less than','less than or equals'];
280 var showDatePicker = wpcf7cf.fieldTypes && wpcf7cf.fieldTypes[if_field] === 'date' && dateOperators.includes(operator);
281 $and_rule.find('.if-value').eq(0).attr('type', showDatePicker ? 'date' : 'text');
282 });
283
284 scale_and_button();
285
286 set_events();
287
288 wpcf7cf.maybeDisableAddButton();
289 }
290
291 function init_autocomplete() {
292
293 wpcf7cf.$if_values.autocomplete({
294 disabled: true,
295 source: function(request, response) {
296 var matcher = new RegExp(jQuery.ui.autocomplete.escapeRegex(request.term), "i");
297 response(jQuery.grep(regexes, function(value) {
298 return matcher.test(value.label || value.value || value) || matcher.test(value.desc);
299 }));
300 },
301 focus: function( event, ui ) {
302 jQuery( event.target ).val( ui.item.desc );
303 return false;
304 },
305 select: function( event, ui ) {
306 jQuery( event.target ).val( ui.item.desc );
307 return false;
308 },
309 open: function(e,ui) {
310 $el = jQuery(e.target);
311 var styledTerm = `<span class='ui-autocomplete-term'>${$el.val()}</span>`;
312
313 jQuery('.ui-autocomplete').find('em').each(function() {
314 var me = jQuery(this);
315 me.html( me.text().replace($el.val(), styledTerm) );
316 });
317 },
318 minLength: 0
319 }).each(function() {
320 jQuery(this).autocomplete( "instance" )._renderItem = function( ul, item ) {
321 return jQuery("<li>")
322 .append("<div><em>" + item.label + "</em><br><em>" + item.desc + "</em></div>")
323 .appendTo(ul);
324 }
325 });
326 wpcf7cf.$if_values.on('focus', function() {
327 jQuery(this).autocomplete("search");
328 });
329 }
330
331 function set_events() { // called at the end of updateDisplayOfEntries
332
333 jQuery('.wpcf7cf-and-rules').sortable();
334
335 jQuery('.and-button').off('click').click(function() {
336 $this = jQuery(this);
337 $andblock = $this.closest('.wpcf7cf-and-rule');
338 $andblocks_container = $this.closest('.wpcf7cf-and-rules');
339 next_index = $andblocks_container.data('next-index');
340 $andblocks_container.data('next-index',next_index+1);
341 var and_i = next_index;
342 clone_html = $andblock.get(0).outerHTML.replace(/wpcf7cf_options\[([0-9]*)\]\[and_rules\]\[([0-9]*)\]/g, 'wpcf7cf_options[$1][and_rules]['+and_i+']');
343 $andblock.after(clone_html);
344 updateDisplayOfEntries();
345 wpcf7cf.copyFieldsToText();
346 return false;
347 });
348
349 jQuery('.delete-button').off('click').click(function(){
350 $and_rule = jQuery(this).closest('.wpcf7cf-and-rule');
351 if ($and_rule.siblings().length > 0) {
352 $and_rule.remove();
353 } else {
354 $and_rule[0].closest('.entry').remove();
355 }
356 updateDisplayOfEntries();
357 wpcf7cf.copyFieldsToText();
358 return false;
359 });
360
361 jQuery('.operator').on('change', (function() {
362 updateDisplayOfEntries();
363 wpcf7cf.copyFieldsToText();
364 return false;
365 }));
366
367 jQuery('.if-field-select').off('change.wpcf7cfdate').on('change.wpcf7cfdate', (function() {
368 updateDisplayOfEntries(); // re-evaluate whether the value input should be a date-picker
369 wpcf7cf.copyFieldsToText();
370 return false;
371 }));
372 }
373
374 function scale_and_button() {
375 jQuery('.wpcf7cf-and-rule:first-child .and-button').each(function(){
376 $and_button = jQuery(this);
377 num_and_rules = $and_button.closest('.wpcf7cf-and-rule').siblings().length+1;
378 var height = (34*num_and_rules-12)+'px';
379 $and_button.css({'height':height,'line-height':height});
380 });
381 }
382
383 // ------------------------------------
384 // TOOGGLE UI MODE
385 // ------------------------------------
386
387 /**
388 * Make either Text-only view or field view visible.
389 *
390 * @param {boolean} is_text_only `true` to show text-only view. `false` for fields view
391 */
392 wpcf7cf.setViewMode = function(is_text_only) {
393 if (is_text_only) {
394 wpcf7cf.currentMode = 'text';
395 wpcf7cf.$entriesUi.hide();
396 wpcf7cf.$textView.show();
397 if (wpcf7cf.getnumberOfFieldEntries() > 0) {
398 wpcf7cf.copyFieldsToText();
399 }
400 } else {
401 wpcf7cf.currentMode = 'normal';
402 wpcf7cf.$entriesUi.show();
403 wpcf7cf.$textView.hide();
404 wpcf7cf.copyTextToFields();
405 }
406 }
407
408 wpcf7cf.$textOnlyLinks.on( 'click', function() {
409 wpcf7cf.$textOnlyCheckbox.prop('checked', true).trigger('change');
410 return false;
411 } )
412
413 wpcf7cf.$textOnlyCheckbox.on('change', function() {
414 wpcf7cf.setViewMode(wpcf7cf.$textOnlyCheckbox.is(':checked'));
415 });
416
417 wpcf7cf.$formEditorForm.on('submit', function() {
418 if (wpcf7cf.currentMode == 'normal' && wpcf7cf.getnumberOfFieldEntries() > 0) {
419 wpcf7cf.copyFieldsToText();
420 }
421 });
422
423 wpcf7cf.$entriesUi.on('change', function(){
424 wpcf7cf.copyFieldsToText();
425 });
426
427
428 // ------------------------------------
429 // CF7 TAG GENERATOR OVERRIDE
430 // ------------------------------------
431
432 window.addEventListener('load', function() {
433
434 document.querySelectorAll(
435 '[data-taggen="open-dialog"]'
436 ).forEach( button => {
437 button.addEventListener( 'click', event => {
438 const dialog = document.querySelector( `#${ button.dataset.target }` );
439 if (!dialog) {
440 return true;
441 }
442 const form = dialog.querySelector( 'form.tag-generator-panel' );
443 if (!form) {
444 return true;
445 }
446 setTimeout( function() {
447 wpcf7cf.updateTagGenerator(form);
448 }, 10 );
449 } );
450 } );
451
452 document.querySelectorAll('form.tag-generator-panel .control-box input').forEach(function(input){
453 const form = input.closest('form.tag-generator-panel');
454 if (!form) { return; }
455 setTimeout( function() {
456 wpcf7cf.updateTagGenerator(form);
457 }, 10 );
458 input.addEventListener('keyup', function(e) {
459 wpcf7cf.updateTagGenerator(form);
460 });
461 input.addEventListener('change', function(e) {
462 wpcf7cf.updateTagGenerator(form);
463 });
464 });
465 });
466
467 wpcf7cf.updateTagGenerator = function( form ) {
468 setTimeout( function() {
469 form.querySelectorAll(
470 '[data-tag-part="mail-tag-closed"]'
471 ).forEach( tag => {
472 const nameField = form.querySelector( '[data-tag-part="name"]' );
473
474 if ( nameField ) {
475 tag.innerText = `[/${ nameField.value.trim() }]`;
476 }
477 } );
478 }, 10 );
479 };
480
481
482
483
484
485
486 function scanFormTags(formCode) {
487 const fieldMatches = [...formCode.matchAll(/\[(?!group|step|repeater|submit)([^\]* ]+)\*? ([^\] ]+)/g)];
488 const fields = fieldMatches.map(e=>e[2]);
489 const fieldTypes = {}; // field name => tag type (e.g. 'date'), used to offer a date-picker
490 fieldMatches.forEach(e => { fieldTypes[e[2]] = e[1]; });
491 const groups = [...formCode.matchAll(/\[group ([^\] ]+)/g)].map(e=>e[1]);
492 return [ fields, groups, fieldTypes ];
493 }
494
495 function updateAvailableGroupsAndFields() {
496 const formCode = wpcf7cf.$formEditor.val();
497 const [ fields, groups, fieldTypes ] = scanFormTags(formCode);
498 wpcf7cf.fieldTypes = fieldTypes;
499
500 $temp = jQuery(wpcf7cf.template_for_condition_fields_with_one_and_rule);
501 $temp.find('.then-field-select').eq(0).html(createOptionsHTML(groups, 'group'));
502 $temp.find('.if-field-select').eq(0).html(createOptionsHTML(fields, 'field'));
503 // $temp.find('.operator').eq(0).html(createOptionsHTML(wpcf7cf.operators, null));
504 wpcf7cf.template_for_condition_fields_with_one_and_rule = $temp[0].outerHTML;
505
506 $temp.find('.wpcf7cf-and-rules').eq(0).html('');
507 wpcf7cf.template_for_condition_fields_without_and_rules = $temp[0].outerHTML;
508
509 $temp = jQuery(wpcf7cf.template_for_and_rule);
510 $temp.find('.if-field-select').eq(0).html(createOptionsHTML(fields, 'field'));
511 wpcf7cf.template_for_and_rule = $temp[0].outerHTML;
512
513 jQuery('.then-field-select').each(function(){
514 const $this = jQuery(this);
515 updateSelectWithValues($this, groups, 'group');
516 });
517 jQuery('.if-field-select').each(function(){
518 const $this = jQuery(this);
519 updateSelectWithValues($this, fields, 'field');
520 });
521 }
522
523 function updateSelectWithValues($select, values, type) {
524 $select.html(createOptionsHTML(values, type));
525 }
526
527 function createOptionsHTML(values, type) {
528 return (type?`<option value="-1">-- Select ${type} --</option>`:'')+values.map(value => `<option value="${value}">${value}</option>`).join('');
529 }
530
531 /**
532 * Set the default value of all fields to their current value.
533 * This prevents CF7 to show an unsaved-changes warning on these fields.
534 * (The single source of truth is the value in the text view.)
535 */
536 wpcf7cf.setDefaultValues = function() {
537 jQuery('#wpcf7cf-entries select').each(function() {
538 const $select = jQuery(this);
539 let selectedIndex = $select.prop('selectedIndex');
540 if (selectedIndex == -1) {
541 selectedIndex = 0;
542 }
543 $select.find('option').removeAttr('selected');
544 $select.find(`option:eq(${selectedIndex})`).prop('selected', 'selected').attr('selected', 'selected');
545 })
546
547 jQuery('#wpcf7cf-entries .if-value').each(function(){
548 const $input = jQuery(this);
549 $input.attr('value',$input.val());
550 });
551 }
552
553 // update available groups and fields each time there is a change in the form code.
554 wpcf7cf.$formEditor.on('change focusout', function() {
555 if( !wpcf7cf_formcode || wpcf7cf_formcode !== this.value ) {
556 wpcf7cf_formcode = this.value;
557 updateAvailableGroupsAndFields();
558 if (!wpcf7cf.isMaxConditionsReached()) {
559 wpcf7cf.copyTextToFields();
560 wpcf7cf.copyFieldsToText();
561 }
562 }
563 });
564
565 wpcf7cf.$addButton.click(function(){
566 add_condition_fields();
567 wpcf7cf.copyFieldsToText();
568 });
569
570 jQuery(document).ready(function() {
571
572 wpcf7cf.$if_values = jQuery('.if-value'); // gets updated now and then
573
574 // init HTML templates (will be updated immediatly by updateAvailableGroupsAndFields())
575 wpcf7cf.template_for_condition_fields_with_one_and_rule = wpcf7cf.$newEntry[0].outerHTML;
576 wpcf7cf.template_for_and_rule = wpcf7cf.$newEntry.find('.wpcf7cf-and-rule')[0] ? wpcf7cf.$newEntry.find('.wpcf7cf-and-rule')[0].outerHTML : '';
577
578 updateAvailableGroupsAndFields();
579 wpcf7cf.copyTextToFields();
580
581 wpcf7cf.maybeDisableAddButton();
582
583 jQuery('#wpcf7cf-entries').sortable();
584
585 wpcf7cf.setViewMode(wpcf7cf.$textOnlyCheckbox.is(':checked'));
586
587 })
588
589 }