Skip to content
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

Duplicate unsaved field opts #1488

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Changes from 11 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
101 changes: 101 additions & 0 deletions js/formidable_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2143,13 +2143,114 @@ function frmAdminBuildJS() {
updateFieldOrder();
afterAddField( msg, false );
maybeDuplicateUnsavedSettings( fieldId, msg );
maybeDuplicateUnsavedOptions( fieldId, msg );
toggleOneSectionHolder( replaceWith.find( '.start_divider' ) );
$field[0].querySelector( '.frm-dropdown-menu.dropdown-menu-right' )?.classList.remove( 'show' );
}
});
return false;
}

/**
* Duplicates field options that are not saved after being modified to the new field.
*
* @param {Number} originalFieldId
* @param {string} newFieldHtml
* @returns {void}
*/
function maybeDuplicateUnsavedOptions( originalFieldId, newFieldHtml ) {
const newFieldId = jQuery( newFieldHtml ).attr( 'data-fid' );
const newOptsContainer = document.getElementById( `frm_field_${newFieldId}_opts` );
const origOptsContainer = document.getElementById( `frm_field_${originalFieldId}_opts` );

if ( ! newOptsContainer || ! origOptsContainer || ! newOptsContainer.dataset.key || ! origOptsContainer.dataset.key ) {
return;
}
newOptsContainer.innerHTML = '';

const newFieldKey = newOptsContainer.dataset.key;
const originalFieldKey = origOptsContainer.dataset.key;

const args = { originalFieldId, originalFieldKey, newFieldId, newFieldKey };

copyUnsavedDeleteOptions( origOptsContainer, newOptsContainer, args );
AbdiTolesa marked this conversation as resolved.
Show resolved Hide resolved

copyUnsavedOptions( args );
}
Comment on lines +2174 to +2199
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The maybeDuplicateUnsavedOptions function is well-documented and its purpose is clear. It checks for the presence of options containers for both the original and new fields before proceeding with the duplication of options. This is a good practice as it ensures that the function operates only when necessary, potentially avoiding errors or unintended behavior.

However, there's an opportunity to improve error handling and logging. In scenarios where the options containers are not found or the data keys are missing, silently returning might make debugging difficult if the duplication does not work as expected.

Consider adding console warnings or logging to help identify when the function exits early due to missing containers or data keys. This could be invaluable for debugging purposes.


/**
* Syncs the options editing elements used to add,modify or delete options in the field options area.
*
* @param {HTMLElement} origOptsContainer
* @param {HTMLElement} newOptsContainer
* @param {object} args
* @returns {void}
*/
function copyUnsavedDeleteOptions( origOptsContainer, newOptsContainer, args ) {
const originalOpts = origOptsContainer.querySelectorAll( 'li' );

if ( ! originalOpts ) {
return;
}

const { originalFieldId, newFieldId } = args;

for ( let li of originalOpts ) {
const newOptLi = replaceElementAttribute( li, args );
const originalOptValue = li.querySelector( `.field_${originalFieldId}_option` );
const newOptValue = newOptLi.querySelector( `.field_${newFieldId}_option` );

if ( newOptValue && originalOptValue ) {
newOptValue.value = originalOptValue.value;
}

const newOptKey = newOptLi.querySelector( `#field_key_${newFieldId}-${li.dataset.optkey}` );
const originalOptKey = li.querySelector( `#field_key_${originalFieldId}-${li.dataset.optkey}` );

if ( newOptKey && originalOptKey ) {
newOptKey.value = originalOptKey.value;
}

newOptsContainer.append( newOptLi );
}
}

/**
* Syncs the options in the form preview area.
*
* @param {object} args
* @returns {void}
*/
function copyUnsavedOptions( args ) {
const { originalFieldId, newFieldId } = args;

const originalFieldOpts = document.getElementById( `field_${originalFieldId}_inner_container` ).querySelector( '.frm_opt_container' );
const newFieldOpts = document.getElementById( `field_${newFieldId}_inner_container` ).querySelector( '.frm_opt_container' );
AbdiTolesa marked this conversation as resolved.
Show resolved Hide resolved

if ( ! originalFieldOpts || ! newFieldOpts ) {
return;
}

newFieldOpts.innerHTML = '';
for ( const child of originalFieldOpts.children ) {
const newOpt = replaceElementAttribute( child, args );
newFieldOpts.append( newOpt );
}
}

function replaceElementAttribute( element, args ) {
const { originalFieldId, originalFieldKey, newFieldId, newFieldKey } = args;

let regex = new RegExp( originalFieldId, 'g' );
let elementString = element.outerHTML.replace( regex, newFieldId );
AbdiTolesa marked this conversation as resolved.
Show resolved Hide resolved
regex = new RegExp( originalFieldKey, 'g' );
elementString = elementString.replace( regex, newFieldKey );
const tempDiv = div();
tempDiv.innerHTML = elementString;

return tempDiv.firstChild;
AbdiTolesa marked this conversation as resolved.
Show resolved Hide resolved
}

function maybeDuplicateUnsavedSettings( originalFieldId, newFieldHtml ) {
var originalSettings, newFieldId, copySettings, fieldOptionKeys, originalDefault, copyDefault;

Expand Down
Loading