-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsee_calc.js
3259 lines (2130 loc) · 84.9 KB
/
see_calc.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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const trig_names = "sin cos tan csc sec cot sinh cosh tanh csch sech coth arcsin arccos arctan arccsc arcsec arccot arcsinh arccosh arctanh arccsch arcsech arccoth"
// const operator_names = "sqrt pi alpha theta omega tau sigma"
const operator_names = "sqrt"
var MQ = MathQuill.getInterface(2);
const MQ_line_parameters = {
autoCommands: operator_names,
autoOperatorNames: trig_names+" visual",
}
const MQ_table_parameters = {
autoCommands: operator_names,
autoOperatorNames: trig_names,
}
/*
eqns
eqns with solve
eqns with solve input adjust
basic visual (with volumn equation)
copy visual
*/
const tutorial_stuff = [
{
name: 'Overview',
text: 'This site solve systems of equations. You can use simple systems as building blocks to create more complex systems. These equations can then be connected to visuals to create interactive graphics.',
sheet: [{"name":"","show_box":"","info":"hi","eqns":[{"input":"","show_output":""}],"display":""}],
solve: {"reference":"","step_sizes":{}}
},
{
name: "System of Equations",
text: "Each block contains a system of equations, with its name on top, and one equation per line below.",
sheet: [
{"name":"SomeName","show_box":"","info":"hi",
"eqns":[
{"show_output":"none","input":"y=x+3"},
{"show_output":"none","input":"x\\cdot y=10"}],"display":""}],
solve: {"reference":"","step_sizes":{}}
},
{
name: "Solving Systems",
text: "To solve the system, reference the block's name in the solve line. Always rerun the sheet (<span class='command-box'>Ctrl</span> + <span class='command-box'>Enter</span>) to see the results.",
sheet: [
{"name":"SomeName","show_box":"","info":"hi",
"eqns":[
{"show_output":"none","input":"y=x+3"},
{"show_output":"none","input":"x\\cdot y=10"}],"display":""}],
solve: {"reference":"SomeName","step_sizes":{},"table":{"data":[["x","y"],["2.275","5.275"]],"output_solve_idxs":[[1,0],[1,1]]}}
},
{
name: "Adjusting Values",
text: "If there are too many unknowns to solve the system, values can be substituted into the table and adjusted with the spinner. Press <b>Clear output</b> to change the variables to substitute.",
sheet: [
{"name":"SomeName","show_box":"","info":"hi",
"eqns":[
{"show_output":"none","input":"y=x+3"},
{"show_output":"none","input":"x\\cdot y=z"}],"display":""}],
solve: {"reference":"SomeName","step_sizes":{},"table":{"data":[["x","y","z"],["","","10"]],"output_solve_idxs":[]},"steps":{"sub":[["x\\cdot y=z","x \\cdot y=(10)"]],"back":[{"eqn0":"x\\cdot y=10","sol":"\\frac{10}{y}","solve_var":"x","substitutions":[{"eqn0":"y=x+3","eqn_subbed":"y=\\frac{10}{y}+3"}]},{"eqn0":"y=\\frac{10}{y}+3","sol":"5","solve_var":"y","substitutions":[]}],"forward":[{"eqn":"x=\\frac{10}{5}","sol":"x=2"}]},"previous_back_solution":{"remaining_trees":[],"ordered_sub":[{"solve_var":"y","sol":"5"},{"solve_var":"x","sol":"2"}],"steps":[{"eqn0":"x\\cdot y=10","sol":"\\frac{10}{y}","solve_var":"x","substitutions":[{"eqn0":"y=x+3","eqn_subbed":"y=\\frac{10}{y}+3"}]},{"eqn0":"y=\\frac{10}{y}+3","sol":"5","solve_var":"y","substitutions":[]}]},"solved_table":[["y","x","z"],["","","10"]],"result":[["y=5","x=2"]]}
},
{
name: "Visuals",
text: "To create a visual, type the <b>visual</b> keyword followed by the name of a visual (e.g. 'Box', 'Sphere', 'Cylinder', or 'Arrow').<br><br><span class='command-box'>Ctrl</span> + <span class='command-box'>Click</span> to orbit<br><span class='command-box'>Shift</span> + <span class='command-box'>Click</span> to pan.<br><br>To write π, type <b>\\pi</b> + <span class='command-box'>space</span> <br>(It's the same for all greek letters).",
sheet: [{"name":"SphereVol","show_box":"","info":"hi","eqns":[{"show_output":"none","input":"V=\\frac{4}{3}\\cdotBS__pi\\cdot R^3"},{"show_output":"none","input":"\\operatorname{visual}Sphere","sub_table":{"data":[["r","x_0","y_0","z_0"],["R","0","0","0"]],"output_solve_idxs":[]}}],"display":""}],
solve: {"reference":"SphereVol","step_sizes":{},"table":{"data":[["R","V","BS__pi"],["","10",""]],"output_solve_idxs":[]},"steps":{"sub":[["V=4.188790204786390525271144724684\\cdot R^{3}","(10)=4.188790204786390525271144724684 \\cdot R^{3}"]],"back":[{"eqn0":"10=4.188790204786390525271144724684\\cdot R^{3}","sol":"1.336504617571975916945348217268","solve_var":"R","substitutions":[]}],"forward":[]},"previous_back_solution":{"remaining_trees":["(0)|(0)|(0)|((1.336504617571975916945348217268))|VISUALSphere"],"ordered_sub":[{"solve_var":"R","sol":"1.336504617571975916945348217268"}],"steps":[{"eqn0":"10=4.188790204786390525271144724684\\cdot R^{3}","sol":"1.336504617571975916945348217268","solve_var":"R","substitutions":[]}]},"solved_table":[["V","R"],["10",""]],"result":[["R=1.336504617571975916945348217268"]]}
},
{
name: "Rerencing Blocks",
text: "Blocks can be referenced by blocks below, inserting their equations in. You can substitute variables using the cells of table, and each row creates a copy. Press the arrow to the right of the table to see the inserted equations.<br><br>You can show only the solve spinners by pressing <b>Hide Equations</b> up top.<br><br>Go to the library to see how all of this is used for practical examples.",
sheet: [{"name":"SphereVol","show_box":"","info":"hi","eqns":[{"show_output":"none","input":"V=\\frac{4}{3}\\cdotBS__pi\\cdot R^2"},{"show_output":"none","input":"\\operatorname{visual}Sphere","sub_table":{"data":[["r","x_0","y_0","z_0"],["R","x","0","0"]],"output_solve_idxs":[]}}],"display":""},{"name":"TwoSpheres","show_box":"","eqns":[{"show_output":"none","input":"SphereVol","sub_table":{"data":[["R","V","x"],["R","V_1","0"],["R","V_2","10"]],"output_solve_idxs":[]}},{"show_output":"none","input":"V_OB__totCB__=V_1+V_2"}],"display":""}],
solve: {"reference":"TwoSpheres","step_sizes":{"V_{tot}":"0.1"},"table":{"data":[["R","V_1","V_2","V_OB__totCB__"],["1.197","6","6","12"]],"output_solve_idxs":[[1,0],[1,1],[1,2]]}}
}
]
let tutorial_idx = 0
/*
buttons to go back and forward have to change the element
oh just change the text of course
*/
const tutorial_btns = [...$(".tutorial-button")]
$("#tutorial-back")[0].onclick = ()=>{
if (tutorial_idx===0){return}
tutorial_idx--
update_tutorial()
if (tutorial_idx===0){
tutorial_btns[0].classList.remove("hoverable")}
}
$("#tutorial-forward")[0].onclick = ()=>{
if (tutorial_idx+1===tutorial_stuff.length){return}
tutorial_idx++
update_tutorial()
if (tutorial_idx+1===tutorial_stuff.length){
tutorial_btns[1].classList.remove("hoverable")}
}
function update_tutorial(){
const tutorial_stage = tutorial_stuff[tutorial_idx]
$("#tutorial")[0].innerHTML = tutorial_stage.text
$("#tutorial-name")[0].innerText = tutorial_stage.name
tutorial_btns.forEach(btn => btn.classList.add("hoverable"))
$("#tutorial-stage")[0].innerText = `${tutorial_idx+1}/${tutorial_stuff.length}`
if (tutorial_idx !== 0){
if (tutorial_stage.solve){
GLOBAL_solve_stuff = tutorial_stage.solve
}
const sheet_data =JSON.parse(JSON.stringify(tutorial_stage.sheet))
send_sheet(sheet_data,0,sheet_data.length)
}
}
addEventListener("resize",e => {
const old_range = scene.range
const old_axis = scene.axis
const old_center = scene.center
setUpGS("vis",false)
display_vis(equation_visuals,false)
scene.range = old_range
scene.axis = old_axis
scene.center = old_center
})
var SoEs= [
{
name:"",
info: "hi",
eqns: [
{input: ""},
]
},
]
const equation_visuals = []
var GLOBAL_solve_stuff = null // oh god......
function clear_equation_visuals(){
while (equation_visuals.length > 0){
equation_visuals.pop()
}
}
function send_sheet(sheet,start_idx,end_idx){
// start is inclusive, end is exclusive (1 to 2 means just 1)
if (end_idx<=start_idx){return}
clear_equation_visuals()
const new_SoEs = calc(sheet,start_idx,end_idx)
data2DOM(new_SoEs)
display_vis(equation_visuals.flat(), true)
}
function run_sheet(){
var sheet_data=DOM2data()
// fixed inputs for start and end kind of defeats the whole purpose, but it's so fast now, so does it really matter
send_sheet(sheet_data,0,sheet_data.length)
}
function resetGS(){
var reached_coord_labels = false
scene.objects.forEach(obj=>{
// this code would prevent it from removing the axes:
/*
if (obj.constructor.name==="label"){
reached_coord_labels = true
return
}
if (!reached_coord_labels){return}
*/
// simple_spheres (created by points) have a bizarre issue
// when you set the visibility off immediately after creating, gs crashes
// ony an issue when testing sheets, when reset immediately after generating
// hack solution is to just move it really far away instead
// then when checking range to adjust scale, ignore spheres
if (obj.constructor === simple_sphere){
obj.pos = vec(10**6,0,0)
}else{
obj.visible=false
}
})
}
// calls toggle so ends up being true
let show_blocks = false
data2DOM(SoEs) // performed without calculations
var start_run_idx
var end_run_idx
let scene
setUpGS("vis")
var n_mq_fields = 0
function remove_those_spinners(){
const spinners = [...$(".solve-spinner")]
spinners.forEach(spinner => {
spinner.parentElement.style.display = 'none'
})
}
document.addEventListener('keyup', (e)=>{
$("#save-field-error-msg")[0].innerText = ""
var in_field=document.activeElement
if ($(in_field).parents(".block").length === 1 && $(in_field).parents("#solve-block").length === 0){
remove_those_spinners()
}
if (in_field.id === "save-field"){
document.getElementById("save-field-error-msg").innerText = ""
}
let should_track = true
if (e.code==="Enter"){
if (e.ctrlKey){
// resetGS()
run_sheet()
}else if(in_field.tagName=="TEXTAREA"){
const in_table = $(in_field).parents("td").length === 1
if (!in_table){
add_line(in_field)
}
}else if(in_field.className=="block-name-txt"){
const is_solve_field = in_field.id === "solve-field"
if (!is_solve_field){
add_block(in_field)
}
}
}else if(e.code ==="KeyZ" && e.ctrlKey){
undo()
should_track = false
}else if(e.code === "KeyY" && e.ctrlKey){
redo()
should_track = false
}
if (should_track){
const new_n_mq_fields = $(".mq-root-block").length
// TODO do the same check for mouseup (clicks to add or remove stuff)
if (new_n_mq_fields !== n_mq_fields){
new_track_dom()
}
n_mq_fields = new_n_mq_fields
}
})
const history_stack = []
const future_stack = []
function new_track_dom(){
const mq_fields = get_input_fields()
const latex = mq_fields.map(field => {return MQ(field).latex()})
const clone = $('#calc').clone(true,true);
history_stack.push({clone: clone, latex: latex})
while(future_stack.length){future_stack.pop()}
}
function get_input_fields(){
return [...$(".mq-root-block")]
.map(child => {return child.parentNode})
.filter(field => {return [...field.classList].includes("mq-editable-field")})
}
function undo(){
return
step_DOM_history(history_stack, future_stack)
}
function redo(){
return
step_DOM_history(future_stack, history_stack)
}
function step_DOM_history(source_stack, destination_stack){
if (source_stack.length === 0){return}
const source = source_stack.pop()
const source_clone = source.clone
const source_latex = source.latex
destination_stack.push({clone: source_clone, latex: source_latex})
$("#calc").replaceWith(source_clone)
const past_mq_fields = get_input_fields()
past_mq_fields.forEach((old_field, idx) => {
let new_field
if ([...old_field.classList].includes("line-input")){
new_field = document.createElement('div')
new_field.classList.add("line-input")
MQ.MathField(new_field, MQ_line_parameters)
}else{
new_field = document.createElement("div")
new_field.style.width = old_field.style.width
MQ.MathField(new_field, MQ_table_parameters)
}
MQ(new_field).latex(source_latex[idx])
old_field.replaceWith(new_field, true, true)
})
}
const fb_config = {
apiKey: "AIzaSyBrjMcMVh5Qe4i1wI28Hu6cWtlBLn-1Fpc",
authDomain: "see-calc-8d690.firebaseapp.com",
databaseURL: "https://see-calc-8d690-default-rtdb.firebaseio.com",
projectId: "see-calc-8d690",
storageBucket: "see-calc-8d690.appspot.com",
messagingSenderId: "712428414817",
appId: "1:712428414817:web:13ebddba5db433e5960720",
measurementId: "G-GP5ZDNCTXX"
};
firebase.initializeApp(fb_config);
const database = firebase.database().ref();
const auth = firebase.auth()
function clear_sheet(){
throw "not using this any more"
clear_equation_visuals()
setUpGS("vis")
$("#save-field")[0].value = ""
GLOBAL_solve_stuff = {reference:""}
$(".block").remove()
const main = $("#calc")[0]
main.appendChild(make_block())
main.appendChild(make_solve_block())
window.location.hash = ""
}
//const save_btn = document.getElementById("save-btn")
function save_sheet(){
const url = window.location.hash
const path_list = url.replaceAll("#","").split(".")
const folder_path = path_list.slice(0,path_list.length-1)
const sheet_name = document.getElementById("save-field").value
// const sheet_name_target = all_names.join(".").replaceAll(" ","-")
const is_alphanumeric = /^[a-zA-Z0-9\s]+$/.test(sheet_name)
const error_message_field = $("#save-field-error-msg")[0]
if (sheet_name === ""){
error_message_field.innerText = "Sheet name cannot be blank."
error_message_field.style.color = 'red'
return
}
if (!is_alphanumeric){
error_message_field.innerText = "Only letters and numbers allowed."
error_message_field.style.color = 'red'
return
}
error_message_field.innerText = ""
clear_equation_visuals()
const blocks = JSON.parse(JSON.stringify((DOM2data())))
const solved_blocks = calc(blocks,0,blocks.length)
// just cause of firebase
const sanitized_solved_blocks = replace_errors_with_messages(solved_blocks)
const sanitized_solve_stuff = replace_errors_with_messages(GLOBAL_solve_stuff)
const about_text = $("#about-field")[0].value
const sheet_data = {
name: sheet_name,
blocks: sanitized_solved_blocks,
visuals: equation_visuals,
solve_stuff: sanitized_solve_stuff,
about: about_text
}
let place_to_save
if (CURRENT_USER){
if (!firebase_data.Users[CURRENT_USER]){
firebase_data.Users[CURRENT_USER] = []
}
place_to_save = firebase_data.Users[CURRENT_USER]
}else{
place_to_save = get_folder_content(folder_path.join("/").replaceAll("-"," "),firebase_data.Library)
}
// const place_to_save = get_folder_content(folder_path.join("/"),get_firebase_data(CURRENT_USER))
save_content(place_to_save, "", sheet_data, true)
// delete_content(firebase_data,dir, old_sheet_name)
// save_content(firebase_data, folder_path, sheet_data, true)
// window.location.hash = full_path
// send_to_url()
database.set(firebase_data).then(()=>{
const save_field = $("#save-field-error-msg")[0]
save_field.innerText = "Sheet saved!"
save_field.style.color = 'green'
})
write_url(CURRENT_USER, [folder_path,sheet_name].flat())
}
let firebase_data
function get_firebase_data(owner){
if (owner){
return firebase_data.Users[owner]
}else{
return firebase_data.Library
}
}
database.on("value", update_library,(e)=>{throw e});
function update_library(package = null){
if (package !== null){
const data = package.val()
firebase_data = data //Object.values(data)
send_to_url()
}
const load_btns = [...document.getElementsByClassName("sheet-load-btn")]
load_btns.forEach((btn)=>{btn.remove()})
const library_root = document.getElementById("public-content")
const user_content_root = document.getElementById("user-content")
// const library_data = firebase_data.filter(entry => {return !entry.owner})
// const user_data = firebase_data.filter(entry => {return entry.owner === CURRENT_USER})
const create_library_buttons = (names, container)=>{create_sheet_buttons(names, container, null)}
const create_user_buttons = (names, container)=>{create_sheet_buttons(names, container, CURRENT_USER)}
let user_data
if (!firebase_data.Users || !firebase_data.Users[CURRENT_USER]){
user_data = []
}else{
user_data = firebase_data.Users[CURRENT_USER]
}
const foldable_class_name = ".toggle-div"
const old_toggle_divs = [...$(foldable_class_name)]
const old_folding = old_toggle_divs.map(div => {
return div.style.display
})
replace_UI_tree(user_data, user_content_root, create_user_buttons)
replace_UI_tree(firebase_data.Library, library_root, create_library_buttons)
const new_toggle_divs = [...$(foldable_class_name)]
const new_arrow_buttons = [...$(".library-arrow-button")]
if (old_toggle_divs.length !== new_toggle_divs.length){
return
}
const arrow_text_mapping = {
"none": '▶',
"block": '▼'
}
new_toggle_divs.forEach((div,idx)=>{
const old_display = old_folding[idx]
div.style.display = old_display
new_arrow_buttons[idx].innerText = arrow_text_mapping[old_display]
})
}
test_folder_name = "Tests"
function add_to_test(sheet_names){
sheet_names.forEach(name => {
move_content(firebase_data.Library, "",test_folder_name, name)
})
}
function write_url(owner, path_split){
//URL add folder, need to join with periods
let username_path
if (owner){
username_path = `${owner}|`
}else{
username_path = ""
}
const target = `${username_path}${path_split.join(".").replaceAll(" ","-")}`
window.location.hash = target;
}
function send_to_url(){
const target = window.location.hash.substring(1);
if (!target) {return}
const stuff = target.split("|")
let owner, content_target
if (stuff.length === 1){
owner = null
content_target = stuff[0]
}else if (stuff.length === 2){
owner = stuff[0]
content_target = stuff[1]
}else{
throw "should only have one pipe in the url" // TODO should be a page not found error as well
}
const split_path = content_target.replaceAll("-"," ").split(".")
load_sheet(split_path, owner);
}
window.addEventListener('hashchange', send_to_url);
function create_unknown_page(){
window.location.hash = ""
return
const hash_name = window.location.hash.slice(1)
alert(`${hash_name} is not an existing page, it may have been moved or deleted.`)
return
}
function load_sheet(all_names, owner){
clear_equation_visuals()
const sheet_name = all_names[all_names. length-1]
const path = all_names.slice(0,all_names.length-1).join("/")
let folder_content
try{
folder_content = get_folder_content(path, get_firebase_data(owner))
}catch (e){
if (typeof e === "string"){
create_unknown_page()
return
}else{
throw e
}
}
const possible_sheets = folder_content.filter(sheet => {
const is_sheet = !sheet.children
const is_correct_name = sheet.name === sheet_name
// second check is since old sheets in the library could be undefined instead of null
// one undefined and the other null should also match
// const is_correct_owner = (sheet.owner === owner) || (!sheet.owner && !owner)
return is_sheet && is_correct_name // && is_correct_owner
})
if (possible_sheets.length === 0){
create_unknown_page()
return
}
if (possible_sheets.length > 1){
throw "check whats going on, there should only be one sheet"
}
const sheet_visuals = possible_sheets[0].visuals
GLOBAL_solve_stuff = possible_sheets[0].solve_stuff
const sanitized_sheet_data = possible_sheets[0].blocks
const sheet_data = replace_messages_with_errors(sanitized_sheet_data)
let about_text = possible_sheets[0].about
if (!about_text){about_text = ""}
resetGS()
document.getElementById("save-field").value=sheet_name
if (GLOBAL_solve_stuff.solved_table && !GLOBAL_solve_stuff.result){
GLOBAL_solve_stuff.result = [[]]
}
//! will not produce a visual right now (would have to run compute_sheet first to get the vis equations), then call use_calc_results
data2DOM(sheet_data)
if (sheet_visuals !== undefined){
// checking if undefined just cause of stuff i saved before adding the visuals attribute
// getting dictionary values cause of stupid firebase ):<
fuck_you_firebase_sheet_visuals = Object.values(sheet_visuals)
display_vis(fuck_you_firebase_sheet_visuals.flat())
// clear_equation_visuals()
fuck_you_firebase_sheet_visuals.forEach(eqn => {
equation_visuals.push(eqn)
})
}
// $("#about-field")[0].value = about_text
}
function create_sheet_buttons(all_names, container, owner){
const library_btn_class = "library-load-btn"
const sel_class = "library-load-btn-sel"
const sheet_name = all_names[all_names.length-1]
const load_btn = document.createElement("button")
load_btn.classList.add(library_btn_class)
load_btn.innerText = sheet_name
load_btn.onclick=()=>{
// writing the url will automatically update the sheet
write_url(owner, all_names)
const prev_sel_btns = document.getElementsByClassName(sel_class)
if (prev_sel_btns.length > 1){
throw "only one should be selected"
}
if (prev_sel_btns.length === 1){
const prev_sel_btn = prev_sel_btns[0]
prev_sel_btn.classList.remove(sel_class)
prev_sel_btn.classList.add(library_btn_class)
}
load_btn.classList.remove(library_btn_class)
load_btn.classList.add(sel_class)
}
const delete_btn = document.createElement("button")
delete_btn.classList.add("add-remove-btn")
delete_btn.classList.add("library-delete-btn")
delete_btn.innerText = "X"
const really_delete_btn = document.createElement('button')
really_delete_btn.innerText = `Delete`
const cancel_delete_btn = document.createElement('button')
cancel_delete_btn.innerText = `Cancel`
really_delete_btn.className = 'check-delete-btn'
cancel_delete_btn.style.backgroundColor = 'white'
cancel_delete_btn.style.border = 'none'
cancel_delete_btn.style.fontWeight = 'bold'
cancel_delete_btn.style.cursor = 'pointer'
cancel_delete_btn.style.color = 'green'
// cancel_delete_btn.className = 'check-delete-btn'
// cancel_delete_btn.style.backgroundColor = 'green'
really_delete_btn.style.display = 'none'
cancel_delete_btn.style.display = 'none'
delete_btn.onclick=()=>{
load_btn.style.display = 'none'
delete_btn.style.display = 'none'
really_delete_btn.style.display = ''
cancel_delete_btn.style.display = ''
}
really_delete_btn.onclick=()=>{
if (sheet_name === window.location.hash.replace("#","")){
window.location.hash = ""
}
const all_dir_names = JSON.parse(JSON.stringify(all_names))
all_dir_names.pop()
const path = all_dir_names.join("/")
delete_content(get_firebase_data(owner), path,sheet_name)
database.set(firebase_data)
}
cancel_delete_btn.onclick = ()=>{
load_btn.style.display = ''
delete_btn.style.display = ''
really_delete_btn.style.display = 'none'
cancel_delete_btn.style.display = 'none'
}
if (owner){
container.appendChild(delete_btn)
container.appendChild(cancel_delete_btn)
container.appendChild(really_delete_btn)
}
container.appendChild(load_btn)
};
function package_firebase(sheets){
// just a temporary function to get things set up
const new_sheets = Object.keys(sheets).map(sheet_name => {
const sheet_content = sheets[sheet_name]
return {name: sheet_name, blocks: sheet_content}
})
download(JSON.stringify(new_sheets),"boop.json","text/plain")
}
function download(content, fileName, contentType) {
var a = document.createElement("a");
var file = new Blob([content], {type: contentType});
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
function change_start_idx(idx){
end_run_idx = $(".block").length
if (start_run_idx===undefined){ // initially set
start_run_idx = idx
}else{
start_run_idx = min(start_run_idx,idx)
}
}
function DOM2data(){
var data=[]
var SoE_boxes = document.getElementsByClassName('block')
var name_fields = document.getElementsByClassName('block-name-txt')
var info_blocks = document.getElementsByClassName('info-box')
var SoE_boxes = $(".block")
var name_fields = $('.block-name-txt')
var info_blocks = $('.info-box')
SoE_boxes.each(function(block_i){
var block = $(this)
if (block[0].id === "solve-block"){
return
}
var name_field = name_fields[block_i].value
var info = info_blocks[block_i].innerHTML
data[block_i] = {}
data[block_i].name = name_field
data[block_i].show_box = block.children(".line")[0].style.display
if (info!=="undefined" && info!==""){
data[block_i].info=info
}
data[block_i].eqns = []
var lines = block.find(".line")
lines.each(function(line_i){
var eqn_row = $(this)
data[block_i].display = ""
const input_raw =MQ(eqn_row.find(".line-input")[0]).latex()
var input = add_char_placeholders(input_raw)
if (input===""){return}
var sub_table = eqn_row.find(".sub-table")[0]
var eqn_info = {}
var line_output = $(eqn_row).find(".line-output")[0]
const is_solve_line = input.includes("\\operatorname{solve}")
if (line_output===undefined){
if (is_solve_line){
var show_output = "block"
}else{
var show_output = "none"
}
}else{
var show_output = line_output.style.display
}
eqn_info.show_output = show_output
eqn_info.input=input
// firebase sometimes doesn't like when undefined is a value
const sub_data = get_sub_data(sub_table)
if (sub_data!==undefined){
eqn_info.sub_table = sub_data
}
//eqn_info.sub_table = get_sub_data(sub_table)
data[block_i].eqns.push(eqn_info)
})
// keeps a single blank line
if (data[block_i].eqns.length === 0){
data[block_i].eqns.push({input:"",show_output:""})
}
})
// oh god this is awful
const block_to_solve = $("#solve-field")[0].value
// MQ($("#solve-field")[0]).latex()
const table_for_solving = get_sub_data($("#solve-table")[0])
GLOBAL_solve_stuff = {
reference: block_to_solve,
}
const step_spinners = [...$("#spinner-row").children().children(".spinner-step")]
const step_sizes = {}
step_spinners.forEach(spinner => {
const cell = spinner.parentNode
const row = cell.parentNode
const cell_siblings = [...row.children]
const spinner_idx = cell_siblings.indexOf(cell)
const variables = table_for_solving.data[0]
const spinner_var = remove_char_placeholders(variables[spinner_idx])
step_sizes[spinner_var] = spinner.value
})
GLOBAL_solve_stuff.step_sizes = step_sizes
// so firebase doesn't complain about undefined
if (table_for_solving){
GLOBAL_solve_stuff.table = table_for_solving
}
return data
}
function toggle_visual_buttons(display){
const visual_buttons = $(".visual-toggle")
for (button of visual_buttons){
button.style.display = display