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