1(function($) { 2 3 var conditional_logic = { 4 5 field_prefix: 'wpuf_', 6 7 init: function() { 8 var self = this; 9 10 self.refresh_conditions(); 11 12 $( document ).on( 'change', '.wpuf-fields input, .wpuf-fields textarea, .wpuf-fields select', function(){ 13 self.refresh_conditions(); 14 }); 15 16 $( document ).on( 'dblclick', '.wpuf-fields input:radio', function () { 17 self.refresh_conditions(); 18 }); 19 20 $( document ).on( 'wpuf-ajax-fetched-child-categories', function ( e, container_id ) { 21 self.refresh_conditions(); 22 23 $( 'select', '#' + container_id ).on( 'change', function () { 24 self.refresh_conditions(); 25 } ); 26 } ); 27 28 // Use MutationObserver instead of deprecated DOMSubtreeModified 29 if (typeof MutationObserver !== 'undefined') { 30 $(".wpuf-fields ul.wpuf-attachment-list").each(function() { 31 var observer = new MutationObserver(function(mutations) { 32 self.refresh_conditions(); 33 }); 34 35 observer.observe(this, { 36 childList: true, 37 subtree: true 38 }); 39 }); 40 } 41 42 tinyMCE.editors.forEach(function(editor) { 43 editor.on('change', function() { 44 self.refresh_conditions(); 45 }); 46 }); 47 48 }, 49 50 refresh_conditions: function () { 51 //need to check radio for default value although its checked but not working 52 if ( $( '[data-type="radio"]' ).closest( 'li' ).css( 'display' ) === 'none' || $( '[data-type="radio"]' ).closest( 'tr' ).css( 'display' ) === 'none' ) { 53 $( 'input[type="radio"], input[type="checkbox"]' ).each( function () { 54 if ( $(this).attr('checked') === 'checked' ) { 55 this.checked = true; 56 } 57 }); 58 } 59 this.apply_logic(); 60 61 // refresh pluploads when uploads show based on conditional logic 62 if ( typeof wpuf_plupload_items !== 'undefined' && wpuf_plupload_items.length ) { 63 for (var i = wpuf_plupload_items.length - 1; i >= 0; i--) { 64 // Check if the item has a refresh method (plupload.Uploader instance) 65 if ( wpuf_plupload_items[i] && typeof wpuf_plupload_items[i].refresh === 'function' ) { 66 wpuf_plupload_items[i].refresh(); 67 } 68 } 69 } 70 }, 71 72 apply_logic: function() { 73 let all = []; 74 75 if ( typeof wpuf_conditional_items === 'undefined' ) { 76 return; 77 } 78 79 $.each( wpuf_conditional_items, function( k, item ) { 80 $.each(item.cond_field, function (key, item_meta) { 81 let input_type = item.input_type !== undefined ? item.input_type[key] : ''; 82 83 // If input_type is empty but item_meta contains a field type, use that 84 if (input_type === '' && item_meta && ['textarea', 'text', 'select', 'checkbox', 'radio'].includes(item_meta)) { 85 input_type = item_meta; 86 } 87 88 // fields with old conditions 89 if (input_type === '') { 90 all = conditional_logic.check_field_without_input_type(item, key, item_meta); 91 } else { 92 switch (input_type) { 93 case 'select': 94 all[key] = conditional_logic.check_select_field(item, key, item_meta); 95 break; 96 case 'taxonomy': 97 all[key] = conditional_logic.check_taxonomy_field(item, key, item_meta); 98 break; 99 case 'radio': 100 case 'checkbox': 101 all[key] = conditional_logic.check_checked_field(item, key, item_meta, input_type); 102 break; 103 case 'text': 104 case 'email': 105 case 'url': 106 case 'password': 107 all[key] = conditional_logic.check_text_field(item, key, item_meta, input_type); 108 break; 109 case 'textarea': 110 all[key] = conditional_logic.check_textarea_field(item, key, item_meta, input_type); 111 break; 112 case 'numeric_text': 113 all[key] = conditional_logic.check_numeric_field(item, key, item_meta); 114 break; 115 default: 116 all[key] = conditional_logic.check_default_field(item, key, item_meta, input_type); 117 break; 118 } 119 } 120 }); 121 122 var field_selector = '.' + conditional_logic.field_prefix + item.name + '_' + item.form_id; 123 124 if (item.cond_logic === 'any') { 125 let check = all.indexOf(true); 126 127 if (check != '-1') { 128 if (item.type === 'address') { 129 $('li.wpuf-el.' + item.name).show(); 130 } else { 131 $(field_selector).closest('li').show(); 132 } 133 134 } else { 135 if (item.type === 'address') { 136 $('li.wpuf-el.' + item.name).hide(); 137 } else { 138 $(field_selector).closest('li').hide(); 139 140 if (item.type === 'checkbox' || item.type === 'radio' || item.type === 'taxonomy') { 141 $(field_selector).closest('li').find(':input').each(function () { 142 this.checked = false;
143 }); 144 } else if (item.type === 'select') { 145 $(field_selector).closest('li').hide(); 146 } else if (item.type === 'submit') { 147 // do nothing 148 } else { 149 // $( field_selector ).closest('li').find(':input').val(''); 150 $(field_selector).closest('li').find(':input').show(); 151 } 152 } 153 154 } 155 156 } else { 157 158 let check = all.indexOf(false); 159 160 if (check == '-1') { 161 if (item.type === 'address') { 162 $('li.wpuf-el.' + item.name).show(); 163 } else { 164 $(field_selector).closest('li').show(); 165 } 166 167 } else { 168 169 if (item.type === 'address') { 170 $('li.wpuf-el.' + item.name).hide(); 171 } else { 172 $(field_selector).closest('li').hide(); 173 174 if (item.type === 'checkbox' || item.type === 'radio' || item.type === 'taxonomy') { 175 $(field_selector).closest('li').find(':input').each(function () { 176 this.checked = false; 177 }); 178 } else if (item.type === 'select') { 179 $(field_selector).closest('li').hide(); 180 } else if (item.type === 'submit') { 181 // do nothing 182 } else { 183 // $( field_selector ).closest('li').find(':input').val(''); 184 $(field_selector).closest('li').find(':input').show(); 185 } 186 } 187 } 188 189 } 190 191 all.length = 0; 192 }); 193 }, 194 195 check_taxonomy_field: (item, key, item_meta) => { 196 let form_id = '_' + item.form_id; 197 let selector = '.' + conditional_logic.field_prefix + item_meta + form_id; 198 199 let type = $(selector).attr('type'); 200 if (type === 'checkbox') { 201 return conditional_logic.check_checked_field(item, key, item_meta, type); 202 } else if (type === 'text') { 203 return conditional_logic.check_taxonomy_text_field(item, key, item_meta, type); 204 } else { 205 return conditional_logic.check_select_field(item, key, item_meta, type); 206 } 207 }, 208 209 check_taxonomy_text_field: (item, key, item_meta, input_type) => { 210 let form_id = '_' + item.form_id; 211 let selector = '.' + conditional_logic.field_prefix + item_meta + form_id; 212 let operator = item.cond_operator[key] !== undefined ? item.cond_operator[key] : ''; 213 let cond_value = item.option_title[key]; 214 let field = $('input[type=text].textfield' + selector); 215 let field_value = field.val(); 216 field_value = field_value.replace(/,\s*$/, ""); 217 218 // condition set to 'has no value' 219 if (operator === '==empty' && field_value === '') { 220 return true; 221 } 222 223 // condition set to 'has any value' 224 if (operator === '!=empty' && field_value !== '') { 225 return true; 226 } 227 228 // condition set to check equals 229 if (operator === '=' && field_value === cond_value) { 230 return true; 231 } 232 233 // condition set to check if contains 234 if (operator === '==contains' && (field_value).indexOf(cond_value) > -1) { 235 return true; 236 } 237 238 // condition set to check if is not the same 239 return operator === '!=' && field_value !== cond_value; 240 }, 241 242 check_select_field: function(item, key, item_meta) { 243 let form_id = '_' + item.form_id; 244 let selector = '.' + conditional_logic.field_prefix + item_meta + form_id; 245 let value = item.cond_option[key]; 246 let operator = item.cond_operator[key] !== undefined ? item.cond_operator[key] : ''; 247 let select = $('select' + selector + '>option[value="' + value + '"]'); 248 let select_fields = $('select' + selector + ' option'); 249 250 // condition set to 'has any value' 251 if (operator === '!=empty') { 252 let has_any_value = false;
253 if (select_fields) { 254 $.each(select_fields, (index, select) => { 255 if ($(select).is(':selected') && $(select).val() !== '-1') { 256 has_any_value = true; 257 return false; 258 } 259 }); 260 } 261 262 return has_any_value; 263 } 264 265 // condition set to 'has no value' 266 if (operator === '==empty') { 267 let has_no_value = true; 268 if (select_fields) { 269 $.each(select_fields, (index, select) => { 270 if ($(select).is(':selected') && $(select).val() !== '-1') { 271 has_no_value = false; 272 return false; 273 } 274 }); 275 } 276 277 return has_no_value; 278 } 279 280 if (select.length) { 281 let selected_status = select.is(':selected') ? true : false; 282 283 if ( operator === '=' && selected_status ) { 284 return true; 285 } 286 287 if ( operator === '!=' && selected_status === false ) { 288 return true; 289 } 290 291 return false; 292 } 293 }, 294 295 // checkbox and radio fields 296 check_checked_field: function (item, key, item_meta, input_type) { 297 let form_id = '_' + item.form_id; 298 let selector = '.' + conditional_logic.field_prefix + item_meta + form_id; 299 let value = item.cond_option[key]; 300 let operator = item.cond_operator[key] !== undefined ? item.cond_operator[key] : ''; 301 let field; 302 let all_fields; 303 304 if (input_type === 'radio') { 305 field = $('input[type=radio][value="' + value + '"]' + selector); 306 all_fields = $('input[type=radio]' + selector); 307 } else { 308 field = $('input[type=checkbox][value="' + value + '"]' + selector); 309 all_fields = $('input[type=checkbox]' + selector); 310 } 311 312 // condition set to 'has any value' 313 if (operator === '!=empty') { 314 let has_any_value = false; 315 316 if (all_fields) { 317 $.each(all_fields, (index, single_field) => { 318 if ( $(single_field).is(':checked') ) { 319 has_any_value = true; 320 return false; 321 } 322 }); 323 } 324 325 return has_any_value; 326 } 327 328 // condition set to 'has no value' 329 if (operator === '==empty') { 330 let has_no_value = true; 331 if (all_fields) { 332 $.each(all_fields, (index, single_field) => { 333 if ($(single_field).is(':checked') && $(single_field).val() !== '-1') { 334 has_no_value = false; 335 return false; 336 } 337 }); 338 } 339 340 return has_no_value; 341 } 342 343 if (field.length) { 344 let field_checked_status = field.is(':checked'); 345 346 if (operator === '=' && field_checked_status) { 347 return true; 348 } 349 350 if (operator === '!=' && field_checked_status === false) { 351 return true; 352 } 353 354 return false; 355 } 356 }, 357 358 check_text_field: function (item, key, item_meta, input_type) { 359 let form_id = '_' + item.form_id; 360 let selector = '.' + conditional_logic.field_prefix + item_meta + form_id; 361 let operator = item.cond_operator[key] !== undefined ? item.cond_operator[key] : ''; 362 let cond_value = item.cond_option[key]; 363 let field; 364 let field_value; 365 366 if (input_type === 'text') { 367 field = $('input[type=text].textfield' + selector); 368 field_value = field.val(); 369 } else if (input_type === 'textarea') { 370 field = $('textarea.textareafield' + selector); 371 field_value = field.val(); 372 } else if (input_type === 'email') { 373 field = $('input[type=email]' + selector); 374 field_value = field.val(); 375 } else if (input_type === 'url') { 376 field = $('input[type=url].url' + selector); 377 field_value = field.val(); 378 } else if (input_type === 'password') { 379 field = $('input[type=password].password' + selector); 380 field_value = field.val(); 381 } 382 383 return this.validate_text_condition( operator, field_value, cond_value); 384 }, 385 386 check_textarea_field: function (item, key, item_meta, input_type) { 387 let form_id = '_' + item.form_id; 388 let selector_id = item_meta + form_id; 389 let operator = item.cond_operator[key] !== undefined ? item.cond_operator[key] : ''; 390 let field_type = item.field_type[key] !== undefined ? item.field_type[key] : ''; 391 let cond_value = item.cond_option[key]; 392
393 // not a rich or teeny rich textarea 394 if (field_type !== 'teeny' && field_type !== 'yes') { 395 return this.check_text_field( item, key, item_meta, input_type ); 396 } 397 398 const field_value = $.trim( tinyMCE.get( selector_id ).getContent( {format: 'text'} ) ); 399 400 return this.validate_text_condition( operator, field_value, cond_value ); 401 }, 402 403 check_numeric_field: function (item, key, item_meta) { 404 let form_id = '_' + item.form_id; 405 let selector = '.' + conditional_logic.field_prefix + item_meta + form_id; 406 let operator = item.cond_operator[key] !== undefined ? item.cond_operator[key] : ''; 407 let cond_value = item.cond_option[key]; 408 let number = $('input[type=number].textfield' + selector); 409 let field_value = number.val(); 410 411 // condition set to 'has no value' 412 if (operator === '==empty' && field_value === '') { 413 return true; 414 } 415 416 // condition set to 'has any value' 417 if (operator === '!=empty' && field_value !== '') { 418 return true; 419 } 420 421 if (operator === '==contains' && (field_value).indexOf(cond_value) > -1) { 422 return true; 423 } 424 425 field_value = parseInt(field_value); 426 cond_value = parseInt(cond_value); 427 428 if ( operator === '=' && field_value === cond_value ) { 429 return true; 430 } 431 432 if ( operator === '!=' && field_value !== cond_value ) { 433 return true; 434 } 435 436 if ( operator === 'greater' && field_value > cond_value ) { 437 return true; 438 } 439 440 if ( operator === 'less' && field_value < cond_value ) { 441 return true; 442 } 443 444 return false; 445 }, 446 447 check_default_field: function (item, key, item_meta, input_type) { 448 let operator = item.cond_operator[key] !== undefined ? item.cond_operator[key] : ''; 449 let form_id = '_' + item.form_id; 450 let field; 451 let field_value = null; 452 453 if (input_type === 'map') { 454 field = $('input[type=text]#wpuf-map-add-' + item_meta); 455 field_value = field.val(); 456 } else if (input_type === 'file_upload') { 457 // Match both the legacy anchor (`a.button.file-selector.<cls>`) and the 458 // redesigned upload trigger (`.wpuf-upload-trigger.<cls>`). The attachment 459 // list is a descendant of `.wpuf-attachment-upload-filelist`, not a direct 460 // sibling of the anchor, so walk up to that container to count uploads. 461 var file_field_class = conditional_logic.field_prefix + item_meta + form_id; 462 field = $('a.file-selector.' + file_field_class + ', .wpuf-upload-trigger.' + file_field_class); 463 field_value = field.closest('.wpuf-attachment-upload-filelist').find('ul.wpuf-attachment-list').children().length; 464 } 465 466 // condition set to 'has no value' 467 if (operator === '==empty' && !field_value) { 468 return true; 469 } 470 471 // condition set to 'has any value' 472 if (operator === '!=empty' && field_value) { 473 return true; 474 } 475 476 return false; 477 }, 478 479 // fields with old conditions 480 check_field_without_input_type: function(item, key, item_meta) { 481 let all = []; 482 let form_id = '_' + item.form_id, 483 value = item.cond_option[key], 484 selector = '.' + conditional_logic.field_prefix + item_meta + form_id, 485 operator = ( item.cond_operator[key] === '=' ) ? true : false, 486 checkbox = $('input[type=checkbox][value="' + value + '"]' + selector), 487 radio = $('input[type=radio][value="' + value+'"]'+ selector), 488 select = $('select' + selector + '>option[value="' + value + '"]'); 489 490 if ( select.length ) { 491 let select_selected_status = select.is(':selected') ? true : false;
492 493 if ( operator && select_selected_status ) { 494 all[key] = true; 495 } else if ( operator === false && select_selected_status === false ) { 496 all[key] = true; 497 } else { 498 all[key] = false; 499 } 500 } else if ( radio.length ) { 501 let radio_checked_status = radio.is(':checked') ? true : false; 502 503 if ( operator && radio_checked_status ) { 504 all[key] = true; 505 } else if ( operator === false && radio_checked_status === false ) { 506 all[key] = true; 507 } else { 508 all[key] = false; 509 } 510 } else if ( checkbox.length ) { 511 let checkbox_checked_status = checkbox.is(':checked') ? true : false; 512 513 if( operator && checkbox_checked_status ) { 514 all[key] = true; 515 } else if ( operator === false && checkbox_checked_status === false ) { 516 all[key] = true; 517 } else { 518 all[key] = false; 519 } 520 } else { 521 all[key] = false; 522 } 523 524 return all; 525 }, 526 527 validate_text_condition: function (operator, field_value, cond_value) { 528 // empty string if field_value is undefined 529 const value = field_value !== undefined ? field_value : ''; 530 531 532 // condition set to 'has no value' 533 if (operator === '==empty' && value === '') { 534 return true; 535 } 536 537 // condition set to 'has any value' 538 if (operator === '!=empty' && value !== '') { 539 return true; 540 } 541 542 if (operator === '=' && value === cond_value) { 543 return true; 544 } 545 546 if (operator === '==contains' && (value).indexOf(cond_value) > -1) { 547 return true; 548 } 549 550 return operator === '!=' && value !== cond_value; 551 } 552 }; 553 554 $( document ).ready(function() { 555 conditional_logic.init(); 556 }); 557 558})(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.