let database; // Load JSON data fetch('database.json') .then(response => response.json()) .then(data => { database = data; populateFilters(database); }) .catch(error => console.error('Error loading JSON:', error)); function searchMXene() { let query = document.getElementById('searchInput').value.toLowerCase(); let matchingMxenes; if (query) { // Search by name if query is present query = query.trim() matchingMxenes = database.filter(m => m.full_name.toLowerCase() === query || m.name.toLowerCase() === query ); } else { // Otherwise search by filters const filter_n = document.getElementById('filter_n').value; const filter_M = document.getElementById('filter_M').value; const filter_X = document.getElementById('filter_X').value; const filter_T = document.getElementById('filter_T').value; const filter_stack = document.getElementById('filter_stack').value; const filter_hollow = document.getElementById('filter_hollow').value; matchingMxenes = database.filter(m => { const nMatch = !filter_n || m.n == filter_n; const MMatch = !filter_M || m.M_label === filter_M; const XMatch = !filter_X || m.X_label === filter_X; const TMatch = !filter_T || (filter_T === 'None' ? m.T_label === null : m.T_label === filter_T); const stackMatch = !filter_stack || m.stack_label === filter_stack; const hollowMatch = !filter_hollow || (filter_hollow === 'None' ? m.hollow_label === null : m.hollow_label === filter_hollow); return nMatch && MMatch && XMatch && TMatch && stackMatch && hollowMatch; }); } displayResults(matchingMxenes); } function displayResults(matchingMxenes) { const resultDiv = document.getElementById('result'); resultDiv.innerHTML = ''; if (matchingMxenes.length > 0) { // Display all matching MXenes matchingMxenes.forEach(mxene => { const mxeneDiv = document.createElement('div'); let mxeneType = mxene.IsGap === 0 ? "Metallic" : "Semiconductor"; let mxene_sub_name = mxene.name.replace(/\d+/g, match => `${match}`); let hollow_sub = mxene.hollow_label === null ? "" : mxene.hollow_label.replace(/[MX]/g, match => `${match}`); let relEnergyTitle = hollow_sub === "" ? "Relative Energy with respect to the ABC stacking" : "Relative Energy with respect to the ABC HM structure"; let relEnergyLabel = hollow_sub === "" ? "ΔEABC" : "ΔEABC HM"; let download_path = mxene.hollow_label === null ? `contcars/pristine/searcher_${mxene.n}_p/${mxene.stack_label}/CONTCAR_${mxene.name}` : `contcars/terminated/searcher_${mxene.n}_${mxene.T_label}/${mxene.stack_label}_${mxene.hollow_label}/CONTCAR_${mxene.name}` let download_name = mxene.hollow_label === null ? `CONTCAR_${mxene.name}_${mxene.stack_label}` : `CONTCAR_${mxene.name}_${mxene.stack_label}_${mxene.hollow_label}` // Download CONTCAR mxeneDiv.innerHTML = `

${mxene_sub_name} ${mxene.stack_label} ${hollow_sub}

${mxeneType}

a ${mxene.a.toFixed(3)} Å

d ${mxene.d.toFixed(3)} Å

EgPBE ${mxene.Eg_PBE.toFixed(3)} eV

EgPBE0 ${mxene.Eg_PBE0.toFixed(3)} eV

${relEnergyLabel} ${mxene.e_rel} eV


Download CONTCAR
`; resultDiv.appendChild(mxeneDiv); }); } else { resultDiv.innerHTML = '

No results found

'; } } function searchOnEnter(event) { if (event.key === 'Enter') { searchMXene(); } } function populateFilters(data) { // const uniqueValues = (key) => [...new Set(data.map(item => item[key]).filter(Boolean))].sort(); const uniqueValues = (key) => { const values = data.map(item => item[key] === null ? 'None' : item[key]); return [...new Set(values)].sort(); }; const addOptions = (selectId, values) => { const select = document.getElementById(selectId); values.forEach(value => { const option = document.createElement('option'); option.value = value; option.textContent = value; select.appendChild(option); }); }; addOptions('filter_n', uniqueValues('n')); addOptions('filter_M', uniqueValues('M_label')); addOptions('filter_X', uniqueValues('X_label')); addOptions('filter_T', uniqueValues('T_label')); addOptions('filter_stack', uniqueValues('stack_label')); addOptions('filter_hollow', uniqueValues('hollow_label')); } function toggleFilters() { const filterContainer = document.getElementById("filterContainer"); const icon = document.getElementById("filterIcon"); const searchInput = document.getElementById('searchInput'); // Clear the input box when the filter icon is pressed searchInput.value = ''; // Toggle the visibility of the filter options const isVisible = filterContainer.style.display === "flex"; // Check if it's already visible filterContainer.style.display = isVisible ? "none" : "flex"; // Toggle between flex and none // Toggle the icon based on the visibility of the filter options icon.classList.remove("fa-filter", "fa-filter-circle-xmark"); icon.classList.add(isVisible ? "fa-filter" : "fa-filter-circle-xmark"); // Change the placeholder text based on filter visibility if (isVisible) { // If filters are hidden, revert the placeholder to the original searchInput.placeholder = "Enter MXene (e.g. Zr2CO2 or Zr2CO2_ABC_HM)"; } else { // If filters are visible, change the placeholder text searchInput.placeholder = "Filter MXenes by composition and structure"; } }