Repository navigation
Expand file tree
/
Copy pathLetsComment.js
More file actions
1190 lines (1059 loc) · 47.5 KB
/
Copy pathLetsComment.js
File metadata and controls
1190 lines (1059 loc) · 47.5 KB
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
var config = {
host: window.location.hostname,
port: window.location.port,
isSecure: window.location.protocol === "https:"
};
var arrayValidCharts = ["map","table", "pivot-table", "barchart","boxplot","combochart","distributionplot","gauge","piechart","scatterplot","treemap","histogram","linechart","kpi","waterfallchart"];
var currentUser;
var onlyUser;
var oldFontSize;
var oldCommentView = null;
var oldCommentLevel = null;
var initFirebase;
var rendered;
var editMode = 0;
var oldCommentRef;
var ref;
var oldRef;
var currentObjectId;
var commentsArray = new Array();
var currentGroups = new Array();
var currentGroupsReceiv = new Array();
var currentGroupsOwners = new Array();
var favorites = [{}];
var currentSelections = '';
var modalName = '';
var checkTooltipSheet = true;
var privacy = 'unlocked';
var receiv = '';
var groupName = '';
var vGroupOwner = '';
var app;
var appId;
var enigma;
var version;
var oneOpened = false;
var millisecToDays = 86400000;
require.config({
paths: {
'firebase': 'https://www.gstatic.com/firebasejs/5.5.6/firebase'
}
})
define( ["jquery",
"qlik",
"css!./LetsComment.css",
'firebase',
'./config'
],
function (jquery,qlik,cssContent,firebase,configfile) {
if(!app){
app = qlik.currApp(this);
appId = app.id;
appId = appId.replace('.','');//dots are not allowed
}
var global = qlik.getGlobal(config);
var urlPath = global.session.options.reloadURI;
var globalPath = urlPath.substring(0,urlPath.indexOf('/sense/'));
global.getAuthenticatedUser(async function (reply) {
currentUser = reply.qReturn;
currentUser = currentUser.replace('UserDirectory=', '');
currentUser = currentUser.replace('; UserId=', '_');
onlyUser = currentUser.substring(currentUser.indexOf('_')+1);
vGroupOwner = currentUser;
});
//version control coded by Riley MacDonald
if (parseInt(version) <= 122447){
$(".qui-buttonset-right").prepend($("<button id = 'butLightBulb' class='lui-button lui-button--toolbar iconToTheRight npsod-bar-btn lui-icon lui-icon--lightbulb'><span data-icon='toolbar-print'></span></button>"));
}else{
$(".qs-toolbar__right").prepend($("<button id = 'butLightBulb' class='lui-button qs-toolbar__element iconToTheRight npsod-bar-btn lui-icon lui-icon--lightbulb'><span data-icon='toolbar-print'></span></button>"));
}
async function toggleId () {
if (!initFirebase) {
firebase = await firebase.initializeApp(config);
initFirebase = true;
// Sign into Firebase
firebase.auth().signInAnonymously().catch(function (error) {
var errorCode = error.code;
var errorMessage = error.message;
});
}
if(document.getElementById('flagId')){
document.getElementById('flagId').remove();
$( '.lui-icon--group' ).remove();
$( '.LetsComment-tooltip-comments' ).remove();
}
if(document.getElementById('messageId')){
$( '.LetsComment-tooltip-comments' ).remove();
}
var ancho = '130px';
var cnt = $( ".LetsComment-tooltip" ).remove();
if ( cnt.length === 0) {
//var cnt = $( ".LetsComment-tooltip" ).remove();
$(".buttons-end").append( '<div class="LetsComment-tooltip" style = "float:left;position:relative;right:0%;padding: 12px 15px;">' +
'<a class="LetsComment-btn-sheet" style = "bottom:0px" title="properties"><i id = "flagId" class="lui-icon lui-icon--group">send</i></a>' +
"</div>" );
$('.LetsComment-btn-sheet').click(function() {
var x = document.getElementsByClassName("LetsComment-modalBase");
if(x.length == 0){
var sheet_id = qlik.navigation.getCurrentSheetId();
var loop = true;
$( '.sheet-grid' ).each( function (ih, elh ) {
if(loop){
var sh = angular.element( elh ).scope();
onceQlik(sh.model);
loop = false;
}
})
}
})
$( '.qv-object,.qv-panel-sheet' ).each( async function ( i, pl ) {
var s = angular.element( pl ).scope();
if (s.layout || (s.$$childHead && s.$$childHead.layout)) {
if(arrayValidCharts.indexOf(s.model.layout.qInfo.qType) >= 0){
var dlayoutId = s.layout.qInfo.qId;
$( pl ).append('<div id = "messageId" class="LetsComment-tooltip-comments"><img title = "Current month messages" class = "icons-comment" src="/Extensions/LetsComment/icons/messge.png"><div id = "totalMessages_' + dlayoutId + '" class = "top-right red-circle">0</div></div>');
lastTen = await read10dayComments(dlayoutId);
$(pl.children[2]).on('click', function(){
if(!oneOpened){
onceQlik(s.model);
}
})
}
}
});
async function read10dayComments(dlayoutId){
readlastten = {
"readlastTen": appId + '/' + dlayoutId
}
var query = firebase.database().ref(readlastten.readlastTen).orderByKey();
query.once("value")
.then(function(snapshot) {
var hmm = 0;
if(snapshot.node_.children_.root_.key){//if there are any comments in that chart
var today = new Date();
var todayMilliSecs = today.getTime();
snapshot.forEach(function(childSnapshot) {
var dateSaved = childSnapshot.key;
var dateMilli = dateSaved.substring(11,24);
var daysAgo = (todayMilliSecs / millisecToDays) - ( dateMilli / millisecToDays);
if(daysAgo <= 30){
var receiverMatch = false;
if(childSnapshot.val().public == 'group'){
var receiverLength = childSnapshot.val().receivers;
var s = receiverLength;
var match = s.split(',')
for (var a in match)
{
if(onlyUser == match[a]){
receiverMatch = true;
}
}
}
if(currentUser == childSnapshot.val().user || childSnapshot.val().public == 'unlocked' || receiverMatch){
hmm++;
}
}
})
}
var elemId = "totalMessages_" + dlayoutId;
var rightMargin = '';
var fontSize = '';
var back_Msg_Color = 'red';
if(hmm > 999){
hmm = '1k';
}
if(hmm == 0){
back_Msg_Color = '#ccc'
}
var div10 = document.getElementById(elemId);
if(div10){
div10.innerHTML = hmm;
div10.style.background = back_Msg_Color;
}
})
}
function onceQlik(model){
oneOpened = true;
model.getProperties().then( function ( reply ) {
if(!app){
app = qlik.currApp(this);
appId = app.id;
}
var vObjectId = reply.qInfo.qId;
var vObjectType = reply.qInfo.qType;
var vObjectTitle;
var vHeaderTitle;
if(vObjectType == 'sheet'){
vObjectTitle = reply.qMetaDef.title;
}else{
vObjectTitle = reply.title;
}
if(!vObjectTitle){
vObjectTitle = ' no title'
}
vHeaderTitle = 'Qlik Chat - ' + vObjectType + ' : ' + vObjectTitle;
currentObjectId = vObjectId;
app.getObject('CurrentSelections').then(async function(model){
var QVLetsComment01 = app.getObject( 'QVLetsComment01', vObjectId );
if (initFirebase) {
readAllGroups();
readAllComments();
}
//myModalBase
modalName = 'modal_' + currentObjectId;
var vBackgroundColor = 'rgb(252,250,237)';
if(vObjectType == 'sheet'){
vBackgroundColor = 'rgb(251, 251, 255)';
}
var vModalBase = '<div id="' + modalName + '" class="LetsComment-modalBase navbar-collapse collapse twbs">' +
'<div class="LetsComment-base-content">' +
'<div class="LetsComment-modal-header">' +
'<span>' + vHeaderTitle + '</span>'+
'<a>' +
'<span id = "baseModalClose" name = "' + modalName + '" class="lui-clear-all lui-icon lui-icon--remove close"></span>' +
'</a>' +
'<input id = "searchText" type="text" style = "padding-left:5px;border-radius: 10px;margin-top: 10px;width: 165px;float: right;" placeholder="Search text">' +
'</div>' +
'<div class="LetsComment-modal-body" style = "background-color: ' + vBackgroundColor + '">' +
'<div id = "box_' + currentObjectId + '" class="box">';
vModalBase += '</div></div>';
vModalBase += '<div class="LetsComment-modal-footer">' +
'<span>'+
'<img id = "' + privacy + '" title = "' + groupName + '" class = "icons" src="/Extensions/LetsComment/icons/' + privacy + '.png">' +
'<img id = "like" class = "icons" src="/Extensions/LetsComment/icons/like2.png" style = "padding-left:5px">' +
'<input rows="1" class = "inputComment" cols="70" wrap="soft" id="TypeComment" value="" placeholder="Type your comment">' +
'</input>' +
'<img id = "picture" class = "icons" src="/Extensions/LetsComment/icons/picture.png">' +
'<img id = "send" class = "icons" src="/Extensions/LetsComment/icons/send.png">' +
'<p id = "currentSel" style = "color:blue;font-size:15px;font-weight: normal"></p>' +
'</span>' +
'</div>' ;
vModalBase += '</div></div>';
$( document.body ).append( vModalBase );
var vSearch = document.getElementById("searchText");
var modalBase = document.getElementById(modalName);
var spanBase = document.getElementById("baseModalClose");
var vLock = document.getElementById(privacy);
var vLike = document.getElementById("like");
var vPicture = document.getElementById("picture");
var vSend = document.getElementById("send");
var vTypeTXT = document.getElementById("TypeComment");
modalBase.style.display = "block";
if (!initFirebase) {
firebase = await firebase.initializeApp(config);
initFirebase = true;
firebase.auth().signInAnonymously().catch(function (error) {
var errorCode = error.code;
var errorMessage = error.message;
});
readAllGroups();
readAllComments();
}
var modalName2 = "#" + modalName;
$(modalName2).draggable({
handle: ".LetsComment-modal-header"
});
vSearch.onchange = function() {
var vSearchTxt = document.getElementById("searchText").value.toUpperCase();
//walk through the bullets and just hide the ones that does not contain the search text
var allBulletsR = document.getElementsByClassName("LetsComment-bullet");
for (var i = 0; i < allBulletsR.length; i++) {
var aaa = allBulletsR[i].innerHTML.toUpperCase();
if(aaa.indexOf(vSearchTxt) >= 0 || vSearchTxt.length == 0){
$(allBulletsR[i]).show();
}else{
$(allBulletsR[i]).hide();
}
}
}
spanBase.onclick = async function() {
var parName = '#' +spanBase.getAttribute('name');
$( parName ).remove();
oneOpened = false;
//Refresh the list of current month messages
$( '.qv-object,.qv-panel-sheet' ).each( async function ( i, pl ) {
var s = angular.element( pl ).scope();
if (s.layout || (s.$$childHead && s.$$childHead.layout)) {
if(arrayValidCharts.indexOf(s.model.layout.qInfo.qType) >= 0){
var dlayoutId = s.layout.qInfo.qId;
lastTen = await read10dayComments(dlayoutId);
}
}
})
}
vLock.onclick = async function() {
getNewPrivacy();
}
vLock.onmouseover = async function() {
console.log(groupName);
}
async function getNewPrivacy(){
var newGroupName = '';
var privacyOld = privacy;
var vModalPriv = document.createElement('div');
var vCurrentChecked0 = '';
var vCurrentChecked1 = '';
var vCurrentChecked2 = '';
if(privacy == 'unlocked'){
vCurrentChecked0 = 'checked';
vCurrentChecked1 = '';
vCurrentChecked2 = '';
}else{
if(privacy == 'locked'){
vCurrentChecked0 = '',
vCurrentChecked1 = 'checked';
vCurrentChecked2 = '';
}else{
vCurrentChecked0 = '';
vCurrentChecked1 = '';
vCurrentChecked2 = 'checked';
}
}
var vModalText = '<div id="myModalPrivate" class="modalPrivate">' +
'<div class="modalPrivate-content">' +
'<div class="modalPrivate-body">' +
'<div id = "boxPriv"class="modalPrivate-box">' +
'<form action="" id = "formPriv" class = "modalPrivate-form">' +
'<input type="radio" name="PrivateType" value="0" ' + vCurrentChecked0 + '> Public<br>' +
'<input type="radio" name="PrivateType" value="1" ' + vCurrentChecked1 + ' style = "margin-top:15px"> Only me<br>' +
'<input type="radio" name="PrivateType" value="2" ' + vCurrentChecked2 + ' style = "margin-top:15px"> Specific people<br>' +
'<div class="form-group" style="width:200px"><br>' +
'<label>Select a current group</label>' +
'<select class="form-control lui-input lui-input-group__item lui-input-group__input" style = "margin-top: 5px" id="comboCurrentGroups" required="required">' +
'<option value=99999>Chose a current group</option>';
for(var ah = 0;ah < currentGroups.length;ah++){
vModalText += '<option value=' + ah + '> ' + currentGroups[ah] + ' </option>';
}
vModalText += '</select><br>' +
'<textarea id = "specificPeopleArea" disabled="" class="expression-editor-function-definition lui-textarea lui-disabled ng-binding textAreaUsers">users</textarea>' +
'<br>' +
'<div>' +
'<label>Create new group</label>' +
'<div style = "margin-top: 5px">' +
'<div class="lui-input-group">' +
'<input id = "specificGroup" class="lui-input lui-input-group__item lui-input-group__input" type="text" placeholder="group name">' +
'<button id = "createNewGroup" class="lui-button lui-input-group__item lui-input-group__button" type="button">' +
'<span class="lui-button__icon lui-icon lui-icon--plus" q-title-translation="Tooltip.AddTag" title="Add tag"></span>' +
'</button>' +
'</div>' +
'</div>' +
'</div>' +
'<br>' +
'<div style = "width: 400px">' +
'<label>Group members / Occasional receivers (", separated")</label>' +
'<div style = "margin-top: 5px;width:300px">' +
'<div class="lui-input-group">' +
'<input id = "specificPeople" class="lui-input lui-input-group__item lui-input-group__input" type="text" placeholder="user1, user2,...">' +
'<div class="lui-buttongroup">' +
'<button id="updateGroup" class="lui-button lui-buttongroup__button ng-scope" type="button">' +
'<span class="lui-icon lui-icon--reload" title="Update group members"></span>' +
'</button>' +
'<button id="deleteGroup" class="lui-button lui-buttongroup__button lui-button--rounded ng-scope" type="button" style = "position:absolute">' +
'<span class="lui-icon lui-icon--bin" title="Delete group"></span>' +
'</button>' +
'</div>' +
'</div>' +
'</div>' +
'</div>' +
'<br><p id = "ownerInfo" style="font-weight:bold;width:400px">Owner : ' + vGroupOwner + '</p>' +
'</div>' +
'</form>'+
'</div>' +
'</div>' +
'<div class="modalPrivate-footer">' +
'<span style="float:right">'+
'<button id = "escPrivButton" class="lui-button cancel button ng-scope" style = "top:13px" name="cancelButton" q-translation="Common.Cancel">Cancel</button>' +
'<button id = "sendPrivButton" class="lui-button confirm button ng-scope" style = "top:13px;margin-left:20px" name="confirmButton" q-translation="Common.Apply">Apply</button>' +
'</span>' +
'</div>' +
'</div></div>';
$(".LetsComment-modalBase").append( vModalText );
var vModalPriv = document.getElementById("myModalPrivate");
var vComboGr = document.getElementById("comboCurrentGroups");
var vSendPriv = document.getElementById("sendPrivButton");
var vNewGroup = document.getElementById("createNewGroup");
var vUpdGroup = document.getElementById("updateGroup");
var vDelGroup = document.getElementById("deleteGroup");
var vEscPriv = document.getElementById("escPrivButton");
vModalPriv.style.display = "block";
if(currentUser != vGroupOwner){
$('#updateGroup').hide();
$('#deleteGroup').hide();
}else{
if(vComboGr.selectedIndex == 0) {
$('#updateGroup').hide();
$('#deleteGroup').hide();
document.getElementById("ownerInfo").value = ''
}else{
$('#updateGroup').show();
$('#deleteGroup').show();
document.getElementById("ownerInfo").value = 'Owner : ' + currentGroupsOwners[(this.selectedIndex - 1)];
}
}
vComboGr.onchange = async function() {
if(this.selectedIndex > 0){
groupName = currentGroups[(this.selectedIndex - 1)];
vGroupOwner = currentGroupsOwners[(this.selectedIndex - 1)];
document.getElementById("specificPeopleArea").value = currentGroupsReceiv[(this.selectedIndex - 1)];
document.getElementById("specificPeople").value = currentGroupsReceiv[(this.selectedIndex - 1)];
var theDivOwner = document.getElementById("ownerInfo");
theDivOwner.innerHTML = 'Owner : ' + vGroupOwner;
if(currentUser != vGroupOwner){
$('#updateGroup').hide();
$('#deleteGroup').hide();
}else{
$('#updateGroup').show();
$('#deleteGroup').show();
}
var vVarOption = document.getElementById("formPriv");
vVarOption.elements[0].checked = false;
vVarOption.elements[1].checked = false;
vVarOption.elements[2].checked = true;
}else{
vGroupOwner = '';
groupName = '';
document.getElementById("specificPeopleArea").value = 'users';
document.getElementById("specificPeople").value = '';
var theDivOwner = document.getElementById("ownerInfo");
theDivOwner.innerHTML = 'Owner : ' + currentUser;
$('#updateGroup').hide();
$('#deleteGroup').hide();
document.getElementById("ownerInfo").value = '';
}
}
vUpdGroup.onclick = async function() {
var vUpdUsersTxt = document.getElementById("specificPeople").value;
document.getElementById("specificPeopleArea").value = vUpdUsersTxt;
var creatime = new Date().toLocaleString();
updgroup = await writeNewGroup(groupName, vUpdUsersTxt, currentUser, creatime);
if(vUpdUsersTxt){
var indexSliceGroup = currentGroups.indexOf(groupName);
currentGroupsReceiv[indexSliceGroup] = vUpdUsersTxt;
var vVarOption = document.getElementById("formPriv");
vVarOption.elements[0].checked = false;
vVarOption.elements[1].checked = false;
vVarOption.elements[2].checked = true;
}else{
alert("Please chose type a set of receivers!")
}
}
vDelGroup.onclick = async function() {
delgr = {
"delGr": 'UserGroups' + '/' + appId + '/' + groupName
}
firebase.database().ref(delgr.delGr).remove();
var indexSliceGroup = currentGroups.indexOf(groupName);
currentGroups.splice(indexSliceGroup,1);
currentGroupsReceiv.splice(indexSliceGroup,1);
currentGroupsOwners.splice(indexSliceGroup,1);
vComboGr.remove((indexSliceGroup + 1));
document.getElementById("specificPeople").value = '';
document.getElementById("specificPeopleArea").value = '';
}
vSendPriv.onclick = async function() {
var vUsersAlert = document.getElementById("specificPeople").value;
var vGroupTxt = document.getElementById("specificGroup").value;
var vUsersTxt = document.getElementById("specificPeopleArea").value;
if(vComboGr.selectedIndex == 0) {
vUsersTxt = document.getElementById("specificPeople").value;
groupName = vUsersTxt;
}
var vGroupTxt = '';
var vVarOption = document.getElementById("formPriv");
document.getElementById("specificPeople").value = '';
if(vVarOption.elements[0].checked){
privacy = 'unlocked';
receiv = '';
groupName = '';
document.getElementById("ownerInfo").value = ''
}
if(vVarOption.elements[1].checked){
privacy = 'locked';
receiv = '';
groupName = '';
}
if(vVarOption.elements[2].checked){
if(vUsersTxt.length>0){
privacy = 'group';
receiv = vUsersTxt;
}else{
alert('Please select a valid set of users')
return;
}
}
var vOldLockBut = document.getElementById(privacyOld);
vOldLockBut.src = globalPath + '/Extensions/LetsComment/icons/' + privacy + '.png';
vOldLockBut.id = privacy;
document.getElementById(privacy).title = groupName
$( "#myModalPrivate" ).remove();
var theFocus = document.getElementById("TypeComment");
theFocus.focus();
}
vNewGroup.onclick = async function() {
var vGroupTxt = document.getElementById("specificGroup").value;
var vUsersTxt = document.getElementById("specificPeople").value;
if(vGroupTxt && vUsersTxt){
//mira si exite
var found = currentGroups.indexOf(vGroupTxt);
//crealo
if(found < 0){
var creatime = new Date().toLocaleString();
newgroup = await writeNewGroup(vGroupTxt, vUsersTxt, currentUser, creatime);
groupName = vGroupTxt;
vGroupOwner = currentUser;
currentGroups.push(groupName);
currentGroupsReceiv.push(vUsersTxt);
currentGroupsOwners.push(currentUser);
var indexAddGroup = currentGroups.indexOf(groupName);
var option = document.createElement('option');
option.text = groupName;
option.value = indexAddGroup;
vComboGr.add(option, (indexAddGroup + 1));
vComboGr.selectedIndex = (indexAddGroup + 1);
var theDivOwner = document.getElementById("ownerInfo");
theDivOwner.innerHTML = 'Owner : ' + currentUser;
document.getElementById("specificPeopleArea").value = vUsersTxt;
document.getElementById("specificGroup").value = '';
var vVarOption = document.getElementById("formPriv");
vVarOption.elements[0].checked = false;
vVarOption.elements[1].checked = false;
vVarOption.elements[2].checked = true;
$('#updateGroup').show();
$('#deleteGroup').show();
}else{
alert('This group already exists!');
}
}else{
alert("Please chose a group name and a set of receivers!")
}
}
vEscPriv.onclick = function() {
$( "#myModalPrivate" ).remove();
var theFocus = document.getElementById("TypeComment");
theFocus.focus();
}
async function writeNewGroup(group, users, user, time) {
refgroup = {
"writeGroup": 'UserGroups' + '/' + appId + '/' + group
}
firebase.database().ref(refgroup.writeGroup).set({
members: users,
owner: user,
created: time
}, function (error) {
if (error) {
console.log('failure to create group')
}
else {
console.log('group created')
}
})
}
}
vLike.onclick = async function() {
milliseconds = new Date().toLocaleString();
var vpublic = privacy;
var vusers = receiv;
var like = '-##like2##-';
var comment = '';
comments = await writeNewComment(milliseconds, currentUser, comment, like, vpublic, vusers);
var theSel = document.getElementById("currentSel");
}
vPicture.onclick = async function() {
currentSelections = await getCurrentSelections();
if(currentSelections == '-'){
currentSelections = '';
}
var theSel = document.getElementById("currentSel");
theSel.innerHTML = currentSelections.substring(0,60);
var getFromBetween = {
results:[],
string:"",
getFromBetween:function (sub1,sub2) {
if(this.string.indexOf(sub1) < 0 || this.string.indexOf(sub2) < 0) return false;
var SP = this.string.indexOf(sub1)+sub1.length;
var string1 = this.string.substr(0,SP);
var string2 = this.string.substr(SP);
var TP = string1.length + string2.indexOf(sub2);
return this.string.substring(SP,TP);
},
removeFromBetween:function (sub1,sub2) {
if(this.string.indexOf(sub1) < 0 || this.string.indexOf(sub2) < 0) return false;
var removal = sub1+this.getFromBetween(sub1,sub2)+sub2;
this.string = this.string.replace(removal,"");
},
getAllResults:function (sub1,sub2) {
// first check to see if we do have both substrings
if(this.string.indexOf(sub1) < 0 || this.string.indexOf(sub2) < 0) return;
// find one result
var result = this.getFromBetween(sub1,sub2);
// push it to the results array
this.results.push(result);
// remove the most recently found one from the string
this.removeFromBetween(sub1,sub2);
// if there's more substrings
if(this.string.indexOf(sub1) > -1 && this.string.indexOf(sub2) > -1) {
this.getAllResults(sub1,sub2);
}
else return;
},
get:function (string,sub1,sub2) {
this.results = [];
this.string = string;
this.getAllResults(sub1,sub2);
return this.results;
}
};
var str = await getCurrentFields();
var result = getFromBetween.get(str,"|",":");
favorites = [{}];
var numv = 0;
result.forEach(async function(childResult) {
var vfield = '"' + childResult + '"';
var vfieldValues = await getCurrentValues(vfield);
item = {}
item [childResult] = vfieldValues;
var myObj = {
[vfield] : vfieldValues
};
favorites.push( myObj );
});
var theFocus = document.getElementById("TypeComment");
theFocus.focus();
}
vSend.onclick = async function() {
var theDivM = document.getElementById("TypeComment").value;
var like = '';
if(theDivM.length > 0){
milliseconds = new Date().toLocaleString();
var vpublic = privacy;
var vusers = receiv;
comments = await writeNewComment(milliseconds, currentUser, theDivM, like, vpublic, vusers);
var theSel = document.getElementById("currentSel");
theSel.innerHTML = '';
document.getElementById("TypeComment").value = '';
//currentSelections = '';
}
}
$("#TypeComment").on('keyup', async function (e) {
if (e.keyCode == 13) {
var theDivM = document.getElementById("TypeComment").value;
var like = '';
if(theDivM.length > 0){
milliseconds = new Date().toLocaleString();
var vpublic = privacy;
var vusers = receiv;
comments = await writeNewComment(milliseconds, currentUser, theDivM, like, vpublic, vusers);
var theSel = document.getElementById("currentSel");
theSel.innerHTML = '';
document.getElementById("TypeComment").value = '';
}
}
})
/*$("#TypeComment").on('click', async function (e) {
alert('Hola');
})*/
async function writeNewComment(time, user, comment, like, public, receivers) {
var theSelReply = document.getElementById("replyDiv");
var theSelReplyName = false;
ref = await writeref(null);
var vcode = ref.writeRef.substr(ref.writeRef.lastIndexOf("/") + 1);
if(theSelReply){
theSelReplyName = theSelReply.getAttribute("name");
$( '.replyDiv' ).remove();
}
firebase.database().ref(ref.writeRef).set({
time: time,
user: user,
comment: comment,
like: like,
public: public,
currentSel: currentSelections,
fieldSel: favorites,
group: groupName,
receivers: receivers,
parent: theSelReplyName
}, function (error) {
if (error) {
}
else {
var vLocked = '';
var vpubcol = '';
var vpubtxt = '';
var vpublike = comment;
if(like == '-##like2##-'){
vpublike = '<p><img id = "like" class = "icons" src="/Extensions/LetsComment/icons/like2.png" style = "width:50px"></p>';
}
if (public == 'unlocked'){
vpubcol = 'LetsComment-bullet-green-r';
vprivatxt = 'public';
}else{
if(public == 'locked'){
vpubcol = 'LetsComment-bullet-yellow-r';
vprivatxt = 'private';
}else{
vpubcol = "LetsComment-bullet-pink-r";
if(groupName.length > 0){
vprivatxt = groupName;
}else{
vprivatxt = receivers;
}
}
}
vpubtxt = '<p><i style = "color:DarkGray">' + vprivatxt + ' : </i><i>' + vpublike + '</i></p>';
favorites = [{}];
var replyText = '';
if(theSelReplyName){
replyText = '<p><i style = "font-size:14px">"' + theSelReplyName + '"</i></p>';
}
$("#box_" + currentObjectId).append(
'<div id = "div_' + vcode + '" class = "LetsComment-bullet ' + vpubcol + '">' +
'<div style = "float:right">' +
'<img id = "' + vcode + '" class = "del" src="/Extensions/LetsComment/icons/del.png">' +
'</div>' +
replyText +
'<p><strong>' + user + '</strong><span style ="font-size:12px"> ' + time + '</span><i>' + vLocked + '</i></p>' +
'<p id = "sel_' + vcode + '" class = "newLinkSel">' + currentSelections + '</p>' +
vpubtxt +
'</div>'
);
currentSelections = '';
var theSel = document.getElementById("currentSel");
theSel.innerHTML = '';
var objBox = document.getElementById("box_" + currentObjectId);
objBox.scrollTop = objBox.scrollHeight;
$('.del').click(function() {
if(vcode){
delref = {
"delRef": appId + '/' + currentObjectId + '/' + vcode
}
firebase.database().ref(delref.delRef).remove();
$("#div_" + vcode).remove();
}
})
$('.newLinkSel').click(async function() {
if(vcode){
app.clearAll();
readref = {
"readRef": appId + '/' + currentObjectId + '/' + vcode
}
var query = firebase.database().ref(readref.readRef);
query.once("value")
.then(function(snapshot) {
snapshot.child("fieldSel").forEach(function(selection){
var valor = selection.node_.children_.root_.value.value_;
var campo = selection.node_.children_.root_.key;
var matchStr = "=[" + campo + "] = '" + valor.split(',').join("' or [" + campo + "] = '") + "'";
app.field(campo).selectMatch(matchStr)
app.field(campo).selectPossible()
})
})
}
})
currentSelections = '';
}
});
var theFocus = document.getElementById("TypeComment");
theFocus.focus();
}
async function selOneComment(readref) {
var query = firebase.database().ref(readref.readRef).orderByKey();
query.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key;
});
});
}
async function readAllGroups() {
currentGroups = [];
currentGroupsReceiv = [];
currentGroupsOwners = [];
groupref = {
"groupRef": 'UserGroups' + '/' + appId
}
var query = firebase.database().ref(groupref.groupRef).orderByKey();
query.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
currentGroups.push(childSnapshot.key);
currentGroupsReceiv.push(childSnapshot.val().members);
currentGroupsOwners.push(childSnapshot.val().owner);
})
})
}
async function readAllComments() {
commentsArray = [];
readref = {
"readRef": appId + '/' + currentObjectId
}
var query = firebase.database().ref(readref.readRef).orderByKey();
query.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key;
var vcodedel = '';
var replyName = '';
var vreply = '';
var vReplyRec = '';
var vReplyGroup = '';
if(childSnapshot.val().public == 'unlocked'){
vReplyGroup = 'public';
}else{
if(childSnapshot.val().public == 'group'){
if(childSnapshot.val().group && childSnapshot.val().group > ''){
vReplyGroup = childSnapshot.val().group;
}else{
vReplyGroup = childSnapshot.val().receivers;
}
vReplyRec = childSnapshot.val().receivers;
}else{
vReplyGroup = 'private';
}
}
if(childSnapshot.val().like != '-##like2##-'){
replyName = childSnapshot.val().user + ' - ' + vReplyGroup + ': ' + childSnapshot.val().comment.substring(0,20) + '..';
vreply = '<div style = "padding-left:0px">' +
'<img id = "' + key + '" class = "reply" title = "' + vReplyRec + '" name = "' + replyName + '" src="/Extensions/LetsComment/icons/reply.png">' +
'</div>';
}
var receiverMatch = false;
if(childSnapshot.val().public == 'group'){
var receiverLength = childSnapshot.val().receivers;
var s = receiverLength;
var match = s.split(',')
for (var a in match)
{
if(onlyUser == match[a]){
receiverMatch = true;
}
}
}
var l1 = '';
if (currentUser == childSnapshot.val().user){
vcodedel = '<div style = "float:right">' +
'<img id = "' + key + '" name = "' + replyName + '" class = "del" src="/Extensions/LetsComment/icons/del.png">' +
'</div>';
if (childSnapshot.val().public == 'unlocked'){
l1 = 'LetsComment-bullet-green-r';
}else{
if(childSnapshot.val().public == 'locked'){
l1 = 'LetsComment-bullet-yellow-r';
}else{
l1 = 'LetsComment-bullet-pink-r';
}
}
}else{
if (childSnapshot.val().public == 'unlocked'){
l1 = 'LetsComment-bullet-blue-l';
}else{
if(receiverMatch){
l1 = 'LetsComment-bullet-pink-l';
}
}
}
var vLocked = '';
var vpubtxt = '';
var vprivatxt = '';
var vpublike = childSnapshot.val().comment;
if(childSnapshot.val().like == '-##like2##-'){
vpublike = '<p><img id = "like" class = "icons" src="/Extensions/LetsComment/icons/like2.png" style = "width:50px"></p>';
}
if (childSnapshot.val().public == 'unlocked'){
vprivatxt = 'public';
}else{
if (childSnapshot.val().public == 'locked'){
vprivatxt = 'private';
}else{
if(childSnapshot.val().group && childSnapshot.val().group > ''){
vprivatxt = childSnapshot.val().group;
}else{
vprivatxt = childSnapshot.val().receivers;
}
}
}
vpubtxt = '<p><i style = "color:DarkGray">' + vprivatxt + ' : </i><i>' + vpublike + '</i></p>';
var replyText = '';
if(childSnapshot.val().parent){
replyText = '<p><i style = "font-size:14px">"' + childSnapshot.val().parent + '"</i></p>';
}
if (childSnapshot.val().user == currentUser || childSnapshot.val().public =='unlocked' || (childSnapshot.val().public == 'group' && receiverMatch)){
$("#box_" + currentObjectId).append(
'<div id = "div_' + key + '" class = "LetsComment-bullet ' + l1 + '">' +
vcodedel + vreply +
replyText +
'<p><strong>' + childSnapshot.val().user + '</strong><span style ="font-size:12px"> ' + childSnapshot.val().time + '</span><i>' + vLocked + '</i></p>' +
'<p id = "sel_' + key + '" class = "linkSel">' + childSnapshot.val().currentSel + '</p>' +
vpubtxt +
'</div>'
//'<br>'
);
}
});
var theFocus = document.getElementById("TypeComment");