1(function ($) { 2 "use strict"; 3 4 $(document).ready(function () { 5 // Reset form handling 6 $(".wpcf7-form input[type='reset']").closest('form').on('reset', function (event) { 7 var $form = $(this); 8 setTimeout(function () { 9 cf7_logic_check($form); 10 }, 10); 11 }); 12 13 // Listen for input, change events on form elements 14 $("body").on("change input", ".wpcf7-form input, .wpcf7-form select, .wpcf7-form textarea", function (event) { 15 cf7_logic_check($(this).closest("form"), $(this)); 16 }); 17 18 $("body").on("cf7_logic", ".wpcf7-form input, .wpcf7-form select, .wpcf7-form textarea", function (event) { 19 cf7_logic_check($(this).closest("form"), $(this)); 20 }); 21 22 // Listen for repeater events 23 $(document).on("cf7_repeater_added done_load_repeater cf7_repeater_removed", function (event, item) { 24 $(".wpcf7-form").each(function () { 25 cf7_logic_check($(this)); 26 }); 27 }); 28 29 // Initial check on load 30 $(".wpcf7-form").each(function () { 31 cf7_logic_check($(this)); 32 }); 33 34 // Delayed check to catch repeaters that initialize asynchronously 35 setTimeout(function () { 36 $(".wpcf7-form").each(function () { 37 cf7_logic_check($(this)); 38 }); 39 }, 200); 40 41 function cf7_logic_check(form, current_field) { 42 if (!form || !form.length) return; 43 var data = form.find('.cf7_logic_data').data("logic"); 44 if (!data) return; 45 46 // Collect all repeater row IDs present in this form 47 var repeater_row_ids = []; 48 form.find(".cf7-repeater .repeater-field-item").each(function () { 49 var rid = $(this).attr("data-id"); 50 if (rid && repeater_row_ids.indexOf(rid) === -1) { 51 repeater_row_ids.push(rid); 52 } 53 }); 54 55 $.each(data, function (key, value) { 56 if (!value || !value.condition || !value.then) return; 57 58 // 1. Evaluate for non-repeater (global) elements 59 var base_then = []; 60 $.each(value.then, function (kt, vt) { 61 var $target = form.find("#" + vt).not(".cf7-field-repeater-data-
61html #" + vt); 62 if ($target.length > 0) { 63 base_then.push(vt); 64 } 65 }); 66 67 if (base_then.length > 0) { 68 var check_base = cf7_logic_check_condition(value.when, value.condition, form); 69 cf7_show_hide(check_base, value.type, base_then, form); 70 } 71 72 // 2. Evaluate for each repeater row 73 if (repeater_row_ids.length > 0) { 74 $.each(repeater_row_ids, function (idx, row_id) { 75 var rand_name = "__" + row_id; 76 var row_then = []; 77 var has_row_target = false; 78 79 $.each(value.then, function (kt, vt) { 80 var add_name_gr = vt + rand_name; 81 if (form.find("#" + add_name_gr).length > 0) { 82 row_then.push(add_name_gr); 83 has_row_target = true; 84 } 85 }); 86 87 if (has_row_target) { 88 var row_condition = []; 89 $.each(value.condition, function (k, v) { 90 var row_field_name = v.name + rand_name; 91 // Check if input exists in this repeater row 92 var exists_in_row = form.find( 93 "input[name='" + row_field_name + "'], " + 94 "input[name='" + row_field_name + "[]'], " + 95 "select[name='" + row_field_name + "'], " + 96 "select[name='" + row_field_name + "[]'], " + 97 "textarea[name='" + row_field_name + "']" 98 ).length > 0; 99 100 var cond_name = exists_in_row ? row_field_name : v.name; 101 row_condition.push({ 102 name: cond_name, 103 value: v.value, 104 equals: v.equals 105 }); 106 }); 107 108 var check_row = cf7_logic_check_condition(value.when, row_condition, form); 109 cf7_show_hide(check_row, value.type, row_then, form); 110 } 111 }); 112 } 113 }); 114 115 // Update hidden fields list for validation once after checking all rules 116 update_cf_logic_hidden(form); 117 } 118 119 function cf7_show_hide(check, type, then, form) { 120 if (!Array.isArray(then) || then.length === 0) return; 121 $.each(then, function (key, value) { 122 var $target = form.find("#" + value).not(".cf7-field-repeater-data-
122html #" + value); 123 if ($target.length === 0) return; 124 125 if (check) { 126 if (type === "show") { 127 $target.removeClass('cf7-logic-hidden'); 128 } else { 129 $target.addClass('cf7-logic-hidden'); 130 } 131 } else { 132 if (type === "show") { 133 $target.addClass('cf7-logic-hidden'); 134 } else { 135 $target.removeClass('cf7-logic-hidden'); 136 } 137 } 138 }); 139 } 140 141 function update_cf_logic_hidden(form) { 142 var hide_name = []; 143 form.find(".cf7-group.cf7-logic-hidden").not(".cf7-field-repeater-data-html *").each(function () { 144 $(this).find("input, select, textarea").each(function () { 145 var name = $(this).attr("name"); 146 if (name && name !== "_cf_logic" && name !== "_yeeaddons_cf7_repeater_fields") { 147 hide_name.push(name); 148 var clean_name = name.replace(/\[\]$/, ""); 149 if (clean_name !== name) { 150 hide_name.push(clean_name); 151 } 152 } 153 }); 154 }); 155 hide_name = Array.from(new Set(hide_name)); 156 form.find("input[name='_cf_logic']").val(hide_name.join("|")); 157 } 158 159 function compare_values(vl, content, equals) { 160 if (equals === "1") { 161 return String(vl) === String(content); 162 } else if (equals === "2") { 163 return String(vl) !== String(content); 164 } else if (equals === "3") { 165 return Number(vl) > Number(content); 166 } else if (equals === "4") { 167 return Number(vl) >= Number(content); 168 } else if (equals === "5") { 169 return Number(vl) < Number(content); 170 } else if (equals === "6") { 171 return Number(vl) <= Number(content); 172 } 173 return false; 174 } 175 176 function cf7_logic_check_condition(when, condition, form) { 177 if (!Array.isArray(condition) || condition.length === 0) return false; 178 179 var all_checks = []; 180 181 $.each(condition, function (key, value) { 182 var name = value.name; 183 var equals = String(value.equals); 184 var content = value.value; 185 var base_name = name.replace(/\[\]$/, ""); 186 187 var $inputs = form.find("input[name='" + name + "'], input[name='" + base_name + "[]'], input[name='" + base_name + "']"); 188 var type = $inputs.length > 0 ? $inputs.first().attr("type") : undefined; 189 190 var is_condition_met = false; 191 192 if (type === "checkbox") { 193 var checked_vals = []; 194 $inputs.filter(":checked").each(function () { 195 checked_vals.push($(this).val()); 196 }); 197 198 if (equals === "1") { 199 is_condition_met = checked_vals.indexOf(content) !== -1; 200 } else if (equals === "2") { 201 is_condition_met = checked_vals.indexOf(content) === -1; 202 } else if (equals === "3") { 203 var c_num = Number(content); 204 is_condition_met = checked_vals.some(function (v) { return Number(v) > c_num; }); 205 } else if (equals === "4") { 206 var c_num = Number(content); 207 is_condition_met = checked_vals.some(function (v) { return Number(v) >= c_num; }); 208 } else if (equals === "5") { 209 var c_num = Number(content); 210 is_condition_met = checked_vals.some(function (v) { return Number(v) < c_num; }); 211 } else if (equals === "6") { 212 var c_num = Number(content); 213 is_condition_met = checked_vals.some(function (v) { return Number(v) <= c_num; }); 214 } 215 } else if (type === "radio") { 216 var $checked = $inputs.filter(":checked"); 217 var vl = $checked.length > 0 ? $checked.val() : ""; 218 is_condition_met = compare_values(vl, content, equals); 219 } else { 220 var $selects = form.find("select[name='" + name + "'], select[name='" + base_name + "[]'], select[name='" + base_name + "']"); 221 var $textareas = form.find("textarea[name='" + name + "'], textarea[name='" + base_name + "']"); 222 223 if ($selects.length > 0) { 224 var sel_val = $selects.val(); 225 if (Array.isArray(sel_val)) { 226 if (equals === "1") { 227 is_condition_met = sel_val.indexOf(content) !== -1; 228 } else if (equals === "2") { 229 is_condition_met = sel_val.indexOf(content) === -1; 230 } else { 231 is_condition_met = compare_values(sel_val.join(", "), content, equals); 232 } 233 } else { 234 var vl = (sel_val !== null && sel_val !== undefined) ? sel_val : ""; 235 is_condition_met = compare_values(vl, content, equals); 236 } 237 } else if ($textareas.length > 0) { 238 var vl = $textareas.val() || ""; 239 is_condition_met = compare_values(vl, content, equals); 240 } else if ($inputs.length > 0) { 241 var vl = ""; 242 if (typeof $.fn.autoNumeric === 'function' && form.find("input[name='" + name + "']+.number-format").length > 0) { 243 try {
244 $inputs.autoNumeric(); 245 vl = $inputs.autoNumeric("get"); 246 } catch (e) { 247 vl = $inputs.val() || ""; 248 } 249 } else { 250 vl = $inputs.val() || ""; 251 } 252 is_condition_met = compare_values(vl, content, equals); 253 } else { 254 is_condition_met = false; 255 } 256 } 257 258 all_checks.push(is_condition_met); 259 }); 260 261 if (when === "one") { 262 return all_checks.some(function (c) { return c === true; }); 263 } else { 264 return all_checks.every(function (c) { return c === true; }); 265 } 266 } 267 }); 268})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.