This repository was archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
2516 lines (1998 loc) · 73.4 KB
/
app.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
"use strict"
require("dotenv").config()
const express = require("express")
const storage = require("express-session")
const parser = require("cookie-parser")
const canvas = require("canvas").createCanvas
const nodemailer = require("nodemailer").createTransport
const size = require("image-size")
const mongodb = require("mongodb").MongoClient
const remarkable = require("remarkable").Remarkable
const store = require("connect-mongo")
const bcrypt = require("bcrypt")
const app = express()
const server = require("http").Server(app)
const markdown = new remarkable({breaks: true})
const emails = {
welcome: name => /*html*/
`<p>
Hello ${name},
<br>
We are very excited to have you as part of the JS-ByteBase community.
</p>`,
verify: (name, address) => /*html*/
`<p>
Hello ${name},
<br>
We have sent this verification email because you have tried to change your password.
Click the link below to verify your email address.
<br>
 
</p>
<p>
<a
href = ${address}
style = "background-color: #4a4; padding: 0.4em; font-family: sans-serif; font-size: 20px; border-radius: 0.3em; text-decoration: none; color: #fff">
Verify
</a>
</p>`
}
const blocks = {
top: (user, gallery, search = "") => /*html*/
`<section class = top>
<div>
<a href = "/">
<svg xmlns = http://www.w3.org/2000/svg viewBox = "0 0 630 630">
<path d = "M 0 0 V 630 H 630 V 183 Q 446 183 446 0" fill = #f7df1e></path>
<path d = "m 423 492 c 13 21 29 36 58 36 c 25 0 40 -12 40 -29 c 0 -20 -16 -28 -43 -39 l -15
-6 c -43 -18 -71 -41 -71 -89 c 0 -44 34 -78 87 -78 c 38 0 65 13 84 47 l -46 30 c -10 -18
-21 -25 -38 -25 c -17 0 -28 11 -28 25 c 0 18 11 25 36 36 l 15 6 c 50 22 79 44 79 93 c 0
53 -42 83 -98 83 c -55 0 -91 -26 -108 -61 z m -209 5 c 9 17 18 30 38 30 c 19 0 32 -8 32
-37 v -201 h 59 v 202 c 0 61 -36 89 -88 89 c -47 0 -75 -25 -89 -54 z" fill = #000></path>
</svg>
<span>ByteBase</span>
</a>${gallery ? /*html*/ `
<input type = text placeholder = Search onchange = submit() value = "${search}">
<select class = button version = plain onchange = submit()>
<option>Top</option>
<option>Newest</option>
<option>Random</option>
</select>` : /*html*/ `
<form action = /gallery method = post>
<input type = text name = search placeholder = Search>
</form>
<a href = /gallery class = button version = plain>Gallery</a>`}${user ? /*html*/ `
<a href = "/profile/${user.name}" class = "profile button" version = plain>
<img src = "${user.image}">${user.name}
</a>
<a href = /project class = button version = plain>New<span> project</span></a>` : /*html*/ `
<a href = /sign class = button version = plain>Sign in</a>
<a href = /create class = button version = green>Create<span> account</span></a>`}
</div>
</section>`,
footer: _ => /*html*/
`<footer>
<span>
© Copyright ${new Date().getFullYear()} JS-ByteBase ·
<a href = mailto:hello@js-bytebase.com>hello@js-bytebase.com</a>
</span>
</footer>`,
sitemap: (req, url, change, priority) => /*xml*/
`<url>
<loc>${protocol(req)}://${req.get("host")}/${url}</loc>
<changefreq>${change}</changefreq>
<priority>${priority}</priority>
</url>`
}
const pages = {
home: data => ({
head: /*html*/
`<title>JS-ByteBase · The JavaScript 2K Coding Challenge</title>
<script type = application/ld+json>
{
"@context": "https://schema.org",
"@type": "Organization",
"url": "https://www.js-bytebase.com",
"logo": "https://www.js-bytebase.com/logo.png"
}
</script>
<meta name = description content = "Join the JavaScript 2k community.
Play games, upload original projects, share your creations and build your portfolio.">
<style>
body > a {
position: fixed;
width: 100%;
height: calc(100% - 3em);
top: 3em;
left: 0
}
body, html {height: 100%}
.main {color: var(--text-contrast)}
.main p {font-size: var(--font)}
</style>`,
body: /*html*/
`${blocks.top(data.user)}
<section class = main page = narrow>
<h1>Welcome to JS-ByteBase</h1>
<p>
Join the JavaScript 2k community.
<br>
Play games, upload original projects, share your creations and build your portfolio.
</p>
</section>
<a href = /gallery></a>
<canvas class = background></canvas>
<script>
const canvas = get("canvas")
const webgl = canvas.getContext("webgl")
const buffer = webgl.createBuffer()
const ratio = devicePixelRatio || 1
const program = shader(webgl, ${"`" + /*glsl*/ `
attribute vec2 vertex;
void main(void) {
gl_Position = vec4(vertex, 0, 1);
}` + "`"}, ${"`" + /*glsl*/ `
precision highp float;
uniform float width;
uniform float contrast;
uniform float fade;
void main(void) {
float zoom = 10000.0;
float value;
vec2 pos = vec2((gl_FragCoord.x - 1.28 * zoom - width) / zoom, (gl_FragCoord.y + 0.035 * zoom) / zoom);
vec2 solid = pos;
for (float index = 0.0; index < 200.0; index ++) {
vec2 power = vec2(pos.x * pos.x - pos.y * pos.y, 2.0 * pos.x * pos.y);
pos.x = power.x + solid.x;
pos.y = power.y + solid.y;
if (pos.x * pos.y > contrast) {
value = index;
break;
}
}
float next = value / 80.0 * fade;
gl_FragColor = vec4(next, next, next, 1);
}` + "`"})
let contrast = 0.01
let fade = 0
const resize = _ => {
canvas.width = document.body.clientWidth * ratio
canvas.height = document.body.clientHeight * ratio
webgl.viewport(0, 0, canvas.width, canvas.height)
webgl.uniform1f(webgl.getUniformLocation(program, "width"), canvas.width)
}
const loop = _ => {
requestAnimationFrame(loop)
fade += (1 - fade) * 0.001
contrast *= 1.01
webgl.clear(webgl.COLOR_BUFFER_BIT)
webgl.uniform1f(webgl.getUniformLocation(program, "contrast"), contrast)
webgl.uniform1f(webgl.getUniformLocation(program, "fade"), fade)
webgl.drawArrays(webgl.TRIANGLE_STRIP, 0, 4)
}
const start = _ => {
webgl.bindBuffer(webgl.ARRAY_BUFFER, buffer)
webgl.bufferData(webgl.ARRAY_BUFFER, new Float32Array([-1, 1, 1, 1, -1, -1, 1, -1]), webgl.STATIC_DRAW)
webgl.vertexAttribPointer(0, 2, webgl.FLOAT, false, 0, 0)
webgl.enableVertexAttribArray(0)
addEventListener("resize", resize)
resize()
loop()
}
start()
</script>`
}),
gallery: data => ({
head: /*html*/
`<title>JS-ByteBase · Gallery</title>
<meta name = description content = "Explore the complete collection of JS-ByteBase projects">
<style>
h1 {
position: fixed;
right: 50%;
bottom: 50%;
transform: translate(50%, 50%);
opacity: 0.5;
color: var(--text)
}
h1[type = "hidden"] {display: none}
</style>`,
body: /*html*/
`${blocks.top(data.user, 1, data.search)}
<section class = main page = wide>
<h1 type = hidden>No results found</h1>
<div class = gallery></div>
</section>
${blocks.footer()}
<script>
const http = new XMLHttpRequest()
const key = Math.random().toString().substring(2)
const submit = _ => {
get(".gallery").innerHTML = ""
request()
}
const request = _ => {
if (innerHeight + Math.ceil(pageYOffset) < document.body.offsetHeight)
return
http.onload = event => {
gallery(event)
const results = get(".gallery").children.length
get("h1").setAttribute("type", results ? "hidden" : "active")
}
http.open("POST", "/search")
http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
http.send(${`\`search=\${
encodeURIComponent(document.querySelector("input").value)}&filter=\${
document.querySelector("select").value}&index=\${
document.querySelector(".gallery").children.length}&key=\${
key}\``})
}
const start = _ => {
const observer = new MutationObserver(request)
observer.observe(document, {childList: true, subtree: true})
window.onscroll = request
}
start()
</script>`
}),
create: data => ({
head: /*html*/
`<title>JS-ByteBase · Create Account</title>
<meta name = description content = "Create a new account">`,
body: /*html*/
`<div class = account>
<div>
<form action = "" method = post>
<input type = text name = name placeholder = Username required>
<input type = email name = email placeholder = Email required>
<input type = password name = password placeholder = Password required>
<button class = button version = green type = submit>
<i class = "fas fa-sign-in-alt"></i>Create
</button>${data.error ? /*html*/ `
<span>${data.error}</span>` : ""}
</form>
</div>
<a href = /sign>Sign in</a>
</div>`
}),
sign: data => ({
head: /*html*/
`<title>JS-ByteBase · Sign In</title>
<meta name = description content = "Sign in to JS-ByteBase">`,
body: /*html*/
`<div class = account>
<div>
<form action = "" method = post>
<input type = text name = name placeholder = Username required>
<input type = password name = password placeholder = Password required>
<button class = button version = green type = submit>
<i class = "fas fa-sign-in-alt"></i>Sign in
</button>${data.error ? /*html*/ `
<span>${data.error}</span>` : ""}
</form>
</div>
<a href = /email>Forgot password</a>
</div>`
}),
email: data => ({
head: /*html*/
`<title>JS-ByteBase · Verify Email</title>
<meta name = description content = "Verify your email address">`,
body: /*html*/
`<div class = account>
<div>
<form action = "" method = post>
<input type = email name = email placeholder = Email required>
<button class = button version = green type = submit>
<i class = "fas fa-unlock-alt"></i>Verify
</button>${data.error ? /*html*/ `
<span>${data.error}</span>` : ""}
</form>
</div>
<a href = /sign>Sign in</a>
</div>`
}),
verify: data => ({
head: /*html*/
`<title>JS-ByteBase · Verify Email</title>`,
body: /*html*/
`<div class = account>
<h3>Verification email sent to <span>${data.email}</span></h3>
<span>You can close this page</span>
</div>`
}),
expired: _ => ({
head: /*html*/
`<title>JS-ByteBase · Link Expired</title>`,
body: /*html*/
`<div class = account>
<h3>This link has expired</h3>
<span>Click <a href = /email>here</a> to try again</span>
</div>`
}),
password: data => ({
head: /*html*/
`<title>JS-ByteBase · Change Password</title>
<meta name = description content = "Change your password for JS-ByteBase">`,
body: /*html*/
`<div class = account>
<div>
<form action = "" method = post>
<input type = password name = password placeholder = "New password" required>
<input type = password name = confirm placeholder = "Confirm password" required>
<button class = button version = green type = submit>
<i class = "fas fa-key"></i>Reset
</button>${data.error ? /*html*/ `
<span>${data.error}</span>` : ""}
</form>
</div>
</div>`
}),
demo: data => ({
head: /*html*/
`<title>JS-ByteBase · ${data.project.title}</title>
<meta name = description content = "${data.project.title} by ${data.creator.name}">
<script src = https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ace.js></script>
<script src = https://cdn.jsdelivr.net/remarkable/1.7.1/remarkable.min.js></script>
<style>
.image {
margin-right: 1em;
float: left;
position: relative
}
.image img {
border-radius: var(--radius);
box-shadow: var(--shadow) var(--box);
max-width: 100%
}
.image button, .image > span {
position: absolute;
left: 0.5em;
bottom: 0.9em;
color: var(--text);
background-color: var(--off);
border-radius: var(--radius);
border: var(--border) var(--box);
font-size: 15px;
padding: 0.2em 0.4em
}
.main > div:first-child {
margin-bottom: 1em;
display: inline-block
}
.image button:hover {background-color: var(--button)}
.image i {padding-right: var(--padding)}
.main .profile {margin-bottom: 0.6em}
.data {overflow: hidden}
h1 {
color: var(--text);
font-weight: bold;
margin: 0 0 0.5em 0;
white-space: nowrap;
overflow: auto
}
.image button[type = "like"] {
background-color: var(--green);
color: var(--text-contrast)
}
.image button[type = "like"]:hover {background-color: var(--green-hover)}
button[type = "report"] {
background-color: var(--error);
color: var(--text-contrast)
}
button[type = "report"]:hover {background-color: var(--error-hover)}
form {display: inline}
.main > div:nth-child(2) {
border-radius: var(--radius);
padding: var(--padding) var(--padding) 0 var(--padding);
border: var(--border) var(--plain);
margin-bottom: 1em
}
.code {
box-shadow: var(--shadow) var(--box);
height: 10em;
margin-bottom: 1em
}
.tags {margin-bottom: 1.5em}
.tags input {
padding: var(--padding);
border-radius: var(--radius);
box-shadow: var(--shadow) var(--box);
font-size: var(--font);
background-color: var(--off);
color: var(--text);
font-family: nunito;
display: inline-block;
margin: 0 0.5em 0.5em 0;
transition-duration: var(--transition);
cursor: pointer;
border: none
}
.tags input:hover {background-color: var(--box)}
</style>`,
body: /*html*/
`${blocks.top(data.user)}
<section class = main page = narrow>
<div>
<div class = image>${data.user ? /*html*/ `
<button ${data.project.smiles.includes(data.user.name) ?
"type = like" : ""} onclick = like(this)>` : /*html*/ `
<span>`}
<i class = "far fa-smile"></i>
<span>${data.project.smiles.length}</span>${data.user ? /*html*/ `
</button>` : /*html*/ `
</span>`}
<a href = "/project/${data.project.id}" target = _blank>
<img src = "${data.project.image}">
</a>
</div>
<div class = data>
<h1>${data.project.title}</h1>
<a href = "/profile/${data.creator.name}" class = "profile button" version = plain>
<img src = "${data.creator.image}">
${data.creator.name}
</a><br>
<span class = tab><i class = "fas fa-link"></i>${data.host}/project/${data.project.id}</span><br>
<span class = tab>${data.project.views + (data.project.views == 1 ? " view" : " views")}</span>
<span class = tab>${Buffer.from(data.project.code).length} bytes</span><br>
<a href = "/project/${data.project.id}" target = _blank class = tab>
<i class = "far fa-play-circle"></i>Play
</a>${data.user ? /*html*/ `${data.creator.name == data.user.name ? /*html*/ `
<form action = /delete method = post onsubmit = "return enter()">
<input type = hidden name = project value = ${data.project.id}>
<button type = submit class = tab><i class = "far fa-trash-alt"></i>Delete</button>
</form>` : /*html*/ `
<button class = tab ${data.project.flags.includes(data.user.name) ?
"type = report" : ""} onclick = report(this)>
<i class = "far fa-flag"></i>Report
</button>`}` : ""}
</div>
</div>
<div>
<div class = markdown>
${markdown.render(data.project.description)}
</div>
</div>
<div class = code>${convert(data.project.code)}</div>
<div class = tags>${data.project.tags.reduce(
(all, item) => all + /*html*/ `
<input type = submit name = search value = ${item}>`, /*html*/ `
<form action = /gallery method = post>`) + /*html*/ `
</form>`}
</div>
<div class = comments>${data.project.comments.reduce(
(all, item, index) => all + /*html*/ `
<div class = comment id = ${index}>
<a href = "/profile/${item.name}">
<img src = "${data.users[item.name]}">${item.name}
</a>${data.user ? /*html*/ `
<i class = "far fa-${item.name == data.user.name ? "trash-alt" :
"flag"}" ${item.name != data.user.name && item.flags.includes(data.user.name) ? "type = flag" : ""}
onclick = ${item.name == data.user.name ? "remove" : "flag"}(this)>
</i>` : ""}
<div class = markdown>
${markdown.render(item.comment)}
</div>
</div>`, "")}
</div>${data.user ? /*html*/ `
<div class = message>
<textarea placeholder = "Add comment" oninput = input(this) autocomplete = off></textarea>
<i onclick = comment(this) class = "fas fa-paper-plane" version = blank></i>
<a href = https://www.markdownguide.org/basic-syntax target = _blank>Markdown supported</a>
</div>` : ""}
</section>
${blocks.footer()}
<script>
const editor = ace.edit(get(".code")) ${data.user ? /*javascript*/ `
const http = new XMLHttpRequest()
const markdown = new Remarkable({breaks: true})
const smiles = get(".image").firstElementChild.lastElementChild
const find = button => [].indexOf.call(
get(".comments").children, button.parentElement)
const post = (link, type, index) => {
http.open("POST", link)
http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
http.send(${`\`project=${data.project.id}\${type ? "&type=1" : ""}&index=\${index}\``})
}
const comment = button => {
const element = button.previousElementSibling
if (!element.value.trim()) return
post("/comment", 1, encodeURIComponent(element.value))
get(".comments").appendChild(construct(
markdown.render(element.value.trim()),
${`"${data.user.name}"`}, ${`"${data.user.image.replace(/"/g, "\\\"").replace(/\n/g, "\\n")}"`}))
element.value = ""
}
const remove = button => {
if (!confirm("Are you sure you want to delete this comment?"))
return
const index = find(button)
post("/comment", 0, index)
get(".comments").children[index].remove()
}
const enter = _ => {
if (Number(smiles.textContent) >= 10) {
alert("You cannot delete a project with 10 or more smiles.")
return false
}
return confirm("Are you sure you want to delete this project?")
}
const flag = button => {
const index = find(button)
if (button.getAttribute("type")) {
post("/flag", 0, index)
button.removeAttribute("type")
}
else {
post("/flag", 1, index)
button.setAttribute("type", "flag")
}
}
const like = button => {
if (button.getAttribute("type")) {
post("/like")
button.removeAttribute("type")
smiles.textContent = Number(smiles.textContent) - 1
}
else {
post("/like", 1)
button.setAttribute("type", "like")
smiles.textContent = Number(smiles.textContent) + 1
}
}
const report = button => {
if (button.getAttribute("type")) {
post("/report")
button.removeAttribute("type")
}
else {
post("/report", 1)
button.setAttribute("type", "report")
}
}
` : ""}
const start = _ => {
editor.setOption("wrap", true)
editor.session.setMode("ace/mode/javascript")
editor.setReadOnly(true)
editor.gotoLine(0, 1, true)
theme(editor)
}
start()
</script>`
}),
main: data => ({
head: /*html*/
`<title>${data.project.title} by ${data.project.name}</title>
<meta name = description content = "${data.project.description}">
<style>
iframe {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: #fff;
border: none
}
</style>`,
body: /*html*/
`<iframe
onload = this.contentWindow.focus()
srcdoc = "<!DOCTYPE html><body style = 'margin: 0; overflow: hidden'><script>${convert(data.project.code)}</script></body></html>"
sandbox = "allow-scripts allow-same-origin">
</iframe>`
}),
profile: data => ({
head: /*html*/
`<title>JS-ByteBase · ${data.profile.name}</title>
<meta name = description content = "Profile of ${data.profile.name}">
<script src = https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ace.js></script>
<style>
.user {
border-radius: var(--radius);
border: var(--border) var(--plain);
margin-bottom: 1em;
padding: 1em
}
.user div {
display: inline-block;
font-family: nunito;
vertical-align: top;
overflow: hidden;
margin-left: 1em
}
.user img {
box-shadow: var(--shadow) var(--box);
border-radius: 50%;
transition-duration: var(--transition)
}
.fa-cog {
color: var(--fade);
transition-duration: var(--transition);
float: right;
cursor: pointer
}
.fa-cog:hover {color: var(--text)}
.user > label > img {cursor: pointer}
.user > label > img:hover {opacity: 0.5}
h1 {
color: var(--text);
font-weight: bold;
margin: 0
}
form button {
color: var(--text);
font-size: var(--small);
float: right;
border: none;
background: none;
margin-bottom: var(--padding)
}
form button:hover {color: var(--fade)}
.settings {
transition-duration: var(--reveal);
overflow: hidden;
transition: var(--reveal);
border-radius: var(--radius);
border: var(--border);
padding-left: 1em;
padding-right: 1em;
color: var(--text)
}
.settings[type = "hidden"] {
border-color: var(--background);
max-height: 0;
margin: 0
}
.settings[type = "active"] {
border-color: var(--plain);
max-height: 20em;
padding: 1em;
margin-bottom: 1em
}
.settings label {
color: var(--text);
border: var(--border) var(--plain);
border-radius: var(--radius);
padding: var(--padding);
display: inline-block;
margin-bottom: 1em
}
.settings select, .settings input {
border-radius: var(--radius);
border: var(--border) var(--plain);
background-color: var(--off);
color: var(--text)
}
.settings select {cursor: pointer}
.settings input {
padding: var(--padding);
display: block;
max-width: 100%;
margin-bottom: 1em
}
.code {
box-shadow: var(--shadow) var(--box);
font-size: var(--font);
border-radius: var(--radius);
margin: var(--padding) 0 1em 0
}
</style>`,
body: /*html*/
`${blocks.top(data.user)}
<section class = main page = narrow>
<div class = user>${data.user && data.user.name == data.profile.name ? /*html*/ `
<label>
<input type = file onchange = image(this) accept = "image/*">
<img src = "${data.profile.image}">
</label>
<i class = "fas fa-cog" onclick = display()></i>` : /*html*/ `
<img src = "${data.profile.image}">`}
<div>
<h1>${data.profile.name}</h1>
<span class = tab>
<i class = "far fa-smile"></i>${data.projects.reduce((all, item) => all += item.smiles.length, 0)}
</span>
<span class = tab>
<i class = "far fa-calendar-alt"></i>${
new Date(data.profile.date).getDate()}.${
new Date(data.profile.date).getMonth()}.${
new Date(data.profile.date).getFullYear()}
</span><br>${data.profile.website ? /*html*/ `
<a class = tab target = _blank href = "${(data.profile.website.startsWith("http://") ||
data.profile.website.startsWith("https://") ? "" : "http://") + data.profile.website}">
<i class = "fas fa-link"></i><span>${data.profile.website.replace(/(http|https):\/\//, "")}</span>
</a>` : ""}
</div>
</div>${data.user && data.user.name == data.profile.name ? /*html*/ `
<div class = settings type = hidden>
<form action = /log method = post>
<button type = submit>Log out</button>
</form>
<input
value = "${data.profile.website}"
type = text
oninput = website(this)
placeholder = "Enter your website here"
onchange = set(this)
autocomplete = off>
<label>
Select theme:
<select onchange = change(this) class = select>
<option>Light</option>
<option>Dark</option>
<option>Gob</option>
<option>Vibrant</option>
<option>Navy</option>
</select>
</label><br>
Generate a link to your profile by pasting the following code into your website.
<div class = code>${convert(`<a href = "${data.protocol}://${data.host}/profile/${data.profile.name}"><svg xmlns = http://www.w3.org/2000/svg viewBox = "0 0 630 630" width = 40 height = 40>\n\t<path d = "M 0 0 V 630 H 630 V 183 Q 446 183 446 0" fill = "#f7df1e"/>\n\t<path d = "m 423 492 c 13 21 29 36 58 36 c 25 0 40 -12 40 -29 c 0 -20 -16 -28 -43 -39 l -15 -6 c -43 -18 -71 -41 -71 -89 c 0 -44 34 -78 87 -78 c 38 0 65 13 84 47 l -46 30 c -10 -18 -21 -25 -38 -25 c -17 0 -28 11 -28 25 c 0 18 11 25 36 36 l 15 6 c 50 22 79 44 79 93 c 0 53 -42 83 -98 83 c -55 0 -91 -26 -108 -61 z m -209 5 c 9 17 18 30 38 30 c 19 0 32 -8 32 -37 v -201 h 59 v 202 c 0 61 -36 89 -88 89 c -47 0 -75 -25 -89 -54 z" fill = "#000"/>\n</svg></a>`)}</div>
${data.projects.reduce((all, item) => all += item.smiles.length, 0) < 5 ? "" : /*html*/ `
<a href = /chat class = button version = green>Join chat room</a>`}
</div>` : ""}
<div class = gallery></div>
</section>
${blocks.footer()}
<script>
const http = new XMLHttpRequest()
const reader = new FileReader() ${data.user && data.user.name == data.profile.name ? /*javascript*/ `
const editor = ace.edit(get(".code"))
const set = input => {
http.open("POST", "/website")
http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
http.send("website=" + encodeURIComponent(input.value))
}
const website = input => {
if (input.value.trim()) {
if (!get("a.tab")) {
const link = document.createElement("a")
const icon = document.createElement("i")
const span = document.createElement("span")
link.className = "tab"
link.target = "_blank"
icon.className = "fas fa-link"
link.appendChild(icon)
link.appendChild(span)
get(".user").lastElementChild.appendChild(link)
defined = true
}
const link = get("a.tab")
link.href = (input.value.startsWith("http://") || input.value.startsWith("https://") ? "" : "http://") + input.value
link.lastElementChild.textContent = input.value.replace(/(http|https):\\\/\\\//, "")
}
else if (get("a.tab").textContent.trim())
get("a.tab").remove()
}
const image = input => {
if (!input.files.length) return
reader.onload = _ => {
const image = new Image()
image.onload = _ => {
const canvas = document.createElement("canvas")
const picture = input.nextElementSibling
const user = get(".profile").firstElementChild
canvas.width = 100
canvas.height = 100
render(canvas, image, 1)
picture.src = canvas.toDataURL("image/jpeg", 0.8)
user.src = picture.src
http.open("POST", "/profile")
http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
http.send("image=" + encodeURIComponent(picture.src))
}
image.src = reader.result
}
reader.readAsDataURL(input.files[0])
}
const change = select => {
document.body.setAttribute("theme", select.value)
localStorage.setItem("theme", select.value)
theme(editor)
}
const display = _ => {
const type = get(".settings").getAttribute("type")
get(".settings").setAttribute("type", type == "hidden" ? "active" : "hidden")
}
` : ""}
const request = _ => {
if (innerHeight + Math.ceil(pageYOffset) < document.body.offsetHeight)
return
const frames = get(".gallery")
http.onload = event => {
gallery(event)
http.onload = _ => {}
}