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

Fix bug when switching to refractive index mode #180

Merged
merged 2 commits into from
Feb 3, 2025
Merged
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
94 changes: 52 additions & 42 deletions www/js/src/components/Navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ const Navbar = ( {
setIsMobileMenuOpen(!isMobileMenuOpen);
};

const loadDataset = async (newSurfaces, newFields, newAperture, newWavelengths, newAppModes) => {
// Clear materials first
materialsService.clearSelectedMaterials();

for (const surface of newSurfaces) {
// Add any material key to the materials service's selected materials
if (surface.material) {
await materialsService.addMaterialToSelectedMaterials(surface.material);
}

// Set a default refractive index so that users are not locked out of the RI cell when
// switching to refractive index mode
if (!surface.n) {
surface.n = 1.5;
}
}

setAppModes(newAppModes);

setSurfaces(newSurfaces);
setFields(newFields);
setAperture(newAperture);
setWavelengths(newWavelengths);
};

const showAlert = (message) => {
// Create alert container if it doesn't exist
let alertContainer = document.getElementById('alert-container');
Expand Down Expand Up @@ -194,21 +219,11 @@ const Navbar = ( {
if (jsonData.specs && jsonData.appModes) {
const { surfaces: newSurfaces, fields: newFields, aperture: newAperture, wavelengths: newWavelengths } = jsonData.specs;

// Clear materials first
materialsService.clearSelectedMaterials();
// Use newSurfaces to add materials
for (const surface of newSurfaces) {
if (surface.material) {
await materialsService.addMaterialToSelectedMaterials(surface.material);
}
if (newSurfaces && newFields && newAperture && newWavelengths) {
await loadDataset(newSurfaces, newFields, newAperture, newWavelengths, jsonData.appModes);
} else {
throw new Error("Invalid file format: missing specific specs data");
}

setAppModes(jsonData.appModes);

if (newSurfaces) setSurfaces(newSurfaces);
if (newFields) setFields(newFields);
if (newAperture) setAperture(newAperture);
if (newWavelengths) setWavelengths(newWavelengths);
} else {
throw new Error("Invalid file format: missing specs data and/or appModes");
}
Expand All @@ -231,39 +246,34 @@ const Navbar = ( {
};

// Examples handlers
const handleConvexplanoLens = () => {
materialsService.clearSelectedMaterials();

setSurfaces(cpLensData.surfaces);
setFields(cpLensData.fields);
setAperture(cpLensData.aperture);
setWavelengths(cpLensData.wavelengths);
setAppModes(cpLensData.appModes);
const handleConvexplanoLens = async () => {
await loadDataset(
cpLensData.surfaces,
cpLensData.fields,
cpLensData.aperture,
cpLensData.wavelengths,
cpLensData.appModes
);
};

const handleConvexplanoLensWithMaterials = async () => {
materialsService.clearSelectedMaterials();
for (const surface of cpmLensData.surfaces) {
if (surface.material) {
await materialsService.addMaterialToSelectedMaterials(surface.material);
}
}

setSurfaces(cpmLensData.surfaces);
setFields(cpmLensData.fields);
setAperture(cpmLensData.aperture);
setWavelengths(cpmLensData.wavelengths);
setAppModes(cpmLensData.appModes);
await loadDataset(
cpmLensData.surfaces,
cpmLensData.fields,
cpmLensData.aperture,
cpmLensData.wavelengths,
cpmLensData.appModes
);
};

const handlePetzvalLens = () => {
materialsService.clearSelectedMaterials();

setSurfaces(petzvalLensData.surfaces);
setFields(petzvalLensData.fields);
setAperture(petzvalLensData.aperture);
setWavelengths(petzvalLensData.wavelengths);
setAppModes(petzvalLensData.appModes);
const handlePetzvalLens = async () => {
await loadDataset(
petzvalLensData.surfaces,
petzvalLensData.fields,
petzvalLensData.aperture,
petzvalLensData.wavelengths,
petzvalLensData.appModes
);
};

return (
Expand Down