-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
798 lines (663 loc) · 25.7 KB
/
main.js
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
//Main user controls file
console.log("BEGIN PROGRAM");
//==========RETREIVE TEXT FILE INFO==========//
//Jquery call to server,js, get files in public
function getFiles(input, loc="files") {
return $.ajax({
type: "GET",
url: "/file?name="+input+"&loc="+loc,
async : false
}).responseText.split("\n");
}
//Upload the element information
let pt = getFiles("periodic_table");
for (let e in pt) {
let add = pt[e].split(",");
chemicaldict[add[1].toString()] = parseFloat(add[0]);
}
console.log(chemicaldict);
//Upload element driver (chemical) information
pt = getFiles("chemical_info");
for (let e in pt) {
if (e == 0) { continue; }
let add = pt[e].split("|");
chemDict[add[0]] = {
state : add[1],
enthalpy : parseFloat(add[2]),
entropy: parseFloat(add[3]),
mp: parseFloat(add[4]),
bp: parseFloat(add[5]),
red_pot: parseFloat(add[6]),
density: parseFloat(add[7]),
ion: parseFloat(add[8]),
name: add[9],
displayInSearch: (add[10] === "true")
};
}
console.log(chemDict);
//Upload reaction information to reactDict
pt = getFiles("reactions");
let idcount = 0;
for (let e in pt) {
if (e == 0) {
continue;
}
let add = pt[e].split("|");
reactDict[add[2]] = {
base: add[0],
name: add[1],
products: add[3],
eq: parseFloat(add[4]),
std: (add[5] == "true"),
id: idcount
};
idcount++;
//Add reverse reaction if eq is not 0
if (parseFloat(add[4]) > 0) {
reactDict[add[3]] = {
base: add[0],
name: add[1],
products: add[2],
eq: parseFloat(add[4]),
std: (add[5] == "true"),
id: idcount
};
idcount++;
}
}
console.log(reactDict);
//For any things that need to be performed onload
window.onload = function () {
}
//=======COSMETICS/GUI=======//
function help(close=false) {
if (!close) {
document.getElementById('not_help').style.display = "none";
document.getElementById('not_help').style.visibility = "hidden";
document.getElementById('help').style.display = "block";
document.getElementById('help').style.visibility = "visible";
}
if (close) {
document.getElementById('help').style.display = "none";
document.getElementById('help').style.visibility = "hidden";
document.getElementById('not_help').style.display = "inline";
document.getElementById('not_help').style.visibility = "visible";
}
}
let mouseOnSearchOut = false;
//Checks if the mouse is still on the searchout div
function searchMouseOver(mouseover = false) {
mouseOnSearchOut = mouseover;
}
//Called from onfocusout event -- Allows the onclick for the searchout box
function searchFocusOut() {
if (!mouseOnSearchOut) {
displaySearch(false);
}
}
//Changes the colour of GUI elements when mouseover
function changeButtonColor(obj, onobj = true, aquamarine = true) {
let element = document.getElementById(obj.id);
let color = "#477862";
if (!onobj && aquamarine) { color = "aquamarine"; }
else if (!onobj && !aquamarine) { color = "white"; }
else if (onobj && !aquamarine) { color = "grey"; }
element.style.backgroundColor = color;
}
//Displays/Removes the search bar div
function displaySearch(infocus){
let searchdiv = document.getElementById('searchout');
if (infocus) {
searchdiv.style.visibility = 'visible';
searchdiv.style.display = 'inherit';
} else {
searchdiv.style.visibility = 'hidden';
searchdiv.style.display = 'none';
}
}
/*
* TYPES OF MOLECULES:
* salt
* ion
* omol
* inmol
* water
* oxygen
* carbonate
* acid
* base
* metal
*/
//Note: Elements will only go as far as Uranium (No. 92) due to radioactivity and inability to properly bond
//=======MAINLINE=======//
//Occurs when main.js begins
//Define mainEq, the output bar
let mainEq = new formula();
let output = document.getElementById("output");
output.innerHTML = "Loading ...";
//Define mainEq, the output bar
const condset = ["temp", "pressure" , "vol", "chem"];
let auxcondset = []; let auxcounter = 0; //The auxillary units set
let formulaOnStage = "";
let oldConditionUnits = [[], []]; //A store for the old units
output.innerHTML = displayReact(mainEq, false);
deleteButton();
//=======DRIVER=======//
let driverText = getFiles("drivercheck", "driver");
driverText.shift();
let driverInst = {};
for (let c in driverText) {
let add = driverText[c].split('|');
driverInst[add[0]] = {
ratio: add[1].split(','),
state: add[2].split(',')
}
}
//The driver module tests the reaction
function driver() {
let faults = [];
//Automatically add chemicals and react
const autoReact = (chems) => {
for (let chem in chems) {
addChemicalToStage({id: chems[chem]});
addChemicalsToReaction();
}
reactButton();
}
//Check if the resulting formula object is as intended
const checkValid = () => {
let valid = true;
const id = mainEq.getReactDictR();
for (let c in mainEq.reactants[0]) {
if (mainEq.reactants[1][c] != parseInt(driverInst[id].ratio[c])) {
valid = false;
}
if (mainEq.reactants[4][c] != driverInst[id].state[c]) {
valid = false;
}
}
for (let c in mainEq.products[0]) {
console.log(driverInst[id].ratio);
c = parseInt(c); //Very weird that JS makes this a string ...
if (mainEq.products[1][c] != parseInt(driverInst[id].ratio[c + mainEq.reactants[0].length])) {
valid = false;
}
if (mainEq.products[4][c] != driverInst[mainEq.getId(true)].state[c + mainEq.reactants[0].length]) {
valid = false;
}
}
return [valid, driverInst[id], reactDict[id], mainEq];
}
//Main subroutine loop
for (let c in reactDict) {
if (!reactDict[c].std && driverInst[c] != undefined) {
autoReact(c.split('+'));
console.log("DRIVER: ", mainEq);
let check = checkValid();
console.log(check);
if (!check[0]) {
faults.push(check[1], check[2], check[3]);
}
deleteButton();
}
}
//Print Results
if (faults.length == 0) {
console.log("DRIVER RESULTS:\nAll good!");
}
else {
console.log("DRIVER RESULTS:");
for (let c in faults) {
console.log("ERROR:"+c);
console.log("inDriverInst:", faults[c][0]);
console.log("inReactDict:", faults[c][1]);
console.log("inReaction:", faults[c][2]);
}
}
}
//=======TESTING STAGE=======//
//In this section, the intrinsic documentation will be incomplete
//driver();
/*
addChemicalToStage({id: "NaCl"});
addChemicalsToReaction();
addChemicalToStage({id: "H2O"});
addChemicalsToReaction();
reactButton();
*/
//=======BUTTON ACTIVATIONS/GUI REACTION DISPLAY=======//
//Displays the amounts/units of the reactants, products and excess in the lower 'auxillary' section of the page
function displayResults(set, code) {
let results = document.getElementById('results_'+code);
let element = document.getElementById('displayelement');
//Define constants added to innerHTML
const selectiontab = '" onchange="ChangeUnits(true);" value="mol"> <option value = "mol" > mol </option> <option value = "Kg" > Kg </option> <option value = "g" > g </option> <option value = "mg" > mg </option> <option value = "µg" > µg </option>';
const captab = ' <option value = "KL" > KL </option> <option value = "L" > L </option> <option value = "mL" > mL </option>';
const Mtab = ' <option value = "M" > M </option>';
const endtab = ' </select> </li>';
results.innerHTML = "<p>"+capitalize(code)+":</p>";
let deviation = [];
//Create a new list element in the results section
for (let c in set[0]) {
let e = element.cloneNode(true);
e.id = "set_"+code+"_"+c;
let primary = '<li onclick="ConditionCheck(true)"> <span id ="chem_t_' + code + "_" + c +
'">' + set[0][c].name + '</span>: <input type = "number" id = "chem_n_' + code + "_" + c +
'" value="' + set[2][c] + '" readonly=true> <select id = "chem_u_' + code + "_" + c +
selectiontab;
console.log(set, c);
//Add capaticty selectors for gasses
if (set[4][c] === "g" || set[0][c].state === "g") {
primary += captab;
}
//Add concentration selectors for aqueous elements
if (set[4][c] === "aq" || set[0][c].state === "aq") {
primary += Mtab;
}
e.innerHTML = primary + endtab;
results.appendChild(e);
deviation.push(document.getElementById("chem_t_" + code + "_" + c).offsetWidth);
}
//Align the text box elements with eachother
let longest = deviation[0];
for (let c in set[0]) {
if (deviation[c] > longest) {
longest = deviation[c];
}
}
for (let c in set[0]) {
document.getElementById("chem_n_" + code + "_" + c).style.position = "relative";
document.getElementById("chem_u_" + code + "_" + c).style.position = "relative";
document.getElementById("chem_n_" + code + "_" + c).style.left = (longest - deviation[c]).toString() + "px";
document.getElementById("chem_u_" + code + "_" + c).style.left = (longest - deviation[c]).toString() + "px";
}
//Alter the products/excess relative position from top
let prod = document.getElementById('results_products');
prod.style.top = (- 50 - (31 * mainEq.reactants[0].length)).toString() + "px";
let addon = 0;
if (mainEq.products[0].length > 0) {addon = -6;}
let excess = document.getElementById('results_excess');
excess.style.top = (addon-85 - (31 * (mainEq.products[0].length + mainEq.reactants[0].length))).toString() + "px";
}
//Activated when the 'React' button is pressed. This causes the reaction of mainEq, along with the display etc.
function reactButton() {
//Add the reactants (Back up method)
for (let c in mainEq.reactants[0]) {
mainEq.reactants[2][c] = parseFloat(document.getElementById("chem_n_reactants_"+c).value);
mainEq.reactants[3][c] = document.getElementById("chem_u_reactants_" + c).value;
}
console.log(mainEq.reactants);
//Remove Previous information and add Conditions
mainEq.clear();
addConditions(mainEq);
//React and display equation
if (!mainEq.react()) {
alert("The reaction could not be calculated. Make sure your inputs are valid or to look up the set of valid reactions in the User Manual");
}
else {
output.innerHTML = displayReact(mainEq, true);
}
//Display auxillary results/calculations
displayResults(mainEq.products, 'products');
displayResults(mainEq.excess, 'excess');
//Append excess/products so that the unit-changing works
for (let c in mainEq.products[0]) {
document.getElementById("chem_n_products_" + c).value = mainEq.products[2][c];
document.getElementById("chem_u_products_" + c).value = mainEq.products[3][c];
auxcondset.push("products_" + c);
}
for (let c in mainEq.excess[0]) {
document.getElementById("chem_n_excess_" + c).value = mainEq.excess[2][c];
document.getElementById("chem_u_excess_" + c).value = mainEq.excess[3][c];
auxcondset.push("excess_" + c);
}
//Update unit stores
ConditionCheck(true);
//Prevent the user from changing the conditions
}
//Clear the reaction, auxillary and addstage
function deleteButton() {
//Clear reaction display
mainEq = new formula();
output.style.color = "grey";
output.innerHTML = "Enter Chemicals -->";
auxcounter = 0;
auxcondset = [];
oldConditionUnits[1] = [];
formulaOnStage = "";
//Update auxillary section
displayResults(mainEq.reactants, 'reactants');
displayResults(mainEq.products, 'products');
displayResults(mainEq.excess, 'excess');
//Remove chemical from stage
let textbox = document.getElementById("chem_n");
let selectbox = document.getElementById("chem_u");
textbox.value = 1;
selectbox.value = "mol";
let stagename = document.getElementById("chemicalonstage");
stagename.innerHTML = ".";
stagename.style.color = "transparent";
formulaOnStage = "";
}
//Displays the reaction onto the output div
function displayReact(equation, displayproducts) {
output.style.color = "black";
let display = "H<sub>3</sub>O<sup>+</sup><sub>(aq)</sub>";
display = "";
const formText = (set) => {
for (let n in set[0]) {
//Formula
let string = set[0][n].formula.split("");
for (let c in string) {
if (parseInt(string[c]).toString() != "NaN") {
string[c] = "<sub>" + string[c] + "</sub>";
}
}
string = string.join("");
//Ion
let tempion = Math.abs(set[0][n].ion);
if (tempion == 1) {
tempion = "";
}
if (set[0][n].ion < 0) {
string += "<sup> -" + tempion + "</sup>";
} else if (set[0][n].ion > 0) {
string += "<sup> +" + tempion + "</sup>";
}
else {
string += " "
}
//State
let state = "";
if (set[4][n] == null) {
state = equation.getState(set[0][n], true);
set[4][n] = state;
} else {
state = set[4][n];
}
string += "<sub>(" + state + ")</sub> ";
//Mole Ratio
let ratio = "";
if (set[1][n] > 1) {
ratio = set[1][n];
}
display += ratio + string + "+ ";
}
}
//Forms the reactants part of the output
formText(equation.reactants);
//Remove the + sign at the end of the display string
display = display.split(""); display.pop(); display.pop(); display = display.join("");
//Display the products -- Requires Unicode charachters
if (displayproducts){
if (!equation.isDynamic) {
display += "→ ";
} else {
display += "⇌ ";
}
formText(equation.products);
display = display.split(""); display.pop(); display.pop(); display = display.join("");
}
return display;
}
//=======CONDITION & UNIT MANAGEMENT=======//
//Checks if the conditions are not negative, stores values in case of onchange event
function ConditionCheck(auxillary = false) {
let element_n = null; let element_u = null;
//For the non-auxillary conditions bar/add stage
if (!auxillary) {
for (let c in condset) {
s = condset[c];
element_n = document.getElementById(s + "_n");
element_u = document.getElementById(s + "_u");
if (parseFloat(element_n.value) <= 0) {
if (element_u.value != "C" || parseFloat(element_n.value) <= -273.15) {
element_n.value = 1;
}
}
if (mainEq.reacted) {
element_n.readOnly = true;
}
else {
element_n.readOnly = false;
}
oldConditionUnits[0][c] = element_u.value;
}
}
//For the auxillary results stage
else {
for (let c in auxcondset) {
s = auxcondset[c];
element_n = document.getElementById("chem_n_" + s);
element_u = document.getElementById("chem_u_" + s);
if (parseFloat(element_n.value) <= 0) {
if (element_u.value != "C" || parseFloat(element_n.value) <= -273.15) {
element_n.value = 1;
}
}
oldConditionUnits[1][c] = element_u.value;
}
}
}
//Activated after the onchange event for all of the unit selections, changes the values based on units
//This function should not operate for the addstage input box
function ChangeUnits(auxillary = false) {
//For the non-auxillary conditions bar/add stage
if (!auxillary) {
for (let c in condset) {
if (document.getElementById(condset[c] + "_u").value !== oldConditionUnits[0][c]) {
if (
((document.getElementById(condset[c] + "_u").value.includes('L') && oldConditionUnits[0][c].includes('L')) ||
(document.getElementById(condset[c] + "_u").value.includes('g') && oldConditionUnits[0][c].includes('g'))) ||
c != 3
) {
document.getElementById(condset[c] + "_n").value =
round(onlyConditionUnits(
[parseFloat(document.getElementById(condset[c] + "_n").value), oldConditionUnits[0][c]],
[1, document.getElementById(condset[c] + "_u").value]
));
}
}
}
}
//For the auxillary results stage
else {
for (let c in auxcondset) {
if (document.getElementById("chem_u_" + auxcondset[c]).value !== oldConditionUnits[1][c]) {
if(!mainEq.reacted) {addConditions(mainEq);}
if (auxcondset[c].includes('reactants')){
document.getElementById("chem_n_" + auxcondset[c]).value = round(mainEq.convertUnits(mainEq.reactants[0][c], parseFloat(document.getElementById("chem_n_" + auxcondset[c]).value), oldConditionUnits[1][c], document.getElementById("chem_u_" + auxcondset[c]).value), 10);
}
else if (auxcondset[c].includes('products')) {
let x = c - mainEq.reactants[0].length;
document.getElementById("chem_n_" + auxcondset[c]).value = round(mainEq.convertUnits(mainEq.products[0][x], parseFloat(document.getElementById("chem_n_" + auxcondset[c]).value), oldConditionUnits[1][c], document.getElementById("chem_u_" + auxcondset[c]).value), 10);
}
else if (auxcondset[c].includes('excess')) {
let x = c - mainEq.reactants[0].length - mainEq.products[0].length;
document.getElementById("chem_n_" + auxcondset[c]).value = round(mainEq.convertUnits(mainEq.excess[0][x], parseFloat(document.getElementById("chem_n_" + auxcondset[c]).value), oldConditionUnits[1][c], document.getElementById("chem_u_" + auxcondset[c]).value), 10);
}
}
}
}
}
//Converts units without the need for a seperate reaction object
function onlyConditionUnits(oldconds, newconds) {
//Parameter: Array [Values, Units]
newconds[0] = oldconds[0];
//Change Units oldconds are in
//Celsius
if (oldconds[1].includes("C")) {
newconds[0] = oldconds[0] + 273.15; //Ignore 273.14999999 ....
}
//Kelvin
else if (oldconds[1] === "K") {
newconds[0] = oldconds[0] - 273.15;
}
//Pascals
if (oldconds[1].includes("Pa")) {
newconds[0] = oldconds[0];
}
//Atmospheres
else if (oldconds[1].includes("atm")) {
newconds[0] = oldconds[0] * 101325;
}
//Mass/Capacity
//Nothing, since the only conversions are from mg -> g -> Kg
//Convert to basic units
if (oldconds[1].includes("m") && !oldconds[1].includes("atm")) {
newconds[0] *= 1 / 1000;
} else if (oldconds[1].includes("K") && oldconds[1].length > 1) {
newconds[0] *= 1000;
} else if (oldconds[1].includes("µ") && oldconds[1].length > 1) {
newconds[0] *= 1/ 1000000;
}
//Convert Pressures/Volumes into new units
if (newconds[1].includes("m") && !newconds[1].includes("atm")) {
newconds[0] *= 1000;
} else if (newconds[1].includes("K") && newconds[1].length > 1) {
newconds[0] *= (1 / 1000);
} else if (newconds[1].includes("µ") && oldconds[1].length > 1) {
newconds[0] *= 1000000;
}
if (newconds[1].includes("atm")) {
newconds[0] *= (1 / 101325);
}
return newconds[0];
}
//=======ADD GUI DATA TO REACTION=======//
//Adds all of the conditions to the reaction
function addConditions(equation) {
equation.conditions[1] = ["C", "KPa", "L"]; //oldConditionUnits[0]; Default
for (let c in condset) {
equation.conditions[0][c] = parseFloat(document.getElementById(condset[c] + "_n").value);
equation.conditions[1][c] = document.getElementById(condset[c] + "_u").value
if (c >= 2) { break; }
}
}
//Adds the chemicals in the add stage to the reaction bar/auxillary
function addChemicalsToReaction() {
//Prevent Duplicates/Null inputs
let sameflag = false;
for (let c in mainEq.reactants[0]) {
if (mainEq.reactants[0][c].formula === formulaOnStage) {
sameflag = true;
}
}
//Output alerts to notify user
if (formulaOnStage === "") {
alert("You need to search and add a chemical to the stage.");
return null;
}
if (sameflag) {
alert("You can't double up on chemicals. Select a different chemical.");
return null;
}
if (mainEq.reacted) {
alert("You need to clear the staged reaction first.");
return null;
}
if (mainEq.reactants[0].length >= 2) {
alert("There is an upper limit of two reactants.");
return null;
}
//Create Chemical Instance
let addition = new chemical(formulaOnStage, "none", "none");
addition.name = addition.getDriver("name");
addition.type = addition.getDriver("type");
addition.ion = addition.getDriver("ion");
addition.state = addition.getDriver("state");
//Input into reation object
addConditions(mainEq);
mainEq.reactants[0][auxcounter] = addition;
mainEq.reactants[1][auxcounter] = 1;
mainEq.reactants[2][auxcounter] =
mainEq.convertUnits(addition, parseFloat(document.getElementById("chem_n").value), document.getElementById("chem_u").value, "mol");
mainEq.reactants[3][auxcounter] = "mol";
mainEq.reactants[4][auxcounter] = mainEq.getState(addition, true);
//AuxillaryCondSet
auxcondset.push("reactants_" + auxcounter);
auxcounter++;
//Update displayReact/output
displayResults(mainEq.reactants, 'reactants');
output.innerHTML = displayReact(mainEq, false);
ConditionCheck(true);
//Remove chemical from stage
let textbox = document.getElementById("chem_n");
let selectbox = document.getElementById("chem_u");
textbox.value = 1;
selectbox.value = "mol";
let stagename = document.getElementById("chemicalonstage");
stagename.innerHTML = ".";
stagename.style.color = "transparent";
formulaOnStage = "";
}
//=======SEARCH BAR=======//
//Displays the search bar results
function displaySearchResults() {
//Algoritim to get most relevant to the search query
const searchAlgoritim = (string) => {
let name = [];
let formula = [];
let search = [];
for (let c in chemDict) {
//Get the likeness rank of both formula and name
search.push([rankString(string, c) + rankString(string, chemDict[c].name), c]);
}
//Sort the search array with highest score first
bubbleSort(search, 0);
search.reverse();
//Add search to name and formula
for (let c in search) {
if (
!(formula.includes(search[c][1]) || name.includes(chemDict[search[c][1]].name)) &&
search[c][0] != 0 && chemDict[search[c][1]].displayInSearch && c < 10
) {
name.push(chemDict[search[c][1]].name);
formula.push(search[c][1]);
}
}
return [name, formula]
}
//Get the list of suggestions
const query = document.getElementById("search").value;
let displayset = searchAlgoritim(query);
//Clear Search element
let searchout = document.getElementById("searchout");
searchout.innerHTML = ""
//Display onto search element
let deviation = []; let chemdeviation = [];
for (let c in displayset[0]) {
searchout.innerHTML += '<li id="' + displayset[1][c] +
'" onclick="addChemicalToStage(this);" onmouseleave="changeButtonColor(this, false, false)" onmouseover="changeButtonColor(this, true, false)">' +
'<span id="search_name_' + c + '">'+ displayset[0][c] + ' </span> <i id="search_formula_' + c + '">' + displayset[1][c] + '</i> </li>';
deviation.push(document.getElementById("search_name_"+c).offsetWidth);
chemdeviation.push(document.getElementById("search_formula_" + c).offsetWidth);
}
//Align the formula i-tag elements with eachother
let longest = deviation[0]; let longestchem = chemdeviation[0];
for (let c in displayset[0]) {
if (deviation[c] > longest) {
longest = deviation[c];
}
if (chemdeviation[c] > longestchem) {
longestchem = chemdeviation[c];
}
}
//Adjusts the size of the search_out div to fit to text width
for (let c in displayset[0]) {
document.getElementById("search_formula_" + c).style.position = "relative";
document.getElementById("search_formula_" + c).style.left = (longest - deviation[c] + 30).toString() + "px";
searchout.style.width = (longest + longestchem + 40).toString() + "px";
}
}
//Adds a chemical selected from the search bar to the stage
function addChemicalToStage(element) {
console.log("Added!");
//Get rid of search bar
displaySearch(false);
//Add chemical to addStage
let stagename = document.getElementById("chemicalonstage");
stagename.innerHTML = "Current Chemical: " + element.id;
stagename.style.color = "black";
formulaOnStage = element.id;
}