Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion js/formidable_admin.js

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions js/src/admin/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { getFieldLoadBatch } = require( './fieldLoadWeights' );
const { hydrateFieldPlaceholders } = require( './fieldPlaceholders' );
const { renderDropdownPreview, updateComboSelectPreview, updateAddressPreview } = require( './fieldPreview' );
const { captureFieldSettings, applyFieldSettings, restoreFieldSettingValues } = require( './duplicateFieldSettings' );
const { addFieldSettingsMetadata, cacheFieldSettings, forgetFieldSettings, materializeFieldSettings, materializeAllFieldSettings } = require( './deferredFieldSettings' );
const { addFieldSettingsMetadata, cacheFieldSettings, forgetFieldSettings, materializeFieldSettings, materializeAllFieldSettings, setPendingFieldSetting } = require( './deferredFieldSettings' );
const { hydrateFieldPreviews, renderFieldPreviews, PENDING_PREVIEW_CLASS } = require( './fieldPreviews' );
const { queueFieldPreview, renderFieldPreviewNow, forgetFieldPreview, setInitialFieldPreviews } = require( './fieldPreviewQueue' );
const { queueFieldSettings, startFieldSettingsQueue, isFieldSettingsPending, requestFieldSettingsNow, waitForFieldSettings, cancelFieldSettings } = require( './fieldSettingsLoader' );
Expand Down Expand Up @@ -2266,7 +2266,15 @@ window.frmAdminBuildJS = function() {

// Update the in_section field value
function updateInSectionValue( fieldId, sectionId ) {
document.getElementById( `frm_in_section_${ fieldId }` ).value = sectionId;
// The input may still be in a cached settings panel, which would otherwise keep the old section.
ensureFieldSettings( fieldId );
const inSectionInput = document.getElementById( `frm_in_section_${ fieldId }` );
if ( inSectionInput ) {
inSectionInput.value = sectionId;
return;
}
// The settings are still loading. Apply the section once the panel is inserted.
setPendingFieldSetting( fieldId, 'in_section', `frm_in_section_${ fieldId }`, sectionId );
}

/**
Expand Down Expand Up @@ -8129,7 +8137,8 @@ window.frmAdminBuildJS = function() {
function checkUniqueOpt( targetInput ) {
const settingsContainer = targetInput.closest( '.frm-single-settings' );
const fieldId = settingsContainer.getAttribute( 'data-fid' );
const areValuesSeparate = settingsContainer.querySelector( `[name="field_options[separate_value_${ fieldId }]"]` ).checked;
// The separate values setting is only there when Pro is active.
const areValuesSeparate = Boolean( settingsContainer.querySelector( `[name="field_options[separate_value_${ fieldId }]"]` )?.checked );

if ( areValuesSeparate && ! targetInput.name.endsWith( '[value]' ) ) {
return;
Expand Down
35 changes: 34 additions & 1 deletion js/src/admin/deferredFieldSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ export function addFieldSettingsMetadata( field, fieldId, metadata ) {
const container = frmDom.div( { className: 'frm-deferred-settings-meta' } );
container.hidden = true;
const inputs = Object.entries( metadataInputs ).flatMap( ( [ key, [ name, prefix ] ] ) => {
const value = metadata[ key ];
let value = metadata[ key ];
if ( value === null || value === undefined ) {
return [];
}
if ( key === 'type' && value === 'select' ) {
// Match the field type setting, which posts "dropdown" because some security tools flag "select" as SQL injection.
value = 'dropdown';
}
const input = frmDom.tag( 'input' );
input.type = 'hidden';
input.name = `field_options[${ name }_${ fieldId }]`;
Expand All @@ -43,6 +47,35 @@ export function addFieldSettingsMetadata( field, fieldId, metadata ) {
field.append( container );
}

/**
* Keep a setting change for a panel that has not loaded yet, so it is applied when the panel is inserted.
*
* @since x.x
* @param {string} fieldId Numeric field ID.
* @param {string} name Setting name, without the field ID suffix.
* @param {string} id ID of the matching input in the settings panel.
* @param {string} value The new setting value.
* @return {boolean} True when the value was kept, false when the field has no deferred settings.
*/
export function setPendingFieldSetting( fieldId, name, id, value ) {
const metadata = document.getElementById( `frm_field_id_${ fieldId }` )?.querySelector( ':scope > .frm-deferred-settings-meta' );
if ( ! metadata ) {
return false;
}

const inputName = `field_options[${ name }_${ fieldId }]`;
let input = Array.from( metadata.querySelectorAll( 'input' ) ).find( candidate => candidate.name === inputName );
if ( ! input ) {
input = frmDom.tag( 'input' );
input.type = 'hidden';
input.name = inputName;
input.id = id;
metadata.append( input );
}
input.value = value;
return true;
}

/**
* Cache server-rendered settings without parsing their HTML or creating controls.
*
Expand Down
110 changes: 110 additions & 0 deletions tests/js/deferredFieldSettings.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { addFieldSettingsMetadata, cacheFieldSettings, materializeFieldSettings, setPendingFieldSetting } from '../../js/src/admin/deferredFieldSettings.js';

const elements = new Map();

function element( tagName, props = {} ) {
const node = {
tagName,
children: [],
className: '',
id: '',
name: '',
value: '',
parent: null,
append( ...nodes ) {
nodes.forEach( child => {
child.parent = node;
node.children.push( child );
} );
},
remove() {
node.parent.children = node.parent.children.filter( child => child !== node );
},
querySelectorAll( selector ) {
const found = [];
const walk = parent => parent.children.forEach( child => {
if ( selector.split( ', ' ).includes( child.tagName ) ) {
found.push( child );
}
walk( child );
} );
walk( node );
return found;
},
querySelector( selector ) {
const className = selector.replace( ':scope > ', '' ).replace( '.', '' );
return node.children.find( child => child.className === className ) || null;
},
// Stands in for parsing the cached settings HTML.
insertAdjacentHTML( position, html ) {
node.append( html() );
},
...props
};
return node;
}

globalThis.frmDom = {
tag: tagName => element( tagName ),
div: props => element( 'div', props )
};
globalThis.document = {
getElementById: id => {
const walk = parent => {
for ( const child of parent.children ) {
if ( child.id === id ) {
return child;
}
const match = walk( child );
if ( match ) {
return match;
}
}
return null;
};
return elements.get( id ) || walk( { children: [ ...elements.values() ] } );
}
};
globalThis.wp = { hooks: { doAction: () => {} } };

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unexpected empty method 'doAction'


Having empty functions hurts readability, and is considered a code-smell. There's almost always a way to avoid using them. If you must use one, consider adding a comment to inform the reader of its purpose.


function addField( fieldId ) {
const field = element( 'li', { id: `frm_field_id_${ fieldId }` } );
elements.set( field.id, field );
return field;
}

test( 'metadata posts dropdown fields as "dropdown" and skips missing values', () => {
const field = addField( '1' );
addFieldSettingsMetadata( field, '1', { name: 'Pick one', type: 'select', key: null } );
const inputs = field.querySelector( '.frm-deferred-settings-meta' ).querySelectorAll( 'input' );
assert.deepEqual( inputs.map( input => [ input.name, input.value ] ), [
[ 'field_options[name_1]', 'Pick one' ],
[ 'field_options[type_1]', 'dropdown' ]
] );
} );

test( 'a setting changed before the panel loads is applied when the panel is inserted', () => {
const field = addField( '2' );
addFieldSettingsMetadata( field, '2', { name: 'Moved' } );
assert.equal( setPendingFieldSetting( '2', 'in_section', 'frm_in_section_2', '9' ), true );
assert.equal( setPendingFieldSetting( '2', 'in_section', 'frm_in_section_2', '10' ), true );

const inSection = element( 'input', { id: 'frm_in_section_2', name: 'field_options[in_section_2]', value: '0' } );
const settings = element( 'div', { id: 'frm-single-settings-2' } );
settings.append( inSection );
cacheFieldSettings( '2', () => settings );

const initialized = [];
assert.equal( materializeFieldSettings( '2', panel => initialized.push( panel ) ), settings );
assert.equal( inSection.value, '10' );
assert.deepEqual( initialized, [ settings ] );
assert.equal( field.querySelector( '.frm-deferred-settings-meta' ), null );
} );

test( 'a pending setting is not kept for a field without deferred settings', () => {
addField( '3' );
assert.equal( setPendingFieldSetting( '3', 'in_section', 'frm_in_section_3', '9' ), false );
assert.equal( setPendingFieldSetting( '4', 'in_section', 'frm_in_section_4', '9' ), false );
} );
Loading