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