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

49 changes: 45 additions & 4 deletions js/src/admin/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3056,6 +3056,12 @@ window.frmAdminBuildJS = function() {
return 'field_options' + opt.substring( 0, at ) + '_' + fieldId + opt.substring( at );
}

/**
* Populates the calculation field list.
*
* @param {HTMLElement} v The calculation box element.
* @param {boolean} force Whether to force the refresh.
*/
function popCalcFields( v, force ) {
let box, exclude, fields, i, list,
p = jQuery( v ).closest( '.frm-single-settings' ),
Expand All @@ -3075,16 +3081,19 @@ window.frmAdminBuildJS = function() {
box = document.getElementById( 'frm-calc-box-' + fieldId );
}

exclude = getExcludeArray( box, isSummary );
const codeList = box.querySelector( '.frm_code_list' );
exclude = getExcludeArray( codeList, isSummary );
const excludedOpts = extractExcludedOptions( exclude );
const numericTypes = isMathCalcType( box ) ? getNumericFieldTypes( codeList ) : null;

fields = getFieldList();
list = document.getElementById( 'frm-calc-list-' + fieldId );
list.innerHTML = '';

for ( i = 0; i < fields.length; i++ ) {
if ( ( exclude && exclude.includes( fields[ i ].fieldType ) ) ||
( excludedOpts.length && hasExcludedOption( fields[ i ], excludedOpts ) ) ) {
( excludedOpts.length && hasExcludedOption( fields[ i ], excludedOpts ) ) ||
( numericTypes && ! numericTypes.includes( fields[ i ].fieldType ) ) ) {
continue;
}

Expand All @@ -3103,8 +3112,14 @@ window.frmAdminBuildJS = function() {
}
}

function getExcludeArray( calcBox, isSummary ) {
const codeList = calcBox.querySelector( '.frm_code_list' );
/**
* Gets the exclude array for the calculation box.
*
* @param {HTMLElement} codeList The code list element.
* @param {boolean} isSummary Whether the calculation box is for a summary.
* @returns {Array} The exclude array.
*/
function getExcludeArray( codeList, isSummary ) {
const exclude = JSON.parse( codeList.getAttribute( 'data-exclude' ) );

if ( isSummary ) {
Expand All @@ -3126,6 +3141,32 @@ window.frmAdminBuildJS = function() {
return exclude;
}

/**
* Gets the numeric field types from the calculation box.
*
* @param {HTMLElement} codeList The code list element.
* @returns {Array|null} The numeric field types, or null if not available.
*/
function getNumericFieldTypes( codeList ) {
const numericTypes = codeList.dataset.numericTypes;
return numericTypes ? JSON.parse( numericTypes ) : null;
}

/**
* Checks if the calculation is a math calculation.
*
* @param {HTMLElement} calcBox The calculation box element.
* @returns {boolean} True if the calculation is a math calculation, false otherwise.
*/
function isMathCalcType( calcBox ) {
const fieldFormula = calcBox.parentElement;
if ( ! fieldFormula?.classList.contains( 'frm-field-formula' ) ) {
return false;
}

return document.querySelector( `input[name="field_options[calc_type_${ fieldFormula.dataset.fieldId }]"]:checked` ).value !== 'text';
}
Comment thread
shervElmi marked this conversation as resolved.

function getIncludedExtras() {
const checked = [];
const checkboxes = document.getElementsByClassName( 'frm_include_extras_field' );
Expand Down