-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1439 lines (1386 loc) · 51.4 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
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
<!doctype html>
<html>
<head>
<title>
Website Builder
</title>
<base target="_blank">
<script>
var Standards = {
general: {
options: { console: "recorded" }
}
};
</script>
<script src="https://epicenterprograms.github.io/standards/behavior/general.js"></script>
<!--
<script src="file:///C:/Users/rtben/Documents/GitHub/standards/behavior/general.js"></script>
-->
<script src="https://www.gstatic.com/firebasejs/4.6.1/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.6.1/firebase-firestore.js"></script>
<script src="https://epicenterprograms.github.io/standards/behavior/firebaseinit.js"></script>
<script src="https://epicenterprograms.github.io/standards/behavior/storage.js"></script>
<script src="https://epicenterprograms.github.io/standards/behavior/game.js"></script>
<script src="https://epicenterprograms.github.io/standards/behavior/presentation.js"></script>
<!-- The extra script imports are in case the website being investigated uses them. -->
<script>
var S = Standards.general;
var M = Standards.storage;
M.session.defaultLocation = "/websitebuilder/";
M.local.defaultLocation = "/websitebuilder/";
M.server.defaultLocation = "/websitebuilder/";
var html = "";
var highlightTimeout;
var projectName = "Default";
var projectLanguage = "HTML";
window.addEventListener("load", function () {
// gives the top-nav buttons functionality
S.getTag("nav")[0].getElementsByTagName("button")[0].addEventListener("click", function () {
this.style.background = "lightsteelblue";
S.getTag("nav")[0].getElementsByTagName("button")[1].style.background = "darkblue";
S.getTag("nav")[0].getElementsByTagName("button")[2].style.background = "darkblue";
S.getId("navigationBuilder").style.display = "none";
S.getId("codeEditor").style.display = "none";
S.getId("websiteBuilder").style.display = "block";
M.local.store("last viewed", "Websites");
});
S.getTag("nav")[0].getElementsByTagName("button")[1].addEventListener("click", function () {
this.style.background = "lightsteelblue";
S.getTag("nav")[0].getElementsByTagName("button")[0].style.background = "darkblue";
S.getTag("nav")[0].getElementsByTagName("button")[2].style.background = "darkblue";
S.getId("websiteBuilder").style.display = "none";
S.getId("codeEditor").style.display = "none";
S.getId("navigationBuilder").style.display = "block";
M.local.store("last viewed", "Navigation");
});
S.getTag("nav")[0].getElementsByTagName("button")[2].addEventListener("click", function () {
this.style.background = "lightsteelblue";
S.getTag("nav")[0].getElementsByTagName("button")[0].style.background = "darkblue";
S.getTag("nav")[0].getElementsByTagName("button")[1].style.background = "darkblue";
S.getId("websiteBuilder").style.display = "none";
S.getId("navigationBuilder").style.display = "none";
S.getId("codeEditor").style.display = "block";
M.local.store("last viewed", "Editor");
});
// sets the screen to be displayed
if (window.location.href.includes("?")) { // if there's a URL extension specifying a screen
let section = window.location.href.slice(window.location.href.indexOf("?s=") + 3).toLowerCase();
if (["websites", "navigation", "editor"].includes(section)) { // if a valid section was provided
section = section[0].toUpperCase() + section.slice(1);
S.getTag("nav")[0].getElementsByTagName("button")[["Websites", "Navigation", "Editor"].indexOf(section)].click();
M.local.store("last viewed", section);
} else {
M.local.store("last viewed", "Websites");
}
} else {
if (S.getType(M.local.recall("last viewed")) == "Error") { // if there isn't information on the section viewed last time
M.local.store("last viewed", "Websites");
} else {
S.getTag("nav")[0].getElementsByTagName("button")[["Websites", "Navigation", "Editor"].indexOf(M.local.recall("last viewed"))].click();
}
}
// saves the locally stored information into the session storage
S.forEach(M.local.list(), function (key) {
if (key != "last viewed") {
M.session.store(key, M.local.recall(key));
}
});
});
function highlightHTML(code, classes) {
let hlHTML = code;
if (classes && classes.includes("comment")) {
hlHTML = hlHTML.replace(/^[^]*$/g, "<span class='highlighting comment'>$&</span>"); // colors comments
} else {
hlHTML = hlHTML.replace(/(\<(?!!--)\/?)([^ \n\r&]+)(.*?)(\>)/g,
"<span class='highlighting outer-tag'>$1</span><span class='highlighting tag-name'>$2</span><span class='highlighting tag-extras'>$3</span><span class='highlighting outer-tag'>$4</span>"); // colors the outer and inner parts of the HTML tags
hlHTML = hlHTML.replace(/\&[^;& \n\r]+?;/g, "<span class='highlighting HTML-char'>$&</span>"); // colors HTML escape sequences
hlHTML = hlHTML.replace(/\<!--[^]*?--\>/g, "<span class='highlighting comment'>$&</span>"); // colors HTML comments
}
return hlHTML;
}
function highlightJavaScript(script, classes) {
if (classes && classes.includes("comment")) {
script = script.replace(/^[^]*$/g, "<span class='highlighting comment'>$&</span>"); // colors comments
} else {
script = script.replace(/(\W)(var|let|const|function\*?|return|if|else|switch|case|default|break|continue|while|do|for|in|of|delete|try|catch|finally|new|yield\*?|typeof|instanceof|throw|async|await|class)(?= |\(|\{|:|;)/g,
"$1<span class='highlighting keyword'>$2</span>"); // colors JavaScript keywords
script = script.replace(/(\W)(window|document|this|Math|Number|Object|JSON|String|Array|Promise|Event|Date|Error|TypeError|ReferenceError|alert|prompt|console|performance|Storage|sessionStorage|localStorage|setTimeout|setInterval|clearTimeout|clearInterval|URL|FileReader|XMLHttpRequest|encodeURIComponent|decodeURIComponent|dispatchEvent|arguments|opener|self)(?= |\.|\()/g,
"$1<span class='highlighting built-in'>$2</span>"); // colors built-in JavaScript objects
script = script.replace(/(\W)(-?\d+\.?\d*|-?\.\d+|true|false|NaN|Infinity|null|undefined)(?=\W)/g,
"$1<span class='highlighting value'>$2</span>"); // colors some values
script = script.replace(/(<span.+?>)|("(?:(?:<\/?span.+?>)|[^<>\n\r])*?"|'(?:(?:<\/?span.+?>)|[^<>\n\r])*?')/g, function (match, tag, str) { // colors strings
if (match == tag) { // prevents putting spans within spans
return tag;
} else if (match == str) {
return "<span class='highlighting quote'>" + str + "</span>";
}
});
script = script.replace(/(^|[^/\\])(\/\*[^]*?\*\/|\/\/.*)/g, "$1<span class='highlighting comment'>$2</span>"); // colors comments
}
return script;
}
function highlightCSS(hlHTML, classes) {
if (classes && classes.includes("comment")) {
hlHTML = hlHTML.replace(/^[^]*$/g, "<span class='highlighting comment'>$&</span>"); // colors comments
} else {
hlHTML = hlHTML.replace(/.+(?=:)/g, "<span class='highlighting CSS-property'>$&</span>"); // colors CSS properties
hlHTML = hlHTML.replace(/.+(?= ?\{)/g, function (match) {
if (match.includes("@")) { // "@" won't be at the beginning
return "<span class='highlighting CSS-at-rule'>" + match + "</span>"; // colors nesting CSS at-rules
} else {
return "<span class='highlighting CSS-selector'>" + match + "</span>"; // colors CSS selectors
}
});
hlHTML = hlHTML.replace(/@.+;/g, "<span class='highlighting CSS-at-rule'>$&</span>"); // colors single line CSS at-rules
hlHTML = hlHTML.replace(/\/\*[^]*?\*\//g, "<span class='highlighting comment'>$&</span>"); // colors comments
}
return hlHTML;
}
function highlightWebsite(hlHTML, classes) {
hlHTML = hlHTML.replace(/\<script\>[^]*?\<\/script\>|\<style\>[^]*?\<\/style\>|\<[^]+?\>|\&[^;& \n]+?;/g, function (match) {
if (match.slice(0, 10) == "<script") {
return highlightJavaScript(match);
} else if (match.slice(0, 9) == "<style") {
return highlightCSS(match);
} else {
return highlightHTML(match);
}
});
hlHTML = hlHTML.replace(/\<\/?(?:script|style)[^+\n\r]*?\>/g, function (match) { // colors the <script> and <style> tags
return highlightHTML(match);
});
return hlHTML;
}
function highlightPython(script, classes) {
if (classes && classes.includes("comment")) {
script = script.replace(/^[^]*$/g, "<span class='highlighting comment'>$&</span>"); // colors comments
} else {
script = script.replace(/(\W)(from|import|as|def|return|if|else|elif|and|or|not|break|pass|while|do|for|in|del|class|try|except|finally|global|raise|lambda)(?= |\(|\{|:|\n|$)/g,
"$1<span class='highlighting keyword'>$2</span>"); // colors Python keywords
script = script.replace(/(\W)(len|int|float|str|range|open|print|hex|round|any|type|eval|Exception|__init__)(?= |\.|\(|\)|\n|$)/g,
"$1<span class='highlighting built-in'>$2</span>"); // colors built-in Python objects
script = script.replace(/(\W)(-?\d+\.?\d*|-?\.\d+|True|False)(?=\W|\n|$)/g,
"$1<span class='highlighting value'>$2</span>"); // colors some values
script = script.replace(/'''[^]*?'''|"""[^]*?"""|#.*/g, "<span class='highlighting comment'>$&</span>"); // colors comments
script = script.replace(/(<span.+?>)|("(?:(?:<\/?span.+?>)|[^<>\n\r])*?"|'(?:(?:<\/?span.+?>)|[^<>\n\r])*?')/g, function (match, tag, str) { // colors strings //// needs to account for comments
if (match == tag) { // prevents putting spans within spans
return tag;
} else if (match == str) {
return "<span class='highlighting quote'>" + str + "</span>";
}
});
}
return script;
}
function highlightGeneric(script, classes) {
if (classes && classes.includes("comment")) {
script = script.replace(/^[^]*$/g, "<span class='highlighting comment'>$&</span>"); // colors comments
} else {
script = script.replace(/(\W)(return|pass|if|else|switch|case|default|break|continue|while|do|for|in|of|try|catch|except|finally|new|class|import|include|use|using)(?= |\(|\{|:|;|\n|$)/gi,
"$1<span class='highlighting keyword'>$2</span>"); // colors generic keywords
script = script.replace(/(\W)(function|func|number|object|json|string|str|char|integer|int|double|float|boolean|bool|byte|short|long|const|void|signed|unsigned|array|promise|event|error|typeerror|referenceerror|self|sizeof)(?= |\.|\(|\n|$)/gi,
"$1<span class='highlighting built-in'>$2</span>"); // colors generic objects
script = script.replace(/(\W)(-?\d+\.?\d*(?:e-?\d+)?|-?\.\d+(?:e-?\d+)?|true|false|NaN|infinity|infin|on|off)(?=\W|\n|$)/gi,
"$1<span class='highlighting value'>$2</span>"); // colors some values
script = script.replace(/(<span.+?>)|("(?:(?:<\/?span.+?>)|[^<>\n\r])*?"|'(?:(?:<\/?span.+?>)|[^<>\n\r])*?')/g, function (match, tag, str) { // colors strings
if (match == tag) { // prevents putting spans within spans
return tag;
} else if (match == str) {
return "<span class='highlighting quote'>" + str + "</span>";
}
});
script = script.replace(/#\w+|#<.+?>\w+?<\/.+?>/g, "<span class='highlighting directive'>$&</span>"); // colors directives
script = script.replace(/(^|[^/\\])(\/\*[^]*?\*\/|\/\/.*|#\s.*)/g, "$1<span class='highlighting comment'>$2</span>"); // colors comments
}
return script;
}
function highlightLine(line) {
if (line.className.includes("HTML")) {
return highlightHTML(line.textContent, line.className);
} else if (line.className.includes("JavaScript")) {
return highlightJavaScript(line.textContent, line.className);
} else if (line.className.includes("CSS")) {
return highlightCSS(line.textContent, line.className);
} else if (line.className.includes("Python")) {
return highlightPython(line.textContent, line.className);
} else if (line.className.includes("General")) {
return highlightGeneral(line.textContent, line.className);
} else {
//// error
}
}
function updateLineNumbers() {
let selectedLines = [];
S.forEach(S.getClass("line-number"), function (num) {
if (num.className.includes("selected")) {
selectedLines.push(Number(num.textContent));
}
});
S.getId("lineNumbers").innerHTML = "";
let newLines = "";
S.forEach(S.getId("codeDisplay").children, function (_, index) {
if (selectedLines.includes(index + 1)) {
newLines += "<section class='line-number selected'>" + (index + 1) + "</section>";
} else {
newLines += "<section class='line-number'>" + (index + 1) + "</section>";
}
});
S.getId("lineNumbers").innerHTML = newLines;
// allows highlighting line numbers
S.forEach(S.getClass("line-number"), function (num) {
num.addEventListener("click", function () {
if (this.className.includes("selected")) {
this.className = "line-number";
} else {
this.className += " selected";
}
});
});
}
function updateCode(updateLines) {
S.getId("codeHighlighter").textContent = S.getId("codeDisplay").textContent;
let hlHTML = S.getId("codeHighlighter").innerHTML; // highlighted HTML (doesn't make the page repeatedly rerender)
clearTimeout(highlightTimeout);
highlightTimeout = setTimeout(function () { // prevents performance issues from needing to refresh so much so often
if (S.getId("highlightOption").checked) {
switch (projectLanguage) {
case "HTML":
hlHTML = highlightWebsite(hlHTML);
break;
case "CSS":
hlHTML = highlightCSS(hlHTML);
break;
case "JavaScript":
hlHTML = highlightJavaScript(hlHTML);
break;
case "Python":
hlHTML = highlightPython(hlHTML);
break;
default:
hlHTML = highlightGeneric(hlHTML);
}
}
S.getId("codeHighlighter").innerHTML = hlHTML;
if (updateLines) {
if (S.getId("codeDisplay").textContent != "") {
S.getId("preview").disabled = false;
}
let selectedLines = [];
S.forEach(S.getClass("line-number"), function (num) {
if (num.className.includes("selected")) {
selectedLines.push(Number(num.textContent));
}
});
S.getId("lineNumbers").innerHTML = "";
let newLines = "";
S.forEach(S.getId("codeDisplay").textContent.split("\n").length, function (_, index) {
if (selectedLines.includes(index + 1)) {
newLines += "<section class='line-number selected'>" + (index + 1) + "</section>";
} else {
newLines += "<section class='line-number'>" + (index + 1) + "</section>";
}
});
S.getId("lineNumbers").innerHTML = newLines;
// allows highlighting line numbers
S.forEach(S.getClass("line-number"), function (num) {
num.addEventListener("click", function () {
if (this.className.includes("selected")) {
this.className = "line-number";
} else {
this.className += " selected";
}
});
});
}
}, 200);
}
function divideLineBreaks(codeElement) {
/**
takes an HTML element containing code with line breaks and turns it into one <section> for each line
*/
// makes sure the caret position isn't lost if inside the code element
let caretPosition = null;
let selection = window.getSelection();
selection.collapseToEnd();
let selectedElement; ////
let selectedSection; //// clean up the stuff that uses this
if (selection.focusNode && codeElement.contains(selection.focusNode)) { // if the caret exists in the element being divided
selectedElement = selection.focusNode.parentElement;
selectedSection = selectedElement;
while (selectedSection.tagName != "SECTION" && selectedSection.tagName != "HTML") {
selectedSection = selectedSection.parentElement;
}
if (selectedSection == codeElement) { // if the caret is in the element being divided
caretPosition = selection.focusOffset;
}
}
// puts the text between line breaks into their own elements
S.forEach(codeElement.getElementsByTagName("br"), function (br) {
br.outerHTML = "\n";
});
let lineList = []; // holds a list of all of the new lines created
let originalSection; // hangs onto the beginning of the code to make sure the caret position can be properly set
S.forEach(codeElement.innerHTML.split(/\n\r?|\r/g), function (line, index) {
let section = document.createElement("section");
section.className = codeElement.className;
section.innerHTML = line;
codeElement.parentNode.insertBefore(section, codeElement); // inserting before maintains the same order
lineList.push(section);
if (index == 0) {
originalSection = section;
}
});
codeElement.parentNode.removeChild(codeElement);
// puts the caret in the correct spot
if (caretPosition !== null) {
while (caretPosition > originalSection.textContent.length) {
caretPosition -= originalSection.textContent.length;
originalSection = originalSection.nextSibling;
}
let newRange = document.createRange();
newRange.setStart(originalSection, caretPosition);
newRange.collapse(true);
selection.removeAllRanges();
selection.addRange(newRange);
}
// returns a list of all of the new lines created
return lineList;
}
function updateCodeLine(textLine, checkForLineBreaks) { ////
let highlightedLine = S.getId("codeHighlighter").children[textLine.parentElement.children.indexOf(textLine)]; // finds the corresponding element in the code highlighter
let code = S.getId("codeHighlighter").innerHTML; // highlighted HTML (doesn't make the page repeatedly rerender)
clearTimeout(highlightTimeout);
highlightTimeout = setTimeout(function () { // prevents performance issues from needing to refresh so much so often
}, 100);
}
function addAsSections(code) {
let textContainer = S.getId("codeDisplay");
textContainer.innerHTML = "";
let section = document.createElement("section");
section.className = projectLanguage;
section.textContent = code;
textContainer.appendChild(section);
let highlighting = S.getId("codeHighlighter");
highlighting.innerHTML = "";
S.forEach(divideLineBreaks(section), function (line) {
let hl = document.createElement("section"); // highlighted line
hl.className = "highlighting " + line.className;
hl.innerHTML = highlightLine(line);
highlighting.appendChild(hl);
});
updateLineNumbers();
}
S.listen("advanced", "click", function() {
if (this.innerHTML.trim() == "Show advanced choices") {
S.getId("advancedSection").style.display = "inline-block";
this.innerHTML = "Hide advanced choices";
} else {
S.getId("advancedSection").style.display = "none";
this.innerHTML = "Show advanced choices";
}
});
S.listen("navigation", "click", function() {
if (this.checked) {
S.getId("navigationSection").style.display = "inline-block";
} else {
S.getId("navigationSection").style.display = "none";
}
});
S.listen("extraScripts", "click", function() {
if (this.checked) {
S.getId("extraScriptsSection").style.display = "inline-block";
} else {
S.getId("extraScriptsSection").style.display = "none";
let scriptNumber = S.getId("extraScriptsSection").children.length - 1;
while (scriptNumber--) {
S.getId("extraScriptsSection").removeChild(S.getId("extraScriptsSection").children[0]);
}
}
});
S.listen("addScript", "click", function() {
let container = document.createElement("div");
container.style.display = "block";
let input = document.createElement("input");
input.type = "text";
input.placeholder = "Script source";
let X = document.createElement("span");
X.innerHTML = " X";
X.className = "big-X";
container.appendChild(input);
container.appendChild(X);
S.insertBefore(container, "addScript");
S.listen(X, "click", function() {
container.parentNode.removeChild(container);
});
});
S.listen("extraStyles", "click", function() {
if (this.checked) {
S.getId("extraStylingSection").style.display = "inline-block";
} else {
S.getId("extraStylingSection").style.display = "none";
let styleNumber = S.getId("extraStylingSection").children.length - 1;
while (styleNumber--) {
S.getId("extraStylingSection").removeChild(S.getId("extraStylingSection").children[0]);
}
}
});
S.listen("addStyling", "click", function() {
let container = document.createElement("div");
container.style.display = "block";
let input = document.createElement("input");
input.type = "text";
input.placeholder = "Style source";
let X = document.createElement("span");
X.textContent = " X";
X.className = "big-X";
container.appendChild(input);
container.appendChild(X);
S.insertBefore(container, "addStyling");
S.listen(X, "click", function() {
container.parentNode.removeChild(container);
});
});
S.listen("createWebsite", "click", function () {
// determines whether everything is filled out correctly
let satisfactorilyCompleted = true;
S.forEach(S.getClass("necessary"), function (item) {
if (item.offsetParent !== null) { // if the element is visible
if (item.tagName == "SPAN" || item.tagName == "DIV") {
S.forEach(item.getElementsByTagName("input"), function (input) {
if (!input.value) {
satisfactorilyCompleted = false;
}
});
} else {
if (!item.value) {
satisfactorilyCompleted = false;
}
}
}
});
if (!satisfactorilyCompleted) {
alert("You missed a required field.");
return;
}
// makes sure there aren't any random spaces in anything
S.forEach(S.getTag("input"), function (input) {
if (input.type == "text") {
input.value = input.value.trim();
}
});
// makes sure inputs have the correct file extensions
if (S.getId("navSource").value && !(S.getId("navSource").value.slice(-5) == ".html" || S.getId("navSource").value.slice(-4) == ".htm")) {
S.getId("navSource").value += ".html";
}
S.forEach(S.getId("extraScriptsSection").getElementsByTagName("input"), function (source) {
if (source.value && source.value.slice(-3) != ".js") {
source.value += ".js";
}
});
S.forEach(S.getId("extraStylingSection").getElementsByTagName("input"), function (source) {
if (source.value && source.value.slice(-4) != ".css") {
source.value += ".css";
}
});
// creates the website HTML
html = "";
function tabs(number) {
return "\t".repeat(number);
}
html += '<!doctype html>\n';
html += '<html>\n';
html += '\t<head>\n';
if (S.getId("favicon").value.trim()) {
html += '\t\t<link rel="icon" href="' + S.getId("favicon").value.trim() + '">\n';
}
html += '\t\t<title>\n';
html += '\t\t\t' + S.getId("title").value + '\n';
html += '\t\t</title>\n';
html += '\t\t<base target="_blank">\n';
html += '\t\t<script src="https://epicenterprograms.github.io/standards/behavior/general.js"></' + 'script>\n';
if (S.getId("login").checked) {
html += '\t\t<script src="https://www.gstatic.com/firebasejs/4.6.1/firebase.js"></' + 'script>\n';
html += '\t\t<script src="https://www.gstatic.com/firebasejs/4.6.1/firebase-firestore.js"></' + 'script>\n';
html += '\t\t<script src="https://epicenterprograms.github.io/standards/behavior/firebaseinit.js"></' + 'script>\n';
html += '\t\t<script src="https://epicenterprograms.github.io/standards/behavior/storage.js"></' + 'script>\n';
}
S.forEach(S.getId("extraScriptsSection").getElementsByTagName("input"), function(source) {
if (source.value != "") {
if (source.value[0] == "/") {
if (!(S.getId("login").checked && source.value == "/storage.js")) {
source.value = "https://epicenterprograms.github.io/standards/behavior" + source.value;
}
}
html += '\t\t<script src="' + source.value + '"></' + 'script>\n';
}
});
html += '\t\t<link rel="stylesheet" href="https://epicenterprograms.github.io/standards/formatting/foundation.css">\n';
S.forEach(S.getId("extraStylingSection").getElementsByTagName("input"), function(source) {
if (source.value != "") {
if (source.value[0] == "/") {
source.value = "https://epicenterprograms.github.io/standards/formatting" + source.value;
}
html += '\t\t<link rel="stylesheet" href="' + source.value + '">\n';
}
});
html += '\t</head>\n';
if (S.getName("navChoice", true) && S.getName("navChoice", true).id == "leftNav") {
html += '\t<body class="left-nav">\n';
html += '\t\t<iframe class="left-nav"';
if (S.getId("navSource").value) {
html += ' src="' + S.getId("navSource").value + '"></iframe>\n';
} else {
html += '></iframe>\n';
}
} else {
html += '\t<body>\n';
}
if (S.getName("navChoice", true) && S.getName("navChoice", true).id == "hiddenLeftNav") {
html += '\t\t<nav class="hidden-left-nav">';
if (S.getId("navSource").value) {
html += '\n\t\t\t<iframe src="' + S.getId("navSource").value + '"></iframe>\n\t\t';
}
html += '</nav>\n';
}
if (S.getName("navChoice", true) && S.getName("navChoice", true).id == "topNav") {
html += '\t\t<iframe class="top-nav"';
if (S.getId("navSource").value) {
html += ' src="' + S.getId("navSource").value + '"></iframe>\n';
} else {
html += '></iframe>\n';
}
}
html += '\t\t<h1 class="main-title">\n';
if (S.getId("navigation").checked) {
html += '\t\t\tTitle\n';
} else {
html += '\t\t\t' + S.getId("title").value + '\n';
}
html += '\t\t</h1>\n';
if (S.getId("login").checked) {
html += '\t\t<div class="user-section">\n';
html += '\t\t\t<button id="signIn">\n';
html += '\t\t\t\tLog in\n';
html += '\t\t\t</button>\n';
html += '\t\t\t<button id="signUp">\n';
html += '\t\t\t\tRegister\n';
html += '\t\t\t</button>\n';
html += '\t\t\t<button id="userSettings">\n';
html += '\t\t\t\tSettings\n';
html += '\t\t\t</button>\n';
html += '\t\t\t<button id="signOut">\n';
html += '\t\t\t\tLog out\n';
html += '\t\t\t</button>\n';
html += '\t\t</div>\n';
}
html += '\t\t<main>\n';
html += '\t\t\t<p>\n';
html += '\t\t\t\tPut your content here.\n';
html += '\t\t\t</p>\n';
html += '\t\t</main>\n';
html += '\t</body>\n';
html += '</html>\n';
S.getId("codeDisplay").textContent = html;
updateCode(true);
});
S.listen("createNavigation", "click", function () {
html = "";
html += '<!doctype html>\n';
html += '<html>\n';
html += '\t<head>\n';
html += '\t\t<base target="_parent">\n';
html += '\t\t<script src="https://epicenterprograms.github.io/standards/behavior/navigation.js"></' + 'script>\n';
html += '\t\t<script>\n';
html += '\t\t\tStandards.navigation.contents = [ // [page URL, title]\n';
html += '\t\t\t\t\n';
html += '\t\t\t];\n';
html += '\t\t</' + 'script>\n';
html += '\t\t<link rel="stylesheet" href="https://epicenterprograms.github.io/standards/formatting/foundation.css">\n';
html += '\t</head>\n';
if (S.getId("biggerFontContents").checked) {
html += '\t<body class="navigation bigger">\n';
} else {
html += '\t<body class="navigation">\n';
}
html += '\t\t<h2>\n';
html += '\t\t\tContents\n';
html += '\t\t</h2>\n';
html += '\t\t<ul id="contentsList"></ul>\n';
html += '\t</body>\n';
html += '</html>\n';
S.getId("codeDisplay").textContent = html;
updateCode(true);
});
S.listen("showConsole", "click", function () {
let log = "<div style='text-align:left; white-space:pre; tab-size:6;'>";
log += console.messages.join("\n\n").replace(/</g, "<").replace(/>/g, ">").replace(/\//g, "<wbr>/");
log += "</div>";
log = log.replace(/\n/g, "<br>");
log = log.replace(/Warning.+\{[^]+?\}/g, "<span style='color:orange'>$&</span>");
log = log.replace(/Error.+\{[^]+?\}/g, "<span style='color:red'>$&</span>");
log = log.replace(/Info.+\{[^]+?\}/g, "<span style='color:blue'>$&</span>");
S.makeDialog(log, "Done");
});
S.listen("importCode", "click", function () {
new Promise(function () {
S.getFile(S.getId("codeURL").value, function (contents) {
S.getId("codeDisplay").textContent = contents;
// attempts to figure out which language the code is written in
switch (S.getId("codeURL").value.slice(S.getId("codeURL").value.lastIndexOf(".") + 1)) {
case "html":
projectLanguage = "HTML";
break;
case "css":
projectLanguage = "CSS";
break;
case "js":
projectLanguage = "JavaScript";
break;
case "py":
projectLanguage = "Python";
break;
default:
projectLanguage = "HTML";
}
S.getId("codeLanguage").value = projectLanguage;
updateCode(true);
//// addAsSections(contents);
}, false);
}).catch(function (error) {
console.error("The file couldn't be retrieved.");
console.error(error);
S.makeDialog("The file couldn't be retrieved.");
});
});
S.listen("projectName", "change", function () {
projectName = encodeURIComponent(this.value.trim());
});
S.listen("codeLanguage", "change", function () {
projectLanguage = this.value;
updateCode();
});
S.listen("loadProject", "click", function () {
if (projectName == "") {
S.makeDialog("Specify a project name to load.");
} else if (projectName == "Default") {
S.makeDialog("No projects can use that name.");
} else {
let project = M.session.recall(projectName + "/");
if (S.getType(project) == "Error") {
S.makeDialog("No project exists with that name.");
} else {
S.getId("codeDisplay").textContent = project.code;
projectLanguage = project.language;
S.getId("codeLanguage").value = projectLanguage;
updateCode(true);
}
}
});
S.listen("saveProject", "click", function () {
if (projectName == "") {
S.makeDialog("Specify a project name to save.");
} else if (projectName == "Default") {
S.makeDialog("No projects can use that name.");
} else {
let project = {};
project.code = S.getId("codeDisplay").textContent;
project.language = projectLanguage;
project.lastUpdated = new Date().getTime();
M.local.store(projectName + "/", project);
M.session.store(projectName + "/", project);
if (M.server.user) {
syncInformation();
}
S.makeDialog("Project saved.");
}
});
S.listen("deleteProject", "click", function () {
if (projectName == "") {
S.makeDialog("Specify a project name to delete.");
} else if (projectName == "Default") {
S.makeDialog("The default project can't be deleted.");
} else if (!M.session.list("./", { shallowKeyList: true }).includes(projectName)) {
S.makeDialog("The specified project doesn't exist.");
} else {
S.makeDialog("Are you sure you want to delete this project?",
["Yes", function () {
M.local.forget(projectName + "/");
M.session.forget(projectName + "/");
if (M.server.user) {
M.server.forget(projectName + "/", function () {
S.makeDialog("Project deleted from account.");
});
}
S.getId("projectName").value = "Default";
projectName = "Default";
S.getId("codeDisplay").textContent = "";
updateCode(true);
S.makeDialog("Project deleted.");
}], "No");
}
});
S.listen("listProjects", "click", function () {
let list = [];
S.forEach(M.session.list("./", { shallowKeyList: true }), function (name) {
if (name != "last synchronized") {
list.push(decodeURIComponent(name));
}
});
S.makeDialog(list.join(", "));
});
/* ////
S.listen("undo", "click", function () {
document.dispatchEvent(new KeyboardEvent("keydown", { key: "z", ctrlKey: true })).call(S.getId("codeDisplay"));
});
S.listen("redo", "click", function () {
S.getId("codeDisplay").focus();
document.dispatchEvent(new KeyboardEvent("keydown", { key: "y", ctrlKey: true }));
});
*/
S.listen("highlightOption", "change", function () {
updateCode();
});
S.listen("codeDisplay", "keydown", function (event) {
if (event.key == "Enter") {
/*
event.preventDefault();
let selection = window.getSelection();
let range = selection.getRangeAt(0);
let offset = range.startOffset;
let container = range.startContainer;
let editableLine = document.createElement("span");
editableLine.className = "editable-code-line";
editableLine.textContent = container.textContent.slice(offset);
let shownLine = document.createElement("span");
shownLine.className = "highlighted-code-line";
shownLine.innerHTML = highlightLine(editableLine.textContent);
//// insert to page
/*/
event.preventDefault();
let selection = window.getSelection();
let range = selection.getRangeAt(0);
let offset = range.startOffset;
let beforeNewLine = this.textContent.slice(0, range.startOffset);
let tabs = "";
if (beforeNewLine.lastIndexOf("\n") < range.startOffset) { // if there are tabs preceeding the current line
tabs = beforeNewLine.slice(beforeNewLine.lastIndexOf("\n")).match(/\t+/);
if (tabs === null) {
tabs = "";
} else {
tabs = tabs[0];
}
if (["{", "[", "(", ":"].includes(S.itemAt(beforeNewLine, -1)) || S.itemAt(beforeNewLine, -1) == ">" && !beforeNewLine.slice(beforeNewLine.lastIndexOf("<")).includes("/")) { // if the new line comes after one of the items in the array or an opening HTML tag
tabs += "\t";
}
}
this.textContent = this.textContent.slice(0, range.startOffset) + "\n" + tabs + this.textContent.slice(range.endOffset);
// puts the caret in the correct spot
let newRange = document.createRange();
newRange.setStart(this.childNodes[0], offset + tabs.split("\t").length);
newRange.collapse(true);
selection.removeAllRanges();
selection.addRange(newRange);
//*/
} else if (event.key == "Tab") {
event.preventDefault();
let selection = window.getSelection();
let range = selection.getRangeAt(0);
let offset = range.startOffset;
this.textContent = this.textContent.slice(0, range.startOffset) + "\t" + this.textContent.slice(range.endOffset);
// puts the caret in the correct spot
let newRange = document.createRange();
newRange.setStart(this.childNodes[0], offset + 1);
newRange.collapse(true);
selection.removeAllRanges();
selection.addRange(newRange);
}
S.getId("codeHighlighter").textContent = S.getId("codeDisplay").textContent;
});
S.listen("codeDisplay", "keyup", function (event) {
let key = event.key;
let selection = window.getSelection();
let range = selection.getRangeAt(0);
let offset = range.startOffset;
if (key == "Unidentified") { // happens on mobile?
key = this.textContent.slice(offset - 1, offset);
}
if (key == "Enter" || key == "Backspace") {
updateCode(true);
} else if (key == "," || key == ".") {
let newRange = document.createRange();
let madeAReplacement = false;
let correction = -2;
switch (this.textContent.slice(offset - 3, offset)) {
case ",,,": // if replacing ",,," with "\t"
this.textContent = this.textContent.slice(0, offset - 3) + "\t" + this.textContent.slice(range.endOffset);
madeAReplacement = true;
break;
case "..,": // if replacing "..," with "<"
this.textContent = this.textContent.slice(0, offset - 3) + "<" + this.textContent.slice(range.endOffset);
madeAReplacement = true;
break;
case ",..": // if replacing ",.." with "/"
this.textContent = this.textContent.slice(0, offset - 3) + "/" + this.textContent.slice(range.endOffset);
madeAReplacement = true;
break;
case ",,.": // if replacing ",,." with ">"
this.textContent = this.textContent.slice(0, offset - 3) + ">" + this.textContent.slice(range.endOffset);
madeAReplacement = true;
break;
case ",.,": // if replacing ",.," with "\n" (when "Enter" isn't recognized)
let beforeNewLine = this.textContent.slice(0, range.startOffset);
let tabs = "";
if (beforeNewLine.lastIndexOf("\n") < offset) { // if there are tabs preceeding the current line
tabs = beforeNewLine.slice(beforeNewLine.lastIndexOf("\n")).match(/\t+/);
if (tabs === null) {
tabs = "";
} else {
tabs = tabs[0];
}
if (["{", "[", "(", ":"].includes(S.itemAt(beforeNewLine, -4)) || S.itemAt(beforeNewLine, -4) == ">" && !beforeNewLine.slice(beforeNewLine.lastIndexOf("<")).includes("/")) { // if the new line comes after one of the items in the array or an opening HTML tag
tabs += "\t";
}
}
this.textContent = this.textContent.slice(0, offset - 3) + "\n" + tabs + this.textContent.slice(range.endOffset);
madeAReplacement = true;
correction += tabs.length;
break;
}
if (madeAReplacement) {
// puts the caret in the correct spot
newRange.setStart(this.childNodes[0], offset + correction);
newRange.collapse(true);
selection.removeAllRanges();
selection.addRange(newRange);
}
updateCode(true);
} else {
updateCode();
}
});
S.listen("codeDisplay", "paste", function () {
setTimeout(function () { // wait until the contents are actually pasted
updateCode(true);
updateLineNumbers();
}, 0);
});
S.listen("preview", "click", function() {
var preview = window.open();
if (preview) { // if pop-ups are allowed
let code = S.getId("codeDisplay").textContent;
preview.document.write(code);
preview.addEventListener("console written", function () {
console.messages.push(S.itemAt(preview.console.messages, -1));
});
if (code.search(/<script>[^]+?<\/script>/) > -1) {
preview.runJavaScript = function () {
preview.dispatchEvent(new Event("load")); // makes sure the real page load isn't missed
preview.Function(code.match(/<script>([^]+?)<\/script>/)[1])();
}
preview.runJavaScript();
}
preview.focus();
} else {
S.makeDialog("You have to allow pop-ups<br>for the preview to appear.");
}
});
S.listen("startOver", "click", function() {
["title", "navSource", "copier"].forEach(function(ID) {
S.getId(ID).value = "";
});
["navigation", "extraScripts", "extraStyles"].forEach(function(ID) {
S.getId(ID).checked = false;
});
["navigationSection", "advancedSection"].forEach(function(ID) {
S.getId(ID).style.display = "none";
});
["navChoice"].forEach(function(ID) {
S.forEach(S.getName(ID), function(radioButton) {
radioButton.checked = false;
}, false);
});
["extraScriptsSection", "extraStylingSection"].forEach(function(ID) {
S.getId(ID).style.display = "none";
let repeatNumber = S.getId(ID).children.length - 1;
while (repeatNumber--) {
S.getId(ID).removeChild(S.getId(ID).children[0]);
}
});
S.getId("login").checked = true;
S.getId("advanced").innerHTML = "Show advanced choices";
S.getId("preview").disabled = true;
S.getId("codeDisplay").textContent = "";
updateCode(true);
});
function syncInformation() {
if (S.getType(M.local.recall("last synchronized")) == "Error") {
M.local.store("last synchronized", -Infinity);
}
let localSync = M.local.recall("last synchronized");
M.server.recall("last synchronized", function (serverSync) {
M.server.list("./", function (serverProjects) { // lists all of the user's projects (only the titles)
let remaining = new S.Listenable();
remaining.value = 1;
remaining.addEventListener("set", function (value) {
if (value == 0) { // once all items have been successfully handled
M.session.forget("/");
S.forEach(M.local.list(), function (name) {
if (name != "last viewed") {
M.session.store(name, M.local.recall(name));
}
});
S.makeDialog("Account data was synchronized.");
}
});
if (S.getType(serverSync) == "Error") {
serverSync = -Infinity;
}
let syncTime = new Date().getTime();
M.local.store("last synchronized", syncTime);
remaining.value++;
M.server.store("last synchronized", syncTime, function () { remaining.value--; });
let localProjects = M.local.list("./", { shallowKeyList: true });
S.forEach(serverProjects, function (name) {
if (name != "last synchronized") {
remaining.value++;
M.server.recall(name + "/", function (project) {
// deletes projects deleted since the last synchronization
// stores the server version of the project if there isn't a local version or if the local version hasn't been updated as recently
// overwrites the server version if the local version has been updated more recently
if (!localProjects.includes(name)) {
if (localSync == serverSync) { // if the project was deleted since the most recent synchronization
M.server.forget(name + "/", function () { remaining.value--; });