forked from SiTLar/pyt-vanilla-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.js
1673 lines (1621 loc) · 61.4 KB
/
draw.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";
define("./draw", [],function(){
function _Drawer(v){
this.cView = v;
}
_Drawer.prototype = {
constructor: _Drawer
,"writeAllLikes":function(id,nodeLikes){
var cView = this.cView;
var post = cView.doc.getElementById(id).rawData;
var context = cView.contexts[post.domain];
nodeLikes.innerHTML = "";
var nodeLike = cView.doc.createElement("span");
nodeLike.className = "p-timeline-user-like";
for(var like = 0; like < post.likes.length; like++){
var nodeCLike = nodeLike.cloneNode();
nodeCLike.innerHTML = context.gUsers[post.likes[like]].link;
//nodeLikes.childNodes[idx].appendChild(nodeCLike);
nodeLikes.appendChild(nodeCLike);
}
var suffix = cView.doc.createElement("span");
suffix.innerHTML = " liked this";
//nodeLikes.childNodes[idx].appendChild(suffix);
nodeLikes.parentNode.appendChild(suffix);
}
,"genLikes":function(nodePost){
var cView = this.cView;
var post = nodePost.rawData;
var context = cView.contexts[post.domain];
var postNBody = nodePost.cNodes["post-body"];
var node = cView.doc.createElement("div");
node.className = "likes";
postNBody.cNodes["post-info"].replaceChild(node, postNBody.cNodes["post-info"].cNodes["likes"]);
postNBody.cNodes["post-info"].cNodes["likes"] = node;
if(!Array.isArray(post.likes) || !post.likes.length ) return;
postNBody.cNodes["post-info"].cNodes["likes"].appendChild(cView.gNodes["likes-smile"].cloneNode(true));
var nodeLikes = cView.doc.createElement( "span");
/*
var l = post.likes.length;
if(cView.ids){
for (var idx = 0; idx< l;idx++) {
var like = post.likes[idx];
if(like == cView.gMe.users.id){
post.likes.splice(idx,1);
post.likes.unshift(like);
break;
}
}
}
*/
var nodeLike = cView.doc.createElement("span");
nodeLike.className = "p-timeline-user-like";
post.likes.forEach(function(like){
var nodeCLike = nodeLike.cloneNode();
nodeCLike.innerHTML = context.gUsers[like].link;
nodeLikes.appendChild(nodeCLike);
});
var suffix = cView.gNodes["likes-suffix"].cloneAll();
if (post.omittedLikes){
suffix.cNodes["likes-omitted"].hidden = false;
suffix.getElementsByTagName("a")[0].innerHTML = post.omittedLikes + " other people "
}
suffix.className = "nocomma";
postNBody.cNodes["post-info"].cNodes["likes"].appendChild(nodeLikes);
postNBody.cNodes["post-info"].cNodes["likes"].cNodes = new Object();
postNBody.cNodes["post-info"].cNodes["likes"].cNodes["comma"] = nodeLikes;
postNBody.cNodes["post-info"].cNodes["likes"].appendChild(suffix);
//postNBody.cNodes["post-info"].cNodes["likes"].appendChild(suffix);
nodeLikes.className = "comma";
if(context.ids.length && (post.likes[0] == context.gMe.users.id)){
postNBody.cNodes["post-info"].myLike = nodeLikes.childNodes[0];
if( postNBody.cNodes["post-info"].nodeLike) {
postNBody.cNodes["post-info"].nodeLike.innerHTML = "Un-like";
postNBody.cNodes["post-info"].nodeLike.action = false;
}
}
}
,"genUserDetails":function(username, context){
var cView = this.cView;
var user = context.gUsers.byName[username];
var nodeUD = cView.gNodes["user-details"].cloneAll();
var nodeInfo = nodeUD.cNodes["ud-info"];
nodeInfo.cNodes["ud-username"].value = user.username;
nodeInfo.getNode(["c","ud-avatar"],["c","ud-avatar-img"]).src = user.profilePictureMediumUrl;
nodeInfo.getNode(["c","ud-text"],["c","ud-title"]).innerHTML = user.untagScreenName;
if(typeof user.description === "string")
nodeInfo.getNode(["c","ud-text"],["c","ud-desc"])[(cView.readMore?"words":"innerHTML")] = context.digestText(user.description);
//nodeInfo.getNode(["c","ud-text"],["c","ud-desc"]).innerHTML = context.digestText(user.description);
if (user.type == "group")
["uds-subs","uds-likes","uds-com"].forEach(function(key){
nodeInfo.getNode(["c","ud-stats"],["c",key]).style.display = "none";
});
if(typeof user.statistics !== "undefined"){
var stats = {
"uds-subs":user.statistics.subscriptions
,"uds-subsc":user.statistics.subscribers > 0? user.statistics.subscribers:""
,"uds-likes":user.statistics.likes
,"uds-com":user.statistics.comments
}
Object.keys(stats).forEach(function(key){
nodeInfo.getNode(["c","ud-stats"],["c",key],["c","val"]).innerHTML = stats[key];
});
}
return nodeUD;
}
,"genProfile": function(user){
var cView = document.cView;
var context = cView.contexts[user.domain];
var nodeProfile = cView.gNodes["settings-profile"].cloneAll();
nodeProfile.getElementsByClassName("sp-username")[0].innerHTML = "@" + user.username;
nodeProfile.cNodes["chng-avatar"].cNodes["sp-avatar-img"].src = user.profilePictureMediumUrl;
if (typeof user.description !== "undefined")
nodeProfile.cNodes["gs-descr"].value = user.description;
var nodes = nodeProfile.getElementsByTagName("input");
if(typeof user.isProtected === "undefined") user.isProtected = "0";
if(typeof user.isPrivate === "undefined") user.isPrivate = "0";
for(var idx = 0; idx < nodes.length; idx++){
var node = nodes[idx];
switch(node.name){
case "id":
node.value = user.id;
break;
case "token":
node.value = context.logins[user.id].token;
break;
case "is-main":
node.checked = (context.logins[user.id].token == context.token);
node.name = "is-main-"+user.domain;
break;
case "email":
if(typeof user.email !== "undefined" )
node.value = user.email;
break;
case "domain":
node.value = user.domain;
break;
case "screen-name":
node.value = user.screenName;
break;
case "access":
node.name = ["access",user.domain, user.id].join("-");
if(JSON.parse(user.isPrivate)
&&( node.value == "is-private"))
node.checked = true;
else if(JSON.parse(user.isProtected)
&&(node.value == "is-protected"))
node.checked = true;
else if((node.value == "is-public")
&&(!JSON.parse(user.isProtected))
&&(!JSON.parse(user.isPrivate)))
node.checked = true;
break;
}
};
return nodeProfile;
}
,"drawSettings":function(){
var cView = this.cView;
var body = cView.doc.getElementById("container");
body.cNodes["pagetitle"].innerHTML = "Settings";
cView.doc.title = "Settings";
var nodeSettingsHead = cView.gNodes["settings-head"].cloneAll();
body.appendChild(nodeSettingsHead);
switch(cView.doc.location.pathname.split("/").pop()){
case "raw":
cView.addons.pr.then(function(){cView.Drawer.drawRaw(body)});
break;
case "accounts":
drawAcc();
break;
case "addons":
drawAddons();
break;
case "blocks":
drawBlocks();
break;
case "customui":
drawCustomUI();
break;
case "display":
default:
drawDisp();
}
cView.Common.setIcon("favicon.ico");
return cView.Utils._Promise.resolve();
function drawAcc(){
nodeSettingsHead.cNodes["sh-acc"].className = "sh-selected";
var nodeSettings = cView.gNodes["accaunts-settings"].cloneAll();
body.appendChild(nodeSettings);
Object.keys(cView.contexts).forEach(function (domain){
var context = cView.contexts[domain];
context.p.then(function(){
if (context.ids)context.ids.forEach(function(id){
nodeSettings
.cNodes["settings-profiles"]
.appendChild(cView.Drawer.genProfile(context.logins[id].data.users));
});
});
});
}
function drawAddons(){
nodeSettingsHead.cNodes["sh-addons"].className = "sh-selected";
cView.addons.pr.then(function(){
cView.addons.all.forEach(function(addon){
var node = addon.settings();
node.classList.add( "post");
body.appendChild(node);
});
});
}
function drawCustomUI(){
var customFunctions = require("./custom_functions.json");
nodeSettingsHead.cNodes["sh-custom-ui"].className = "sh-selected";
var nodeCtrl = cView.gNodes["custom-ui-settings-page"].cloneAll();
body.appendChild( nodeCtrl);
var host = cView.doc.getElementById("custom-ui-um-display")
var nodeSelect = cView.doc.getElementById("cu-um-function");
var chkBox = cView.doc.getElementById("use-upper");
chkBox.checked = JSON.parse (cView.localStorage.getItem(chkBox.value));
var usedFuncs = new Object();
try{
var scheme = JSON.parse(cView.localStorage.getItem("custom-ui-upper"));
scheme.forEach(function(item){
var nodeItem = cView.gNodes["custom-ui-item"].cloneAll();
var slots = cView.Utils.getInputsByName(nodeItem);
var funcId;
var text = "";
var i = null ;
var node;
Object.keys(item).forEach(function(key){
switch (key){
case "fn":
funcId = item[key];
break;
case "icon":
i = cView.doc.createElement("i");
i.className = "fa fa-2x fa-" + item[key];
slots["val"].value = item[key];
break;
case "text":
text = item[key];
slots["val"].value = item[key];
break;
}
});
if(funcId == "spacer"){
slots["type"].value = "spacer";
nodeItem.cNodes["title"].innerHTML = "•";
host.appendChild(nodeItem);
return;
}
slots["type"].value = funcId;
nodeItem.cNodes["title"].innerHTML = text;
if(i)nodeItem.cNodes["title"].appendChild(i);
nodeItem.title = funcId;
host.appendChild(nodeItem);
usedFuncs[funcId] = true;
});
}catch(e){};
Object.keys(customFunctions).forEach(function(funcId){
var nodeOption = cView.doc.createElement("option");
nodeOption.innerHTML = customFunctions[funcId].descr;
nodeOption.value = funcId;
if (usedFuncs[funcId] === true ) nodeOption.disabled = true;
nodeSelect.appendChild(nodeOption);
});
}
function drawBlocks(){
var lists = cView.blockLists;
nodeSettingsHead.cNodes["sh-blocks"].className = "sh-selected";
var nodeCtrl = cView.gNodes["blocks-settings-page-ctrl"].cloneAll();
cView.Utils.getInputsByName(nodeCtrl)["hideCups"].checked = JSON.parse(
cView.localStorage.getItem("addons-linkcups-hide")
);
body.appendChild( nodeCtrl);
Object.keys(cView.contexts).forEach(function (domain){
var context = cView.contexts[domain];
var page = cView.gNodes["blocks-settings-page"].cloneAll();
page.cNodes["title"].innerHTML = domain;
page.cNodes["domain"].value = domain;
cView.Utils.setChild(
page.cNodes["strings"]
,"content"
,cView.Drawer.genBlockStrPage(domain)
);
var appendUser = function(user){
page.getNode(["c","posts"],["c","content"]).appendChild(genBUser(user, "posts"));
};
Object.keys(lists).forEach(function(type){
var count = 0;
var list = cView.blocks[lists[type]][domain];
if(typeof list !== "undefined") Object.keys(list).forEach(function(id){
page.getNode(["c",type]).hidden = false;
var username;
if(list[id] === true){
var user = context.gUsers[id];
if (typeof user === "undefined"){
count++;
return;
}else username = user.username;
}else username = list[id];
var item = cView.gNodes["blocks-item"].cloneAll(true);
var inputs = cView.Utils.getInputsByName(item);
inputs["type"].value = type;
inputs["val"].value = id;
item.cNodes["title"].innerHTML = "@"+username;
page.getNode(["c",type],["c","content"]).appendChild(item);
});
if(count){
var span = cView.doc.createElement("span");
span.innerHTML = count + " unrecognized users";
page.getNode(["c",type],["c","content"]).appendChild(span);
}
});
body.appendChild(page);
});
cView.Common.updateBlockList();
}
function drawDisp(){
nodeSettingsHead.cNodes["sh-displ"].className = "sh-selected";
var nodeSettings = cView.gNodes["display-settings"].cloneAll();
body.appendChild(nodeSettings);
var mode = cView.localStorage.getItem("display_name");
if (mode == null) mode = "screen";
var theme = cView.localStorage.getItem("display_theme");
if (theme == null) theme = "expanded.css";
var nodes = nodeSettings.getElementsByTagName("input");
for(var idx = 0; idx < nodes.length; idx++){
var node = nodes[idx];
switch(node.type){
case "radio" :
if (( node.name == "display_name") &&(node.value == mode)
|| ( node.name == "display_theme") &&(node.value == theme))
node.checked = true;
else if(node.value == cView.localStorage.getItem(node.name))
node.checked = true;
break;
case "checkbox":
node.checked = JSON.parse (cView.localStorage.getItem(node.value));
break;
}
};
cView.doc.getElementById("rt-chkbox").checked = JSON.parse(cView.localStorage.getItem("rt"));
var bump = JSON.parse(cView.localStorage.getItem("rtbump"));
cView.doc.getElementById("rt-params").hidden = !bump;
cView.doc.getElementById("rt-bump").checked = bump ;
var oRTParams = cView.localStorage.getItem("rt_params");
if (oRTParams != null)
oRTParams = JSON.parse(oRTParams);
["rt-bump-int", "rt-bump-cd", "rt-bump-d"].forEach(function(id){
var node = cView.doc.getElementById(id);
if(oRTParams)node.value = oRTParams[id];
node.parentNode.getElementsByTagName("span")[0].innerHTML = node.value + " minutes";
});
}
}
,"drawSearch":function(search){
var cView = this.cView;
cView.doc.getElementById("container").cNodes["pagetitle"]
.innerHTML = "Search: " + search.query;
cView.doc.title ="Search: " + search.query;
var node = cView.gNodes["search-big"].cloneAll();
cView.Utils.setChild(node, "search-input", cView.gNodes["search-input"].cloneAll());
node.getElementsByTagName("form")[0].target = "_self";
Object.keys(cView.contexts).forEach(function(domain){
var el = cView.gNodes["search-domain"].cloneAll(true);
el.cNodes["i"].checked = (search.domains.indexOf(domain) != -1);
el.cNodes["i"].value = domain;
el.cNodes["s"].innerHTML = domain;
node.getElementsByClassName("search-domains")[0].appendChild(el);
});
cView.Utils.setChild(cView.doc.getElementById("container"), "details", node);
if(search.query == "")return;
cView.Utils.getInputsByName(node)["qs"].value = search.query ;
if(!cView.doc.getElementsByClassName("post").length){
var node = cView.gNodes["nothig-found"].cloneAll();
node.innerHTML += search.query;
cView.doc.posts.appendChild(node);
}else node.removeChild(node.cNodes["search-info"]);
}
,"genMore":function(isLast ){
var cView = this.cView;
var nodeMore = cView.doc.createElement("div");
nodeMore.className = "more-node";
var htmlPrefix = '<a href="' + gConfig.front+cView.fullPath + "?";
if( cView.search != "") htmlPrefix += cView.search+"&";
var htmlForward= "";
var htmlBackward = "";
//var fLastPage = (content.posts.length != cView.offset);
var backward = cView.skip*1 - gConfig.offset*1;
var forward = cView.skip*1 + gConfig.offset*1;
if (cView.skip){
if (backward>=0) htmlBackward = htmlPrefix + "offset="
+ backward*1+ "&limit="+gConfig.offset*1
+ '"><span style="font-size: 120%">←</span> Newer entries</a>';
nodeMore.innerHTML = htmlBackward ;
}
if(!isLast){
htmlForward = htmlPrefix + "offset="
+ forward*1 + "&limit="+gConfig.offset*1
+'">Older entries<span style="font-size: 120%">→</span></a>';
}
if ( (htmlBackward != "") && (htmlForward != "")) nodeMore.innerHTML += '<span class="spacer">—</span>'
nodeMore.innerHTML += htmlForward;
return nodeMore;
}
,"drawTimeline":function(posts,contexts){
var cView = this.cView;
var Drawer = cView.Drawer;
var body = cView.doc.getElementById("container");
var nodeMore = Drawer.genMore(!posts.length);
body.appendChild(nodeMore.cloneNode(true));
cView.doc.posts = cView.doc.createElement("div");
cView.doc.posts.className = "posts";
cView.doc.posts.id = "posts";
body.appendChild(cView.doc.posts);
cView.posts = new Array();
cView.doc.hiddenCount = 0;
var idx = 0;
posts.forEach(function(post){
var nodePost = null;
post.idx = idx++;
if (post.type == "metapost"){
var dups = post.dups.filter(function(post){
return post.isHidden != true;
});
if (dups.length == 1)
nodePost = Drawer.genPost(dups[0]);
else if(dups.length != 0)
nodePost = Drawer.makeMetapost( dups.map(Drawer.genPost, cView));
if (dups.length != post.dups.length) cView.doc.hiddenCount++;
}else if(post.isHidden) cView.doc.hiddenCount++;
else{
post.isHidden = false;
nodePost = Drawer.genPost(post);
}
if(nodePost)cView.doc.posts.appendChild(nodePost);
cView.posts.push({"hidden":post.isHidden,"data":post});
});
var nodeShowHidden = cView.gNodes["show-hidden"].cloneAll();
nodeShowHidden.cNodes["href"].action = true;
body.appendChild(nodeShowHidden);
if(cView.doc.hiddenCount) nodeShowHidden.cNodes["href"].innerHTML= "Show "+ cView.doc.hiddenCount + " hidden entries";
body.appendChild(nodeMore);
/*
var drop = Math.floor(cView.skip/3);
var toAdd = drop + Math.floor(gConfig.offset/3);
if((!gPrivTimeline.done)&& (cView.timeline == "home")&& matrix.ready){
gPrivTimeline.done = true;
new Promise(function (){addPosts(drop,toAdd,0);});
};
*/
}
,"drawPost": function(content,context) {
var cView = this.cView;
var singlePost = cView.Drawer.genPost(content);
var body = cView.doc.getElementById("container");
body.appendChild(singlePost);
var nodesHide = singlePost.getElementsByClassName("hide");
singlePost.hidden = false;
if (nodesHide.length)nodesHide[0].hidden = true;
cView.doc.title = "@"
+ context.gUsers[singlePost.rawData.createdBy].username + ": "
+ singlePost.rawData.body.slice(0,20).trim()
+ (singlePost.rawData.body.length > 20?"\u2026":"" )
+ " ("+ context.domain +")";
}
/*
var nodeRTCtrl = body.getElementsByClassName("rt-controls")[0];
nodeRTCtrl.cNodes["rt-chkbox"].checked = JSON.parse(cView.localStorage.getItem("rt"));
var nodeBump = nodeRTCtrl.cNodes["rt-bump"];
for(var idx = 0; idx<nodeBump.childNodes.length; idx++)
if(nodeBump.childNodes[idx].value == bump){
nodeBump.selectedIndex = idx;
break;
}
if(content.timelines) context.rtSub = {"timeline":[content.timelines.id]};
else context.rtSub = {"post":[content.posts.id]};
*/
,"drawRequests":function(){
var cView = this.cView;
var whoamis = new Array();
Object.keys(cView.contexts).forEach( function (domain){
var context = cView.contexts[domain];
context.ids.forEach(function(id){
whoamis.push(context.getWhoami(context.logins[id].token));
});
});
return cView.Utils._Promise.all(whoamis).then(function(){
var body = cView.doc.getElementById("container");
Object.keys(cView.contexts).forEach( function (domain){
genRequests(cView.contexts[domain]);
});
if (!body.getElementsByClassName("sub-request").length)
body.getElementsByClassName("pagetitle")[0].innerHTML = "No requests";
});
function genRequests(context){
var cView = context.cView;
var body = cView.doc.getElementById("container");
context.ids.forEach(function(loginId){
var login = context.logins[loginId].data;
if(!Array.isArray(login.requests)|| (login.requests.length == 0))
return;
var nodeH = cView.doc.createElement("h2");
nodeH.innerHTML = "@"+login.users.username+" requests";
body.appendChild(nodeH);
var nodeReqs = body.appendChild(cView.gNodes["req-body"].cloneAll());
login.requests.forEach( cView.Common.addUser, context);
login.requests.forEach(function(req){
var node = genReqNode(req, loginId);
if(req.src == loginId){
nodeReqs.cNodes["req-body-sent"].hidden = false;
node.cNodes["sr-ctrl"].hidden = true;
nodeReqs.cNodes["req-body-sent"].appendChild(node);
}else{
nodeReqs.cNodes["req-body-pend"].hidden = false;
nodeReqs.cNodes["req-body-pend"].appendChild(node);
}
});
});
function genReqNode(req, loginId){
var node = cView.gNodes["sub-request"].cloneAll();
var user = context.gUsers[req.id];
node.cNodes["sr-name"].innerHTML = user.link;
/*
'<a href="'
+gConfig.front + "as/"
+context.domain + "/"
+user.username + '">'
+user.screenName
+"</a>"
+" @" + user.username;
*/
if(req.type == "group")
node.cNodes["sr-name"].innerHTML += "<br />to "
+ context.gUsers[req.dest].link;
node.cNodes["sr-avatar"].src = user.profilePictureMediumUrl ;
node.cNodes["sr-user"].value = user.username;
node.cNodes["sr-id"].value = loginId;
node.cNodes["sr-src"].value = req.src;
node.cNodes["sr-dest"].value = req.dest;
node.cNodes["sr-reqid"].value = req.reqid;
node.cNodes["sr-type"].value = req.type;
node.cNodes["sr-domain"].value = context.domain;
return node;
}
}
}
,"drawGroups": function( ){
var cView = this.cView;
var out = cView.doc.createElement("div");
out.className = "subs-cont";
var domains = Object.keys(cView.contexts);
domains.forEach(function(domain){
var context = cView.contexts[domain];
if((context.gMe == null)
|| (typeof context.gMe.users.subscriptions === "undefined") )
return;
var subHead = cView.doc.createElement("h3");
subHead.innerHTML = domain;
out.appendChild(subHead);
var oSubscriptions = new Object();
context.gMe.subscriptions.forEach(function(sub){
oSubscriptions[sub.id] = sub;
});
var nodeGrps = cView.doc.createElement("div");
context.gMe.users.subscriptions.forEach(function(subid){
var sub = oSubscriptions[subid];
var user = context.gUsers[sub.user];
if((user.type == "user")||(sub.name != "Posts"))
return;
var node = cView.gNodes["sub-item"].cloneAll();
var a = node.cNodes["link"];
a.href = gConfig.front+ "as/" + context.domain+ "/" + user.username;
a.cNodes["usr-avatar"].src = user.profilePictureMediumUrl;
a.cNodes["usr-title"].innerHTML = user.title;
nodeGrps.appendChild(node);
});
out.appendChild(nodeGrps);
});
cView.doc.getElementById("container").appendChild(out);
return cView.Utils._Promise.resolve();
}
,"drawFriends": function(content,context){
var cView = context.cView;
var out = cView.gNodes["subs-cont"].cloneAll();
content.subscriptions.forEach(function(sub){
if(sub.name != "Posts")return;
var node = cView.gNodes["sub-item"].cloneAll();
var a = node.cNodes["link"];
var user = context.gUsers[sub.user];
a.href = gConfig.front+ "as/" + context.domain+ "/" + user.username;
a.cNodes["usr-avatar"].src = user.profilePictureMediumUrl;
a.cNodes["usr-title"].innerHTML = user.title;
if(user.type == "user")out.cNodes["sc-users"].appendChild(node);
else out.cNodes["sc-grps"].appendChild(node);
});
cView.doc.getElementById("container").appendChild(out);
}
,"drawSubs": function(content,context){
var cView = context.cView;
var out0 = cView.doc.createElement("div");
var out = cView.doc.createElement("div");
out0.appendChild(out);
out0.className = "subs-cont";
content.subscribers.forEach(function(sub){
var node = cView.gNodes["sub-item"].cloneAll();
var user = context.gUsers[sub.id];
var a = node.cNodes["link"];
a.href = gConfig.front+ "as/" + context.domain+ "/" + user.username;
a.cNodes["usr-avatar"].src = user.profilePictureMediumUrl;
a.cNodes["usr-title"].innerHTML = user.title;
out.appendChild(node);
});
cView.doc.getElementById("container").appendChild(out0);
}
,"genUserPopup": function(node, user){
var cView = this.cView;
var context = cView.contexts[user.domain];
var nodePopup = cView.gNodes["user-popup"].cloneAll(true);
cView.doc.getElementsByTagName("body")[0].appendChild(nodePopup);
nodePopup.id = "userPopup" + context.domain + user.id;
nodePopup.cNodes["up-avatar"].innerHTML = '<img src="'+ user.profilePictureMediumUrl+'" />';
nodePopup.cNodes["up-info"].innerHTML ="<span>@" + user.username + "</span><br>"+ user.link;
if((typeof context.gMe !== "undefined") && (context.ids.indexOf(user.id) == -1))
cView.Utils.setChild(nodePopup, "up-controls", cView.Drawer.genUpControls(user));
if (typeof node.createdAt !== "undefined"){
var spanDate = cView.doc.createElement("span");
spanDate.className = "up-date";
var txtdate = new Date(node.createdAt*1).toString();
spanDate.innerHTML = txtdate.slice(0, txtdate.indexOf("(")).trim();
nodePopup.appendChild(spanDate);
}
return nodePopup;
}
,"genUpControls":function(user){
var cView = this.cView;
var context = cView.contexts[user.domain];
var controls = cView.gNodes["up-controls"].cloneAll();
var subs = controls.cNodes["up-sbs"];
controls.user = user.username;
controls.domain = context.domain;
var isMulti = context.ids.length > 1;
context.ids.forEach(perLogin);
return controls;
function perLogin(id){
if (user.id == id)return;
var login = context.logins[id].data;
var friend = (typeof login.oFriends[user.id] !== "undefined");
var envelop = cView.gNodes["up-c-mu"].cloneAll();
envelop.loginId = id;
if (isMulti) envelop.cNodes["uname"].innerHTML = "@"+login.users.username+": ";
var nodeSub = envelop.cNodes["up-s"];
nodeSub.innerHTML = friend?"Unsubscribe":"Subscribe";
nodeSub.subscribed = friend;
if (!friend && (user.isPrivate == 1 )){
nodeSub.removeEventListener("click",cView["Actions"]["evtSubscribe"]);
var oRequests = new Object();
if (Array.isArray(login.requests)){
login.requests.forEach(function(req){
oRequests[req.id] = req;
});
}
if(Array.isArray(login.users.pendingSubscriptionRequests)
&&login.users.pendingSubscriptionRequests.some(function(a){
return oRequests[a].username == user.username;
})){
nodeSub = cView.Utils.setChild(envelop, "up-s", cView.doc.createElement("span"));
nodeSub.innerHTML = "Subscription request sent";
}else{
nodeSub.innerHTML = "Request subscription";
nodeSub.addEventListener("click", cView["Actions"]["reqSubscription"] );
}
}
controls.cNodes["up-sbs"].getElementsByTagName("ul")[0].appendChild(envelop);
if(friend
&& ((user.type == "group")
|| login.users.subscribers.some(function(sub){ return sub.id == user.id;}))){
envelop.cNodes["up-d"].href = gConfig.front + "filter/direct#"+ user.username;
envelop.cNodes["up-d"].target = "_blank";
}else{
envelop.cNodes["up-d"].hidden = true;
envelop.cNodes["up-d"].previousSibling.hidden = true;
}
var aBan = envelop.cNodes["up-b"];
/*if (user.type == "group"){
aBan.hidden = true;
envelop.cNodes["up-b"].previousSibling.hidden = true;
return;
}
*/
aBan.banned = login.users.banIds.indexOf( user.id) != -1;
if (aBan.banned){
aBan.innerHTML = "Un-ban";
aBan.removeEventListener("click",cView["Actions"]["genBlock"]);
aBan.addEventListener("click", cView["Actions"]["doUnBan"]);
}
}
}
,"regenHides":function(){
var cView = this.cView;
cView.posts.forEach(function(victim,idx){
victim.data.idx = idx;
});
}
,"updateDate":function(node, cView){
node.innerHTML = cView.Utils.relative_time(node.date);
var txtdate = new Date(node.date).toString();
node.title = txtdate.slice(0, txtdate.indexOf("(")).trim();
window.setTimeout(cView.Drawer.updateDate, 30000, node, cView);
}
,"genPost":function(post){
var cView = this.cView;
var Drawer = cView.Drawer;
if (post.isHidden !== true) post.isHidden = false;
var context = cView.contexts[post.domain];
var nodePost = cView.gNodes["post"].cloneAll();
var postNBody = nodePost.cNodes["post-body"];
var user = undefined;
if(post.createdBy) user = context.gUsers[post.createdBy];
nodePost.rtCtrl = new Object();
nodePost.homed = false;
nodePost.rawData = post;
nodePost.id = context.domain + "-post-" + post.id;
nodePost.isPrivate = false;
nodePost.commentsModerated = false;
if( typeof post.body === "string")
//postNBody.cNodes["post-cont"].innerHTML = context.digestText(post.body);
postNBody.cNodes["post-cont"][(cView.readMore?"words":"innerHTML")] = context.digestText(post.body);
var urlMatch ;
nodePost.hidden = cView.Common.chkBlocked(post);
nodePost.gotLock = false;
nodePost.gotShield = false;
if(typeof user !== "undefined"){
nodePost.cNodes["avatar"].cNodes["avatar-h"].innerHTML = '<img src="'+ user.profilePictureMediumUrl+'" />';
nodePost.cNodes["avatar"].cNodes["avatar-h"].userid = user.id;
postNBody.cNodes["title"].innerHTML = Drawer.genTitle(nodePost);
}
var nodeLock = postNBody.getNode(["c","post-info"],["c","post-controls"],["c","post-lock"]);
if(nodePost.gotLock)
nodeLock.innerHTML = "<i class='fa fa-lock icon'> </i>";
else if (nodePost.gotShield)
nodeLock.innerHTML = "<i class='fa fa-shield icon'> </i>";
if(nodePost.direct)
nodeLock.innerHTML += "<i class='fa fa fa-envelope icon'> </i>";
if(Array.isArray(post.attachments)&&post.attachments.length){
var attsNode = postNBody.cNodes["attachments"];
var bFirstImg = true;
post.attachments.forEach(function(att){
var nodeAtt = cView.doc.createElement("div");
var oAtt = context.gAttachments[att];
switch(oAtt.mediaType){
case "image":
var nodeA = cView.doc.createElement("a");
nodeA.target = "_blank";
nodeA.href = oAtt.url;
nodeA.border = "none";
if (typeof oAtt.imageSizes.t !== "undefined")
nodeAtt.t = oAtt.imageSizes.t;
else if(typeof oAtt.imageSizes.o !== "undefined")
nodeAtt.t = oAtt.imageSizes.o;
else nodeAtt.t = {"w":0};
var nodeImg = cView.doc.createElement("img");
/*
var showUnfolder = (post.src === "rt")?
cView.Actions.showUnfolderRt
:cView.Actions.showUnfolder;
*/
var showUnfolder = cView.Actions.showUnfolderRt;
nodeImg.style.height = 0;
nodeAtt.img = nodeImg;
nodeImg.addEventListener("load", showUnfolder);
if(bFirstImg){
nodeImg.src = oAtt.thumbnailUrl;
bFirstImg = false;
}else{
nodeAtt.url = oAtt.thumbnailUrl;
nodeAtt.hidden = true;
}
nodeA.appendChild(nodeImg);
nodeAtt.appendChild(nodeA);
attsNode.cNodes["atts-img"].appendChild(nodeAtt);
nodeAtt.className = "att-img";
break;
case "audio":
nodeAtt.innerHTML = '<audio style="height:40" preload="none" controls><source src="'+oAtt.url+'" ></audio> <br><a href="'+oAtt.url+'" target="_blank" ><i class="fa fa-download"></i> '+oAtt.fileName+'</a>';
nodeAtt.className = "att-audio";
attsNode.cNodes["atts-audio"].appendChild(nodeAtt);
break;
default:
nodeAtt.innerHTML = '<a href="'+oAtt.url+'" target="_blank" ><i class="fa fa-download"></i> '+oAtt.fileName+'</a>';
attsNode.appendChild(nodeAtt);
}
});
}else
if(((urlMatch = post.body.match(/(^|[^!])https?:\/\/[^\s\/$.?#].[^\s]*/i) )!= null)
&&(JSON.parse(cView.localStorage.getItem("show_link_preview")))){
cView.gEmbed.p.then(function(oEmbedPr){
Drawer.embedPreview(oEmbedPr
,urlMatch[0]
,postNBody.cNodes["attachments"]
//);
).then((post.src == "rt")?cView.Utils.unscroll:function(){});
});
}
var anchorDate = cView.doc.createElement("a");
if(typeof user !== "undefined") anchorDate.href = [gConfig.front + "as", context.domain, user.username , post.id].join("/");
postNBody.cNodes["post-info"].cNodes["post-controls"].cNodes["post-date"].appendChild(anchorDate);
anchorDate.date = JSON.parse(post.createdAt);
window.setTimeout(Drawer.updateDate, 10,anchorDate, cView);
if((typeof post.commentsDisabled !== "undefined")
&& (post.commentsDisabled == "1")){
postNBody.getNode(["c","post-info"],["c","post-controls"],["c","cmts-lock-msg"]).hidden = false;
} else post.commentsDisabled = "0";
if(context.ids.length){
var nodeControls;
if (context.ids.indexOf(post.createdBy) != -1){
nodeControls = cView.gNodes["controls-self"].cloneAll();
}else {
nodeControls = cView.gNodes["controls-others"].cloneAll();
postNBody.cNodes["post-info"].nodeLike = nodeControls.cNodes["post-control-like"];
nodeControls.cNodes["post-control-like"].action = true;
if(post.commentsDisabled == "1"){
var nodeCmtControls = nodeControls.getElementsByClassName("cmts-add");
for(var idx = 0; idx < nodeCmtControls.length; idx++){
nodeCmtControls[idx].style.display = "none";
nodeCmtControls[idx].nextSibling.style.display = "none";
}
}
}
nodeControls.className = "controls";
var aHide = nodeControls.cNodes["hide"];
//aHide.className = "hide";
aHide.innerHTML = post.isHidden?"Un-hide":"Hide";
aHide.action = !post.isHidden;
postNBody.cNodes["post-info"].cNodes["post-controls"].appendChild( nodeControls);
postNBody.cNodes["post-info"].cNodes["post-controls"].cNodes["controls"] = nodeControls;
//postNBody.cNodes["post-info"].cNodes["post-controls"].nodeHide = aHide;
}
if (post.likes) Drawer.genLikes(nodePost );
if (post.comments){
if(post.omittedComments){
if(post.comments[0])
postNBody.cNodes["comments"].appendChild(Drawer.genComment.call(context, context.gComments[post.comments[0]]));
var nodeLoad = cView.gNodes["comments-load"].cloneAll();
nodeLoad.getElementsByClassName("num")[0].innerHTML = post.omittedComments;
postNBody.cNodes["comments"].appendChild(nodeLoad);
if(post.comments[1])
postNBody.cNodes["comments"].appendChild(Drawer.genComment.call(context, context.gComments[post.comments[1]]));
}
else post.comments.forEach(function(commentId){
postNBody.cNodes["comments"]
.appendChild(Drawer.genComment.call(context, context.gComments[commentId]))
});
}
if (postNBody.cNodes["comments"].childNodes.length > 4)
postNBody.cNodes["many-cmts-ctrl"].hidden = false;
return nodePost;
}
,"genTitle":function(nodePost){
var cView = this.cView;
var domain = nodePost.rawData.domain;
var context = cView.contexts[domain];
var post = nodePost.rawData;
var user = context.gUsers[post.createdBy];
var title = "<span><a href='//"+ [gConfig.domains[domain].front, user.username, post.id].join("/")
+ "'><img src='"+gConfig.static + domain + ".ico' /></a></span> "+user.link;
//if(nodePost.isPrivate) title += "<span> posted a secret to "+StringView.makeFromBase64(matrix.gSymKeys[cpost.payload.feed].name)+"</span>";
nodePost.gotLock = true;
nodePost.gotShield = true;
if(false);
else if(post.postedTo){
post.postedTo.forEach(function(id){
nodePost.gotLock &= context.gFeeds[id].isPrivate;
nodePost.gotShield &= context.gFeeds[id].isProtected;
nodePost.direct = context.gFeeds[id].direct;
});
if ((post.postedTo.length >1)||(context.gFeeds[post.postedTo[0]].user.id!=user.id)){
title += "<span> posted to: </span>";
post.postedTo.forEach(function(id){
title += context.gFeeds[id].user.link;
});
}
}
if(post.isDirect == true)
nodePost.direct = true;
return title;
}
,"embedPreview": function (oEmbedPrs, victim, target){
var cView = this.cView;
var oEmbedURL;
var m;
var fake = {"then":function(){}};
var blacklist = gConfig.domains["FreeFeed"].fronts;
if(blacklist.some(function(item){return victim.indexOf(item)!= -1;})) return fake;
if((m = /^https:\/\/(?:docs\.google\.com\/(?:document|spreadsheets|presentation|drawings)|drive\.google\.com\/file)\/d\/([^\/]+)/.exec(victim)) !== null) {
return new Promise(function(resolve,reject){
var oReq = new XMLHttpRequest();
oReq.onload = function(){
if(oReq.status < 400)
resolve(JSON.parse(oReq.response));
else reject(oReq.response);
}
oReq.open("get","https://www.googleapis.com/drive/v2/files/" + m[1] + "?key=AIzaSyA8TI6x9A8VdqKEGFSE42zSexn5HtUkaT8",true);
oReq.send();
}).then(function(info){
//var nodeiFrame = cView.doc.createElement("iframe");
//nodeiFrame.src = info.embedLink;
var nodeA = cView.doc.createElement("a");
var img = cView.doc.createElement("img");
var width = cView.doc.getElementById("container").clientWidth*3/4;
img.src = info.thumbnailLink.replace("=s220","=w"+ width+"-c-h"+ width/5 );// "=s"+cView.doc.getElementById("container").clientWidth/2+"-p");
var node = cView.doc.createElement("div");
node.className = "att-img";
nodeA.appendChild(img);
nodeA.href = victim;
node.appendChild(nodeA);
target.appendChild(node);
img.onerror=function(){nodeA.hidden = true;};
return node;
});
}else if (/^https?:\/\/(www\.)?pinterest.com\/pin\/.*/.exec(victim) !== null){
var node = cView.doc.createElement("div");
node.className = "att-img";
node.innerHTML = '<a data-pin-do="embedPin" href="' + victim + '"></a>';
target.appendChild(node);
return Promise.resolve(node);
}
var bIsOEmbed = oEmbedPrs.some(function(o){
return o.endpoints.some(function(endp){
if(!endp.schemes)console.log(endp.url)
else if (endp.schemes.some(function (scheme){
return victim.match(scheme) != null; })){
oEmbedURL = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%3D'"
+ encodeURIComponent(endp.url
+ "?url=" + victim
+ "&format=json"
+ "&maxwidth="+cView.doc.getElementById("container").clientWidth*3/4
)
+ "'&format=json";
return true;
}else return false;
});
});
if(bIsOEmbed){
return new Promise(function(resolve,reject){
var oReq = new XMLHttpRequest();
oReq.onload = function(){
if(oReq.status < 400)
resolve(JSON.parse(oReq.response));
else reject(oReq.response);
}
oReq.open("get",oEmbedURL,true);
oReq.send();
}).then(function(qoEmbed){
if (!qoEmbed.query.count) return null;
var oEmbed = qoEmbed.query.results.json;
if(oEmbed.type == "photo"){
return target.appendChild(oEmbedImg(oEmbed.url,victim));
}else if (typeof oEmbed.html !== "undefined"){