-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDLOG.js
4398 lines (3116 loc) · 131 KB
/
DLOG.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
//----------------------------------------------------------------------------------
//---- ______ _____ ______ ----------------------------------------
//---- | \ | | | | ____ ---- HYPERMEDIA WEBLOG AND SOCIAL ----
//---- |_____/ |_____ |_____| |_____| ---- FEED FOR THE "http", "hyper", ----
//---- ---- AND "file" URI SCHEMES ----
//----------------------------------------------------------------------------------
(function() { // All the code is in an unnamed function to avoid unintentionally making things global
// Export with a single line like exporty({function1, function2});
function exporty(o) { var exportToGlobal = true; // Set true to not have to import stuff
var d; // Destination object where we'll attach all the exports
if (exportToGlobal) d = window; // Simple and works everywhere
if (!d && module && module.exports) d = module.exports; // Node
if (!d) d = window; // Fall back to global
for (k in o) { if (d[k]) logError("exporty overwriting", { exists: o[k], incoming: d[k] }); d[k] = o[k]; }
}
//tiny tests
var tests = [];
var assertionsPassed, assertionsFailed, testsThrew;
function test(f) {
tests.push(f);
}
function ok(assertion) {
if (assertion) {
assertionsPassed++;
} else {
assertionsFailed++;
console.error("Test not ok, second line number expanded below:");
}
}
function runTests() {
var g = String.fromCodePoint(0x2705); // Green check emoji
var r = String.fromCodePoint(0x274C); // Red X
var a = String.fromCodePoint(0x1F815); // Up arrow
assertionsPassed = 0;
assertionsFailed = 0;
testsThrew = 0;
for (var i = 0; i < tests.length; i++) {
try { tests[i](); } catch (e) { testsThrew++; console.error(e); }
}
if (assertionsFailed || testsThrew) {
console.error(`${r} ${a}${a}${a} Tests failed ${a}${a}${a} ${r}`);
} else {
console.log(`${g} ${assertionsPassed} assertions in ${tests.length} tests all passed ${g}`);
}
}
exporty({test, ok, runTests});
var noop = (function(){});
exporty({noop});
// _ _
// | | | | ___ _ __ ___ ___
// | |_| |/ _ \| '_ ` _ \ / _ \
// | _ | (_) | | | | | | __/
// |_| |_|\___/|_| |_| |_|\___|
//
var homePage = registerComponent({
name: "homePage",
template: `<homeTag :m="m" />`,
watch: { $route(to, from) { this.m = homeTag.make(); } },
data() { keepPageData(this); return { m: homeTag.make(), remake() { this.m = homeTag.make(); } } }
});
var homeTag = registerComponent({
name: "homeTag",
props: ["m"],
template: `
<div>
</div>
`,
make() {
var empty = {};
try {
var m = {};
//redirect to the title of this installation /posts if exported or not ours, or /feed if editable in beaker
editRoute(`/${brand}/${lookupTitle(hash1()).full}/${isEditable() ? "feed" : "posts"}`);
return m;
} catch (e) { logError("homeTag make", {e}); return empty; }
}
});
var topTag = registerComponent({
name: "topTag",
props: ["m"],
template: `
<div>
<installTag :m="m.installTagModel" />
<myTag :m="m.myTagModel" />
<titleTag :m="m.titleTagModel" />
</div>
`,
make() {
var empty = {
installTagModel: installTag.make(),
myTagModel: myTag.make(),
titleTagModel: titleTag.make(),
navigationTagModel: navigationTag.make(),
};
try {
var m = {};
m.installTagModel = installTag.make();
m.myTagModel = myTag.make();
m.titleTagModel = titleTag.make();
m.navigationTagModel = navigationTag.make();
simpleGather(getRouteSegments().hash2, true);//gather strong from hash2 navigate
return m;
} catch (e) { logError("topTag make", {e}); return empty; }
}
});
var navigationTag = registerComponent({
name: "navigationTag",
props: ["m"],
template: `
<div>
<p>
<i>~ navigation tag ~</i>
<router-link to="/">Root</router-link>
<router-link :to="m.home">Home</router-link>
<router-link :to="m.feed">Feed</router-link>
<router-link :to="m.posts">Posts</router-link>
-
<router-link to="/demo">Demo</router-link>
<router-link to="/fresh">Fresh</router-link>
<router-link to="/mill">Mill</router-link>
-
<router-link :to="m.settings">Settings</router-link>
<router-link :to="m.follows">Follows</router-link>
<router-link :to="m.write">Write</router-link>
<router-link :to="m.about">About</router-link>
<router-link :to="m.system">System</router-link>
</p>
<p>
<i>~ navigation tag ~</i>
Feeds:
<router-link :to="m.feedA">feed A</router-link>
<router-link :to="m.feedB">feed B</router-link>
<router-link :to="m.feedI">feed I</router-link>,
posts:
<router-link :to="m.postsA">posts A</router-link>
<router-link :to="m.postsB">posts B</router-link>
<router-link :to="m.postsI">posts I</router-link>,
and likes:
<router-link :to="m.likesA">likes A</router-link>
<router-link :to="m.likesB">likes B</router-link>
<router-link :to="m.likesI">likes I</router-link>
</p>
</div>
`,
make() {
var empty = {
home: "/BLOG/b10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10g/",
feed: "/BLOG/b10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10g/feed",
posts: "/BLOG/b10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10g/posts",
settings: "/BLOG/b10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10g/settings",
follows: "/BLOG/b10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10g/follows",
write: "/BLOG/b10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10g/write",
about: "/BLOG/b10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10g/about",
system: "/BLOG/b10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10g/system",
feedA: "/BLOG/b10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10g/feed",
feedB: "/BLOG/b10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10g/feed",
feedI: "/BLOG/b10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10g/feed",
postsA: "/BLOG/b10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10g/posts",
postsB: "/BLOG/b10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10g/posts",
postsI: "/BLOG/b10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10g/posts",
likesA: "/BLOG/b10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10g/likes",
likesB: "/BLOG/b10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10g/likes",
likesI: "/BLOG/b10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10gb10g/likes"
};
try {
var m = {};
m.home = `/${brand}/${lookupTitle(hash1()).full}/${isEditable() ? "feed" : "posts"}`;
m.feed = `/${brand}/${lookupTitle(hash1()).full}/feed`;
m.posts = `/${brand}/${lookupTitle(hash1()).full}/posts`;
m.settings = `/${brand}/${lookupTitle(hash1()).full}/settings`;
m.follows = `/${brand}/${lookupTitle(hash1()).full}/follows`;
m.write = `/${brand}/${lookupTitle(hash1()).full}/write`;
m.about = `/${brand}/${lookupTitle(hash1()).full}/about`;
m.system = `/${brand}/${lookupTitle(hash1()).full}/system`;
m.feedA = `/${brand}/8419b15dd118174d596d51027a97e85a49537bdc4ade330b503e588479ce847d/feed`;
m.feedB = `/${brand}/2e3d57995640fcb03103ec7c38f5f6aed8689e49395d0629c5b2e29cbba31925/feed`;
m.feedI = `/${brand}/67faabfa405ba9c578f26fcbde53a472ab5ff9651f4ed961c949c8dd960c98e9/feed`;
m.postsA = `/${brand}/8419b15dd118174d596d51027a97e85a49537bdc4ade330b503e588479ce847d/posts`;
m.postsB = `/${brand}/2e3d57995640fcb03103ec7c38f5f6aed8689e49395d0629c5b2e29cbba31925/posts`;
m.postsI = `/${brand}/67faabfa405ba9c578f26fcbde53a472ab5ff9651f4ed961c949c8dd960c98e9/posts`;
m.likesA = `/${brand}/8419b15dd118174d596d51027a97e85a49537bdc4ade330b503e588479ce847d/likes`;
m.likesB = `/${brand}/2e3d57995640fcb03103ec7c38f5f6aed8689e49395d0629c5b2e29cbba31925/likes`;
m.likesI = `/${brand}/67faabfa405ba9c578f26fcbde53a472ab5ff9651f4ed961c949c8dd960c98e9/likes`;
return m;
} catch (e) { logError("homeTag make", {e}); return empty; }
}
});
var notFoundPage = registerComponent({
name: "notFoundPage",
template: `
<div>
<p>Not Found</p>
<p>Please check the URL, go back, or return <router-link to="/">Home</router-link>.</p>
</div>
`
});
exporty({homePage, homeTag, topTag, navigationTag, notFoundPage});
//navigate to a new page like the user clicked a link
function navigateBrowser(url) { window.location.href = url; }//to another page, like refresh, back goes back
function navigateRoute(route) { router.push(route); }//to another route, no refresh, back goes back
function editRoute(route) { router.replace(route); }//to another route, no refresh, back goes back twice
exporty({navigateBrowser, navigateRoute, editRoute});
var myTag = registerComponent({
name: "myTag",
props: ["m"],
template: `
<p v-if="m.show">
My {{ m.brand }} <i>{{ m.title }}</i>
<router-link :to="m.feed">My Feed</router-link>
<router-link :to="m.posts">My Posts</router-link>
<router-link :to="m.write">Write New Post</router-link>
<router-link :to="m.follows">Blogs I'm Following</router-link>
<router-link :to="m.settings">Settings</router-link>
</p>
`,
make() {
var empty = { show: false, brand: "", title: "", feed: "", posts: "", write: "", follows: "", settings: "" };
try {
if (!isEditable()) return empty;
var m = {};
m.show = true;
m.brand = brand;
m.title = lookupTitle(hash1()).short;
m.feed = `/${brand}/${lookupTitle(hash1()).full}/feed`;
m.posts = `/${brand}/${lookupTitle(hash1()).full}/posts`;
m.write = `/${brand}/${lookupTitle(hash1()).full}/write`;
m.follows = `/${brand}/${lookupTitle(hash1()).full}/follows`;
m.settings = `/${brand}/${lookupTitle(hash1()).full}/settings`;
return m;
} catch (e) { logError("myTag make", {e}); return empty; }
}
});
exporty({myTag});
// _____ _
// | ___| __ ___ ___| |__
// | |_ | '__/ _ \/ __| '_ \
// | _|| | | __/\__ \ | | |
// |_| |_| \___||___/_| |_|
//
var freshPage = registerComponent({
name: "freshPage",
template: `<freshTag :m="m" />`,
watch: { $route(to, from) { this.m = freshTag.make(); } },
data() { keepPageData(this); return { m: freshTag.make(), remake() { this.m = freshTag.make(); } } }
});
var freshTag = registerComponent({
name: "freshTag",
props: ["m"],
template: `
<div>
<topTag :m="m.topTagModel" />
<h1>Fresh</h1>
<p><i>~ fresh tag ~</i></p>
<button @click="m.snippet">Snippet</button>
<p>fresh tag made on "{{ m.stamp }}" and has message: "{{ m.message }}"</p>
<p>Count {{ m.count }} <button @click="m.increment">Increment</button></p>
</div>
`,
make() {
var empty = {
topTagModel: topTag.make(),
snippet: noop, stamp: "", message: "", count: 0, increment: noop
};
try {
var m = {};
m.topTagModel = topTag.make();
m.stamp = moment(now()).format("YYYY MMMM Do, h:mm:ss.SSS a");
m.message = "starting message";
m.count = 0;
m.increment = function() { m.count++; }
m.snippet = function() {
log("fresh snippet");
};
return m;
} catch (e) { logError("freshTag make", {e}); return empty; }
}
});
exporty({freshPage, freshTag});
// ____
// | _ \ ___ _ __ ___ ___
// | | | |/ _ \ '_ ` _ \ / _ \
// | |_| | __/ | | | | | (_) |
// |____/ \___|_| |_| |_|\___/
//
var demoPage = registerComponent({
name: "demoPage",
template: `<demoTag :m="m" />`,
watch: { $route(to, from) { this.m = demoTag.make(); } },
data() { keepPageData(this); return { m: demoTag.make(), remake() { this.m = demoTag.make(); } } }
});
var demoTag = registerComponent({
name: "demoTag",
props: ["m"],
template: `
<div>
<topTag :m="m.topTagModel" />
<h1>Demo</h1>
<p><i>~ demo tag ~</i></p>
<p>
<button @click="m.addRow">Add Row</button>
<button @click="m.remove1">Remove Start</button>
<button @click="m.remove2">Remove Middle</button>
<button @click="m.remove3">Remove End</button>,
Sort
<button @click="m.sort1">Count ▲</button>
<button @click="m.sort2">Count ▼</button>
<button @click="m.sort3">Note ▲</button>
<button @click="m.sort4">Note ▼</button>
</p>
<rowTag v-for="(n, index) in m.a" :key="n.id" :m="n" :i="index"></rowTag>
</div>
`,
make() {
var empty = {
topTagModel: topTag.make(),
a: [], addRow: noop,
remove1: noop, remove2: noop, remove3: noop,
sort1: noop, sort2: noop, sort3: noop, sort4: noop
};
try {
var m = {
topTagModel: topTag.make(),
a: [],
addRow() { m.a.push(rowTag.make(m, "")); },
remove1() { m.a.splice(0, 1); },
remove2() { m.a.splice(m.a.length / 2, 1); },
remove3() { m.a.splice(m.a.length - 1, 1); },
sort1() { m.a.sort(function(a, b) { return a.count - b.count; }); },
sort2() { m.a.sort(function(a, b) { return b.count - a.count; }); },
sort3() { m.a.sort(function(a, b) { return a.note.localeCompare(b.note); }); },
sort4() { m.a.sort(function(a, b) { return b.note.localeCompare(a.note); }); },
};
return m;
} catch (e) { logError("demoTag make", {e}); return empty; }
}
});
var rowTag = registerComponent({
name: "rowTag",
props: ["m", "i"],
template: `
<div>
index{{ i }}:
Row {{ m.id }},
Count {{ m.count }} <button @click="m.increment">Increment</button>,
Note <input type="text" v-model="m.note" @input="m.noteInput" placeholder="~ write here ~" /> len{{ m.note.length }},
<button @click="m.remove(i)">Remove</button>
</div>
`,
make(up, message) {
var empty = { up: up, id: idn(), count: 0, note: "", increment: noop, remove: noop, noteInput: noop };
try {
var m = {
up: up,
id: idn(),
count: 0,
note: message,
increment() { m.count++; },
remove(i) { m.up.a.splice(i, 1); },
noteInput() { /*log("note input: " + m.note);*/ }
};
return m;
} catch (e) { logError("rowTag make", {e}); return empty; }
}
});
exporty({demoPage, demoTag, rowTag});
// ____ _ _ _ _
// | _ \ ___ ___| |_ | | (_)___| |_
// | |_) / _ \/ __| __| | | | / __| __|
// | __/ (_) \__ \ |_ | |___| \__ \ |_
// |_| \___/|___/\__| |_____|_|___/\__|
//
var listPage = registerComponent({
name: "listPage",
template: `<div><listTag :m="m" /></div>`,
watch: { $route(to, from) { this.m = listTag.make(); } },
data() { keepPageData(this); return { m: listTag.make(), remake() { this.m = listTag.make(); } } }
});
var listTag = registerComponent({
name: "listTag",
props: ["m"],
template: `
<div>
<topTag :m="m.topTagModel" />
<p v-if="m.showPosts && m.ours">
Showing your posts.
Also check out the <router-link :to="m.targetFollow">blogs you follow</router-link>.
</p>
<p v-if="m.showPosts && !m.ours">
Showing <i>{{ m.title }}</i>'s posts.
Also check out the <router-link :to="m.targetFollow">blogs <i>{{ m.title }}</i> follows</router-link>.
</p>
<p v-if="m.showFeed && m.ours">
Showing your feed,
including <router-link :to="m.targetPosts">your posts</router-link>
and posts from <router-link :to="m.targetFollow">blogs you follow</router-link>.
</p>
<p v-if="m.showFeed && !m.ours">
Showing <i>{{ m.title }}</i>'s feed,
including <router-link :to="m.targetPosts"><i>{{ m.title }}</i>'s posts</router-link>
and posts from <router-link :to="m.targetFollow">blogs <i>{{ m.title }}</i> follows</router-link>.
</p>
<p>
<span v-if="!m.countNewer" class="ghost">{{ m.labelNewer }}</span><router-link v-if="m.countNewer" :to="m.routeNewer" class="ghost">{{ m.labelNewer }}</router-link>{{ m.linkSeparator }}
<span v-if="!m.countOlder" class="ghost">{{ m.labelOlder }}</span><router-link v-if="m.countOlder" :to="m.routeOlder" class="ghost">{{ m.labelOlder }}</router-link>
</p>
<postTag v-for="(n, index) in m.a" :key="n.key" :m="n" :i="index"></postTag>
<p>
<span v-if="!m.countNewer" class="ghost">{{ m.labelNewer }}</span><router-link v-if="m.countNewer" :to="m.routeNewer" class="ghost">{{ m.labelNewer }}</router-link>{{ m.linkSeparator }}
<span v-if="!m.countOlder" class="ghost">{{ m.labelOlder }}</span><router-link v-if="m.countOlder" :to="m.routeOlder" class="ghost">{{ m.labelOlder }}</router-link>
</p>
</div>
`,
make() {
var empty = {
topTagModel: topTag.make(), gatherTagModel: gatherTag.make(),
ours: false, showPosts: false, showFeed: false,
title: "",
targetPosts: "", targetFollow: "",
countNewer: 0, countOlder: 0,
labelNewer: "", labelOlder: "",
routeNewer: "", routeOlder: "",
linkSeparator: "",
a: []
};
try {
var m = {};
m.topTagModel = topTag.make();
m.gatherTagModel = gatherTag.make();
var s = getRouteSegments();//split the route that led us here into segments
var postsPerPage = validPostsPerPage(store.settings.header.reader.postsPerPage);//customizable in settings
//the end of the route might have an action, like "~~andfollow" or "~~andlike.somepostid"
if (hasText(s.action)) {
log("found action", action);
}
//compose a line of text for the top of the page that attempts to explain what the hell is going on
m.ours = isEditable() && s.hash2 == hash1();
m.showPosts = s.s3 == "posts";
m.showFeed = s.s3 == "feed";
m.title = lookupTitle(s.hash2).short;
m.targetPosts = `/${brand}/${lookupTitle(s.hash2).full}/posts`;
m.targetFollow = `/${brand}/${lookupTitle(s.hash2).full}/follows`;
//show most recent posts
if ((s.s3 == "posts" || s.s3 == "feed") && s.s4 == "") {//blank to show recent posts
var f = listFilter(store.ticks.a, r => messageFilter(r, s)); if (!f.length) return empty;//filter
var p = paginateRecent(f, postsPerPage);//paginate
m.a = listMake(f, p.indexC, p.indexD, s);//render
m.countNewer = p.postsNewer;//link
m.countOlder = p.postsOlder;
m.labelNewer = "";
m.linkSeparator = "";
m.labelOlder = things(p.pagesOlder, "Page")+" with "+things(p.postsOlder, "Older Post")+" >";
m.routeNewer = "";
m.routeOlder = p.hasEF ? `/${brand}/${lookupTitle(s.hash2).full}/${s.s3}/page/${p.tickE}-${p.tickF}` : "";
m.labelPage = "most recent posts";
//show a page of posts between two tick counts
} else if ((s.s3 == "posts" || s.s3 == "feed") && s.s4 == "page") {
var c = cut(s.s5, "-");//check
var tickC = toIntCheck(c.before, 0);
var tickD = toIntCheck(c.after, 0);
var f = listFilter(store.ticks.a, r => messageFilter(r, s)); if (!f.length) return empty;//filter
var p = paginateRange(f, postsPerPage, tickC, tickD);//paginate
m.a = listMake(f, p.indexC, p.indexD, s);//render
m.countNewer = p.postsNewer;//link
m.countOlder = p.postsOlder;
m.labelNewer = "< "+things(p.pagesNewer, "Page")+" with "+things(p.postsNewer, "Newer Post");
m.linkSeparator = " - ";
m.labelOlder = things(p.pagesOlder, "Page")+" with "+things(p.postsOlder, "Older Post")+" >";
m.routeNewer = p.hasAB ? `/${brand}/${lookupTitle(s.hash2).full}/${s.s3}/page/${p.tickA}-${p.tickB}` : "";
m.routeOlder = p.hasEF ? `/${brand}/${lookupTitle(s.hash2).full}/${s.s3}/page/${p.tickE}-${p.tickF}` : "";
m.labelPage = "tick range of posts";
//show a single post
} else if ((s.s3 == "posts" || s.s3 == "feed") && s.s4 == "post") {
checkText(s.s5);//check
var f = listFilter(store.ticks.a, r => messageFilter(r, s)); if (!f.length) return empty;//filter
var i = listFind(f, r => r.uid == s.s5); if (i == -1) return empty;//find
m.a = listMake(f, i, i, s);//render
m.countNewer = i;//link
m.countOlder = f.length - i - 1;
m.labelNewer = "< "+things(m.countNewer, "Newer Post");
m.linkSeparator = " - ";
m.labelOlder = things(m.countOlder, "Older Post")+" >";
m.routeNewer = (i > 0) ? `/${brand}/${lookupTitle(s.hash2).full}/${s.s3}/post/${f[i - 1].uid}` : "";
m.routeOlder = (i < f.length - 1) ? `/${brand}/${lookupTitle(s.hash2).full}/${s.s3}/post/${f[i + 1].uid}` : "";
m.labelPage = "single post";
} else { return empty; }
//TODO here's where you would list all the posts liked by a blog, identified with a tag, and more stuff like that
return m;
} catch (e) { logError("listTag make", {e}); return empty; }
}
});
//a single post
var postTag = registerComponent({
name: "postTag",
props: ["m", "i"],
template: `
<div>
<p>
<router-link :to="m.targetPost">post {{ m.labelPost }}</router-link>
<router-link :to="m.targetAuthor">by <i>{{ m.labelAuthor }}</i></router-link>
<span class="leafy" v-if="m.showFollow" @click="m.clickFollow">{{ m.labelFollow }}</span>
<span v-if="m.showUnfollowConfirm">Stop following <i>{{ m.labelAuthor }}</i>?
<span class="leafy" @click="m.clickUnfollowCancel">No.</span>
<span class="flame" @click="m.clickUnfollowConfirmed">Yes, unfollow.</span>
</span>
<router-link class="leafy" v-if="m.showEdit" :to="m.targetEdit">Edit</router-link>
</p>
<div>{{ m.body }}</div>
</div>
`,
make(r, s) {
var empty = {
key: "",
targetPost: "", targetAuthor: "", targetEdit: "",
labelPost: "", labelAuthor: "", labelFollow: "",
showFollow: false, showUnfollowConfirm: false, showEdit: false,
clickFollow: noop, clickUnfollowCancel: noop, clickUnfollowConfirmed: noop,
date: "", title: "", body: ""
};
try {
var m = {};
m.key = r.uid;
//permalink
m.targetPost = `/${brand}/${lookupTitle(s.hash2).full}/${s.s3}/post/${r.uid}`;
m.labelPost = sayDatePageShort(r.tick);
//edit
m.showEdit = isEditable() && r.source == hash1();//running editable and this post authored by us
m.targetEdit = `/${brand}/${lookupTitle(s.hash2).full}/edit/${r.uid}`;
//all the post author's posts
m.labelAuthor = lookupTitle(r.source).short;
m.targetAuthor = `/${brand}/${lookupTitle(r.source).full}/posts`;
m.showFollow = false;
m.showUnfollowConfirm = false;
m.labelFollow = "";
m.clickFollow = noop;
m.clickUnfollowCancel = noop;
m.clickUnfollowConfirmed = noop;
//follow
if (isEditable() && r.source != hash1()) {//running our installation looking at someone else's post
//show Follow/Unfollow
//click edits settings directly
m.showFollow = true;
if (store.settings.follows[r.source]) {
m.labelFollow = "Following";
m.clickFollow = function() { m.showUnfollowConfirm = true; };
m.clickUnfollowCancel = function() { m.showUnfollowConfirm = false; };
m.clickUnfollowConfirmed = directUnfollow;
} else {
m.labelFollow = "Follow";
m.clickFollow = directFollow;
}
} else if (isBeaker() && !isEditable()) {//beaker browsed to someone else's installation, so we can do an install, but can't know if the author of this post is someone we already follow or even us
//show Follow
//click does install and redirect
m.showFollow = true;
m.labelFollow = "Follow";
m.clickFollow = linkFollow;
}
async function directFollow() {
addFollow(r.source);
remake();
}
async function directUnfollow() {
removeFollow(r.source);
remake();
}
async function linkFollow() {
var r = await findOrMakeInstallDrive();
if (r.driveHash) navigateBrowser(`${driveHashToHyperUrl(r.driveHash)}#${getRouteSegments().path}~~andfollow`);
}
m.date = sayDatePageLong(r.tick);
m.title = r.message.header.title;
var body = linesToString(r.message.bodyLines);
m.body = body;
//TODO here's where you would do markdownToHTML, you think
return m;
} catch (e) { logError("postTag make", {e}); return empty; }
}
});
exporty({listPage, listTag, postTag});
//a new list with just the messages in list that the given function says true to
function listFilter(l, filterFunction) {
var filtered = [];
l.forEach(r => { if (filterFunction(r)) filtered.push(r); });
return filtered;
}
//true if we should include this message in the list we're going to let the user click through
function messageFilter(r, s) {
if (r.message.header.type == "post") {//the message we're looking at describes a post
if (s.hash2 == r.message.header.source) {//the route is navigated to the author of this post
return true;
} else if (s.s3 == "feed" &&//we're preapring a feed, so also include posts by blogs this one follows, and
getSettings(s.hash2).follows[r.message.header.source]) {//this blog follows the source of this message
return true;
} else {
return false;
}
} else {//the message we're looking at is something else
return false;//exclude for now
}
//TODO much more to do here later for reposts, likes, and notes
}
//index of the first item in list that the given function says true to
function listFind(f, findFunction) {
for (var i = 0; i < f.length; i++) { if (findFunction(f[i])) return i; }
return -1;//not found, your function hated everything
}
//given an array of messages f, render vue components for indices p through q
function listMake(f, p, q, s) {
var rendered = [];
for (var i = p; i <= q; i++) rendered.push(postTag.make(f[i], s));
return rendered;
}
// __ __ _ _ _ _____ _ _ _
// \ \ / / __(_) |_ ___ __ _ _ __ __| | | ____|__| (_) |_
// \ \ /\ / / '__| | __/ _ \ / _` | '_ \ / _` | | _| / _` | | __|
// \ V V /| | | | || __/ | (_| | | | | (_| | | |__| (_| | | |_
// \_/\_/ |_| |_|\__\___| \__,_|_| |_|\__,_| |_____\__,_|_|\__|
//
var editPage = registerComponent({
name: "editPage",
template: `<editTag :m="m" />`,
watch: { $route(to, from) { this.m = editTag.make(); } },
data() { keepPageData(this); return { m: editTag.make(), remake() { this.m = editTag.make(); } } }
});
var editTag = registerComponent({
name: "editTag",
props: ["m"],
template: `
<div>
<topTag :m="m.topTagModel" />
<p><textarea v-model="m.body" :placeholder="m.prompt"></textarea></p>
<div>
<button :disabled="!m.enableSave" @click="m.save">Save</button>
<span v-if="m.showDelete">
<button @click="m.delete">Delete</button>
<span v-if="m.showDeleteConfirm">Are you sure?
<span class="leafy" @click="m.deleteCancel">No, nevermind.</span>
<span class="flame" @click="m.buttonDelete">Yes, I'm sure, Delete.</span>
</span>
</span>
</div>
</div>
`,
make() {
var empty = {
topTagModel: topTag.make(), prompt: "",
enableSave: false,
title: "", body: "",
save: noop,
showDelete: false, delete: noop,
showDeleteConfirm: false, deleteCancel: noop, buttonDelete: noop,
snippet: noop,
};
try {
var m = {};
m.topTagModel = topTag.make();
//get information from the address bar
var s = getRouteSegments();
var post;
if (hasText(s.params.segmentPost)) post = store.uids[s.params.segmentPost];
m.title = "";
m.body = "";
m.showDelete = false;
m.showDeleteConfirm = false;
//write a new post
if (s.s3 == "write") {
m.prompt = "Your message to hyperspace";
m.enableSave = isEditable();
m.save = buttonSave;
//edit an existing post that's in the store
} else if (s.s3 == "edit" && post) {
m.prompt = "";
m.enableSave = isEditable();
m.save = buttonEdit;
m.title = validNote(post.message.header.title);
m.body = linesToString(post.message.bodyLines);
m.showDelete = true;
//we're being asked to edit a post that's not in the store yet
} else if (s.s3 == "edit") {
m.prompt = "Searching for this post to edit...";
m.enableSave = false;
m.save = noop;
log("asked to edit, but not in store yet");
//neither write nor edit
} else { return empty; }
async function buttonSave() {
var uid = await editSave(m.title, m.body);
editRoute(`/${brand}/${lookupTitle(hash1()).full}/edit/${uid}`);//make it so back goes to edit
navigateRoute(`/${brand}/${lookupTitle(hash1()).full}/posts/post/${uid}`);//and show rendered
}
async function buttonEdit() {
await editEdit(m.title, m.body, post);
navigateRoute(`/${brand}/${lookupTitle(hash1()).full}/posts/post/${s.params.segmentPost}`);//show rendered
}
m.delete = function() { m.showDeleteConfirm = true; };
m.deleteCancel = function() { m.showDeleteConfirm = false; }
m.buttonDelete = async function() {
await editDelete(post);
editRoute(`/${brand}/${lookupTitle(hash1()).full}/write`);//change where we are now to write
navigateRoute(`/${brand}/${lookupTitle(hash1()).full}/${isEditable() ? "feed" : "posts"}`);//and go home, so back goes back to write instead to editing a post that doesn't exist anymore
}
m.snippet = async function() {
log("write snippet");
};
return m;
} catch (e) { logError("editTag make", {e}); return empty; }
}
});
exporty({editPage, editTag});
//save a new post from the information the user wrote in the form
async function editSave(title, areaBlock) {
title = validNote(title);//step1: prepare text
var bodyLines = areaBlockToBodyLines(areaBlock);
var header = {};//step2: create header
header.type = "post";
header.source = hash1();
if (hasText(title)) header.title = title;//step3: set title
var t = now();//step4: set time and choose unique post identifier
header.date = sayDateDataLong(t);
header.uid = "p"+start(header.source, 8)+"-"+sayDateDataShort(t)+"-"+randomBase16(8);
var message = createMessage(header, bodyLines);//step5: create message
var fileBlock = messagesToFileBlock([message]);//step6: save message in a file in the hyperdrive here
var path = "hyper://"+header.source+"/posts/"+header.uid+".js";
await writeFile(path, fileBlock);
messageFromRunningPage(message);//step7: add message to store
return header.uid;//step8: return the identifier we chose
}
//edit and existing post with an edited title and body text
async function editEdit(title, areaBlock, post) {//note we do not (and could not easily) change the tick or uid
title = validNote(title);//step1: prepare edited text
var bodyLines = areaBlockToBodyLines(areaBlock);
delete post.message.header.title;//step3: set title, edit the object right in the store
if (hasText(title)) post.message.header.title = title;
post.message = createMessage(post.message.header, bodyLines);//step5: remake the message in the store
var fileBlock = messagesToFileBlock([post.message]);//step6: overwrite the message's file in the hyperdrive here
var path = "hyper://"+post.message.header.source+"/posts/"+post.message.header.uid+".js";
await writeFile(path, fileBlock);//will overwrite and we want it to
}
//delete the post from the disk and store
async function editDelete(post) {
//delete post from store.ticks
function findInStoreTicksByUid(uid) {
for (var i = 0; i < store.ticks.a.length; i++) {//not the fastest, but ok because delete should be uncommon
if (store.ticks.a[i].message.header.uid == uid) return i;
}
toss("can't find post uid to delete", {post, uid});
}
var i = findInStoreTicksByUid(post.message.header.uid);//find the index where post appears in store.ticks.a
store.ticks.a.splice(i, 1);//we're editing the array inside a SortedArray directly, but should be ok?
//delete post from store.uids
if (!store.uids[post.message.header.uid]) toss("cant find post to delete in store.uids", {post, i});
delete store.uids[post.message.header.uid];
//TODO so that's all pretty cavalier; and while you think that's all of it now, what about after media, likes and tags?