Skip to content

Implemented recursive dependency evaluation to handle multi-level chains #4073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions redux-core/inc/extensions/repeater/repeater/redux-repeater.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,51 @@
redux.field_objects = redux.field_objects || {};
redux.field_objects.repeater = redux.field_objects.repeater || {};

// Helper function to trigger dependency evaluation efficiently
redux.field_objects.repeater.triggerDependencyChain = function( container ) {
const maxRounds = 3; // Maximum rounds to handle deep nesting
let round = 0;

function triggerRound() {
if ( round >= maxRounds ) {
return;
}

round++;
let triggeredAny = false;

container.find( '.redux-field select, .redux-field input[type=radio]:checked, .redux-field input[type=checkbox], .redux-field input[type=hidden]' ).each( function() {
const field = $( this );
if ( field.hasClass( 'in-repeater' ) ) {
const value = field.val();
if ( value && value !== '' && value !== '0' && value !== 'false' ) {
field.trigger( 'change' );
triggeredAny = true;
}
}
});

// Handle switch fields specifically
container.find( '.redux-field input[type=hidden]' ).each( function() {
const hiddenField = $( this );
if ( hiddenField.hasClass( 'in-repeater' ) && hiddenField.attr( 'name' ) && hiddenField.attr( 'name' ).indexOf( '[' ) > -1 ) {
const value = hiddenField.val();
if ( value === '1' || value === 'true' ) {
hiddenField.trigger( 'change' );
triggeredAny = true;
}
}
});

// If we triggered any changes, schedule another round to catch dependencies of newly shown fields
if ( triggeredAny && round < maxRounds ) {
setTimeout( triggerRound, 100 );
}
}

triggerRound();
};

redux.field_objects.repeater.getOptName = function ( el ) {
let optName;

Expand Down Expand Up @@ -78,6 +123,11 @@
redux.field_objects.repeater.bindTitle( el );
redux.field_objects.repeater.remove( el, gid );
redux.field_objects.repeater.add( el );

// Use efficient dependency chain evaluation instead of performance-heavy recursive checking
setTimeout( function() {
redux.field_objects.repeater.triggerDependencyChain( el );
}, 150 );
}
);
};
Expand Down Expand Up @@ -329,6 +379,13 @@

$.redux.initFields();

// Use efficient dependency evaluation for newly activated panel
if ( ui.newPanel && ui.newPanel.length ) {
setTimeout( function() {
redux.field_objects.repeater.triggerDependencyChain( ui.newPanel );
}, 100 );
}

if ( 'function' === typeof reduxRepeaterAccordionActivate ) {
a = $( this ).next( '.redux-repeaters-add' );
relName = a.attr( 'data-name' );
Expand Down