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.2 0.2.3
cf7-conditional-fields / js / temp.js
cf7-conditional-fields / js Last commit date
polyfill.js 3 years ago scripts.js 1 week 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
temp.js
510 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 if (typeof(_wpcf7) != 'undefined' || typeof(wpcf7) != 'undefined') {
7
8 var wpcf7cf = {};
9
10 wpcf7cf.MAX_CONDITIONS = 50;
11
12 wpcf7cf.$newEntry = jQuery(`<div class="entry">
13 <div class="wpcf7cf-if">
14 <span class="label">Show</span>
15 <select class="then-field-select"></select>
16 </div>
17 <div class="wpcf7cf-and-rules ui-sortable" data-next-index="1">
18 <div class="wpcf7cf-and-rule ui-sortable-handle">
19 <span class="rule-part if-txt label">if</span>
20 <select class="rule-part if-field-select"></select>
21 <select class="rule-part operator">
22 <option value="equals" selected="">equals</option>
23 <option value="not equals">not equals</option>
24 <option value="equals (regex)">equals (regex)</option>
25 <option value="not equals (regex)">not equals (regex)</option>
26 <option value="greater than">greater than</option>
27 <option value="greater than or equals">greater than or equals</option>
28 <option value="less than">less than</option>
29 <option value="less than or equals">less than or equals</option>
30 <option value="is empty">is empty</option>
31 <option value="not empty">not empty</option>
32 <option value="function">function</option>
33 </select>
34 <input class="rule-part if-value" type="text" placeholder="value" value="" style="visibility: visible;">
35 <span class="and-button" style="height: 22px; line-height: 22px;">And</span>
36 <span title="delete rule" class="rule-part delete-button">remove</span>
37 </div>
38 </div>
39 </div>`);
40 wpcf7cf.$textView = jQuery('#wpcf7cf-settings-text').eq(0);
41 wpcf7cf.$textOnlyCheckbox = jQuery('#wpcf7cf-text-only-checkbox').eq(0);
42 wpcf7cf.$entriesUi = jQuery('#wpcf7cf-entries-ui').eq(0);
43 wpcf7cf.$addButton = jQuery('#wpcf7cf-add-button').eq(0);
44 wpcf7cf.$maxReachedWarning = jQuery('#wpcf7cf-a-lot-of-conditions').eq(0);
45 wpcf7cf.$formEditorForm = jQuery('#wpcf7-admin-form-element').eq(0);
46 wpcf7cf.$formEditor = jQuery('#wpcf7-form').eq(0);
47
48 // Smart Grid compat https://wordpress.org/support/topic/rule-sets-only-saving-when-in-text-mode/
49 if(jQuery('#cf7sg-editor').length>0) wpcf7cf.$formEditorForm = jQuery('form#post').eq(0);
50
51 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;
52 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;
53
54 wpcf7cf.transformConditionsFromStringToArrayOfObjects = function(str) {
55
56 if (!str) str = '';
57
58 var conditionsAsStrings = str.split(/\r?\n(?=show)/);
59 var conditionsAsObjects = [];
60 for (var i = 0; i<conditionsAsStrings.length; i++) {
61
62 var lines = conditionsAsStrings[i].split(/\r?\n/);
63
64 wpcf7cf.regexCondition.lastIndex = 0;
65 var line1Match = wpcf7cf.regexCondition.exec(lines[0]);
66
67 if (line1Match != null) {
68
69 var conditionObject = {
70 then_field:line1Match[1],
71 and_rules: [
72 {
73 if_field: line1Match[2],
74 operator: line1Match[3],
75 if_value: line1Match[4],
76 },
77 ],
78 };
79
80 for(var and_i = 1; and_i < lines.length; and_i++) {
81 wpcf7cf.regexConditionAnd.lastIndex = 0;
82 lineMatch = wpcf7cf.regexConditionAnd.exec(lines[and_i]);
83 if (lineMatch != null) {
84 conditionObject.and_rules.push({
85 if_field: lineMatch[1],
86 operator: lineMatch[2],
87 if_value: lineMatch[3],
88 });
89 }
90 }
91
92 conditionsAsObjects.push(conditionObject);
93
94 }
95 }
96 return conditionsAsObjects;
97 }
98
99 wpcf7cf.getnumberOfTextEntries = function () {
100 const textConditions = wpcf7cf.transformConditionsFromStringToArrayOfObjects(wpcf7cf.$textView.val());
101 return textConditions.length;
102 }
103
104 wpcf7cf.getnumberOfFieldEntries = function () {
105 return wpcf7cf.$entriesUi.find('.entry').length;
106 }
107
108 wpcf7cf.transformConditionsFromArrayOfObjectsToString = function(conditions) {
109 return conditions.map(function(condition){
110 var indent = ' '.repeat(condition.then_field.length + 4);
111 return `show [${condition.then_field}] `+condition.and_rules.map(function(rule, i){
112 return ( i>0 ? indent+'and ':'' ) + `if [${rule.if_field}] ${rule.operator} "${rule.if_value}"`
113 }).join('\n');
114 }).join('\n');
115 }
116
117 /**
118 * Tranform an array of conditions (Objects) to HTML fields
119 * @param Array conditions
120 * @returns jQuery
121 */
122 wpcf7cf.transformConditionsFromArrayOfObjectsToFieldElements = function(conditions) {
123
124 if ( wpcf7cf.MAX_CONDITIONS < conditions.length ) {
125 jQuery('#wpcf7cf-entries').html('');
126 wpcf7cf.maybeDisableAddButton();
127 return;
128 }
129
130 var entries = [];
131
132 for (var c_i = 0; c_i<conditions.length; c_i++) {
133
134 var condition = conditions[c_i];
135 var id=0;
136
137 // setup then_field
138 var $entry = jQuery(wpcf7cf.template_for_condition_fields_without_and_rules);
139 jQuery('.then-field-select', $entry).val(condition.then_field).attr('value', condition.then_field );
140
141 for (var a_i = 0; a_i < condition.and_rules.length; a_i++) {
142 var and_rule = condition.and_rules[a_i];
143
144 $rule = jQuery(wpcf7cf.template_for_and_rule);
145
146 jQuery('.if-field-select', $rule).val(and_rule.if_field).attr('value', and_rule.if_field );
147 jQuery('.operator', $rule).val(and_rule.operator).attr('value', and_rule.operator );
148 jQuery('.if-value', $rule).val(and_rule.if_value).attr('value', and_rule.if_value );
149
150 jQuery('.wpcf7cf-and-rules', $entry).eq(0).append($rule);
151
152 }
153
154 entries.push($entry);
155 }
156
157 jQuery('#wpcf7cf-entries').html(entries);
158
159 update_entries();
160
161 }
162
163 wpcf7cf.maybeDisableAddButton = function() {
164 if (wpcf7cf.getnumberOfTextEntries() >= wpcf7cf.MAX_CONDITIONS && wpcf7cf.getnumberOfFieldEntries() == 0 ||
165 wpcf7cf.getnumberOfFieldEntries() >= wpcf7cf.MAX_CONDITIONS
166 ) {
167 wpcf7cf.$addButton.hide();
168 wpcf7cf.$maxReachedWarning.show();
169 } else {
170 wpcf7cf.$addButton.show();
171 wpcf7cf.$maxReachedWarning.hide();
172 }
173 }
174
175 wpcf7cf.transformConditionsFromFieldsToArrayOfObjects = function($entries) {
176
177 if (!$entries) {
178 $entries = jQuery('#wpcf7cf-entries .entry');
179 }
180
181 var conditionsAsObjects = [];
182
183 $entries.each(function() {
184
185 var $entry = jQuery(this);
186 var then_field = $entry.find('.then-field-select').val() ?? '';
187
188 var conditionObject = {
189 then_field: then_field,
190 and_rules: [],
191 };
192
193 $entry.find('.wpcf7cf-and-rule').each(function(i) {
194 const $and_rule = jQuery(this);
195 conditionObject.and_rules.push({
196 operator : $and_rule.find('.operator').val() ?? '',
197 if_field : $and_rule.find('.if-field-select').val() ?? '',
198 if_value : $and_rule.find('.if-value').val() ?? '',
199 });
200 });
201
202 conditionsAsObjects.push(conditionObject);
203
204 });
205
206 return conditionsAsObjects;
207 }
208
209
210 wpcf7cf.copyTextToFields = function() {
211 var str = wpcf7cf.$textView.val();
212 var obj = wpcf7cf.transformConditionsFromStringToArrayOfObjects(str);
213 wpcf7cf.transformConditionsFromArrayOfObjectsToFieldElements(obj);
214 }
215
216 wpcf7cf.copyFieldsToText = function() {
217 var obj = wpcf7cf.transformConditionsFromFieldsToArrayOfObjects();
218 var str = wpcf7cf.transformConditionsFromArrayOfObjectsToString(obj);
219 wpcf7cf.$textView.val(str);
220 }
221
222 function add_condition_fields() {
223 $c = jQuery(wpcf7cf.template_for_condition_fields_with_one_and_rule)
224 $c.appendTo('#wpcf7cf-entries');
225 update_entries();
226 }
227
228 function update_entries() {
229 wpcf7cf.$if_values = jQuery('.if-value');
230 init_autocomplete();
231 wpcf7cf.$if_values.css({'visibility':'visible'});
232 wpcf7cf.$if_values.autocomplete( "disable" );
233
234 jQuery('#wpcf7cf-entries .wpcf7cf-and-rule').each(function() {
235 var $and_rule = jQuery(this);
236 var $operatorField = $and_rule.find('.operator').eq(0);
237 var operator = $operatorField.val() || 'equals';
238 if ($and_rule.find('.operator').eq(0).val() === 'is empty' || $and_rule.find('.operator').eq(0).val() === 'not empty') {
239 $and_rule.find('.if-value').eq(0).css({'visibility':'hidden'});
240 } else if (operator.endsWith('(regex)')) {
241 $and_rule.find('.if-value').eq(0).autocomplete( "enable" );
242 }
243 });
244
245 scale_and_button();
246
247 set_events();
248
249 wpcf7cf.maybeDisableAddButton();
250 }
251
252 function init_autocomplete() {
253
254 wpcf7cf.$if_values.autocomplete({
255 disabled: true,
256 source: function(request, response) {
257 var matcher = new RegExp(jQuery.ui.autocomplete.escapeRegex(request.term), "i");
258 response(jQuery.grep(regexes, function(value) {
259 return matcher.test(value.label || value.value || value) || matcher.test(value.desc);
260 }));
261 },
262 focus: function( event, ui ) {
263 jQuery( event.target ).val( ui.item.desc );
264 return false;
265 },
266 select: function( event, ui ) {
267 jQuery( event.target ).val( ui.item.desc );
268 return false;
269 },
270 open: function(e,ui) {
271 $el = jQuery(e.target);
272 var styledTerm = `<span class='ui-autocomplete-term'>${$el.val}</span>`;
273
274 jQuery('.ui-autocomplete').find('em').each(function() {
275 var me = jQuery(this);
276 me.html( me.text().replace($el.val(), styledTerm) );
277 });
278 },
279 minLength: 0
280 }).each(function() {
281 jQuery(this).autocomplete( "instance" )._renderItem = function( ul, item ) {
282 return jQuery("<li>")
283 .append("<div><em>" + item.label + "</em><br><em>" + item.desc + "</em></div>")
284 .appendTo(ul);
285 }
286 });
287 wpcf7cf.$if_values.on('focus', function() {
288 jQuery(this).autocomplete("search");
289 });
290 }
291
292 function set_events() { // called at the end of update_entries
293
294 jQuery('.wpcf7cf-and-rules').sortable();
295
296 jQuery('.and-button').off('click').click(function() {
297 $this = jQuery(this);
298 $andblock = $this.closest('.wpcf7cf-and-rule');
299 $andblocks_container = $this.closest('.wpcf7cf-and-rules');
300 next_index = $andblocks_container.data('next-index');
301 $andblocks_container.data('next-index',next_index+1);
302 var and_i = next_index;
303 clone_html = $andblock.get(0).outerHTML.replace(/wpcf7cf_options\[([0-9]*)\]\[and_rules\]\[([0-9]*)\]/g, 'wpcf7cf_options[$1][and_rules]['+and_i+']');
304 $andblock.after(clone_html);
305 //update_settings_textarea();
306 update_entries();
307 return false;
308 });
309
310 jQuery('.delete-button').off('click').click(function(){
311 $and_rule = jQuery(this).closest('.wpcf7cf-and-rule');
312 if ($and_rule.siblings().length > 0) {
313 $and_rule.remove();
314 } else {
315 $and_rule[0].closest('.entry').remove();
316 }
317
318 //update_settings_textarea();
319 update_entries();
320
321 return false;
322 });
323
324 jQuery('.operator').off('change').change(function() {
325 update_entries();
326 return false;
327 });
328 }
329
330 function scale_and_button() {
331 jQuery('.wpcf7cf-and-rule:first-child .and-button').each(function(){
332 $and_button = jQuery(this);
333 num_and_rules = $and_button.closest('.wpcf7cf-and-rule').siblings().length+1;
334 var height = (34*num_and_rules-12)+'px';
335 $and_button.css({'height':height,'line-height':height});
336 });
337 }
338
339 // ------------------------------------
340 // TOOGGLE UI MODE
341 // ------------------------------------
342
343 function setUiMode(is_text_only) {
344 if (is_text_only) {
345 wpcf7cf.currentMode = 'text';
346 wpcf7cf.$entriesUi.hide();
347 wpcf7cf.$textView.show();
348 if (wpcf7cf.getnumberOfFieldEntries() > 0) {
349 wpcf7cf.copyFieldsToText();
350 }
351 } else {
352 wpcf7cf.currentMode = 'normal';
353 wpcf7cf.$entriesUi.show();
354 wpcf7cf.$textView.hide();
355 wpcf7cf.copyTextToFields();
356 }
357 }
358
359 wpcf7cf.$textOnlyCheckbox.on('change', function() {
360 setUiMode(wpcf7cf.$textOnlyCheckbox.is(':checked'));
361 });
362
363 wpcf7cf.$formEditorForm.on('submit', function() {
364 if (wpcf7cf.currentMode == 'normal' && wpcf7cf.getnumberOfFieldEntries() > 0) {
365 wpcf7cf.copyFieldsToText();
366 }
367 });
368
369
370 // ------------------------------------
371 // CF7 TAG GENERATOR OVERRIDE
372 // ------------------------------------
373
374 if (_wpcf7 == null) { var _wpcf7 = wpcf7}; // wpcf7 4.8 fix
375 var old_compose = _wpcf7.taggen.compose;
376 // ...before overwriting the jQuery extension point
377 _wpcf7.taggen.compose = function(tagType, $form)
378 {
379
380 jQuery('#tag-generator-panel-group-style-hidden').val(jQuery('#tag-generator-panel-group-style').val());
381
382 // original behavior - use function.apply to preserve context
383 var ret = old_compose.apply(this, arguments);
384 //tagType = arguments[0];
385 //$form = arguments[1];
386
387 // START: code here will be executed after the _wpcf7.taggen.update function
388 if (tagType== 'group') ret += "[/group]";
389 if (tagType== 'repeater') ret += "[/repeater]";
390 // END
391
392 if (tagType== 'togglebutton') {
393 $val1 = jQuery('#tag-generator-panel-togglebutton-value-1');
394 $val2 = jQuery('#tag-generator-panel-togglebutton-value-2');
395 var val1 = $val1.val();
396 var val2 = $val2.val();
397
398 if (val1 == "") val1 = $val1.data('default');
399 if (val2 == "") val2 = $val2.data('default');
400
401 str_val = ' "'+val1+'" "'+val2+'"';
402
403 ret = ret.replace(']', str_val+']');
404 }
405
406 return ret;
407 };
408
409
410 // console.log('huh');
411 // jQuery( window ).on( 'beforeunload', function( event ) {
412 // wpcf7cf.copyFieldsToText();
413 // if (wpcf7cf.$textView.val() === wpcf7cf.$textView[0].defaultValue) {
414 // // jQuery('#wpcf7cf-entries-ui').remove();
415 // jQuery("#wpcf7cf-entries-ui :input").prop("disabled", true);
416 // }
417 // });
418
419 function scanFormTags(formCode) {
420 const fields = [...formCode.matchAll(/\[(?!group|step|repeater|submit)[^\] ]+ ([^\] ]+)/g)].map(e=>e[1]);
421 const groups = [...formCode.matchAll(/\[group ([^\] ]+)/g)].map(e=>e[1]);
422 return [ fields, groups ];
423 }
424
425 function updateAvailableGroupsAndFields() {
426 const formCode = wpcf7cf.$formEditor.val();
427 const [ fields, groups ] = scanFormTags(formCode);
428
429 jQuery('.then-field-select').each(function(){
430 const $this = jQuery(this);
431 updateSelectWithValues($this, groups, 'group');
432 });
433 jQuery('.if-field-select').each(function(){
434 const $this = jQuery(this);
435 updateSelectWithValues($this, fields, 'field');
436 });
437
438 $temp = jQuery(wpcf7cf.template_for_condition_fields_with_one_and_rule);
439 $temp.find('.then-field-select').eq(0).html(createOptionsHTML(groups, 'group'));
440 $temp.find('.if-field-select').eq(0).html(createOptionsHTML(fields, 'field'));
441 wpcf7cf.template_for_condition_fields_with_one_and_rule = $temp[0].outerHTML;
442
443 $temp.find('.wpcf7cf-and-rules').eq(0).html('');
444 wpcf7cf.template_for_condition_fields_without_and_rules = $temp[0].outerHTML;
445
446 $temp = jQuery(wpcf7cf.template_for_and_rule);
447 $temp.find('.if-field-select').eq(0).html(createOptionsHTML(fields, 'field'));
448 wpcf7cf.template_for_and_rule = $temp[0].outerHTML;
449
450 //wpcf7cf.template_for_and_rule = wpcf7cf.$newEntry.find('.wpcf7cf-and-rule')[0] ? wpcf7cf.$newEntry.find('.wpcf7cf-and-rule')[0].outerHTML : '';
451 }
452
453 function updateSelectWithValues($select, values, type) {
454 const originalValue = $select.val();
455 $select.html(createOptionsHTML(values, type, originalValue));
456 }
457
458 function createOptionsHTML(values, type, originalValue = '-1') {
459 return `<option value="-1" ${'-1' == originalValue ? 'selected' : ''}>-- Select ${type} --</option>`+values.map(value => `<option value="${value}" ${value == originalValue ? 'selected' : ''}>${value}</option>`).join('');
460 }
461
462 // update available groups and fields each time there is a change in the form code.
463 wpcf7cf.$formEditor.on('change', function() {
464 updateAvailableGroupsAndFields();
465 });
466
467 var regexes = [
468 { label: wpcf7cf_options_0.regex_email_label, desc: wpcf7cf_options_0.regex_email },
469 { label: wpcf7cf_options_0.regex_numeric_label, desc: wpcf7cf_options_0.regex_numeric },
470 { label: wpcf7cf_options_0.regex_alphanumeric_label, desc: wpcf7cf_options_0.regex_alphanumeric },
471 { label: wpcf7cf_options_0.regex_alphabetic_label, desc: wpcf7cf_options_0.regex_alphabetic },
472 { label: wpcf7cf_options_0.regex_date_label, desc: wpcf7cf_options_0.regex_date },
473 { label: wpcf7cf_options_0.regex_custom_1_label, desc: wpcf7cf_options_0.regex_custom_1 },
474 { label: wpcf7cf_options_0.regex_custom_2_label, desc: wpcf7cf_options_0.regex_custom_2 },
475 { label: wpcf7cf_options_0.regex_custom_3_label, desc: wpcf7cf_options_0.regex_custom_3 },
476 { label: wpcf7cf_options_0.regex_custom_4_label, desc: wpcf7cf_options_0.regex_custom_4 },
477 { label: wpcf7cf_options_0.regex_custom_5_label, desc: wpcf7cf_options_0.regex_custom_5 },
478 ];
479
480 var i = regexes.length;
481 while (i--) {
482 if (null == regexes[i].label || null == regexes[i].desc || regexes[i].label == '' || regexes[i].desc == '') {
483 regexes.splice(i,1);
484 }
485 }
486
487 wpcf7cf.$addButton.click(function(){
488 add_condition_fields();
489 });
490
491 jQuery(document).on('ready', function() {
492
493 wpcf7cf.$if_values = jQuery('.if-value'); // gets updated now and then
494
495 // init HTML templates (will be updated immediatly by updateAvailableGroupsAndFields())
496 wpcf7cf.template_for_condition_fields_with_one_and_rule = wpcf7cf.$newEntry[0].outerHTML;
497 wpcf7cf.template_for_and_rule = wpcf7cf.$newEntry.find('.wpcf7cf-and-rule')[0] ? wpcf7cf.$newEntry.find('.wpcf7cf-and-rule')[0].outerHTML : '';
498
499 updateAvailableGroupsAndFields();
500 wpcf7cf.copyTextToFields();
501
502 wpcf7cf.maybeDisableAddButton();
503
504 jQuery('#wpcf7cf-entries').sortable();
505
506 setUiMode(wpcf7cf.$textOnlyCheckbox.is(':checked'));
507
508 })
509
510 }