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.2 0.2.3
cf7-conditional-fields / js / scripts_admin.js
cf7-conditional-fields / js Last commit date
scripts.js 6 years ago scripts.js.map 6 years ago scripts_admin.js 6 years ago scripts_es6.js 6 years ago
scripts_admin.js
373 lines
1 /**
2 * Created by jules on 7/17/2015.
3 */
4 var $wpcf7cf_new_entry = jQuery('#wpcf7cf-new-entry').eq(0);
5
6 if ($wpcf7cf_new_entry.length > 0) {
7
8 var wpcf7cf_new_and_rule_html = $wpcf7cf_new_entry.find('.wpcf7cf-and-rule')[0].outerHTML;
9 var wpcf7cf_new_entry_html = $wpcf7cf_new_entry.html();
10
11 var cf_rule_regex = /(?: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;
12 var cf_rule_regex_and = /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;
13
14
15 if (_wpcf7 == null) { var _wpcf7 = wpcf7}; // wpcf7 4.8 fix
16
17 var old_compose = _wpcf7.taggen.compose;
18
19 var regexes = [
20 { label: wpcf7cf_options_0.regex_email_label, desc: wpcf7cf_options_0.regex_email },
21 { label: wpcf7cf_options_0.regex_numeric_label, desc: wpcf7cf_options_0.regex_numeric },
22 { label: wpcf7cf_options_0.regex_alphanumeric_label, desc: wpcf7cf_options_0.regex_alphanumeric },
23 { label: wpcf7cf_options_0.regex_alphabetic_label, desc: wpcf7cf_options_0.regex_alphabetic },
24 { label: wpcf7cf_options_0.regex_date_label, desc: wpcf7cf_options_0.regex_date },
25 { label: wpcf7cf_options_0.regex_custom_1_label, desc: wpcf7cf_options_0.regex_custom_1 },
26 { label: wpcf7cf_options_0.regex_custom_2_label, desc: wpcf7cf_options_0.regex_custom_2 },
27 { label: wpcf7cf_options_0.regex_custom_3_label, desc: wpcf7cf_options_0.regex_custom_3 },
28 { label: wpcf7cf_options_0.regex_custom_4_label, desc: wpcf7cf_options_0.regex_custom_4 },
29 { label: wpcf7cf_options_0.regex_custom_5_label, desc: wpcf7cf_options_0.regex_custom_5 },
30 ];
31
32 var i = regexes.length;
33 while (i--) {
34 if (null == regexes[i].label || null == regexes[i].desc || regexes[i].label == '' || regexes[i].desc == '') {
35 regexes.splice(i,1);
36 }
37 }
38
39 var termTemplate = "<span class='ui-autocomplete-term'>%s</span>";
40
41 (function($) {
42
43 $('#wpcf7cf-entries').sortable();
44 $(('.wpcf7cf-and-rules')).sortable();
45
46
47 // ...before overwriting the jQuery extension point
48 _wpcf7.taggen.compose = function(tagType, $form)
49 {
50
51 $('#tag-generator-panel-group-style-hidden').val($('#tag-generator-panel-group-style').val());
52
53 // original behavior - use function.apply to preserve context
54 var ret = old_compose.apply(this, arguments);
55 //tagType = arguments[0];
56 //$form = arguments[1];
57
58 // START: code here will be executed after the _wpcf7.taggen.update function
59 if (tagType== 'group') ret += "[/group]";
60 if (tagType== 'repeater') ret += "[/repeater]";
61
62 // END
63
64 if (tagType== 'togglebutton') {
65 $val1 = $('#tag-generator-panel-togglebutton-value-1');
66 $val2 = $('#tag-generator-panel-togglebutton-value-2');
67 var val1 = $val1.val();
68 var val2 = $val2.val();
69
70 if (val1 == "") val1 = $val1.data('default');
71 if (val2 == "") val2 = $val2.data('default');
72
73 str_val = ' "'+val1+'" "'+val2+'"';
74
75 ret = ret.replace(']', str_val+']');
76 }
77
78 return ret;
79 };
80
81 var index = $('#wpcf7cf-entries .entry').length;
82 var index_and = 0;
83
84 $('#wpcf7cf-add-button').click(function(){
85
86 var id = add_condition_fields();
87
88 return false;
89
90 });
91
92 function clear_all_condition_fields() {
93 $('.entry').remove();
94 }
95
96 function add_condition_fields() {
97 $('<div class="entry" id="entry-'+index+'">'+(wpcf7cf_new_entry_html.replace(/{id}/g, index))+'</div>').appendTo('#wpcf7cf-entries');
98 index++;
99 update_entries();
100 update_settings_textarea();
101 return (index-1);
102 }
103
104 function add_and_condition_fields(id) {
105 // $('#entry-'+id+' .wpcf7cf-and-rules').eq(0).append($wpcf7cf_new_and_rule.clone());
106 $('#entry-'+id+' .wpcf7cf-and-rules').eq(0).append(wpcf7cf_new_and_rule_html.replace(/{id}/g, index-1).replace(/\[and_rules\]\[0\]/g, '[and_rules]['+index_and+']'));
107 index_and++;
108 return (index_and-1);
109 }
110
111 /**
112 * Copy the textarea field to the entries.
113 */
114 function import_condition_fields() {
115
116 $if_values = $('.if-value');
117
118 var lines = $('#wpcf7cf-settings-text').val().split(/\r?\n/);
119
120 var id = -1;
121
122 for (var i = 0; i<lines.length; i++) {
123
124 var str = lines[i];
125
126 var match = cf_rule_regex.exec(str);
127
128 if (match != null) {
129
130 index_and = 0; // reset this for each first condition (This one has and_index [0]).
131
132 id = add_condition_fields();
133
134 $('#entry-'+id+' .then-field-select').val(match[1]);
135 $('#entry-'+id+' .if-field-select').val(match[2]);
136 $('#entry-'+id+' .operator').val(match[3]);
137 $('#entry-'+id+' .if-value').val(match[4]);
138
139 index_and = 1; // the next and condition will have and_index [1];
140
141 cf_rule_regex.lastIndex = 0;
142
143 }
144
145 match = cf_rule_regex_and.exec(str);
146
147 if (match != null && id != -1) {
148
149 var and_id = add_and_condition_fields(id);
150
151 $('#entry-'+id+' .wpcf7cf-and-rule:last-child .if-field-select').val(match[1]);
152 $('#entry-'+id+' .wpcf7cf-and-rule:last-child .operator').val(match[2]);
153 $('#entry-'+id+' .wpcf7cf-and-rule:last-child .if-value').val(match[3]);
154
155 cf_rule_regex_and.lastIndex = 0;
156
157 }
158 }
159
160 update_entries();
161
162 }
163
164 $('#wpcf7-admin-form-element, #post').on('submit.wpcf7cf', function() {
165 update_settings_textarea();
166 return true;
167 });
168
169 // export/import settings
170
171 $('#wpcf7cf-settings-text-wrap').hide();
172
173 $('#wpcf7cf-settings-to-text').click(function() {
174 $('#wpcf7cf-settings-text-wrap').show();
175 update_settings_textarea();
176 return false;
177 });
178
179 /**
180 * Copy the entries to the textarea field.
181 */
182 function update_settings_textarea() {
183 $('#wpcf7cf-settings-text').val('');
184 $('#wpcf7cf-entries .entry').each(function() {
185 var $entry = $(this);
186 var line = 'show [' + $entry.find('.then-field-select').val() + ']';
187 var text_indent = line.length-3;
188 $entry.find('.wpcf7cf-and-rule').each(function(i) {
189 const $and_rule = $(this);
190 const operator = $and_rule.find('.operator').val();
191 if (i>0) {
192
193 line += '\n'+' '.repeat(text_indent)+'and';
194
195 }
196 line += ' if [' + $and_rule.find('.if-field-select').val() + ']' + ' ' + operator;
197 if (!['is empty', 'not empty'].includes(operator)) {
198 line += ' "' + $and_rule.find('.if-value').val() + '"';
199 }
200 });
201 $('#wpcf7cf-settings-text').val($('#wpcf7cf-settings-text').val() + line + "\n" );
202 });
203 }
204
205 $if_values = $('.if-value');
206
207 $('#add-fields').click(function() {
208 import_condition_fields();
209 return false;
210 });
211
212 $('#overwrite-fields').click(function() {
213 clear_all_condition_fields();
214 import_condition_fields();
215 return false;
216 });
217
218 $('#wpcf7cf-settings-text').on('change.wpcf7cf', function(){
219 clear_all_condition_fields();
220 import_condition_fields();
221 return true;
222 });
223
224
225
226 $('#wpcf7cf-settings-text-clear').click(function() {
227 $('#wpcf7cf-settings-text-wrap').hide();
228 $('#wpcf7cf-settings-text').val('');
229 return false;
230 });
231
232 function update_entries() {
233 $if_values = $('.if-value');
234 init_autocomplete();
235 $if_values.css({'visibility':'visible'});
236 $if_values.autocomplete( "disable" );
237
238 $('#wpcf7cf-entries .wpcf7cf-and-rule').each(function() {
239 var $and_rule = $(this);
240 if ($and_rule.find('.operator').eq(0).val() === 'is empty' || $and_rule.find('.operator').eq(0).val() === 'not empty') {
241 $and_rule.find('.if-value').eq(0).css({'visibility':'hidden'});
242 } else if ($and_rule.find('.operator').eq(0).val().endsWith('(regex)')) {
243 $and_rule.find('.if-value').eq(0).autocomplete( "enable" );
244 }
245 });
246
247 scale_and_button();
248
249 set_events();
250 }
251
252 function init_autocomplete() {
253
254 $if_values.autocomplete({
255 disabled: true,
256 source: function(request, response) {
257 var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
258 response($.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 $( event.target ).val( ui.item.desc );
264 return false;
265 },
266 select: function( event, ui ) {
267 $( event.target ).val( ui.item.desc );
268 return false;
269 },
270 open: function(e,ui) {
271 $el = $(e.target);
272 var styledTerm = termTemplate.replace('%s', $el.val());
273
274 $('.ui-autocomplete').find('em').each(function() {
275 var me = $(this);
276 me.html( me.text().replace($el.val(), styledTerm) );
277 });
278 },
279 minLength: 0
280 }).each(function() {
281 $(this).autocomplete( "instance" )._renderItem = function( ul, item ) {
282 return $("<li>")
283 .append("<div><em>" + item.label + "</em><br><em>" + item.desc + "</em></div>")
284 .appendTo(ul);
285 }
286 });
287 $if_values.on('focus', function() {
288 $(this).autocomplete("search");
289 });
290 }
291
292 update_entries();
293
294 function set_events() { // called at the end of update_entries
295
296 $('.wpcf7cf-and-rules').sortable();
297
298 $('.and-button').off('click').click(function() {
299 $this = $(this);
300 $andblock = $this.closest('.wpcf7cf-and-rule');
301 $andblocks_container = $this.closest('.wpcf7cf-and-rules');
302 next_index = $andblocks_container.data('next-index');
303 $andblocks_container.data('next-index',next_index+1);
304 var and_i = next_index;
305 clone_html = $andblock.get(0).outerHTML.replace(/wpcf7cf_options\[([0-9]*)\]\[and_rules\]\[([0-9]*)\]/g, 'wpcf7cf_options[$1][and_rules]['+and_i+']');
306 $andblock.after(clone_html);
307 update_settings_textarea();
308 update_entries();
309 return false;
310 });
311
312 $('.delete-button').off('click').click(function(){
313 $and_rule = $(this).closest('.wpcf7cf-and-rule');
314 if ($and_rule.siblings().length > 0) {
315 $and_rule.remove();
316 } else {
317 $and_rule[0].closest('.entry').remove();
318 }
319
320 update_settings_textarea();
321 update_entries();
322
323 return false;
324 });
325
326 $('.operator').off('change').change(function() {
327 update_entries();
328 return false;
329 });
330
331 $('input,select', '#wpcf7cf-entries').off('change.wpcf7cf_sync').on('change.wpcf7cf_sync', function() {
332 update_settings_textarea();
333 });
334 }
335
336 function scale_and_button() {
337 $('.wpcf7cf-and-rule:first-child .and-button').each(function(){
338 $and_button = $(this);
339 num_and_rules = $and_button.closest('.wpcf7cf-and-rule').siblings().length+1;
340 var height = (34*num_and_rules-12)+'px';
341 $and_button.css({'height':height,'line-height':height});
342 });
343 }
344
345 })( jQuery );
346
347 }
348
349 (function($) {
350 // ------------------------------------
351 // OPTIONS PAGE
352 // ------------------------------------
353
354 $(document).ready(function() {
355
356 $('.wpcf7cf-options-notice .notice-dismiss-2').click(function () {
357 $('.wpcf7cf-options-notice .notice-dismiss').click();
358 });
359 $('.wpcf7cf-options-notice .notice-dismiss').click(function () {
360 wpcf7cf_dismiss_notice();
361 });
362
363 function wpcf7cf_dismiss_notice() {
364
365 $('input[name="wpcf7cf_options[notice_dismissed]"]').val('true');
366
367 $.post(ajaxurl, {action:'wpcf7cf_dismiss_notice'}, function(response) {
368 // nothing to do. dismiss_notice option should be set to TRUE server side by now.
369 });
370 }
371
372 });
373 })( jQuery );