|
39 | 39 | bindEvents: function() { |
40 | 40 | var self = this; |
41 | 41 |
|
42 | | - // Use event delegation for dynamically added conditional selects (Add form only). |
43 | | - $( document ).on( 'change', '#add-adcode select[name="acm-conditionals[]"]', function() { |
| 42 | + // Use event delegation for dynamically added conditional selects (Add and Edit forms). |
| 43 | + $( document ).on( 'change', '#add-adcode select[name="acm-conditionals[]"], #edit-adcode select[name="acm-conditionals[]"]', function() { |
44 | 44 | self.handleConditionalChange( $( this ) ); |
45 | 45 | self.updateAddButtonVisibility(); |
| 46 | + self.updateOperatorVisibility(); |
46 | 47 | }); |
47 | 48 |
|
48 | | - // Re-initialize when new conditional rows are added. |
49 | | - $( document ).on( 'click', '.add-more-conditionals', function() { |
50 | | - // Small delay to allow DOM to update. |
| 49 | + // Handle adding new conditional rows. |
| 50 | + $( document ).on( 'click', '.add-more-conditionals', function( e ) { |
| 51 | + e.preventDefault(); |
| 52 | + var $button = $( this ); |
| 53 | + var $form = $button.closest( 'form' ); |
| 54 | + var $container = $form.find( '.form-new-row' ); |
| 55 | + |
| 56 | + self.addConditionalRow( $container ); |
| 57 | + |
| 58 | + // Small delay to allow DOM to update, then clean up. |
51 | 59 | setTimeout( function() { |
52 | 60 | self.cleanupNewRows(); |
53 | 61 | self.updateAddButtonVisibility(); |
| 62 | + self.updateOperatorVisibility(); |
54 | 63 | }, 100 ); |
55 | 64 | }); |
56 | 65 |
|
57 | 66 | // Handle remove conditional click. |
58 | | - $( document ).on( 'click', '.acm-remove-conditional', function() { |
| 67 | + $( document ).on( 'click', '.acm-remove-conditional', function( e ) { |
| 68 | + e.preventDefault(); |
| 69 | + var $row = $( this ).closest( '.conditional-single-field' ); |
| 70 | + $row.remove(); |
| 71 | + |
59 | 72 | setTimeout( function() { |
60 | 73 | self.updateAddButtonVisibility(); |
| 74 | + self.updateOperatorVisibility(); |
61 | 75 | }, 100 ); |
62 | 76 | }); |
63 | 77 | }, |
64 | 78 |
|
65 | 79 | /** |
66 | 80 | * Initialize any existing conditional fields on page load. |
67 | | - * Only targets the Add form, not inline edit. |
| 81 | + * Targets both Add and Edit forms. |
68 | 82 | */ |
69 | 83 | initExistingFields: function() { |
70 | 84 | var self = this; |
71 | 85 |
|
72 | | - // Only target the Add form to avoid conflicts with inline edit. |
73 | | - $( '#add-adcode select[name="acm-conditionals[]"]' ).each( function() { |
| 86 | + // Target both Add and Edit forms. |
| 87 | + $( '#add-adcode select[name="acm-conditionals[]"], #edit-adcode select[name="acm-conditionals[]"]' ).each( function() { |
74 | 88 | var $select = $( this ); |
75 | 89 | var conditional = $select.val(); |
76 | 90 | var $argumentsContainer = $select.closest( '.conditional-single-field' ).find( '.conditional-arguments' ); |
|
92 | 106 | }); |
93 | 107 | }, |
94 | 108 |
|
| 109 | + /** |
| 110 | + * Add a new conditional row to the form. |
| 111 | + * |
| 112 | + * @param {jQuery} $container The container to add the row to. |
| 113 | + */ |
| 114 | + addConditionalRow: function( $container ) { |
| 115 | + var $master = $container.find( '#conditional-single-field-master' ); |
| 116 | + |
| 117 | + if ( ! $master.length ) { |
| 118 | + $master = $container.find( '.conditional-single-field' ).first(); |
| 119 | + } |
| 120 | + |
| 121 | + if ( ! $master.length ) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + // Clone the master row. |
| 126 | + var $newRow = $master.clone(); |
| 127 | + |
| 128 | + // Remove the master ID so only one exists. |
| 129 | + $newRow.removeAttr( 'id' ); |
| 130 | + |
| 131 | + // Reset select to default. |
| 132 | + $newRow.find( 'select[name="acm-conditionals[]"]' ).val( '' ); |
| 133 | + |
| 134 | + // Reset and show the input. |
| 135 | + var $input = $newRow.find( 'input[name="acm-arguments[]"]' ); |
| 136 | + $input.val( '' ).show(); |
| 137 | + |
| 138 | + // Remove any Select2 elements that may have been cloned. |
| 139 | + $newRow.find( 'select.acm-autocomplete-select' ).remove(); |
| 140 | + $newRow.find( '.select2-container' ).remove(); |
| 141 | + |
| 142 | + // Restore hidden input if present. |
| 143 | + var $hiddenInput = $newRow.find( 'input[data-original-name="acm-arguments[]"]' ); |
| 144 | + if ( $hiddenInput.length ) { |
| 145 | + $hiddenInput |
| 146 | + .attr( 'name', 'acm-arguments[]' ) |
| 147 | + .removeAttr( 'data-original-name' ) |
| 148 | + .val( '' ) |
| 149 | + .show(); |
| 150 | + } |
| 151 | + |
| 152 | + // Add remove link if not present. |
| 153 | + var $arguments = $newRow.find( '.conditional-arguments' ); |
| 154 | + if ( ! $arguments.find( '.acm-remove-conditional' ).length ) { |
| 155 | + $arguments.append( ' <a href="#" class="acm-remove-conditional">Remove</a>' ); |
| 156 | + } |
| 157 | + |
| 158 | + // Hide the arguments container initially. |
| 159 | + $arguments.hide(); |
| 160 | + |
| 161 | + // Append to container. |
| 162 | + $container.append( $newRow ); |
| 163 | + }, |
| 164 | + |
95 | 165 | /** |
96 | 166 | * Clean up newly added conditional rows. |
97 | 167 | * |
|
366 | 436 | * Shows the button only when at least one condition is selected. |
367 | 437 | */ |
368 | 438 | updateAddButtonVisibility: function() { |
369 | | - var $form = $( '#add-adcode' ); |
370 | | - var $addButton = $form.find( '.form-add-more' ); |
371 | | - var hasSelectedCondition = false; |
| 439 | + var $forms = $( '#add-adcode, #edit-adcode' ); |
| 440 | + |
| 441 | + $forms.each( function() { |
| 442 | + var $form = $( this ); |
| 443 | + var $addButton = $form.find( '.form-add-more' ); |
| 444 | + var hasSelectedCondition = false; |
| 445 | + |
| 446 | + // Check if any conditional select has a value. |
| 447 | + $form.find( 'select[name="acm-conditionals[]"]' ).each( function() { |
| 448 | + if ( $( this ).val() ) { |
| 449 | + hasSelectedCondition = true; |
| 450 | + return false; // Break the loop. |
| 451 | + } |
| 452 | + }); |
| 453 | + |
| 454 | + if ( hasSelectedCondition ) { |
| 455 | + $addButton.addClass( 'visible' ); |
| 456 | + } else { |
| 457 | + $addButton.removeClass( 'visible' ); |
| 458 | + } |
| 459 | + }); |
| 460 | + }, |
| 461 | + |
| 462 | + /** |
| 463 | + * Update visibility of the Logical Operator row. |
| 464 | + * |
| 465 | + * Shows the operator row only when 2+ conditions are selected. |
| 466 | + */ |
| 467 | + updateOperatorVisibility: function() { |
| 468 | + var $operatorRow = $( '#operator-row' ); |
| 469 | + |
| 470 | + if ( ! $operatorRow.length ) { |
| 471 | + return; |
| 472 | + } |
372 | 473 |
|
373 | | - // Check if any conditional select has a value. |
374 | | - $form.find( 'select[name="acm-conditionals[]"]' ).each( function() { |
| 474 | + // Count selected conditionals. |
| 475 | + var selectedCount = 0; |
| 476 | + $( '#edit-adcode select[name="acm-conditionals[]"]' ).each( function() { |
375 | 477 | if ( $( this ).val() ) { |
376 | | - hasSelectedCondition = true; |
377 | | - return false; // Break the loop. |
| 478 | + selectedCount++; |
378 | 479 | } |
379 | 480 | }); |
380 | 481 |
|
381 | | - if ( hasSelectedCondition ) { |
382 | | - $addButton.addClass( 'visible' ); |
| 482 | + if ( selectedCount >= 2 ) { |
| 483 | + $operatorRow.show(); |
383 | 484 | } else { |
384 | | - $addButton.removeClass( 'visible' ); |
| 485 | + $operatorRow.hide(); |
385 | 486 | } |
386 | 487 | } |
387 | 488 | }; |
|
0 commit comments