-
Notifications
You must be signed in to change notification settings - Fork 41
Duplicate unsaved field opts #1488
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
base: master
Are you sure you want to change the base?
Changes from all commits
82dc9db
3230fc9
9ddd399
c3fabce
b4634a0
b139f22
07c1460
8608da6
85f56fd
97bfaf8
8cf8478
1642c9b
0eae7b6
4735b6c
e29dc28
4abc8a3
e88a543
8736115
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2163,13 +2163,134 @@ 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 ); | ||
|
|
||
| copyUnsavedOptions( args ); | ||
| } | ||
|
Comment on lines
+2174
to
+2199
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The 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' ); | ||
|
|
||
| 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 elementString = element.outerHTML; | ||
| for ( const endChar of [ '-', '"', ']', '_' ] ) { | ||
| elementString = replaceFieldIdPatterns( elementString, originalFieldId, newFieldId, endChar ); | ||
| } | ||
| elementString = replaceFieldIdPatterns( elementString, originalFieldId, newFieldId, '"', '"' ); | ||
| elementString = replaceFieldIdPatterns( elementString, originalFieldKey, newFieldKey, '-' ); | ||
|
|
||
| const tempDiv = div(); | ||
| tempDiv.innerHTML = elementString; | ||
|
|
||
| return tempDiv.firstChild; | ||
| } | ||
|
|
||
| /** | ||
| * Replaces original field id/key prepended by startChar and followed by endChar with new field id/key. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param {string} elementString | ||
| * @param {Number|string} originalFieldId | ||
| * @param {Number|string} newFieldId | ||
| * @param {string} endChar | ||
| * @param {string} startChar | ||
| * | ||
| * @return {string} | ||
| */ | ||
| function replaceFieldIdPatterns( elementString, originalFieldId, newFieldId, endChar, startChar = '_' ) { | ||
| const regex = new RegExp( `${startChar}${originalFieldId}${endChar}`, 'g' ); | ||
| return elementString.replace( regex, `${startChar}${newFieldId}${endChar}` ); | ||
| } | ||
|
|
||
| function maybeDuplicateUnsavedSettings( originalFieldId, newFieldHtml ) { | ||
| var originalSettings, newFieldId, copySettings, fieldOptionKeys, originalDefault, copyDefault; | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.