File size: 7,503 Bytes
25cc038
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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 => `<sub>${match}</sub>`);
            let hollow_sub = mxene.hollow_label === null ? "" : mxene.hollow_label.replace(/[MX]/g, match => `<sub>${match}</sub>`);
            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 === "" ? "&Delta;E<sub>ABC</sub>" : "&Delta;E<sub>ABC HM</sub>";
            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}`
            // <a href="contcars/terminated/searcher_${mxene.n}_${mxene.T_label}/${mxene.stack_label}_${mxene.hollow_label}/CONTCAR_${mxene.name}" download="CONTCAR_${mxene.name}_${mxene.stack_label}_${mxene.hollow_label}">Download CONTCAR</a>

            mxeneDiv.innerHTML = `
                <h2>${mxene_sub_name} ${mxene.stack_label} ${hollow_sub}</h2>

                <p class="mxene-type"> ${mxeneType} </p>

                <div class="flex-container">

                    <p class="outer-box" title="Lattice parameter">
                        <span class="label-box"><i style="font-family: 'Parisienne' ">a</i></span>
                        <span class="result-box">${mxene.a.toFixed(3)} &#8491;</span>
                    </p>
                    <p class="outer-box" title="MXene width">
                        <span class="label-box"><i style="font-family: 'Parisienne' ">d</i>
                        </span><span class="result-box">${mxene.d.toFixed(3)} &#8491;</span>
                    </p>
                    <p class="outer-box" title="PBE0 Bandgap">
                        <span class="label-box">E<sub>g</sub><sup>PBE</sup></span>
                        <span class="result-box">${mxene.Eg_PBE.toFixed(3)} eV</span>
                    </p>
                    <p class="outer-box" title="PBE0 Bandgap">
                        <span class="label-box">E<sub>g</sub><sup>PBE0</sup></span>
                        <span class="result-box">${mxene.Eg_PBE0.toFixed(3)} eV</span>
                    </p>
                    <p class="outer-box" title="${relEnergyTitle}">
                        <span class="label-box">${relEnergyLabel}</span>
                        <span class="result-box">${mxene.e_rel} eV</span>
                    </p>

                </div>

                <br>
                <a href="${download_path}" download="${download_name}">Download CONTCAR</a>
                
                <hr>
            `;
            resultDiv.appendChild(mxeneDiv);
        });
    } else {
        resultDiv.innerHTML = '<p class="not-found">No results found</p>';
    }
}


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";
    }
}