forked from borisruf/carbon-footprint-modeling-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.html
201 lines (165 loc) · 6.37 KB
/
search.html
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<!DOCTYPE html>
<html>
<head>
<title>Emission Scenario Reference Database</title>
<style>
div#container {
max-width: 800px;
margin: auto;
}
form {
padding-bottom: 20px;
}
h1, h2 {
font-family: courier;
}
table td:nth-child(1) {
width: 450px;
font-family: courier;
}
table td:nth-child(2) {
width: 200px;
font-family: courier;
}
table td:nth-child(3) {
font-family: sans-serif !important;
font-weight: bold;
}
input[type="text"],
select,
button {
padding: 10px; /* Adjust padding as needed */
border-radius: 0; /* Use sharp corners */
box-shadow: none; /* Remove shadows */
font-size: 16px; /* Adjust font size as needed */
border: 1px solid #000; /* Replace #000 with desired border color */
-webkit-appearance: none;
}
input[type="text"] {
width: 300px;
}
input[type="text"],
select,
button {
border: 1px solid #000; /* Replace #000 with desired border color */
}
</style>
<script src="js/carbon-footprint-modeling-tool.js"></script>
<script>
async function loadIndexData() {
try {
const response = await fetch('data/index.json');
if (!response.ok) {
throw new Error('Failed to load index data');
}
const indexData = await response.json();
return indexData;
} catch (error) {
console.error('Error loading index data:', error);
return null;
}
}
function buildResultsTable(records, emissionType, scenarioId) {
const table = document.createElement('table');
// Populate table rows with scenario data
records.forEach(record => {
const row = table.insertRow();
const titleCell = row.insertCell();
const emissionValueCell = row.insertCell();
const referenceCell = row.insertCell();
let [value, unit] = bestMassUnit(record.emissions[emissionType]);
emissionValueCell.appendChild(document.createTextNode(value.toFixed(2) + " " + unit + " " + emissionUnitMap[emissionType]));
const url = record.id ? `index.html?id=${record.id}` : record.url
const titleLink = document.createElement('a');
titleLink.setAttribute('href', url);
titleLink.appendChild(document.createTextNode(record.title));
titleCell.appendChild(titleLink);
if (scenarioId) {
const referenceLink = document.createElement('a');
referenceLink.setAttribute('href', `benchmark.html?ids[]=${record.id}&reference_id=${scenarioId}`);
referenceLink.appendChild(document.createTextNode("\u0131\u0049\u0131"));
referenceCell.appendChild(referenceLink);
}
});
return table;
}
function filterData(indexData, searchValueInKg, emissionType, factor, margin) {
const results = indexData.filter(scenario => {
const scenarioValueInKg = scenario.emissions[emissionType];
if (scenarioValueInKg !== undefined) {
const difference = Math.abs(scenarioValueInKg/factor - searchValueInKg);
return (difference < searchValueInKg*margin);
}
return false
});
results.sort((a, b) => {
const diffA = convertValue(Math.abs(a.ValueInKg - searchValueInKg));
const diffB = convertValue(Math.abs(b.ValueInKg - searchValueInKg));
return diffA - diffB;
});
return results
}
function init() {
var urlParams = new URLSearchParams(window.location.search);
var searchValue = urlParams.get('value');
var massUnit = urlParams.get('mass_unit');
var emissionType = urlParams.get('emission_type');
var scenarioId = urlParams.get('scenario_id');
//populate form
document.getElementById('searchInput').value = searchValue;
if (massUnit) document.getElementById('massUnit').value = massUnit;
if (emissionType) document.getElementById('emissionType').value = emissionType;
var searchValueInKg = convertValue(searchValue, massUnit, 'kg');
loadIndexData().then(indexData => {
if (indexData) {
let similarScenarios = filterData(indexData, searchValueInKg, emissionType, 1, 0.2);
document.getElementById('similarScenarios').appendChild(buildResultsTable(similarScenarios, emissionType, scenarioId));
let x10Scenarios = filterData(indexData, searchValueInKg, emissionType, 10, 0.2);
document.getElementById('x10Scenarios').appendChild(buildResultsTable(x10Scenarios, emissionType, scenarioId));
let x100Scenarios = filterData(indexData, searchValueInKg, emissionType, 100, 0.2);
document.getElementById('x100Scenarios').appendChild(buildResultsTable(x100Scenarios, emissionType, scenarioId));
let x01Scenarios = filterData(indexData, searchValueInKg, emissionType, 0.1, 0.2);
document.getElementById('x01Scenarios').appendChild(buildResultsTable(x01Scenarios, emissionType, scenarioId));
}
});
}
window.onload = init;
</script>
</head>
<body>
<div id="container">
<h1>Emission Scenario Reference Database</h1>
<form id="searchForm" onsubmit="">
<input type="text" name="value" id="searchInput" placeholder="Emission value">
<select name="mass_unit" id="massUnit">
<option value="mg">Miligrams</option>
<option value="g">Grams</option>
<option value="kg" selected>Kilograms</option>
<option value="t">Metric Tons</option>
<option value="kt">Kilotons</option>
<option value="mt">Megatons</option>
</select>
<select name="emission_type" id="emissionType">
<option value="co2e" selected>CO2e</option>
<option value="co2">CO2</option>
<option value="ch4">CH4</option>
<option value="n2o">N2O</option>
<option value="hfcs">HFCs</option>
<option value="pfcs">PFCs</option>
<option value="sf6">SF6</option>
<option value="nf33">NF33</option>
</select>
<button type="submit">Search</button>
<button onclick="window.location.href=window.location.href.split('?')[0];return false">Reset</button>
</form>
<h2>x1</h2>
<div id="similarScenarios"></div>
<h2>x10</h2>
<div id="x10Scenarios"></div>
<h2>x100</h2>
<div id="x100Scenarios"></div>
<h2>x0.1</h2>
<div id="x01Scenarios"></div>
</div>
</body>
</html>