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