-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.html
executable file
·576 lines (540 loc) · 20.3 KB
/
index.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
<!DOCTYPE html>
<html manifest="cache.manifest" lang="en">
<head>
<meta charset="utf-8" />
<title id="title">StrainFinder</title>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="default" />
<meta name="viewport" content="user-scalable=no, width=device-width" />
<meta name="apple-mobile-web-app-title" content="StrainFinder" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<link rel="manifest" href="manifest.webmanifest" />
<link rel="canonical" href="https://maxvalue.github.io/Terpene-Profile-Parser-for-Cannabis-Strains/" />
<meta name="Description" content="Compare terpene profiles of cannabis strains using lab test data. Find out which compounds of your favourite strain affect your body." />
<meta property="og:url" id="ogurl" content="https://maxvalue.github.io/Terpene-Profile-Parser-for-Cannabis-Strains/" />
<meta property="og:title" id="ogtitle" content="StrainFinder" />
<meta property="og:description" content="Compare terpene profiles of cannabis strains using lab test data. Find out which compounds of your favourite strain affect your body." />
<meta property="og:image" content="https://maxvalue.github.io/Terpene-Profile-Parser-for-Cannabis-Strains/img/opengraph-icon-1200x1200.png" />
<link rel="apple-touch-icon" sizes="120x120" href="img/apple-touch-icon-120x120.png" />
<link rel="apple-touch-icon" sizes="152x152" href="img/apple-touch-icon-152x152.png" />
<link rel="apple-touch-icon" sizes="167x167" href="img/apple-touch-icon-167x167.png" />
<link rel="apple-touch-icon" sizes="180x180" href="img/apple-touch-icon-180x180.png" />
<link rel="apple-touch-icon" sizes="1200x1200" href="img/apple-touch-icon.png" />
<link rel="apple-touch-icon" href="img/apple-touch-icon.png" />
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" />
<script id="loader_db" type="text/javascript" src="db.js"></script>
<link href="style.css" rel="stylesheet">
</head>
<body ontouchstart="">
<div id="GUI">
<form id="searchform" action="." onsubmit="return load_sample_data(this);">
<div id="searchwrapper">
<input class="UIKit-UIButton"
value="Search"
type="submit"
>
<div class="UIKit-UISearchBar">
<label for="search">Strain Name:</label>
<input type="search"
name="q"
id="search"
inputmode="search"
placeholder="Search"
autofocus=""
minlength="1"
oninput="delay_search();"
>
</div>
<div class="expert-mode">
<label for="exactmatch">Exact Match?</label>
<input class="UIKit-UISwitch" type="checkbox" name="exact" id="exactmatch" value="1">
<fieldset>
<legend>Sample Types:</legend>
<ul id="sampleTypes" onchange="checkTypeCheckboxes()">
</ul>
</fieldset>
<fieldset>
<legend>Labs:</legend>
<ul id="labs">
</ul>
</fieldset>
</div>
<div>
<label for="expertmode">Expert Mode</label>
<input class="UIKit-UISwitch" type="checkbox" name="expert" id="expertmode" value="1" onchange="toggle_expertMode(this);">
</div>
<input class="UIKit-UIButton"
value="Search"
type="submit"
>
</div>
<output name="resultswrapper" class="searchform-output" for="search exactmatch sampleTypes labs">
<div class="placeholder-noresults">No results found</div>
<div class="placeholder-results">
<div class="expert-mode">
<div class="segmented-control" onchange="update_sortOrder()"
><label
><input type="radio" name="sortorder" value="average" checked=""
><div title="Sort by Average Descending">Average ▼</div
></label
><label
><input type="radio" name="sortorder" value="static"
><div title="Sort by Names">Static</div
></label
><label
><input type="radio" name="sortorder" value="variance"
><div title="Sort by Variance Descending">Variance σ² ▼</div
></label
><label
><input type="radio" name="sortorder" value="stability"
><div title="Sort by Stability Descending">Stability ▼</div
></label
></div>
</div>
<div id="searchSummary"></div>
<table id="terpenes" class="ResultsTable">
<thead>
<tr>
<td>Terpene</td>
<td class="graphcell" title="(Mapped to an 1% maximum)">Graph</td>
<td title="Average Percentage">%</td>
<td title="The square of the standard deviation (σ²)">σ²</td>
<td title="How stable the appearance of the terpene is">Stability</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
<table id="cannabinoids" class="ResultsTable">
<thead>
<tr>
<td>Cannabinoid</td>
<td class="graphcell" title="(Mapped to an 1% maximum)">Graph</td>
<td title="Average Percentage">%</td>
<td title="The square of the standard deviation (σ²)">σ²</td>
<td title="How stable the appearance of the cannabinoid is">Stability</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</output>
</form>
<div id="about">
<a id="infolink" href="https://gitlab.com/cannabis-terpene-parser/Terpene-Profile-Parser-for-Cannabis-Strains#terpene-profile-parser-for-cannabis-strains">Learn more about this project here</a>.</span>
<span id="appversion">v2023-04-29T01:13:00+02:00/001</span>
</div>
</div>
<template id="templateResultsRow">
<tr>
<td class="component-name"></td><td class="graphcell"></td><td></td><td></td><td></td>
</tr>
</template>
</body>
<script type="text/javascript">
function addLabItem(identifier, label) {
let labListItem = document.createElement("li");
let labInput = document.createElement("input");
let labLabel = document.createElement("label");
labInput.type = "checkbox";
labInput.name = "lab";
labInput.id = "lab_" + identifier;
labInput.value = identifier;
labInput.classList.add("UIKit-UISwitch");
labListItem.appendChild(labInput);
labLabel.htmlFor = "lab_" + identifier;
labLabel.innerText = label;
labListItem.appendChild(labLabel);
elem_labList.appendChild(labListItem);
}
function addSampletype(sampleType) {
if (!sampleTypes.includes(sampleType)) {
let li = document.createElement("li");
let input = document.createElement("input");
let label = document.createElement("label");
sampleTypes.push(sampleType);
input.type = "checkbox";
input.name = "type";
input.id = "type_"+sampleType;
input.value = sampleType;
input.classList.add("UIKit-UISwitch");
li.appendChild(input);
label.htmlFor = "type_"+sampleType;
label.innerText = sampleType;
li.appendChild(label);
elem_sampleTypes.appendChild(li);
}
}
function checkTypeCheckboxes() {
let anyChecked = false;
let unprocessedIndex = -1;
elem_form.type.forEach(function(typeElem,typeIndex){
if (typeElem.checked) {
anyChecked = true;
}
if (typeElem.value == "Unprocessed") {
unprocessedIndex = typeIndex;
}
})
if (!anyChecked && unprocessedIndex!=-1) {
elem_form.type[unprocessedIndex].checked = true;
};
}
function checkLabCheckboxes() {
let anyChecked = false;
elem_form.lab.forEach(function(labElem){
if (labElem.checked) {
anyChecked = true;
}
})
if (!anyChecked) {
elem_form.lab.forEach(function(labElem){
labElem.checked = true;
})
};
}
function toggle_expertMode(elem_expertSwitch) {
let elems_expert = document.querySelectorAll(".expert-mode");
elems_expert.forEach(function(elem_expert){
if (elem_expertSwitch.checked) {
elem_expert.classList.add("show");
} else {
elem_expert.classList.remove("show");
}
});
}
function sort_table(tableElem, componentOrder) {
for (let tBody of tableElem.tBodies) {
let rows = Array.from(tBody.rows);
switch (elem_form.sortorder.value) {
case "stability":
rows.sort((a,b)=>parseFloat(b.getAttribute("data-stability")) - parseFloat(a.getAttribute("data-stability")));
break;
case "variance":
rows.sort((a,b)=>parseFloat(b.getAttribute("data-variance")) - parseFloat(a.getAttribute("data-variance")));
break;
case "static":
rows.sort((a,b)=>componentOrder.indexOf(a.getAttribute("data-component")) - componentOrder.indexOf(b.getAttribute("data-component")));
break;
default:
case "average":
rows.sort((a,b)=>parseFloat(b.getAttribute("data-average")) - parseFloat(a.getAttribute("data-average")));
break;
}
tBody.innerHTML = "";
rows.forEach(function(row){
tBody.appendChild(row);
});
}
}
function update_sortOrder() {
var elem_terpenesTable = document.getElementById("terpenes");
sort_table(elem_terpenesTable, terpeneOrder);
var elem_cannabinoidsTable = document.getElementById("cannabinoids");
sort_table(elem_cannabinoidsTable, cannabinoidOrder);
}
function create_ComponentRow(component) {
let row = document.importNode(templateResultsRow.content, true).children[0];
row.setAttribute("data-component", component.id);
row.setAttribute("style", `--component-color: ${component.color}`);
row.children[0].innerText = component.name;
return row;
}
function update_ComponentRow(component, searchResults) {
let compID = component.id;
let average = searchResults[compID].avg;
let stability = searchResults[compID].stability;
let variance = searchResults[compID].variance;
let row = document.querySelector(`tr[data-component='${compID}']`);
row.setAttribute("data-average", average);
row.setAttribute("data-stability", stability);
row.setAttribute("data-variance", variance);
if (average <= 0.01) {
row.classList.add("not-detected");
} else {
row.classList.remove("not-detected");
}
if (searchResults[compID].hidden) {
row.classList.add("hidden");
} else {
row.classList.remove("hidden");
}
// graph
var cell = row.children[1];
let graphStartPoint = (50-((50*average)/maxGraphPercentage));
let graphEndPoint = (50+((50*average)/maxGraphPercentage));
let backgroundImage = `linear-gradient(
to right,
#0000,
#0000 ${graphStartPoint}%,
var(--component-color) ${graphStartPoint}%,
var(--component-color) ${graphEndPoint}%,
#0000 ${graphEndPoint}%
)`;
cell.style.backgroundImage = backgroundImage;
// component amount
var cell = row.children[2];
cell.innerText = FormatAverage.format(average);
////component variance
var cell = row.children[3];
cell.innerText = FormatVariance.format(variance);
////component stability
var cell = row.children[4];
cell.innerText = FormatStability.format(stability);
}
function delay_search() {
window.clearTimeout(search_timeoutID);
search_timeoutID = window.setTimeout(load_sample_data, 100, elem_form);
}
function load_sample_data(elem_form) {
let searchResults = {};
let resultsNum = 0;
let searchstring = elem_form.q.value.toLowerCase();
let newURL = new URL(window.location);
newURL.search = "";
if (elem_form.q.value != "") {
newURL.searchParams.append(elem_form.q.name, elem_form.q.value);
}
if (elem_form.exact.checked) {
newURL.searchParams.append(elem_form.exact.name, elem_form.exact.value);
}
elem_form.type.forEach(function(typeCheckbox){
if (typeCheckbox.checked) {
newURL.searchParams.append(typeCheckbox.name, typeCheckbox.value);
}
})
elem_form.lab.forEach(function(labCheckbox){
if (labCheckbox.checked) {
newURL.searchParams.append(labCheckbox.name, labCheckbox.value);
}
})
window.history.pushState({},"",newURL);
elem_pagetitle.text = "StrainFinder - '" + searchstring + "'";
elem_pageCanonicalURL.href = newURL;
elem_pageogurl.content = newURL;
elem_pageogtitle.content = "StrainFinder: '" + searchstring + "'";
elem_form.lab.forEach(function(labCheckbox){
if (labCheckbox.checked) {
let lab = db.labs[labCheckbox.value];
elem_form.type.forEach(function(typeCheckbox){
if (typeCheckbox.checked && (typeCheckbox.value in lab)) {
lab[typeCheckbox.value].forEach(function(sample){
let matched = false;
if (elem_form.exact.checked) {
if (sample["Sample Name"].toLowerCase() == searchstring) {
matched = true
}
} else {
if (sample["Sample Name"].toLowerCase().includes(searchstring)) {
matched = true
}
}
if (matched) {
resultsNum ++;
terpenes.forEach(function(terpene){
let currentTerpeneValue = parseFloat(sample[terpene.id]) || 0.0;
if (terpene.id in searchResults) {
searchResults[terpene.id].avg += currentTerpeneValue;
if (currentTerpeneValue > terpeneStabilityThreshold) {
searchResults[terpene.id].stability ++;
}
searchResults[terpene.id].values.push(currentTerpeneValue);
} else {
searchResults[terpene.id] = {
"avg": currentTerpeneValue,
"stability": ( currentTerpeneValue>terpeneStabilityThreshold ? 1 : 0 ),
"values": [currentTerpeneValue]
};
}
});
cannabinoids.forEach(function(cannabinoid){
let currentCannabinoidValue = parseFloat(sample[cannabinoid.id]) || 0.0;
if (cannabinoid.id in searchResults) {
searchResults[cannabinoid.id].avg += currentCannabinoidValue;
if (currentCannabinoidValue > cannabinoidStabilityThreshold) {
searchResults[cannabinoid.id].stability ++;
}
searchResults[cannabinoid.id].values.push(currentCannabinoidValue);
} else {
searchResults[cannabinoid.id] = {
"avg": currentCannabinoidValue,
"stability": ( currentCannabinoidValue>cannabinoidStabilityThreshold ? 1 : 0 ),
"values": [currentCannabinoidValue]
};
}
});
}
})
}
})
}
})
if (resultsNum == 0) {
elem_resultswrapper.classList.remove("has-results");
elem_resultswrapper.classList.add("has-noresults");
} else {
elem_searchSummary.innerText = "Search Results for '"+searchstring+"' ("+resultsNum+" Sample"+(resultsNum==1?"":"s")+")";
// Calculate averages
terpenes.forEach(function(terpene){
searchResults[terpene.id].avg /= resultsNum;
})
cannabinoids.forEach(function(cannabinoid){
searchResults[cannabinoid.id].avg /= resultsNum;
})
// Calculate stabilities
terpenes.forEach(function(terpene){
searchResults[terpene.id].stability /= resultsNum;
})
cannabinoids.forEach(function(cannabinoid){
searchResults[cannabinoid.id].stability /= resultsNum;
})
// Calculate variances
terpenes.forEach(function(terpene){
let average = searchResults[terpene.id].avg;
let sumSquaredDelta = 0;
for (let individualValue of searchResults[terpene.id].values) {
sumSquaredDelta += Math.pow(individualValue - average, 2);
}
searchResults[terpene.id].variance = sumSquaredDelta / resultsNum;
})
cannabinoids.forEach(function(cannabinoid){
let average = searchResults[cannabinoid.id].avg;
let sumSquaredDelta = 0;
for (let individualValue of searchResults[cannabinoid.id].values) {
sumSquaredDelta += Math.pow(individualValue - average, 2);
}
searchResults[cannabinoid.id].variance = sumSquaredDelta / resultsNum;
})
// Get terpenes ordered by avg
let terpeneOrderAvg = Array.from(terpeneOrder);
terpeneOrderAvg.sort((a,b) => searchResults[b].avg - searchResults[a].avg);
let visibleTerpenes = 10;
terpeneOrderAvg.forEach(function(terpene_id){
if (visibleTerpenes <= 0) {
searchResults[terpene_id].hidden = true;
}
visibleTerpenes --;
});
// Get cannabinoids ordered by avg
let cannabinoidOrderAvg = Array.from(cannabinoidOrder);
cannabinoidOrderAvg.sort((a,b) => searchResults[b].avg - searchResults[a].avg);
let visibleCannabinoids = 10;
cannabinoidOrderAvg.forEach(function(cannabinoid_id){
if (visibleCannabinoids <= 0) {
searchResults[cannabinoid_id].hidden = true;
}
visibleCannabinoids --;
});
// Update component rows in the result tables
terpenes.forEach(function(terpene){
update_ComponentRow(terpene, searchResults);
})
cannabinoids.forEach(function(cannabinoid,cannabinoidIndex){
update_ComponentRow(cannabinoid, searchResults);
})
// Make table visible
elem_resultswrapper.classList.remove("has-noresults");
elem_resultswrapper.classList.add("has-results");
// Apply current sort order to the result tables
update_sortOrder();
}
return false;
}
function on_load() {
// add labs from container to GUI
for (const labName of Object.keys(db.labs)) {
addLabItem(labName, labName);
// add sample types from those labs to the GUI too
Object.keys(db.labs[labName]).forEach(function(newSampleType){
addSampletype(newSampleType);
})
}
// parse search parameters from URL and fill them into the GUI
let parsedURL = new URL(window.location);
let doSearch = false;
for (let param of parsedURL.searchParams) {
let paramKey = param[0];
let paramValue = param[1];
switch (paramKey) {
case "q":
elem_form["q"].value = paramValue;
doSearch = true;
break;
case "exact":
if (paramValue=="1") {
elem_form["exact"].checked = true;
doSearch = true;
}
break;
case "type":
elem_form["type"].forEach(function(checkbox){
if (checkbox.value == paramValue) {
checkbox.checked = true;
doSearch = true;
}
})
break;
case "lab":
elem_form["lab"].forEach(function(checkbox){
if (checkbox.value == paramValue) {
checkbox.checked = true;
doSearch = true;
}
})
break;
}
}
// sanitize lab checkboxes
checkLabCheckboxes();
// sanitize sample type checkboxes
checkTypeCheckboxes();
// create terpene rows
terpenes.forEach(function(terpene) {
elem_terpeneResultsTable.tBodies[0].appendChild(
create_ComponentRow(terpene)
);
});
// create cannabinoid rows
cannabinoids.forEach(function(cannabinoid) {
elem_cannabinoidResultsTable.tBodies[0].appendChild(
create_ComponentRow(cannabinoid)
);
});
// perform search only if non-default parameters were given
if (doSearch) {
load_sample_data(elem_form);
}
toggle_expertMode(elem_form.expert);
}
// Hi there fellow code enthusiast!
// You can change those values directly below, but please don't touch the others.
var maxGraphPercentage = 1;
var terpeneStabilityThreshold = 0.01;
var cannabinoidStabilityThreshold = 0.01;
var FormatAverage = new Intl.NumberFormat('en',{style:'decimal',minimumFractionDigits:2,maximumFractionDigits:2});
var FormatStability = new Intl.NumberFormat('en',{style:'percent',minimumFractionDigits:0,maximumFractionDigits:0});
var FormatVariance = new Intl.NumberFormat('en',{style:'decimal',minimumFractionDigits:2,maximumFractionDigits:2});
// That's it!
var elem_pageCanonicalURL = document.querySelector("link[rel='canonical']");
var baseURL = elem_pageCanonicalURL.href;
var elem_pagetitle = document.getElementById("title");
var elem_pageogtitle = document.getElementById("ogtitle");
var elem_pageogurl = document.getElementById("ogurl");
var elem_form = document.getElementById("searchform");
var search_timeoutID;
var elem_resultswrapper = elem_form.resultswrapper;
var elem_labList = document.getElementById("labs");
var elem_sampleTypes = document.getElementById("sampleTypes");
var template_resultsTableRow = document.getElementById("templateResultsRow");
var elem_terpeneResultsTable = elem_resultswrapper.querySelector("#terpenes");
var elem_cannabinoidResultsTable = elem_resultswrapper.querySelector("#cannabinoids");
var elem_searchSummary = elem_resultswrapper.querySelector("#searchSummary");
var sampleTypes = [];
var terpenes = db.components.terpenes;
var cannabinoids = db.components.cannabinoids;
var terpeneOrder = terpenes.map((terpene)=>terpene.id);
var cannabinoidOrder = cannabinoids.map((cannabinoid)=>cannabinoid.id);
on_load();
</script>
</html>