Skip to content

Commit 545ff39

Browse files
committed
refactor: import KDMAUtils directly in table-formatter.js
Replace awkward parameter passing of KDMAUtils with direct import from state.js. This simplifies function signatures and makes the code more maintainable by removing the need to thread KDMAUtils through multiple function calls. All tests continue to pass, confirming the refactor preserves functionality.
1 parent 229cd1b commit 545ff39

2 files changed

Lines changed: 19 additions & 24 deletions

File tree

align_browser/static/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ document.addEventListener("DOMContentLoaded", () => {
599599
if (isDifferent) {
600600
td.style.borderLeft = '3px solid #007bff';
601601
}
602-
td.innerHTML = formatValue(pinnedValue, paramInfo.type, paramName, runData.id, appState.pinnedRuns, KDMAUtils);
602+
td.innerHTML = formatValue(pinnedValue, paramInfo.type, paramName, runData.id, appState.pinnedRuns);
603603

604604
row.appendChild(td);
605605

align_browser/static/table-formatter.js

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Table formatting functions for displaying experiment data
22

3+
import { KDMAUtils } from './state.js';
4+
35
// HTML Templates
46
const HTML_NA_SPAN = '<span class="na-value">N/A</span>';
57
const HTML_NO_OPTIONS_SPAN = '<span class="na-value">No options available</span>';
@@ -269,11 +271,8 @@ const TEXT_PREVIEW_LENGTH = 800;
269271
const FLOATING_POINT_TOLERANCE = 0.001;
270272

271273
// Format KDMA value consistently across the application
272-
export function formatKDMAValue(value, KDMAUtils = null) {
273-
if (KDMAUtils) {
274-
return KDMAUtils.formatValue(value);
275-
}
276-
return typeof value === 'number' ? value.toFixed(2) : value.toString();
274+
export function formatKDMAValue(value) {
275+
return KDMAUtils.formatValue(value);
277276
}
278277

279278
// Format KDMA association bar for choice display
@@ -381,19 +380,15 @@ export function compareValues(val1, val2) {
381380
}
382381

383382
// Main value formatting function for table cells
384-
export function formatValue(value, type, paramName = '', runId = '', pinnedRuns = null, KDMAUtils = null) {
383+
export function formatValue(value, type, paramName = '', runId = '', pinnedRuns = null) {
385384
if (value === null || value === undefined || value === 'N/A') {
386385
return HTML_NA_SPAN;
387386
}
388387

389388
// Handle dropdown parameters for pinned runs
390389
if (runId !== '' && pinnedRuns && PARAMETER_DROPDOWN_HANDLERS[paramName]) {
391390
const handler = PARAMETER_DROPDOWN_HANDLERS[paramName];
392-
if (paramName === 'kdma_values') {
393-
return handler(runId, value, pinnedRuns, KDMAUtils);
394-
} else {
395-
return handler(runId, value, pinnedRuns);
396-
}
391+
return handler(runId, value, pinnedRuns);
397392
}
398393

399394
switch (type) {
@@ -613,7 +608,7 @@ export function getValidKDMAsForRun(runId, pinnedRuns) {
613608
}
614609

615610
// Get valid KDMA types that can be selected for a specific run
616-
export function getValidKDMATypesForRun(runId, currentKdmaType, currentKDMAs, pinnedRuns, KDMAUtils) {
611+
export function getValidKDMATypesForRun(runId, currentKdmaType, currentKDMAs, pinnedRuns) {
617612
const run = pinnedRuns.get(runId);
618613
if (!run?.availableOptions?.kdmas?.validCombinations) {
619614
return [currentKdmaType]; // Fallback to just current type
@@ -655,7 +650,7 @@ export function getValidKDMATypesForRun(runId, currentKdmaType, currentKDMAs, pi
655650
}
656651

657652
// Check if a specific KDMA can be removed from a run
658-
export function canRemoveSpecificKDMA(runId, kdmaType, pinnedRuns, KDMAUtils) {
653+
export function canRemoveSpecificKDMA(runId, kdmaType, pinnedRuns) {
659654
const run = pinnedRuns.get(runId);
660655
if (!run) return false;
661656

@@ -692,7 +687,7 @@ export function canRemoveSpecificKDMA(runId, kdmaType, pinnedRuns, KDMAUtils) {
692687
}
693688

694689
// Check if we can add another KDMA given current KDMA values
695-
export function canAddKDMAToRun(runId, currentKDMAs, pinnedRuns, KDMAUtils) {
690+
export function canAddKDMAToRun(runId, currentKDMAs, pinnedRuns) {
696691
const run = pinnedRuns.get(runId);
697692
if (!run?.availableOptions?.kdmas?.validCombinations) {
698693
return false;
@@ -725,18 +720,18 @@ export function canAddKDMAToRun(runId, currentKDMAs, pinnedRuns, KDMAUtils) {
725720
}
726721

727722
// Create KDMA controls HTML for table cells
728-
export function createKDMAControlsForRun(runId, currentKDMAs, pinnedRuns, KDMAUtils) {
723+
export function createKDMAControlsForRun(runId, currentKDMAs, pinnedRuns) {
729724
const run = pinnedRuns.get(runId);
730725
if (!run) return HTML_NA_SPAN;
731726

732727
const currentKDMAEntries = Object.entries(currentKDMAs || {});
733-
const canAddMore = canAddKDMAToRun(runId, currentKDMAs, pinnedRuns, KDMAUtils);
728+
const canAddMore = canAddKDMAToRun(runId, currentKDMAs, pinnedRuns);
734729

735730
let html = `<div class="table-kdma-container" data-run-id="${runId}">`;
736731

737732
// Render existing KDMA controls
738-
currentKDMAEntries.forEach(([kdmaType, value], index) => {
739-
html += createSingleKDMAControlForRun(runId, kdmaType, value, index, pinnedRuns, KDMAUtils);
733+
currentKDMAEntries.forEach(([kdmaType, value]) => {
734+
html += createSingleKDMAControlForRun(runId, kdmaType, value, pinnedRuns);
740735
});
741736

742737
// Add button - always show but enable/disable based on availability
@@ -759,13 +754,13 @@ export function createKDMAControlsForRun(runId, currentKDMAs, pinnedRuns, KDMAUt
759754
}
760755

761756
// Create individual KDMA control for table cell
762-
export function createSingleKDMAControlForRun(runId, kdmaType, value, index, pinnedRuns, KDMAUtils) {
757+
export function createSingleKDMAControlForRun(runId, kdmaType, value, pinnedRuns) {
763758
const availableKDMAs = getValidKDMAsForRun(runId, pinnedRuns);
764759
const run = pinnedRuns.get(runId);
765760
const currentKDMAs = run.kdmaValues || {};
766761

767762
// Get available types (only those that can form valid combinations)
768-
const availableTypes = getValidKDMATypesForRun(runId, kdmaType, currentKDMAs, pinnedRuns, KDMAUtils);
763+
const availableTypes = getValidKDMATypesForRun(runId, kdmaType, currentKDMAs, pinnedRuns);
769764

770765
const validValues = Array.from(availableKDMAs[kdmaType] || []);
771766

@@ -816,12 +811,12 @@ export function createSingleKDMAControlForRun(runId, kdmaType, value, index, pin
816811
min="${minVal}" max="${maxVal}" step="${step}"
817812
value="${value}"
818813
oninput="handleRunKDMASliderInput('${runId}', '${kdmaType}', this)">
819-
<span class="table-kdma-value-display" id="kdma-value-${runId}-${kdmaType}">${formatKDMAValue(value, KDMAUtils)}</span>
814+
<span class="table-kdma-value-display" id="kdma-value-${runId}-${kdmaType}">${formatKDMAValue(value)}</span>
820815
821816
<button class="table-kdma-remove-btn"
822817
onclick="removeKDMAFromRun('${runId}', '${kdmaType}')"
823-
${!canRemoveSpecificKDMA(runId, kdmaType, pinnedRuns, KDMAUtils) ? 'disabled' : ''}
824-
title="${!canRemoveSpecificKDMA(runId, kdmaType, pinnedRuns, KDMAUtils) ? 'No valid experiments exist without this KDMA' : 'Remove KDMA'}">×</button>
818+
${!canRemoveSpecificKDMA(runId, kdmaType, pinnedRuns) ? 'disabled' : ''}
819+
title="${!canRemoveSpecificKDMA(runId, kdmaType, pinnedRuns) ? 'No valid experiments exist without this KDMA' : 'Remove KDMA'}">×</button>
825820
</div>
826821
`;
827822
}

0 commit comments

Comments
 (0)