|
521 | 521 | */ |
522 | 522 | evaluateAllConditions() { |
523 | 523 | const fieldsWithConditions = document.querySelectorAll('[data-conditional-logic]'); |
524 | | - |
525 | | - fieldsWithConditions.forEach(field => { |
526 | | - const conditionsJson = field.dataset.conditionalLogic; |
527 | | - if (!conditionsJson) return; |
528 | | - |
529 | | - try { |
530 | | - const conditions = JSON.parse(conditionsJson); |
531 | | - |
532 | | - // Determine scope: if this field is in a repeater row, scope to that row |
| 524 | + |
| 525 | + console.debug(`[OpenFields] Found ${fieldsWithConditions.length} fields with conditional logic`); |
| 526 | + |
| 527 | + fieldsWithConditions.forEach(field => { |
| 528 | + const conditionsJson = field.dataset.conditionalLogic; |
| 529 | + if (!conditionsJson) return; |
| 530 | + |
| 531 | + const fieldId = field.getAttribute('data-field-id'); |
| 532 | + console.debug(`[OpenFields] Evaluating conditions for field ${fieldId}. Raw JSON: ${conditionsJson}`); |
| 533 | + |
| 534 | + try { |
| 535 | + // Decode HTML entities if present (from PHP esc_attr) |
| 536 | + let decodedJson = conditionsJson; |
| 537 | + if (decodedJson.includes('"')) { |
| 538 | + const textarea = document.createElement('textarea'); |
| 539 | + textarea.innerHTML = decodedJson; |
| 540 | + decodedJson = textarea.value; |
| 541 | + console.debug(`[OpenFields] Decoded JSON for field ${fieldId}: ${decodedJson}`); |
| 542 | + } |
| 543 | + |
| 544 | + const conditions = JSON.parse(decodedJson); // Determine scope: if this field is in a repeater row, scope to that row |
533 | 545 | // Otherwise, null scope means global field lookup |
534 | 546 | let scopeElement = null; |
535 | 547 | const repeaterRow = field.closest('.openfields-repeater-row'); |
536 | 548 | if (repeaterRow) { |
537 | 549 | // Field is inside a repeater row - scope to this row |
538 | 550 | scopeElement = repeaterRow; |
| 551 | + console.debug(`[OpenFields] Field ${fieldId} is in repeater row, scoping to row`); |
539 | 552 | } |
540 | 553 |
|
541 | 554 | const shouldShow = this.evaluateCondition(conditions, scopeElement); |
|
612 | 625 |
|
613 | 626 | // rule.field is now a FIELD ID (immutable UUID/identifier) |
614 | 627 | const fieldId = rule.field; |
615 | | - let fieldElement = null; |
| 628 | + let fieldElement = null; |
616 | 629 |
|
617 | | - // If we have a scope element (e.g., repeater row), search within it first |
618 | | - if (scopeElement) { |
619 | | - fieldElement = scopeElement.querySelector(`[data-field-id="${fieldId}"]`); |
620 | | - } |
| 630 | + // If we have a scope element (e.g., repeater row), search within it first |
| 631 | + if (scopeElement) { |
| 632 | + fieldElement = scopeElement.querySelector(`[data-field-id="${fieldId}"]`); |
| 633 | + |
| 634 | + // Debug logging for repeater fields |
| 635 | + if (!fieldElement) { |
| 636 | + console.debug(`[OpenFields] Field ${fieldId} not found in scope (repeater row), searching globally`); |
| 637 | + } |
| 638 | + } |
621 | 639 |
|
622 | 640 | // If not found in scope, search globally (for root fields) |
623 | 641 | if (!fieldElement) { |
624 | 642 | fieldElement = document.querySelector(`[data-field-id="${fieldId}"]`); |
625 | | - } |
626 | | - |
627 | | - if (fieldElement) { |
| 643 | + } if (fieldElement) { |
628 | 644 | // Found by field ID - this is the preferred method |
629 | 645 | // Look for the actual input element within this wrapper |
630 | 646 | // For repeater subfields, the wrapper is .openfields-repeater-subfield |
|
669 | 685 | if (!fieldElement) { |
670 | 686 | fieldElement = document.querySelector(selector); |
671 | 687 | } |
672 | | - } if (!fieldElement) { |
673 | | - console.warn(`[OpenFields] Conditional field not found: ${fieldId}`); |
674 | | - return true; // If field not found, don't block (assume condition passes). |
675 | | - } |
676 | | - |
677 | | - const currentValue = this.getFieldValue(fieldElement); |
678 | | - return this.compareValues(currentValue, rule.operator, rule.value); |
679 | | - }); |
680 | | - }, |
| 688 | + } |
| 689 | + |
| 690 | + if (!fieldElement) { |
| 691 | + console.warn(`[OpenFields] Conditional field not found: ${fieldId}`); |
| 692 | + return true; // If field not found, don't block (assume condition passes). |
| 693 | + } |
681 | 694 |
|
682 | | - /** |
| 695 | + const currentValue = this.getFieldValue(fieldElement); |
| 696 | + const result = this.compareValues(currentValue, rule.operator, rule.value); |
| 697 | + |
| 698 | + // Debug logging for conditional logic |
| 699 | + console.debug(`[OpenFields] Condition check: field=${fieldId}, value="${currentValue}", operator=${rule.operator}, expected="${rule.value}", result=${result}`); |
| 700 | + |
| 701 | + return result; |
| 702 | + }); |
| 703 | + }, /** |
683 | 704 | * Get the current value of a field element. |
684 | 705 | * |
685 | 706 | * @param {HTMLElement} element - The field element. |
|
0 commit comments